OLDataProvider.m 721 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152
  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) uploadFile:(NSString*) file
  1363. //{
  1364. // NSData* data = [NSData dataWithContentsOfFile: file];
  1365. // UIApplication * app = [UIApplication sharedApplication];
  1366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1367. //
  1368. //
  1369. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1370. //
  1371. //
  1372. //
  1373. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1374. //
  1375. // if(appDelegate.user!=nil)
  1376. // [params setValue:appDelegate.user forKey:@"user"];
  1377. // // if(appDelegate.contact_id!=nil)
  1378. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1379. // if(appDelegate.password!=nil)
  1380. // [params setValue:appDelegate.password forKey:@"password"];
  1381. //
  1382. //
  1383. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1384. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1385. // } error:nil];
  1386. //
  1387. //
  1388. //
  1389. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1390. //
  1391. // NSProgress *progress = nil;
  1392. //
  1393. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1394. //
  1395. //
  1396. //
  1397. // if (error) {
  1398. //
  1399. // NSString* err_msg = [error localizedDescription];
  1400. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1401. //
  1402. //
  1403. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1404. //
  1405. // DebugLog(@"data string: %@",str);
  1406. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1407. //
  1408. // } else {
  1409. // DebugLog(@"response ");
  1410. //
  1411. //
  1412. //
  1413. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1414. //
  1415. // // 再将NSData转为字符串
  1416. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1417. //
  1418. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1419. //
  1420. // DebugLog(@"data string: %@",jsonStr);
  1421. //
  1422. // NSDictionary* json = responseObject;
  1423. //
  1424. //
  1425. // if([[json valueForKey:@"result"] intValue]==2)
  1426. // {
  1427. //// NSString* img_url_down = json[@"img_url_aname"];
  1428. //// NSString* img_url_up = json[@"img_url"];
  1429. //
  1430. //
  1431. // }
  1432. // else
  1433. // {
  1434. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1435. // }
  1436. //
  1437. //
  1438. // }
  1439. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1440. // }];
  1441. //
  1442. //
  1443. //
  1444. //
  1445. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1446. // //
  1447. //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. // [uploadTask resume];
  1454. //
  1455. //}
  1456. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1457. //{
  1458. //
  1459. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1460. // NSString *documents = [paths objectAtIndex:0];
  1461. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1462. //
  1463. //
  1464. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1465. //
  1466. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1467. //
  1468. //
  1469. //
  1470. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1471. //
  1472. //
  1473. //
  1474. //
  1475. //
  1476. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1477. //
  1478. //
  1479. //
  1480. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1481. //
  1482. //
  1483. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1484. //
  1485. //
  1486. //
  1487. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1488. //
  1489. //
  1490. //
  1491. //
  1492. // NSMutableDictionary* orderlist = nil;
  1493. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1494. //
  1495. //
  1496. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1497. //
  1498. //
  1499. // orderlist = [NSMutableDictionary new];
  1500. //
  1501. //// [orderlist addObject:appDelegate.order_code];
  1502. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1503. //
  1504. // }
  1505. // else
  1506. // {
  1507. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1508. //
  1509. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1510. //
  1511. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1512. // }
  1513. //
  1514. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1515. // orderitem[@"create_by"] = appDelegate.user;
  1516. // orderitem[@"price"] = upparams[@"total_price"];
  1517. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1518. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1519. // orderitem[@"order_status"] = @"Saved Order";
  1520. // orderitem[@"order_code"] = appDelegate.order_code;
  1521. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1522. //
  1523. //
  1524. // int count = [orderlist[@"count"] intValue];
  1525. //
  1526. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1527. //
  1528. // newlist[@"item_0"]=orderitem;
  1529. // int newcount = 1;
  1530. // for(int i=0;i<count;i++)
  1531. // {
  1532. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1533. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1534. // continue;
  1535. // else
  1536. // {
  1537. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1538. // newcount++;
  1539. // }
  1540. // }
  1541. // newlist[@"count"] = @(newcount);
  1542. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1543. //
  1544. // return;
  1545. //}
  1546. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1547. {
  1548. if(submitlist==nil)
  1549. return;
  1550. NSString *userdir = [OLDataProvider getUserPath];
  1551. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1552. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1553. }
  1554. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1555. {
  1556. if(orderlist==nil)
  1557. return;
  1558. NSString *userdir = [OLDataProvider getUserPath];
  1559. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1560. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1561. }
  1562. +(NSMutableDictionary*) getScanSubmitList
  1563. {
  1564. NSString *userdir = [OLDataProvider getUserPath];
  1565. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1566. NSMutableDictionary* submitlist = nil;
  1567. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1568. submitlist = [NSMutableDictionary new];
  1569. }
  1570. else
  1571. {
  1572. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1573. }
  1574. return submitlist;
  1575. }
  1576. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1577. //// NSString *userdir = [self getUserPath:user];
  1578. // NSMutableDictionary* orderlist = nil;
  1579. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1580. //
  1581. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1582. //
  1583. //
  1584. // orderlist = [NSMutableDictionary new];
  1585. //
  1586. // // [orderlist addObject:appDelegate.order_code];
  1587. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1588. //
  1589. // }
  1590. // else
  1591. // {
  1592. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1593. //
  1594. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1595. //
  1596. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1597. // }
  1598. // return orderlist;
  1599. //}
  1600. +(NSMutableDictionary*) getScanOrderList{
  1601. NSString *userdir = [OLDataProvider getUserPath];
  1602. NSMutableDictionary* orderlist = nil;
  1603. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1604. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1605. orderlist = [NSMutableDictionary new];
  1606. // [orderlist addObject:appDelegate.order_code];
  1607. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1608. }
  1609. else
  1610. {
  1611. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1612. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1613. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1614. }
  1615. return orderlist;
  1616. }
  1617. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1618. {
  1619. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1620. // NSString *documents = [paths objectAtIndex:0];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. // NSString *userdir = [OLDataProvider getUserPath];
  1623. NSMutableDictionary* orderlist = [self getScanOrderList];
  1624. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1625. int count = [orderlist[@"count"] intValue];
  1626. if(count==0)
  1627. {
  1628. // order list 为空,不用维护
  1629. #ifdef RA_NOTIFICATION
  1630. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1631. #else
  1632. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1633. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1634. #endif
  1635. return;
  1636. }
  1637. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1638. //
  1639. // // order list 为空,不用维护
  1640. //
  1641. //
  1642. // // [orderlist addObject:appDelegate.order_code];
  1643. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1644. //
  1645. // }
  1646. // else
  1647. // {
  1648. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1649. //
  1650. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1651. //
  1652. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1653. // }
  1654. //==================== order list 删除
  1655. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1656. NSMutableDictionary* submititem =nil;
  1657. int newcount = 0;
  1658. for(int i=0;i<count;i++)
  1659. {
  1660. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1661. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1662. {
  1663. submititem = item;
  1664. continue;
  1665. }
  1666. else
  1667. {
  1668. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1669. newcount++;
  1670. }
  1671. }
  1672. if(submititem==nil)
  1673. {
  1674. //order list 里没有和当前打开的order code一致的order
  1675. //这种情况往往是因为重复点提交按钮,order已关闭。
  1676. return;
  1677. }
  1678. newlist[@"count"] = @(newcount);
  1679. [self saveScanOrderList:newlist];
  1680. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1681. //////////////////---=============================== submitlist 添加
  1682. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1683. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1684. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1685. //
  1686. // submitlist = [NSMutableDictionary new];
  1687. //
  1688. //
  1689. // }
  1690. // else
  1691. // {
  1692. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1693. //
  1694. //
  1695. // }
  1696. int submitcount =[submitlist[@"count"] intValue];
  1697. submititem[@"order_status"] = soid;//@"Submitted Order";
  1698. submititem[@"customer_name"] = company;//@"Submitted Order";
  1699. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1700. submitlist[@"count"] = @(submitcount+1);
  1701. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1702. [self saveScanSubmitList:submitlist];
  1703. #ifdef RA_NOTIFICATION
  1704. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1705. #else
  1706. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1707. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1708. #endif
  1709. }
  1710. +(void) save2submitScanOrder1
  1711. {
  1712. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1713. // NSString *documents = [paths objectAtIndex:0];
  1714. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1715. // NSString *userdir = [OLDataProvider getUserPath];
  1716. NSMutableDictionary* orderlist = [self getScanOrderList];
  1717. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1718. int count = [orderlist[@"count"] intValue];
  1719. if(count==0)
  1720. {
  1721. // order list 为空,不用维护
  1722. #ifdef RA_NOTIFICATION
  1723. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1724. #else
  1725. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1726. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1727. #endif
  1728. return;
  1729. return;
  1730. }
  1731. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1732. int newcount = 0;
  1733. for(int i=0;i<count;i++)
  1734. {
  1735. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1736. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1737. continue;
  1738. else
  1739. {
  1740. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1741. newcount++;
  1742. }
  1743. }
  1744. newlist[@"count"] = @(newcount);
  1745. [self saveScanOrderList:newlist];
  1746. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1747. #ifdef RA_NOTIFICATION
  1748. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1749. #else
  1750. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1751. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1752. #endif
  1753. }
  1754. //+(void) createScanTempFolder
  1755. //{
  1756. // return;
  1757. //// NSString* scanFilePath = [self getScanTempPath];
  1758. //// BOOL bdir=YES;
  1759. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1760. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1761. //// {
  1762. ////
  1763. //// NSError *error = nil;
  1764. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1765. ////
  1766. //// if(!bsuccess)
  1767. //// DebugLog(@"Create cache folder failed");
  1768. ////
  1769. //// // if(bsuccess)
  1770. //// // {
  1771. //// // sqlite3 *db = [self get_db];
  1772. //// //
  1773. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1774. //// // [iSalesDB close_db:db];
  1775. //// // }
  1776. //// }
  1777. //}
  1778. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1779. {
  1780. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1781. NSString *documents = [paths objectAtIndex:0];
  1782. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1783. NSString* servername = addressDic[@"name"];
  1784. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1785. NSString* user = appDelegate.user;
  1786. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1787. orderpath = [orderpath stringByAppendingPathComponent:user];
  1788. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1789. BOOL bdir=YES;
  1790. NSFileManager* fileManager = [NSFileManager defaultManager];
  1791. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1792. {
  1793. NSError *error = nil;
  1794. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1795. if(!bsuccess)
  1796. DebugLog(@"Create cache folder failed");
  1797. // if(bsuccess)
  1798. // {
  1799. // sqlite3 *db = [self get_db];
  1800. //
  1801. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1802. // [iSalesDB close_db:db];
  1803. // }
  1804. }
  1805. return orderpath;
  1806. }
  1807. +(NSString*) getScanOrderPath
  1808. {
  1809. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1810. NSString *documents = [paths objectAtIndex:0];
  1811. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1812. NSString* servername = addressDic[@"name"];
  1813. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1814. NSString* user = appDelegate.user;
  1815. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1816. orderpath = [orderpath stringByAppendingPathComponent:user];
  1817. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1818. BOOL bdir=YES;
  1819. NSFileManager* fileManager = [NSFileManager defaultManager];
  1820. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1821. {
  1822. NSError *error = nil;
  1823. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1824. if(!bsuccess)
  1825. DebugLog(@"Create cache folder failed");
  1826. // if(bsuccess)
  1827. // {
  1828. // sqlite3 *db = [self get_db];
  1829. //
  1830. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1831. // [iSalesDB close_db:db];
  1832. // }
  1833. }
  1834. return orderpath;
  1835. }
  1836. //+(NSString*) getScanTempPath
  1837. //{
  1838. // return nil;
  1839. ////
  1840. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1841. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1842. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1843. //}
  1844. //+(void) moveScanTemp2Order
  1845. //{
  1846. // return;
  1847. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1848. ////
  1849. //// NSError *error = nil;
  1850. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1851. ////
  1852. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1853. //// // for(int i=0;i<arr_files.count;i++)
  1854. //// // {
  1855. //// // NSString* file=arr_files[i];
  1856. //// //
  1857. //// //
  1858. //// // }
  1859. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1860. ////
  1861. //
  1862. //}
  1863. //+(NSString*) getUserPath: (NSString*) user
  1864. //{
  1865. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1866. // NSString *documents = [paths objectAtIndex:0];
  1867. //
  1868. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1869. //
  1870. // NSString* servername = addressDic[@"name"];
  1871. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1872. //// NSString* user = appDelegate.user;
  1873. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1874. // userpath = [userpath stringByAppendingPathComponent:user];
  1875. // return userpath;
  1876. //}
  1877. +(NSString*) getUserPath
  1878. {
  1879. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1880. NSString *documents = [paths objectAtIndex:0];
  1881. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1882. NSString* servername = addressDic[@"name"];
  1883. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1884. // NSString* user = appDelegate.user;
  1885. NSString* user = RASingleton.sharedInstance.user;
  1886. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1887. userpath = [userpath stringByAppendingPathComponent:user];
  1888. return userpath;
  1889. }
  1890. +(bool) isSavedScanOrder:(NSString*) order_code
  1891. {
  1892. if(order_code == nil)
  1893. return false;
  1894. NSMutableDictionary * orderlist=[self getScanOrderList];
  1895. int count = [orderlist[@"count"] intValue];
  1896. for(int i=0;i<count;i++)
  1897. {
  1898. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1899. if([item[@"order_code"] isEqualToString: order_code ])
  1900. {
  1901. return true;
  1902. }
  1903. }
  1904. return false;
  1905. }
  1906. +(NSString*) getScanPath
  1907. {
  1908. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1909. if(appDelegate.order_code==nil)
  1910. {
  1911. appDelegate.order_code=[[NSUUID new] UUIDString];
  1912. }
  1913. return [self getScanOrderPath];
  1914. // else
  1915. // {
  1916. // [self createScanTempFolder];
  1917. // return [self getScanTempPath];
  1918. // }
  1919. }
  1920. +(void) scanCloseOrder
  1921. {
  1922. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1923. // if(appDelegate.user.length<=0)
  1924. // {
  1925. // [RAUtils message_alert:@"Please login first." title:@"Warring" controller:self];
  1926. // return;
  1927. // }
  1928. // if(appDelegate.order_code.length>0)
  1929. {
  1930. // 当前订单先关闭
  1931. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1932. appDelegate.order_code = nil;
  1933. // RASingleton.sharedInstance.scan_temp_code = nil;
  1934. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1935. { appDelegate.customerInfo = nil;
  1936. appDelegate.contact_id = nil;
  1937. }
  1938. RASingleton.sharedInstance.scan_cart = nil;
  1939. RASingleton.sharedInstance.price_type = 1;
  1940. // [appDelegate updateScanButton:false];
  1941. [appDelegate update_count_mark];
  1942. [appDelegate closeOrder];
  1943. }
  1944. }
  1945. +(NSString*) createScanTempCode
  1946. {
  1947. return [[NSUUID new] UUIDString];
  1948. }
  1949. +(NSString*) scanTemplatePath:(NSString*)file
  1950. {
  1951. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1952. NSString *documents = [paths objectAtIndex:0];
  1953. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1954. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1955. return templatefile;
  1956. }
  1957. +(void) realguestLogin
  1958. {
  1959. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1960. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1961. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1962. }
  1963. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1964. {
  1965. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1966. NSString *documents = [paths objectAtIndex:0];
  1967. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1968. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1969. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1970. return [dict mutableCopy];
  1971. }
  1972. +(void) deleteSubmittedOrder:(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. // // [appDelegate updateScanButton:false];
  1988. //
  1989. // [appDelegate update_count_mark];
  1990. // }
  1991. //删除订单目录
  1992. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1993. //维护订单列表
  1994. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1995. int count = [submitlist[@"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:@"submitlist.json"];
  2008. //
  2009. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2010. //
  2011. //
  2012. //
  2013. // // [orderlist addObject:appDelegate.order_code];
  2014. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2015. //
  2016. // }
  2017. // else
  2018. // {
  2019. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2020. //
  2021. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2022. //
  2023. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2024. // }
  2025. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2026. int newcount = 0;
  2027. for(int i=0;i<count;i++)
  2028. {
  2029. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  2030. if([item[@"order_code"] isEqualToString: order_code ])
  2031. continue;
  2032. else
  2033. {
  2034. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2035. newcount++;
  2036. }
  2037. }
  2038. newlist[@"count"] = @(newcount);
  2039. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2040. [self saveScanSubmitList:newlist];
  2041. #ifdef RA_NOTIFICATION
  2042. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2043. #else
  2044. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2045. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2046. #endif
  2047. }
  2048. +(void) updateScanOrder:(NSString*) order_code
  2049. {
  2050. #ifdef SCANNER_ORDER
  2051. NSString *userdir = [OLDataProvider getUserPath];
  2052. if(order_code.length==0)
  2053. return;
  2054. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2055. // NSString *documents = [paths objectAtIndex:0];
  2056. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2057. NSMutableDictionary* orderlist = [self getScanOrderList];
  2058. int count = [orderlist[@"count"] intValue];
  2059. if(count==0)
  2060. {
  2061. // order list 为空,不用维护
  2062. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2063. return;
  2064. }
  2065. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2066. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2067. //
  2068. //
  2069. //
  2070. // // [orderlist addObject:appDelegate.order_code];
  2071. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2072. //
  2073. // }
  2074. // else
  2075. // {
  2076. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2077. //
  2078. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2079. //
  2080. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2081. // }
  2082. //
  2083. //
  2084. // int count = [orderlist[@"count"] intValue];
  2085. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  2086. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2087. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2088. float p =0;
  2089. float s =0;
  2090. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  2091. {
  2092. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  2093. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  2094. int mcount = [cartitem[@"count"] intValue];
  2095. double msurcharge =0;
  2096. // remove net price
  2097. // if(RASingleton.sharedInstance.price_type==1)
  2098. // {
  2099. // if([cartitem[@"special_price"] boolValue])
  2100. // {
  2101. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2102. // }
  2103. // else
  2104. // {
  2105. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2106. // }
  2107. // }
  2108. s+=msurcharge;
  2109. }
  2110. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  2111. int order_qty=0;
  2112. for(int c=0;c<[section[@"count"] intValue];c++)
  2113. {
  2114. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  2115. order_qty+=[item[@"count"] intValue];
  2116. }
  2117. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  2118. // int newcount = 0;
  2119. for(int i=0;i<count;i++)
  2120. {
  2121. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2122. if([item[@"order_code"] isEqualToString: order_code ])
  2123. {
  2124. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  2125. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  2126. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  2127. }
  2128. }
  2129. // newlist[@"count"] = @(newcount);
  2130. [self saveScanOrderList:orderlist];
  2131. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2132. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2133. #endif
  2134. }
  2135. +(void) deleteScanOrder:(NSString*) order_code
  2136. {
  2137. NSString *userdir = [OLDataProvider getUserPath];
  2138. if(order_code.length==0)
  2139. return;
  2140. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2141. // NSString *documents = [paths objectAtIndex:0];
  2142. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2143. if([appDelegate.order_code isEqualToString:order_code])
  2144. {
  2145. //要删除的订单是当前打开的订单;
  2146. // appDelegate.order_code = nil;
  2147. // appDelegate.customerInfo = nil;
  2148. // RASingleton.sharedInstance.scan_cart = nil;
  2149. // appDelegate.contact_id = nil;
  2150. [self scanCloseOrder];
  2151. // [appDelegate updateScanButton:false];
  2152. [appDelegate update_count_mark];
  2153. }
  2154. //删除订单目录
  2155. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2156. //维护订单列表
  2157. NSMutableDictionary* orderlist = [self getScanOrderList];
  2158. int count = [orderlist[@"count"] intValue];
  2159. if(count==0)
  2160. {
  2161. // order list 为空,不用维护
  2162. #ifdef RA_NOTIFICATION
  2163. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2164. #else
  2165. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2166. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2167. #endif
  2168. return;
  2169. }
  2170. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2171. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2172. //
  2173. //
  2174. //
  2175. // // [orderlist addObject:appDelegate.order_code];
  2176. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2177. //
  2178. // }
  2179. // else
  2180. // {
  2181. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2182. //
  2183. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2184. //
  2185. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2186. // }
  2187. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2188. int newcount = 0;
  2189. for(int i=0;i<count;i++)
  2190. {
  2191. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2192. if([item[@"order_code"] isEqualToString: order_code ])
  2193. continue;
  2194. else
  2195. {
  2196. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2197. newcount++;
  2198. }
  2199. }
  2200. newlist[@"count"] = @(newcount);
  2201. [self saveScanOrderList:newlist];
  2202. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2203. #ifdef RA_NOTIFICATION
  2204. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2205. #else
  2206. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2207. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2208. #endif
  2209. }
  2210. #ifdef SCANNER_ORDER
  2211. +(void) scanRefreshCart
  2212. {
  2213. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2214. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2215. NSMutableDictionary * section = cart[@"section_0"];
  2216. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2217. int count = [section[@"count"] intValue];
  2218. int newcount = 0;
  2219. for(int i=0;i<count;i++)
  2220. {
  2221. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2222. NSString* model = item[@"model"];
  2223. NSDictionary* modeljson=appDelegate.scan_model[model];
  2224. if(modeljson==nil)
  2225. continue;
  2226. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2227. cartitem[@"note"]=item[@"note"];
  2228. cartitem[@"discount"]=item[@"discount"];
  2229. // [item removeObjectForKey:@"discount"];
  2230. // [item removeObjectForKey:@"note"];
  2231. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2232. newcount++;
  2233. }
  2234. newsection[@"count"]=@(newcount);
  2235. newsection[@"available"] = @(1);
  2236. newsection[@"title"] = @"Available List";
  2237. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2238. }
  2239. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2240. {
  2241. // _modelJson = modelJson;
  2242. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2243. // self.labelPrice0.text = appDelegate.price0_name;
  2244. // self.labelPrice1.text = appDelegate.price1_name;
  2245. // self.labelPrice2.text = appDelegate.price2_name;
  2246. // self.labelPrice3.text = appDelegate.price3_name;
  2247. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2248. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2249. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2250. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2251. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2252. NSLog( [RAConvertor dict2string:_modelJson]);
  2253. NSString * port=_modelJson[@"port"];
  2254. if(port.length==0)
  2255. port = @"N/A";
  2256. NSString * origin=_modelJson[@"origin"];
  2257. if(origin.length==0)
  2258. origin = @"N/A";
  2259. NSString * dimension=_modelJson[@"dimension"];
  2260. if(dimension.length==0)
  2261. dimension = @"N/A";
  2262. // _labelModel.text=_modelJson[@"model"];
  2263. // _labelDescription.text=_modelJson[@"description"];
  2264. // _labelDimension.text=dimension;
  2265. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2266. // _labelOrigin.text=origin;
  2267. // _labelPort.text=port;
  2268. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2269. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2270. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2271. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2272. if(![price0 isEqualToString: @"N/A"])
  2273. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2274. if(![price1 isEqualToString: @"N/A"])
  2275. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2276. if(![price2 isEqualToString: @"N/A"])
  2277. {
  2278. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2279. // if(RASingleton.sharedInstance.price_type==1)
  2280. _modelJson [@"special_price"] = @true;
  2281. }
  2282. if(![price3 isEqualToString: @"N/A"])
  2283. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2284. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2285. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2286. {
  2287. // if(RASingleton.sharedInstance.price_type==1)
  2288. _modelJson [@"net_price"] = @true;
  2289. // remove net price
  2290. _modelJson [@"net_price"] = @false;
  2291. }
  2292. //
  2293. // if(![price1 isEqualToString:@"N/A"])
  2294. // {
  2295. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2296. // _modelJson[@"price2"]= price2;
  2297. // }
  2298. // else
  2299. // {
  2300. // price2=@"N/A";
  2301. // }
  2302. //
  2303. // if([price0 isEqualToString:@"N/A"])
  2304. // _labelPriceCTNR.text= @"";//price0;
  2305. // else
  2306. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2307. //
  2308. // if([price3 isEqualToString:@"N/A"])
  2309. // _labelPriceNCA.text= price3;
  2310. // else
  2311. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2312. // _labelPrice25p.text=price2;
  2313. //
  2314. // if([_modelJson [@"special_price"] boolValue])
  2315. // {
  2316. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2317. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2318. // else
  2319. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2320. // }
  2321. // else
  2322. // {
  2323. // _labelPriceSpecial.text=@"";//@"N/A";
  2324. // }
  2325. //
  2326. // if([_modelJson [@"net_price"] boolValue])
  2327. // {
  2328. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2329. //// {
  2330. //// _labelPriceNet.text=_modelJson[@"price1"];
  2331. ////// _labelPriceNCA.text=@"N/A";
  2332. //// }
  2333. //// else
  2334. //// {
  2335. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2336. ////// _labelPriceNCA.text=@"N/A";
  2337. //// }
  2338. //
  2339. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2340. // {
  2341. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2342. //// _labelPriceNCA.text=@"N/A";
  2343. // }
  2344. // else
  2345. // {
  2346. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2347. //// _labelPriceNCA.text=@"N/A";
  2348. // }
  2349. //
  2350. // }
  2351. // else
  2352. // {
  2353. // _labelPriceNet.text=@"";//@"N/A";
  2354. // }
  2355. //
  2356. // {
  2357. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2358. // {
  2359. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2360. //// _labelPriceNCA.text=@"N/A";
  2361. // }
  2362. // else
  2363. // {
  2364. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2365. //// _labelPriceNet.text=@"N/A";
  2366. // }
  2367. // }
  2368. NSString * available=_modelJson[@"available"];
  2369. if(available.length==0)
  2370. available = @"N/A";
  2371. // _labelAvailable.text=available;
  2372. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2373. if(unit_cuft.length==0)
  2374. unit_cuft = @"N/A";
  2375. else
  2376. {
  2377. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2378. }
  2379. NSString * cell_price;
  2380. if(RASingleton.sharedInstance.price_type==1)
  2381. {
  2382. // if(_modelJson [@"net_price"])
  2383. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2384. // else if(_modelJson [@"special_price"])
  2385. // {
  2386. // unit_price= _modelJson[@"price2"];
  2387. // }
  2388. // else
  2389. // {
  2390. // unit_price= _modelJson[@"price1"];
  2391. // }
  2392. }
  2393. else
  2394. {
  2395. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2396. }
  2397. NSString * mpack=_modelJson[@"stockUom"];
  2398. if([mpack isEqualToString:@"N/A"])
  2399. mpack=@"1";
  2400. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2401. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2402. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2403. NSMutableDictionary* jitem = nil;
  2404. jitem = [_modelJson mutableCopy];
  2405. int stockUom =[jitem[@"stockUom"] intValue];
  2406. if(stockUom==0)
  2407. stockUom=1;
  2408. bool newitem = true;
  2409. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2410. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2411. // for(int i=0;i<count;i++)
  2412. // {
  2413. //
  2414. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2415. //
  2416. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2417. // {
  2418. //
  2419. //// int oldcount = [litem[@"stockUom"] intValue];
  2420. // newitem = false;
  2421. //
  2422. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2423. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2424. //
  2425. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2426. // break;
  2427. //
  2428. //
  2429. // }
  2430. //
  2431. //
  2432. //
  2433. // }
  2434. // if(true)
  2435. // {
  2436. jitem[@"count"]=@(stockUom);
  2437. jitem[@"check"]=@(true);
  2438. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2439. NSString * unit_price;
  2440. if(RASingleton.sharedInstance.price_type==0)
  2441. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2442. else if(_modelJson [@"special_price"])
  2443. {
  2444. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2445. }
  2446. else if(_modelJson [@"net_price"])
  2447. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2448. else
  2449. {
  2450. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2451. }
  2452. if([unit_price isEqualToString:@"N/A"])
  2453. unit_price = @"0";
  2454. else
  2455. {
  2456. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2457. }
  2458. jitem[@"unit_price"] = unit_price;
  2459. jitem[@"erp_unit_price"] = unit_price;
  2460. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2461. // section[@"count"]= @(count+1);
  2462. // count++;
  2463. // }
  2464. return jitem;
  2465. }
  2466. +(void) saveScanCart:(NSMutableDictionary*) cart
  2467. {
  2468. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2469. if(appDelegate.order_code.length<=0)
  2470. return;
  2471. if(cart==nil)
  2472. return;
  2473. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2474. // NSString *documents = [paths objectAtIndex:0];
  2475. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2476. if(RASingleton.sharedInstance.price_type==0)
  2477. {
  2478. NSMutableDictionary * section = cart[@"section_0"];
  2479. int count = [section[@"count"] intValue];
  2480. for(int i=0;i<count;i++)
  2481. {
  2482. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2483. [item removeObjectForKey:@"discount"];
  2484. [item removeObjectForKey:@"note"];
  2485. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2486. }
  2487. cart[@"section_0"]=section;
  2488. }
  2489. NSString *orderdir = [self getScanPath];
  2490. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2491. cart[@"price_group"] = appDelegate.price_group;
  2492. [RAUtils dicttofile:cartpath dict:cart];
  2493. }
  2494. #endif
  2495. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2496. {
  2497. assert(add_params[@"user"]!=nil);
  2498. // assert(add_params[@"contact_id"]!=nil);
  2499. // assert(add_params[@"password"]!=nil);
  2500. NSString* serial= [[NSUUID UUID] UUIDString];
  2501. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2502. NSString *cachefolder = [paths objectAtIndex:0];
  2503. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2504. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2505. NSFileManager* fileManager = [NSFileManager defaultManager];
  2506. BOOL bdir=YES;
  2507. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2508. {
  2509. NSError *error = nil;
  2510. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2511. if(!bsuccess)
  2512. DebugLog(@"Create UPLOAD folder failed");
  2513. }
  2514. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2515. ret[@"contact"]=[self prepareContact:serial];
  2516. ret[@"wishlist"]=[self prepareWishlist:serial];
  2517. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2518. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2519. ret[@"view_portfolio"] = [self preparePDF:serial];
  2520. NSString* str= [RAConvertor dict2string:ret];
  2521. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2522. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2523. NSError *error=nil;
  2524. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2525. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2526. ZipArchive* zip = [[ZipArchive alloc] init];
  2527. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2528. for(int i=0;i<arr_files.count;i++)
  2529. {
  2530. NSString* file=arr_files[i];
  2531. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2532. }
  2533. if( ![zip CloseZipFile2] )
  2534. {
  2535. zippath = @"";
  2536. }
  2537. ret[@"file"]=zippath;
  2538. return ret;
  2539. }
  2540. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2541. {
  2542. if(filename.length==0)
  2543. return false;
  2544. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2545. bool ret=false;
  2546. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2547. NSString *cachefolder = [paths objectAtIndex:0];
  2548. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2549. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2550. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2551. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2552. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2553. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2554. NSFileManager* fileManager = [NSFileManager defaultManager];
  2555. BOOL bdir=NO;
  2556. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2557. {
  2558. NSError *error = nil;
  2559. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2560. {
  2561. ret=false;
  2562. }
  2563. else
  2564. {
  2565. ret=true;
  2566. }
  2567. }
  2568. return ret;
  2569. }
  2570. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2571. {
  2572. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2573. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2574. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2575. NSString* where=@" 1=1";
  2576. if (ver!=nil) {
  2577. where=@"is_dirty=1";
  2578. }
  2579. sqlite3 *db = [iSalesDB get_db];
  2580. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2581. sqlite3_stmt * statement;
  2582. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2583. int count=0;
  2584. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2585. if ( dbresult== SQLITE_OK)
  2586. {
  2587. while (sqlite3_step(statement) == SQLITE_ROW)
  2588. {
  2589. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2590. int _id = sqlite3_column_int(statement, 0);
  2591. int product_id = sqlite3_column_int(statement, 1);
  2592. int item_id = sqlite3_column_int(statement, 2);
  2593. int qty = sqlite3_column_int(statement, 3);
  2594. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2595. NSString* nscreate_time=nil;
  2596. if(create_time!=nil)
  2597. {
  2598. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2599. }
  2600. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2601. NSString* nsmodify_time=nil;
  2602. if(modify_time!=nil)
  2603. {
  2604. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2605. }
  2606. int is_delete = sqlite3_column_int(statement, 6);
  2607. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2608. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2609. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2610. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2611. itemjson[@"create_time"]=nscreate_time;
  2612. itemjson[@"modify_time"]=nsmodify_time;
  2613. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2615. count++;
  2616. }
  2617. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2618. sqlite3_finalize(statement);
  2619. }
  2620. ret[@"count"]=[NSNumber numberWithInt:count ];
  2621. [iSalesDB close_db:db];
  2622. return ret;
  2623. }
  2624. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2625. {
  2626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2627. // UIApplication * app = [UIApplication sharedApplication];
  2628. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2629. assert(add_params[@"user"]!=nil);
  2630. // assert(add_params[@"password"]!=nil);
  2631. 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 ];
  2632. // 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 ];
  2633. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2634. DebugLog(@"offline_login sql:%@",sqlQuery);
  2635. sqlite3_stmt * statement;
  2636. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2637. int count=0;
  2638. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2639. if ( dbresult== SQLITE_OK)
  2640. {
  2641. while (sqlite3_step(statement) == SQLITE_ROW)
  2642. {
  2643. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2644. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2645. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2646. int product_id = sqlite3_column_int(statement, 0);
  2647. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2648. int item_id = sqlite3_column_int(statement, 7);
  2649. NSString* Price=nil;
  2650. if(str_price==nil)
  2651. {
  2652. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2653. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2654. if(price==nil)
  2655. Price=@"No Price.";
  2656. else
  2657. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2658. }
  2659. else
  2660. {
  2661. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2662. }
  2663. double discount = sqlite3_column_double(statement, 2);
  2664. int item_count = sqlite3_column_int(statement, 3);
  2665. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2666. NSString *nsline_note=nil;
  2667. if(line_note!=nil)
  2668. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2669. char *name = (char*)sqlite3_column_text(statement, 5);
  2670. NSString *nsname=nil;
  2671. if(name!=nil)
  2672. nsname= [[NSString alloc]initWithUTF8String:name];
  2673. char *description = (char*)sqlite3_column_text(statement, 6);
  2674. NSString *nsdescription=nil;
  2675. if(description!=nil)
  2676. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2677. // int stockUom = sqlite3_column_int(statement, 8);
  2678. // int _id = sqlite3_column_int(statement, 9);
  2679. //
  2680. //
  2681. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2682. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2683. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2684. // int carton=[bsubtotaljson[@"carton"] intValue];
  2685. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2686. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2687. itemjson[@"model"]=nsname;
  2688. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2689. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2690. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2691. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2692. // itemjson[@"check"]=@"true";
  2693. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2694. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2695. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2696. itemjson[@"unit_price"]=Price;
  2697. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2698. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2699. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2700. itemjson[@"note"]=nsline_note;
  2701. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2702. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2703. count++;
  2704. }
  2705. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2706. sqlite3_finalize(statement);
  2707. }
  2708. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2709. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2710. return ret;
  2711. }
  2712. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2713. {
  2714. assert(add_params[@"user"]!=nil);
  2715. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2716. sqlite3 *db = [iSalesDB get_db];
  2717. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2718. // for(int i=0;i<arr_soid.count;i++)
  2719. sqlite3_stmt * statement;
  2720. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2721. //int count=0;
  2722. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2723. if ( dbresult== SQLITE_OK)
  2724. {
  2725. int count=0;
  2726. while (sqlite3_step(statement) == SQLITE_ROW)
  2727. {
  2728. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2729. int _id = sqlite3_column_int(statement, 0);
  2730. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2731. NSString* nssync_data=nil;
  2732. if(sync_data!=nil)
  2733. {
  2734. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2735. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2736. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2737. }
  2738. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2739. NSString* nsimg_1=nil;
  2740. if(img_1!=nil)
  2741. {
  2742. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2743. [self copy_upImg:serial file:nsimg_1];
  2744. }
  2745. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2746. NSString* nsso_no=nil;
  2747. if(so_no!=nil)
  2748. {
  2749. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2750. }
  2751. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2752. itemjson[@"order_type"]=@"submit order";
  2753. else
  2754. itemjson[@"order_type"]=@"archive order";
  2755. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2756. itemjson[@"json_data"]= nssync_data;
  2757. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2758. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2759. count++;
  2760. }
  2761. ret[@"count"]=[NSNumber numberWithInt:count ];
  2762. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2763. }
  2764. sqlite3_finalize(statement);
  2765. [iSalesDB close_db:db];
  2766. return ret;
  2767. }
  2768. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2769. {
  2770. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2771. sqlite3 *db = [iSalesDB get_db];
  2772. // UIApplication * app = [UIApplication sharedApplication];
  2773. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2774. 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";
  2775. sqlite3_stmt * statement;
  2776. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2777. int count=0;
  2778. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2779. if ( dbresult== SQLITE_OK)
  2780. {
  2781. while (sqlite3_step(statement) == SQLITE_ROW)
  2782. {
  2783. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2784. int _id = sqlite3_column_int(statement, 0);
  2785. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2786. NSString* nsimg_0=nil;
  2787. if(img_0!=nil)
  2788. {
  2789. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2790. [self copy_upImg:serial file:nsimg_0];
  2791. }
  2792. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2793. NSString* nsimg_1=nil;
  2794. if(img_1!=nil)
  2795. {
  2796. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2797. [self copy_upImg:serial file:nsimg_1];
  2798. }
  2799. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2800. NSString* nsimg_2=nil;
  2801. if(img_2!=nil)
  2802. {
  2803. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2804. [self copy_upImg:serial file:nsimg_2];
  2805. }
  2806. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2807. NSString* nssync_data=nil;
  2808. if(sync_data!=nil)
  2809. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2810. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2811. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2812. itemjson[@"json_data"]= nssync_data;
  2813. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2814. count++;
  2815. }
  2816. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2817. sqlite3_finalize(statement);
  2818. }
  2819. ret[@"count"]=[NSNumber numberWithInt:count ];
  2820. [iSalesDB close_db:db];
  2821. return ret;
  2822. }
  2823. +(bool) check_offlinedata
  2824. {
  2825. // UIApplication * app = [UIApplication sharedApplication];
  2826. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2827. sqlite3 *db = [iSalesDB get_db];
  2828. NSString * where=@"1=1";
  2829. // if(appDelegate.user!=nil)
  2830. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2831. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2832. [iSalesDB close_db:db];
  2833. if(count==0)
  2834. {
  2835. return false;
  2836. }
  2837. return true;
  2838. //
  2839. //[iSalesDB close_db:db];
  2840. }
  2841. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2842. {
  2843. // UIApplication * app = [UIApplication sharedApplication];
  2844. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2845. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2846. sqlite3 *db = [iSalesDB get_db];
  2847. NSString* collectId=params[@"collectId"];
  2848. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2849. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2850. [iSalesDB execSql:sqlQuery db:db];
  2851. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2852. [iSalesDB close_db:db];
  2853. // appDelegate.wish_count =count;
  2854. //
  2855. // [appDelegate update_count_mark];
  2856. ret[@"result"]= [NSNumber numberWithInt:2];
  2857. ret[@"wish_count"]=@(count);
  2858. return ret;
  2859. }
  2860. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2861. //{
  2862. //
  2863. // UIApplication * app = [UIApplication sharedApplication];
  2864. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2865. //
  2866. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2867. // sqlite3 *db = [iSalesDB get_db];
  2868. // NSString* product_id=params[@"product_id"];
  2869. //
  2870. //
  2871. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2872. //
  2873. //
  2874. // for(int i=0;i<arr.count;i++)
  2875. // {
  2876. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2877. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2878. // if(count==0)
  2879. // {
  2880. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2881. // [iSalesDB execSql:sqlQuery db:db];
  2882. // }
  2883. // }
  2884. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2885. // [iSalesDB close_db:db];
  2886. //
  2887. // appDelegate.wish_count =count;
  2888. //
  2889. // [appDelegate update_count_mark];
  2890. // ret[@"result"]= [NSNumber numberWithInt:2];
  2891. // return ret;
  2892. // //
  2893. // //return ret;
  2894. //}
  2895. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2896. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2897. DebugLog(@"time interval: %lf",interval);
  2898. }
  2899. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2900. //{
  2901. // UIApplication * app = [UIApplication sharedApplication];
  2902. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2903. //
  2904. // int sort = [[params objectForKey:@"sort"] intValue];
  2905. // NSString *sort_str = @"";
  2906. // switch (sort) {
  2907. // case 0:{
  2908. // sort_str = @"order by w.modify_time desc";
  2909. // }
  2910. // break;
  2911. // case 1:{
  2912. // sort_str = @"order by w.modify_time asc";
  2913. // }
  2914. // break;
  2915. // case 2:{
  2916. // sort_str = @"order by m.name asc";
  2917. // }
  2918. // break;
  2919. // case 3:{
  2920. // sort_str = @"order by m.name desc";
  2921. // }
  2922. // break;
  2923. // case 4:{
  2924. // sort_str = @"order by m.description asc";
  2925. // }
  2926. // break;
  2927. //
  2928. // default:
  2929. // break;
  2930. //
  2931. // }
  2932. //
  2933. //
  2934. //// NSString* user = appDelegate.user;
  2935. //
  2936. // sqlite3 *db = [iSalesDB get_db];
  2937. //
  2938. // // order by w.create_time
  2939. // 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];
  2940. //
  2941. //// 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];
  2942. //
  2943. //
  2944. // sqlite3_stmt * statement;
  2945. //
  2946. // NSDate *date1 = [NSDate date];
  2947. //// NSDate *date2 = nil;
  2948. //
  2949. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2950. // int count=0;
  2951. //
  2952. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2953. // {
  2954. //
  2955. //
  2956. // while (sqlite3_step(statement) == SQLITE_ROW)
  2957. // {
  2958. //
  2959. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2960. //
  2961. // int product_id = sqlite3_column_double(statement, 0);
  2962. //
  2963. //
  2964. //
  2965. //
  2966. //
  2967. // char *description = (char*)sqlite3_column_text(statement, 1);
  2968. // if(description==nil)
  2969. // description= "";
  2970. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2971. //
  2972. // int item_id = sqlite3_column_double(statement, 2);
  2973. //
  2974. // NSDate *date_image = [NSDate date];
  2975. //
  2976. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2977. //
  2978. // printf("image : ");
  2979. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2980. //
  2981. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2982. // // if(url==nil)
  2983. // // url="";
  2984. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2985. //
  2986. // int qty = sqlite3_column_int(statement, 3);
  2987. //
  2988. //
  2989. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2990. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2991. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2992. // item[@"description"]= nsdescription;
  2993. // item[@"img"]= nsurl;
  2994. //
  2995. //
  2996. //
  2997. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2998. // count++;
  2999. //
  3000. // }
  3001. // printf("total time:");
  3002. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3003. //
  3004. // ret[@"count"]= [NSNumber numberWithInt:count];
  3005. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  3006. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  3007. // ret[@"result"]= [NSNumber numberWithInt:2];
  3008. //
  3009. //
  3010. // appDelegate.wish_count =count;
  3011. //
  3012. // [appDelegate update_count_mark];
  3013. // sqlite3_finalize(statement);
  3014. //
  3015. //
  3016. //
  3017. //
  3018. // }
  3019. //
  3020. // [iSalesDB close_db:db];
  3021. //
  3022. // return ret;
  3023. //}
  3024. +(NSDictionary*) offline_notimpl
  3025. {
  3026. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3027. ret[@"result"]=@"8";
  3028. ret[@"err_msg"]=@"offline mode does not support this function.";
  3029. return ret;
  3030. }
  3031. +(NSDictionary*) offline_home
  3032. {
  3033. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3034. NSString *cachefolder = [paths objectAtIndex:0];
  3035. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  3036. NSData* json =nil;
  3037. json=[NSData dataWithContentsOfFile:img_cache];
  3038. if(json==nil)
  3039. return nil;
  3040. NSError *error=nil;
  3041. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3042. return menu;
  3043. }
  3044. +(NSDictionary*) offline_category_menu
  3045. {
  3046. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3047. NSString *cachefolder = [paths objectAtIndex:0];
  3048. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  3049. NSData* json =nil;
  3050. json=[NSData dataWithContentsOfFile:img_cache];
  3051. if(json==nil)
  3052. return nil;
  3053. NSError *error=nil;
  3054. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3055. return menu;
  3056. }
  3057. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3058. {
  3059. NSString* offline_command=params[@"offline_Command"];
  3060. NSDictionary* ret=nil;
  3061. if([offline_command isEqualToString:@"model_NIYMAL"])
  3062. {
  3063. NSString* category = params[@"category"];
  3064. ret = [self refresh_model_NIYMAL:category];
  3065. }
  3066. return ret;
  3067. }
  3068. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3069. {
  3070. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3071. [ret setValue:@"2" forKey:@"result"];
  3072. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3073. sqlite3* db= [iSalesDB get_db];
  3074. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3075. [iSalesDB close_db:db];
  3076. [ret setObject:detail1_section forKey:@"detail_1"];
  3077. return ret;
  3078. }
  3079. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3080. {
  3081. //assert(user!=nil);
  3082. // UIApplication * app = [UIApplication sharedApplication];
  3083. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3084. // NSArray* arr1 = [self get_user_all_price_type];
  3085. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3086. // NSSet *set1 = [NSSet setWithArray:arr1];
  3087. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3088. // if(appDelegate.contact_id==nil)
  3089. // set2=[set1 mutableCopy];
  3090. // else
  3091. // [set2 intersectsSet:set1];
  3092. // NSArray *retarr = [set2 allObjects];
  3093. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3094. // sqlite3 *db = [iSalesDB get_db];
  3095. NSString* sqlQuery = nil;
  3096. if(contact_id==nil)
  3097. {
  3098. if(!blogin)
  3099. return nil;
  3100. 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];
  3101. }
  3102. else
  3103. 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];
  3104. sqlite3_stmt * statement;
  3105. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3106. int count=0;
  3107. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3108. {
  3109. while (sqlite3_step(statement) == SQLITE_ROW)
  3110. {
  3111. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3112. char *name = (char*)sqlite3_column_text(statement, 0);
  3113. if(name==nil)
  3114. name="";
  3115. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3116. // double price = sqlite3_column_double(statement, 1);
  3117. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3118. // if(isnull==nil)
  3119. // item[nsname]= @"No Price";
  3120. // else
  3121. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3122. char *price = (char*)sqlite3_column_text(statement, 1);
  3123. if(price!=nil)
  3124. {
  3125. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3126. nsprice=[AESCrypt fastdecrypt:nsprice];
  3127. if(nsprice.length>0)
  3128. {
  3129. double dp= [nsprice doubleValue];
  3130. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3131. }
  3132. }
  3133. else
  3134. {
  3135. item[nsname]= @"No Price";
  3136. }
  3137. // int type= sqlite3_column_int(statement, 2);
  3138. //item[@"type"]=@"price";
  3139. // item[nsname]= nsprice;
  3140. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3141. count++;
  3142. }
  3143. ret[@"count"]= [NSNumber numberWithInt:count];
  3144. sqlite3_finalize(statement);
  3145. }
  3146. // [iSalesDB close_db:db];
  3147. return ret;
  3148. }
  3149. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3150. //{
  3151. // NSArray* arr1 = [self get_user_all_price_type:db];
  3152. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3153. //
  3154. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3155. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3156. // // [set2 intersectsSet:set1];
  3157. // //
  3158. // //
  3159. // // NSArray *retarr = [set2 allObjects];
  3160. //
  3161. // NSString* whereprice=nil;
  3162. // if(contact_id==nil)
  3163. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3164. // else
  3165. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3166. //
  3167. //
  3168. // // sqlite3 *db = [iSalesDB get_db];
  3169. //
  3170. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3171. // sqlite3_stmt * statement;
  3172. //
  3173. //
  3174. // NSNumber* ret = nil;
  3175. // double dprice=DBL_MAX;
  3176. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3177. // {
  3178. //
  3179. //
  3180. // while (sqlite3_step(statement) == SQLITE_ROW)
  3181. // {
  3182. //
  3183. // // double val = sqlite3_column_double(statement, 0);
  3184. // char *price = (char*)sqlite3_column_text(statement, 0);
  3185. // if(price!=nil)
  3186. // {
  3187. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3188. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3189. // if(nsprice.length>0)
  3190. // {
  3191. // double dp= [nsprice doubleValue];
  3192. // if(dp<dprice)
  3193. // dprice=dp;
  3194. // }
  3195. // }
  3196. // }
  3197. //
  3198. //
  3199. //
  3200. //
  3201. // sqlite3_finalize(statement);
  3202. //
  3203. //
  3204. //
  3205. //
  3206. // }
  3207. //
  3208. // // [iSalesDB close_db:db];
  3209. //
  3210. // if(dprice==DBL_MAX)
  3211. // ret= nil;
  3212. // else
  3213. // ret= [NSNumber numberWithDouble:dprice];
  3214. // return ret;
  3215. //}
  3216. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3217. {
  3218. assert(user!=nil);
  3219. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3220. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3221. NSString* whereprice=nil;
  3222. if(contact_id==nil)
  3223. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3224. else
  3225. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3226. // sqlite3 *db = [iSalesDB get_db];
  3227. NSString *productIdCondition = @"1 = 1";
  3228. if (product_id) {
  3229. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3230. }
  3231. NSString *itemIdCondition = @"";
  3232. if (item_id) {
  3233. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3234. }
  3235. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3236. sqlite3_stmt * statement;
  3237. NSNumber* ret = nil;
  3238. double dprice=DBL_MAX;
  3239. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3240. {
  3241. while (sqlite3_step(statement) == SQLITE_ROW)
  3242. {
  3243. // double val = sqlite3_column_double(statement, 0);
  3244. char *price = (char*)sqlite3_column_text(statement, 0);
  3245. if(price!=nil)
  3246. {
  3247. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3248. nsprice=[AESCrypt fastdecrypt:nsprice];
  3249. if(nsprice.length>0)
  3250. {
  3251. double dp= [nsprice doubleValue];
  3252. if(dp<dprice)
  3253. dprice=dp;
  3254. }
  3255. }
  3256. }
  3257. sqlite3_finalize(statement);
  3258. }
  3259. // [iSalesDB close_db:db];
  3260. if(dprice==DBL_MAX)
  3261. ret= nil;
  3262. else
  3263. ret= [NSNumber numberWithDouble:dprice];
  3264. return ret;
  3265. }
  3266. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3267. {
  3268. assert(user!=nil);
  3269. NSArray* ret=nil;
  3270. // sqlite3 *db = [iSalesDB get_db];
  3271. // no customer assigned , use login user contact_id
  3272. // UIApplication * app = [UIApplication sharedApplication];
  3273. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3274. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3275. sqlite3_stmt * statement;
  3276. // int count=0;
  3277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3278. {
  3279. if (sqlite3_step(statement) == SQLITE_ROW)
  3280. {
  3281. char *val = (char*)sqlite3_column_text(statement, 0);
  3282. if(val==nil)
  3283. val="";
  3284. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3285. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3286. }
  3287. sqlite3_finalize(statement);
  3288. }
  3289. // [iSalesDB close_db:db];
  3290. return ret;
  3291. }
  3292. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3293. {
  3294. // sqlite3 *db = [iSalesDB get_db];
  3295. if(contact_id==nil)
  3296. {
  3297. // no customer assigned , use login user contact_id
  3298. // UIApplication * app = [UIApplication sharedApplication];
  3299. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3300. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3301. sqlite3_stmt * statement;
  3302. // int count=0;
  3303. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3304. {
  3305. if (sqlite3_step(statement) == SQLITE_ROW)
  3306. {
  3307. char *val = (char*)sqlite3_column_text(statement, 0);
  3308. if(val==nil)
  3309. val="";
  3310. contact_id = [[NSString alloc]initWithUTF8String:val];
  3311. }
  3312. sqlite3_finalize(statement);
  3313. }
  3314. if(contact_id.length<=0)
  3315. {
  3316. // [iSalesDB close_db:db];
  3317. return nil;
  3318. }
  3319. }
  3320. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3321. sqlite3_stmt * statement;
  3322. NSArray* ret=nil;
  3323. // int count=0;
  3324. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3325. {
  3326. if (sqlite3_step(statement) == SQLITE_ROW)
  3327. {
  3328. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3329. if(price_type==nil)
  3330. price_type="";
  3331. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3332. if(nsprice_type.length>0)
  3333. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3334. else
  3335. ret=nil;
  3336. }
  3337. sqlite3_finalize(statement);
  3338. }
  3339. // [iSalesDB close_db:db];
  3340. return ret;
  3341. }
  3342. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3343. {
  3344. NSString* ret= nil;
  3345. // sqlite3 *db = [iSalesDB get_db];
  3346. NSString *sqlQuery = nil;
  3347. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3348. // 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;
  3349. // 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
  3350. if(product_id==nil && model_name)
  3351. 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;
  3352. else if (product_id)
  3353. 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
  3354. sqlite3_stmt * statement;
  3355. // int count=0;
  3356. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3357. {
  3358. if (sqlite3_step(statement) == SQLITE_ROW)
  3359. {
  3360. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3361. if(imgurl==nil)
  3362. imgurl="";
  3363. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3364. ret=nsimgurl;
  3365. }
  3366. sqlite3_finalize(statement);
  3367. }
  3368. else
  3369. {
  3370. [ret setValue:@"8" forKey:@"result"];
  3371. }
  3372. // [iSalesDB close_db:db];
  3373. DebugLog(@"data string: %@",ret );
  3374. return ret;
  3375. }
  3376. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3377. {
  3378. NSString* ret= nil;
  3379. sqlite3 *db = [iSalesDB get_db];
  3380. NSString *sqlQuery = nil;
  3381. if(product_id==nil)
  3382. if(upc_code==nil)
  3383. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3384. else
  3385. 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='%@';
  3386. else
  3387. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3388. sqlite3_stmt * statement;
  3389. // int count=0;
  3390. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3391. {
  3392. if (sqlite3_step(statement) == SQLITE_ROW)
  3393. {
  3394. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3395. if(default_category==nil)
  3396. default_category="";
  3397. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3398. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3399. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3400. ret=nsdefault_category;
  3401. }
  3402. sqlite3_finalize(statement);
  3403. }
  3404. else
  3405. {
  3406. [ret setValue:@"8" forKey:@"result"];
  3407. }
  3408. [iSalesDB close_db:db];
  3409. DebugLog(@"data string: %@",ret );
  3410. return ret;
  3411. }
  3412. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3413. NSString *sql = nil;
  3414. if (product_id != nil) {
  3415. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3416. } else {
  3417. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3418. }
  3419. NSString *result_set = [iSalesDB jk_queryText:sql];
  3420. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3421. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3422. return result_set;
  3423. }
  3424. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3425. //{
  3426. //
  3427. //
  3428. // assert(params[@"user"]!=nil);
  3429. //
  3430. //
  3431. //
  3432. // NSString* model_name = [params valueForKey:@"product_name"];
  3433. //
  3434. // NSString* product_id = [params valueForKey:@"product_id"];
  3435. //
  3436. // NSString* category = [params valueForKey:@"category"];
  3437. //
  3438. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3439. // if(category==nil) {
  3440. // category = default_category_id;
  3441. // } else {
  3442. //
  3443. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3444. // // 参数重有多个category id
  3445. // if (arr_0.count > 1) {
  3446. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3447. //
  3448. // // 取交集
  3449. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3450. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3451. // [set_0 intersectSet:set_1];
  3452. //
  3453. // if (set_0.count == 1) {
  3454. // category = (NSString *)[set_0 anyObject];
  3455. // } else {
  3456. // if ([set_0 containsObject:default_category_id]) {
  3457. // category = default_category_id;
  3458. // } else {
  3459. // category = (NSString *)[set_0 anyObject];
  3460. // }
  3461. // }
  3462. // }
  3463. // }
  3464. //
  3465. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3466. //
  3467. //
  3468. //
  3469. //
  3470. //
  3471. //
  3472. // sqlite3 *db = [iSalesDB get_db];
  3473. //
  3474. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3475. //
  3476. //
  3477. // NSString *sqlQuery = nil;
  3478. //
  3479. // if(product_id==nil)
  3480. // 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='%@';
  3481. // else
  3482. //
  3483. // 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=%@;
  3484. //
  3485. //
  3486. // sqlite3_stmt * statement;
  3487. // [ret setValue:@"2" forKey:@"result"];
  3488. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3489. //
  3490. // // int count=0;
  3491. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3492. // {
  3493. //
  3494. //
  3495. // if (sqlite3_step(statement) == SQLITE_ROW)
  3496. // {
  3497. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3498. //
  3499. //
  3500. // char *name = (char*)sqlite3_column_text(statement, 0);
  3501. // if(name==nil)
  3502. // name="";
  3503. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3504. //
  3505. // char *description = (char*)sqlite3_column_text(statement, 1);
  3506. // if(description==nil)
  3507. // description="";
  3508. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3509. //
  3510. //
  3511. // int product_id = sqlite3_column_int(statement, 2);
  3512. //
  3513. //
  3514. // char *color = (char*)sqlite3_column_text(statement, 3);
  3515. // if(color==nil)
  3516. // color="";
  3517. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3518. // //
  3519. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3520. // // if(legcolor==nil)
  3521. // // legcolor="";
  3522. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3523. // //
  3524. // //
  3525. // int availability = sqlite3_column_int(statement, 5);
  3526. // //
  3527. // int incoming_stock = sqlite3_column_int(statement, 6);
  3528. //
  3529. //
  3530. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3531. // if(demension==nil)
  3532. // demension="";
  3533. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3534. //
  3535. //
  3536. //
  3537. // // ,,,,,,,,,
  3538. //
  3539. //
  3540. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3541. // if(seat_height==nil)
  3542. // seat_height="";
  3543. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3544. //
  3545. //
  3546. // char *material = (char*)sqlite3_column_text(statement, 9);
  3547. // if(material==nil)
  3548. // material="";
  3549. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3550. //
  3551. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3552. // if(box_dim==nil)
  3553. // box_dim="";
  3554. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3555. //
  3556. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3557. // if(volume==nil)
  3558. // volume="";
  3559. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3560. //
  3561. // double weight = sqlite3_column_double(statement, 12);
  3562. //
  3563. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3564. // if(model_set==nil)
  3565. // model_set="";
  3566. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3567. //
  3568. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3569. // if(load_ability==nil)
  3570. // load_ability="";
  3571. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3572. //
  3573. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3574. //// if(default_category==nil)
  3575. //// default_category="";
  3576. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3577. //
  3578. //
  3579. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3580. // if(fabric_content==nil)
  3581. // fabric_content="";
  3582. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3583. //
  3584. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3585. // if(assembling==nil)
  3586. // assembling="";
  3587. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3588. //
  3589. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3590. // if(made_in==nil)
  3591. // made_in="";
  3592. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3593. //
  3594. //
  3595. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3596. // if(special_remarks==nil)
  3597. // special_remarks="";
  3598. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3599. //
  3600. //
  3601. // int stockUcom = sqlite3_column_double(statement, 20);
  3602. //
  3603. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3604. // if(product_group==nil)
  3605. // product_group="";
  3606. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3607. //
  3608. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3609. // // if(fashion_selector==nil)
  3610. // // fashion_selector="";
  3611. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3612. //
  3613. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3614. // if(selector_field==nil)
  3615. // selector_field="";
  3616. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3617. //
  3618. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3619. // if(property_field==nil)
  3620. // property_field="";
  3621. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3622. //
  3623. //
  3624. //
  3625. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3626. // if(packaging==nil)
  3627. // packaging="";
  3628. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3629. //
  3630. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3631. //
  3632. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3633. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3634. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3635. //
  3636. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3637. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3638. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3639. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3640. //
  3641. //
  3642. //
  3643. //
  3644. //
  3645. // NSString* Availability=nil;
  3646. // if(availability>0)
  3647. // Availability=[NSString stringWithFormat:@"%d",availability];
  3648. // else
  3649. // Availability = @"Out of Stock";
  3650. //
  3651. // [img_section setValue:Availability forKey:@"Availability"];
  3652. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3653. //
  3654. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3655. //
  3656. //
  3657. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3658. // if(eta==nil)
  3659. // eta="";
  3660. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3661. // if ([nseta isEqualToString:@"null"]) {
  3662. // nseta = @"";
  3663. // }
  3664. // if (availability <= 0) {
  3665. // [img_section setValue:nseta forKey:@"ETA"];
  3666. // }
  3667. //
  3668. //
  3669. // int item_id = sqlite3_column_int(statement, 26);
  3670. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3671. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3672. //
  3673. // NSString* Price=nil;
  3674. // if(appDelegate.bLogin==false)
  3675. // Price=@"Must Sign in.";
  3676. // else
  3677. // {
  3678. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3679. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3680. // if(price==nil)
  3681. // Price=@"No Price.";
  3682. // else
  3683. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3684. // }
  3685. //
  3686. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3687. // [img_section setValue:Price forKey:@"price"];
  3688. // [img_section setValue:nsname forKey:@"model_name"];
  3689. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3690. //
  3691. // if (appDelegate.order_code) { // 离线order code即so#
  3692. //
  3693. // 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];
  3694. // __block int cartQTY = 0;
  3695. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3696. //
  3697. // cartQTY = sqlite3_column_int(stmt, 0);
  3698. //
  3699. // }];
  3700. //
  3701. // if (cartQTY > 0) {
  3702. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3703. // }
  3704. // }
  3705. //
  3706. // [ret setObject:img_section forKey:@"img_section"];
  3707. //
  3708. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3709. // int detail0_item_count=0;
  3710. //
  3711. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3712. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3713. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3714. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3715. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3716. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3717. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3718. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3719. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3720. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3721. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3722. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3723. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3724. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3725. //
  3726. //
  3727. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3728. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3729. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3730. // {
  3731. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3732. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3733. // }
  3734. //
  3735. //
  3736. //
  3737. //
  3738. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3739. // [detail0_section setValue:@"kv" forKey:@"type"];
  3740. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3741. //
  3742. // [ret setObject:detail0_section forKey:@"detail_0"];
  3743. //
  3744. //
  3745. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3746. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3747. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3748. // // [detail1_section setValue:@"content" forKey:@"type"];
  3749. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3750. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3751. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3752. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3753. // [ret setObject:detail1_section forKey:@"detail_1"];
  3754. //
  3755. //
  3756. //
  3757. //
  3758. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3759. // [detail2_section setValue:@"detail" forKey:@"target"];
  3760. // [detail2_section setValue:@"popup" forKey:@"action"];
  3761. // [detail2_section setValue:@"content" forKey:@"type"];
  3762. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3763. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3764. // [detail2_section setValue:@"local" forKey:@"data"];
  3765. // [ret setObject:detail2_section forKey:@"detail_2"];
  3766. // }
  3767. //
  3768. //
  3769. //
  3770. //
  3771. // sqlite3_finalize(statement);
  3772. // }
  3773. // else
  3774. // {
  3775. // [ret setValue:@"8" forKey:@"result"];
  3776. // }
  3777. //// DebugLog(@"count:%d",count);
  3778. //
  3779. //
  3780. // [iSalesDB close_db:db];
  3781. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3782. //
  3783. // return ret;
  3784. //}
  3785. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3786. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3787. NSString* keyword = [params valueForKey:@"keyword"];
  3788. keyword=keyword.lowercaseString;
  3789. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3790. int limit = [[params valueForKey:@"limit"] intValue];
  3791. int offset = [[params valueForKey:@"offset"] intValue];
  3792. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3793. NSString *limit_str = @"";
  3794. if (limited) {
  3795. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3796. }
  3797. sqlite3 *db = [iSalesDB get_db];
  3798. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3799. // UIApplication * app = [UIApplication sharedApplication];
  3800. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3801. NSString *sqlQuery = nil;
  3802. if(exactMatch )
  3803. 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 ;
  3804. else
  3805. 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
  3806. DebugLog(@"offline_search sql:%@",sqlQuery);
  3807. sqlite3_stmt * statement;
  3808. [ret setValue:@"2" forKey:@"result"];
  3809. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3810. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3811. // int count=0;
  3812. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3813. {
  3814. int i=0;
  3815. while (sqlite3_step(statement) == SQLITE_ROW)
  3816. {
  3817. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3818. // char *name = (char*)sqlite3_column_text(statement, 1);
  3819. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3820. char *name = (char*)sqlite3_column_text(statement, 0);
  3821. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3822. char *description = (char*)sqlite3_column_text(statement, 1);
  3823. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3824. int product_id = sqlite3_column_int(statement, 2);
  3825. // char *url = (char*)sqlite3_column_text(statement, 3);
  3826. // if(url==nil)
  3827. // url="";
  3828. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3829. int wid = sqlite3_column_int(statement, 3);
  3830. int closeout = sqlite3_column_int(statement, 4);
  3831. int cid = sqlite3_column_int(statement, 5);
  3832. int wisdelete = sqlite3_column_int(statement, 6);
  3833. int more_color = sqlite3_column_int(statement, 7);
  3834. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3835. if(wid !=0 && wisdelete != 1)
  3836. [item setValue:@"true" forKey:@"wish_exists"];
  3837. else
  3838. [item setValue:@"false" forKey:@"wish_exists"];
  3839. if(closeout==0)
  3840. [item setValue:@"false" forKey:@"is_closeout"];
  3841. else
  3842. [item setValue:@"true" forKey:@"is_closeout"];
  3843. if(cid==0)
  3844. [item setValue:@"false" forKey:@"cart_exists"];
  3845. else
  3846. [item setValue:@"true" forKey:@"cart_exists"];
  3847. if (more_color == 0) {
  3848. [item setObject:@(false) forKey:@"more_color"];
  3849. } else if (more_color == 1) {
  3850. [item setObject:@(true) forKey:@"more_color"];
  3851. }
  3852. [item addEntriesFromDictionary:imgjson];
  3853. // [item setValue:nsurl forKey:@"img"];
  3854. [item setValue:nsname forKey:@"fash_name"];
  3855. [item setValue:nsdescription forKey:@"description"];
  3856. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3857. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3858. i++;
  3859. }
  3860. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3861. [ret setObject:items forKey:@"items"];
  3862. sqlite3_finalize(statement);
  3863. }
  3864. DebugLog(@"count:%d",count);
  3865. [iSalesDB close_db:db];
  3866. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3867. return ret;
  3868. }
  3869. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3870. {
  3871. return [self search:params limited:YES];
  3872. }
  3873. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3874. //{
  3875. // UIApplication * app = [UIApplication sharedApplication];
  3876. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3877. //
  3878. //// [iSalesDB disable_trigger]
  3879. // [iSalesDB disable_trigger];
  3880. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3881. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3882. //
  3883. // [iSalesDB enable_trigger];
  3884. //// [iSalesDB enable_trigger]
  3885. // //
  3886. // // NSString* user = [params valueForKey:@"user"];
  3887. // //
  3888. // // NSString* password = [params valueForKey:@"password"];
  3889. //
  3890. //
  3891. //
  3892. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3893. //
  3894. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3895. //
  3896. //
  3897. //
  3898. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3899. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3900. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3901. //
  3902. // [appDelegate update_count_mark];
  3903. //
  3904. //
  3905. // appDelegate.can_show_price =false;
  3906. // appDelegate.can_see_price =false;
  3907. // appDelegate.can_create_portfolio =false;
  3908. // appDelegate.can_create_order =false;
  3909. //
  3910. //
  3911. // appDelegate.can_cancel_order =false;
  3912. // appDelegate.can_set_cart_price =false;
  3913. // appDelegate.can_delete_order =false;
  3914. // appDelegate.can_submit_order =false;
  3915. // appDelegate.can_set_tearsheet_price =false;
  3916. // appDelegate.can_update_contact_info = false;
  3917. //
  3918. // appDelegate.save_order_logout = false;
  3919. // appDelegate.submit_order_logout = false;
  3920. // appDelegate.alert_sold_in_quantities = false;
  3921. //
  3922. // appDelegate.ipad_perm =nil ;
  3923. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3924. // appDelegate.OrderFilter= nil;
  3925. // [appDelegate SetSo:nil];
  3926. // [appDelegate set_main_button_panel];
  3927. //
  3928. //
  3929. // // sqlite3 *db = [iSalesDB get_db];
  3930. // //
  3931. // //
  3932. // //
  3933. // //
  3934. // //
  3935. // // 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"]];
  3936. // //
  3937. // //
  3938. // //
  3939. // //
  3940. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3941. // // sqlite3_stmt * statement;
  3942. // //
  3943. // //
  3944. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3945. // //
  3946. // //
  3947. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3948. // // {
  3949. // //
  3950. // //
  3951. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3952. // // {
  3953. // //
  3954. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3955. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3956. // //
  3957. // //
  3958. // //
  3959. // // int can_show_price = sqlite3_column_int(statement, 0);
  3960. // // int can_see_price = sqlite3_column_int(statement, 1);
  3961. // //
  3962. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3963. // // if(contact_id==nil)
  3964. // // contact_id="";
  3965. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3966. // //
  3967. // // int user_type = sqlite3_column_int(statement, 3);
  3968. // //
  3969. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3970. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3971. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3972. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3973. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3974. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3975. // // int can_create_order = sqlite3_column_int(statement, 10);
  3976. // //
  3977. // //
  3978. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3979. // // if(mode==nil)
  3980. // // mode="";
  3981. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3982. // //
  3983. // //
  3984. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3985. // // if(username==nil)
  3986. // // username="";
  3987. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3988. // //
  3989. // //
  3990. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3991. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3992. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3993. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3994. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3995. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3996. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3997. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3998. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3999. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4000. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4001. // //
  4002. // // [header setValue:nsusername forKey:@"username"];
  4003. // //
  4004. // //
  4005. // // [ret setObject:header forKey:@"header"];
  4006. // // [ret setValue:nsmode forKey:@"mode"];
  4007. // //
  4008. // //
  4009. // // }
  4010. // //
  4011. // //
  4012. // //
  4013. // // sqlite3_finalize(statement);
  4014. // // }
  4015. // //
  4016. // //
  4017. // //
  4018. // // [iSalesDB close_db:db];
  4019. // //
  4020. // //
  4021. // //
  4022. // //
  4023. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4024. //
  4025. // return ret;
  4026. //}
  4027. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  4028. //{
  4029. //
  4030. // UIApplication * app = [UIApplication sharedApplication];
  4031. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4032. //
  4033. // [iSalesDB disable_trigger];
  4034. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4035. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4036. // [iSalesDB enable_trigger];
  4037. //
  4038. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4039. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4040. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  4041. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  4042. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  4043. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  4044. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  4045. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  4046. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  4047. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  4048. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  4049. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  4050. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  4051. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4052. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4053. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4054. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4055. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4056. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4057. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4058. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4059. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4060. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4061. //
  4062. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4063. //
  4064. // [arr_name addObject:customer_first_name];
  4065. // [arr_name addObject:customer_last_name];
  4066. //
  4067. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4068. //
  4069. // // default ship from
  4070. // 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';";
  4071. //
  4072. // __block NSString *cid = @"";
  4073. // __block NSString *name = @"";
  4074. // __block NSString *ext = @"";
  4075. // __block NSString *contact = @"";
  4076. // __block NSString *email = @"";
  4077. // __block NSString *fax = @"";
  4078. // __block NSString *phone = @"";
  4079. //
  4080. // sqlite3 *db = [iSalesDB get_db];
  4081. //
  4082. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4083. //
  4084. // cid = [self textAtColumn:0 statement:statment];
  4085. // name = [self textAtColumn:1 statement:statment];
  4086. // ext = [self textAtColumn:2 statement:statment];
  4087. // contact = [self textAtColumn:3 statement:statment];
  4088. // email = [self textAtColumn:4 statement:statment];
  4089. // fax = [self textAtColumn:5 statement:statment];
  4090. // phone = [self textAtColumn:6 statement:statment];
  4091. //
  4092. // }];
  4093. //
  4094. // NSString* so_id = [self get_offline_soid:db];
  4095. // if(so_id==nil)
  4096. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4097. //
  4098. // 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];
  4099. //
  4100. //
  4101. //
  4102. // int result =[iSalesDB execSql:sql_neworder db:db];
  4103. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4104. //
  4105. //
  4106. //
  4107. // //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'
  4108. // //soId
  4109. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4110. //
  4111. //
  4112. //
  4113. //
  4114. //
  4115. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4116. // sqlite3_stmt * statement;
  4117. //
  4118. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4119. // {
  4120. // if (sqlite3_step(statement) == SQLITE_ROW)
  4121. // {
  4122. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4123. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4124. //
  4125. // //ret = sqlite3_column_int(statement, 0);
  4126. //
  4127. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4128. // if(soId==nil)
  4129. // soId="";
  4130. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4131. // [ret setValue:nssoId forKey:@"soId"];
  4132. // [ret setValue:nssoId forKey:@"orderCode"];
  4133. //
  4134. // }
  4135. // sqlite3_finalize(statement);
  4136. // }
  4137. //
  4138. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4139. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4140. //
  4141. //
  4142. // [iSalesDB close_db:db];
  4143. //
  4144. // return [RAConvertor dict2data:ret];
  4145. //
  4146. //}
  4147. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4148. {
  4149. //assert(params[@"order_code"]);
  4150. // assert(params[@"order_code"]);
  4151. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4152. NSString* orderCode = [params valueForKey:@"orderCode"];
  4153. NSString* app_order_code= params[@"appDelegate.order_code"];
  4154. // UIApplication * app = [UIApplication sharedApplication];
  4155. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4156. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4157. {
  4158. [iSalesDB disable_trigger];
  4159. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4160. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4161. [iSalesDB enable_trigger];
  4162. }
  4163. sqlite3 *db = [iSalesDB get_db];
  4164. int cart_count=[self query_ordercartcount:orderCode db:db];
  4165. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4166. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4167. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4168. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4169. [iSalesDB close_db:db];
  4170. return [RAConvertor dict2data:ret];
  4171. }
  4172. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4173. {
  4174. assert(params[@"can_create_backorder"]!=nil);
  4175. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4176. sqlite3 *db = [iSalesDB get_db];
  4177. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4178. int count =0;
  4179. if(params[@"count"]!=nil)
  4180. {
  4181. count = [params[@"count"] intValue];
  4182. }
  4183. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4184. NSString* product_id=params[@"product_id"];
  4185. NSString* orderCode=params[@"orderCode"];
  4186. NSString *qty = params[@"qty"];
  4187. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4188. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4189. __block int number_of_outOfStock = 0;
  4190. for(int i=0;i<arr_id.count;i++)
  4191. {
  4192. NSInteger item_qty= count;
  4193. if (qty) {
  4194. item_qty = [qty_arr[i] integerValue];
  4195. }
  4196. if(item_qty==0)
  4197. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4198. // 检查新加Model数量是否大于库存
  4199. if (![params[@"can_create_backorder"] boolValue]) {
  4200. __block BOOL needContinue = NO;
  4201. [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) {
  4202. int availability = sqlite3_column_int(stmt, 0);
  4203. // 库存小于购买量为缺货
  4204. if (availability < item_qty || availability <= 0) {
  4205. number_of_outOfStock++;
  4206. needContinue = YES;
  4207. }
  4208. }];
  4209. if (needContinue) {
  4210. continue;
  4211. }
  4212. }
  4213. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4214. NSString* sql=nil;
  4215. sqlite3_stmt *stmt = nil;
  4216. BOOL shouldStep = NO;
  4217. if(_id<0)
  4218. {
  4219. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4220. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4221. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4222. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4223. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4224. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4225. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4226. shouldStep = YES;
  4227. }
  4228. else
  4229. {
  4230. if (qty) { // wish list move to cart
  4231. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4232. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4233. sqlite3_bind_int(stmt, 1, _id);
  4234. shouldStep = YES;
  4235. } else {
  4236. 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];
  4237. __block BOOL update = YES;
  4238. if (![params[@"can_create_backorder"] boolValue]) {
  4239. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4240. int newQTY = sqlite3_column_int(stmt, 0);
  4241. int availability = sqlite3_column_int(stmt, 1);
  4242. if (newQTY > availability) { // 库存不够
  4243. update = NO;
  4244. number_of_outOfStock++;
  4245. }
  4246. }];
  4247. }
  4248. if (update) {
  4249. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4250. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4251. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4252. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4253. shouldStep = YES;
  4254. }
  4255. }
  4256. }
  4257. if (shouldStep) {
  4258. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4259. [iSalesDB execSql:@"ROLLBACK" db:db];
  4260. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4261. [iSalesDB close_db:db];
  4262. DebugLog(@"add to cart error");
  4263. return [RAConvertor dict2data:ret];
  4264. }
  4265. }
  4266. }
  4267. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4268. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4269. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4270. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4271. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4272. if (![params[@"can_create_backorder"] boolValue]) {
  4273. if (number_of_outOfStock > 0) {
  4274. ret[@"result"]=[NSNumber numberWithInt:8];
  4275. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4276. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4277. }
  4278. }
  4279. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4280. [iSalesDB close_db:db];
  4281. return [RAConvertor dict2data:ret];
  4282. }
  4283. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4284. {
  4285. // UIApplication * app = [UIApplication sharedApplication];
  4286. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4287. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4288. 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];
  4289. sqlite3_stmt * statement;
  4290. int count=0;
  4291. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4292. {
  4293. while (sqlite3_step(statement) == SQLITE_ROW)
  4294. {
  4295. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4296. int bitem_id = sqlite3_column_int(statement, 0);
  4297. int bitem_qty = sqlite3_column_int(statement, 1);
  4298. char *name = (char*)sqlite3_column_text(statement, 2);
  4299. if(name==nil)
  4300. name="";
  4301. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4302. char *description = (char*)sqlite3_column_text(statement, 3);
  4303. if(description==nil)
  4304. description="";
  4305. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4306. double unit_price = sqlite3_column_double(statement, 4);
  4307. int use_unitprice = sqlite3_column_int(statement, 5);
  4308. if(use_unitprice!=1)
  4309. {
  4310. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4311. }
  4312. itemjson[@"model"]=nsname;
  4313. itemjson[@"description"]=nsdescription;
  4314. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4315. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4316. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4317. if(compute)
  4318. {
  4319. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4320. }
  4321. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4322. count++;
  4323. }
  4324. sqlite3_finalize(statement);
  4325. }
  4326. ret[@"count"]=@(count);
  4327. if(count==0)
  4328. return nil;
  4329. else
  4330. return ret;
  4331. }
  4332. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4333. {
  4334. //compute: add part to subtotal;
  4335. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4336. dict_item[@(item_id)]=@"1";
  4337. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4338. double cuft=0;
  4339. double weight=0;
  4340. int carton=0;
  4341. int impack=0;
  4342. 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];
  4343. sqlite3_stmt * statement;
  4344. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4345. {
  4346. if (sqlite3_step(statement) == SQLITE_ROW)
  4347. {
  4348. double ulength = sqlite3_column_double(statement, 0);
  4349. double uwidth = sqlite3_column_double(statement, 1);
  4350. double uheight = sqlite3_column_double(statement, 2);
  4351. double uweight = sqlite3_column_double(statement, 3);
  4352. double mlength = sqlite3_column_double(statement, 4);
  4353. double mwidth = sqlite3_column_double(statement, 5);
  4354. double mheight = sqlite3_column_double(statement, 6);
  4355. double mweight = sqlite3_column_double(statement, 7);
  4356. double ilength = sqlite3_column_double(statement, 8);
  4357. double iwidth = sqlite3_column_double(statement, 9);
  4358. double iheight = sqlite3_column_double(statement, 10);
  4359. double iweight = sqlite3_column_double(statement, 11);
  4360. // int pcs = sqlite3_column_int(statement,12);
  4361. int mpack = sqlite3_column_int(statement, 13);
  4362. impack = mpack;
  4363. int ipack = sqlite3_column_int(statement, 14);
  4364. double ucbf = sqlite3_column_double(statement, 15);
  4365. // double icbf = sqlite3_column_double(statement, 16);
  4366. // double mcbf = sqlite3_column_double(statement, 17);
  4367. if(ipack==0)
  4368. {
  4369. carton= count/mpack ;
  4370. weight = mweight*carton;
  4371. cuft= carton*(mlength*mwidth*mheight);
  4372. int remain=count%mpack;
  4373. if(remain==0)
  4374. {
  4375. //do nothing;
  4376. }
  4377. else
  4378. {
  4379. carton++;
  4380. weight += uweight*remain;
  4381. cuft += (ulength*uwidth*uheight)*remain;
  4382. }
  4383. }
  4384. else
  4385. {
  4386. carton = count/(mpack*ipack);
  4387. weight = mweight*carton;
  4388. cuft= carton*(mlength*mwidth*mheight);
  4389. int remain=count%(mpack*ipack);
  4390. if(remain==0)
  4391. {
  4392. // do nothing;
  4393. }
  4394. else
  4395. {
  4396. carton++;
  4397. int icarton =remain/ipack;
  4398. int iremain=remain%ipack;
  4399. weight += iweight*icarton;
  4400. cuft += (ilength*iwidth*iheight)*icarton;
  4401. if(iremain==0)
  4402. {
  4403. //do nothing;
  4404. }
  4405. else
  4406. {
  4407. weight += uweight*iremain;
  4408. cuft += (ulength*uwidth*uheight)*iremain;
  4409. }
  4410. }
  4411. }
  4412. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4413. cuft=ucbf*count;
  4414. weight= uweight*count;
  4415. #endif
  4416. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4417. cuft=ucbf*count;
  4418. weight= uweight*count;
  4419. #endif
  4420. }
  4421. sqlite3_finalize(statement);
  4422. }
  4423. if(compute)
  4424. {
  4425. NSArray * arr_count=nil;
  4426. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4427. for(int i=0;i<arr_bundle.count;i++)
  4428. {
  4429. dict_item[arr_bundle[i]]=@"1";
  4430. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4431. cuft+=[bundlejson[@"cuft"] doubleValue];
  4432. weight+=[bundlejson[@"weight"] doubleValue];
  4433. carton+=[bundlejson[@"carton"] intValue];
  4434. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4435. }
  4436. }
  4437. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4438. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4439. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4440. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4441. ret[@"items"]=dict_item;
  4442. return ret;
  4443. }
  4444. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4445. {
  4446. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4447. // 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 ];
  4448. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4449. DebugLog(@"offline_login sql:%@",sqlQuery);
  4450. sqlite3_stmt * statement;
  4451. int cart_count=0;
  4452. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4453. if ( dbresult== SQLITE_OK)
  4454. {
  4455. while (sqlite3_step(statement) == SQLITE_ROW)
  4456. {
  4457. int item_id = sqlite3_column_int(statement, 0);
  4458. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4459. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4460. }
  4461. sqlite3_finalize(statement);
  4462. }
  4463. return cart_count;
  4464. }
  4465. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4466. //{
  4467. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4468. // sqlite3 *db = [iSalesDB get_db];
  4469. // UIApplication * app = [UIApplication sharedApplication];
  4470. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4471. //
  4472. // NSString* orderCode=params[@"orderCode"];
  4473. //
  4474. // int sort = [[params objectForKey:@"sort"] intValue];
  4475. // NSString *sort_str = @"";
  4476. // switch (sort) {
  4477. // case 0:{
  4478. // sort_str = @"order by c.modify_time desc";
  4479. // }
  4480. // break;
  4481. // case 1:{
  4482. // sort_str = @"order by c.modify_time asc";
  4483. // }
  4484. // break;
  4485. // case 2:{
  4486. // sort_str = @"order by m.name asc";
  4487. // }
  4488. // break;
  4489. // case 3:{
  4490. // sort_str = @"order by m.name desc";
  4491. // }
  4492. // break;
  4493. // case 4:{
  4494. // sort_str = @"order by m.description asc";
  4495. // }
  4496. // break;
  4497. //
  4498. // default:
  4499. // break;
  4500. //
  4501. // }
  4502. //
  4503. //
  4504. //
  4505. // 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 ];
  4506. //
  4507. //
  4508. //// 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 ];
  4509. //
  4510. //
  4511. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4512. //
  4513. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4514. // sqlite3_stmt * statement;
  4515. //
  4516. //
  4517. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4518. //
  4519. // NSDate *date1 = [NSDate date];
  4520. //
  4521. // int count=0;
  4522. // int cart_count=0;
  4523. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4524. // if ( dbresult== SQLITE_OK)
  4525. // {
  4526. //
  4527. //
  4528. // while (sqlite3_step(statement) == SQLITE_ROW)
  4529. // {
  4530. //// NSDate *row_date = [NSDate date];
  4531. //
  4532. //
  4533. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4534. //
  4535. //
  4536. //
  4537. //
  4538. // int product_id = sqlite3_column_int(statement, 0);
  4539. //
  4540. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4541. //
  4542. // int item_id = sqlite3_column_int(statement, 7);
  4543. //
  4544. // NSString* Price=nil;
  4545. // if(str_price==nil)
  4546. // {
  4547. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4548. //// NSDate *price_date = [NSDate date];
  4549. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4550. //// DebugLog(@"price time interval");
  4551. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4552. //
  4553. // if(price==nil)
  4554. // Price=@"No Price.";
  4555. // else
  4556. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4557. // }
  4558. // else
  4559. // {
  4560. //
  4561. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4562. // }
  4563. //
  4564. //
  4565. // double discount = sqlite3_column_double(statement, 2);
  4566. // int item_count = sqlite3_column_int(statement, 3);
  4567. //
  4568. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4569. // NSString *nsline_note=nil;
  4570. // if(line_note!=nil)
  4571. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4572. //
  4573. //
  4574. // char *name = (char*)sqlite3_column_text(statement, 5);
  4575. // NSString *nsname=nil;
  4576. // if(name!=nil)
  4577. // nsname= [[NSString alloc]initWithUTF8String:name];
  4578. //
  4579. // char *description = (char*)sqlite3_column_text(statement, 6);
  4580. // NSString *nsdescription=nil;
  4581. // if(description!=nil)
  4582. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4583. //
  4584. //
  4585. //
  4586. // int stockUom = sqlite3_column_int(statement, 8);
  4587. // int _id = sqlite3_column_int(statement, 9);
  4588. // int availability = sqlite3_column_int(statement, 10);
  4589. //
  4590. //// NSDate *subtotal_date = [NSDate date];
  4591. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4592. //// DebugLog(@"subtotal_date time interval");
  4593. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4594. //
  4595. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4596. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4597. // int carton=[bsubtotaljson[@"carton"] intValue];
  4598. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4599. //
  4600. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4601. //// NSDate *img_date = [NSDate date];
  4602. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4603. //// DebugLog(@"img_date time interval");
  4604. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4605. //
  4606. // itemjson[@"model"]=nsname;
  4607. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4608. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4609. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4610. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4611. // itemjson[@"check"]=@"true";
  4612. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4613. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4614. // itemjson[@"unit_price"]=Price;
  4615. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4616. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4617. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4618. // itemjson[@"note"]=nsline_note;
  4619. // if (!appDelegate.can_create_backorder) {
  4620. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4621. // }
  4622. //// NSDate *date2 = [NSDate date];
  4623. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4624. //// DebugLog(@"model_bundle time interval");
  4625. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4626. //
  4627. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4628. // count++;
  4629. //
  4630. //// DebugLog(@"row time interval");
  4631. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4632. // }
  4633. //
  4634. //
  4635. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4636. // sqlite3_finalize(statement);
  4637. // }
  4638. //
  4639. //
  4640. // DebugLog(@"request cart total time interval");
  4641. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4642. //
  4643. //
  4644. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4645. //
  4646. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4647. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4648. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4649. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4650. //
  4651. // ret[@"mode"]=@"Regular Mode";
  4652. //
  4653. // [iSalesDB close_db:db];
  4654. //
  4655. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4656. //
  4657. // DebugLog(@"general notes :%@",general_note);
  4658. //
  4659. // ret[@"general_note"]= general_note;
  4660. //
  4661. // return [RAConvertor dict2data:ret];
  4662. //}
  4663. +(NSData*) offline_login :(NSMutableDictionary *) params
  4664. {
  4665. NSString* user = [params valueForKey:@"user"];
  4666. NSString* password = [params valueForKey:@"password"];
  4667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4668. sqlite3 *db = [iSalesDB get_db];
  4669. 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"]];
  4670. DebugLog(@"offline_login sql:%@",sqlQuery);
  4671. sqlite3_stmt * statement;
  4672. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4673. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4674. {
  4675. if (sqlite3_step(statement) == SQLITE_ROW)
  4676. {
  4677. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4678. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4679. int can_show_price = sqlite3_column_int(statement, 0);
  4680. int can_see_price = sqlite3_column_int(statement, 1);
  4681. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4682. if(contact_id==nil)
  4683. contact_id="";
  4684. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4685. int user_type = sqlite3_column_int(statement, 3);
  4686. int can_cancel_order = sqlite3_column_int(statement, 4);
  4687. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4688. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4689. int can_delete_order = sqlite3_column_int(statement, 7);
  4690. int can_submit_order = sqlite3_column_int(statement, 8);
  4691. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4692. int can_create_order = sqlite3_column_int(statement, 10);
  4693. char *mode = (char*)sqlite3_column_text(statement, 11);
  4694. if(mode==nil)
  4695. mode="";
  4696. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4697. char *username = (char*)sqlite3_column_text(statement, 12);
  4698. if(username==nil)
  4699. username="";
  4700. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4701. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4702. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4703. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4704. [header setValue:nscontact_id forKey:@"contact_id"];
  4705. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4706. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4707. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4708. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4709. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4710. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4711. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4712. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4713. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4714. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4715. [header setValue:nsusername forKey:@"username"];
  4716. NSError* error=nil;
  4717. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4718. [header setValue:statusFilter forKey:@"statusFilter"];
  4719. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4720. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4721. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4722. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4723. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4724. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4725. [ret setObject:header forKey:@"header"];
  4726. [ret setValue:nsmode forKey:@"mode"];
  4727. }
  4728. sqlite3_finalize(statement);
  4729. }
  4730. [iSalesDB close_db:db];
  4731. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4732. return [RAConvertor dict2data:ret];
  4733. }
  4734. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4735. {
  4736. assert(params[@"mode"]!=nil);
  4737. NSString* contactId = [params valueForKey:@"contactId"];
  4738. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4739. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4740. sqlite3 *db = [iSalesDB get_db];
  4741. NSString *sqlQuery = nil;
  4742. {
  4743. 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];
  4744. }
  4745. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4746. sqlite3_stmt * statement;
  4747. [ret setValue:@"2" forKey:@"result"];
  4748. int carrier = -1;
  4749. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4750. {
  4751. //int i = 0;
  4752. if (sqlite3_step(statement) == SQLITE_ROW)
  4753. {
  4754. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4755. // int editable = sqlite3_column_int(statement, 0);
  4756. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4757. NSString *nscompany_name =nil;
  4758. if(company_name==nil)
  4759. nscompany_name=@"";
  4760. else
  4761. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4762. char *country = (char*)sqlite3_column_text(statement, 2);
  4763. if(country==nil)
  4764. country="";
  4765. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4766. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4767. // if(addr==nil)
  4768. // addr="";
  4769. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4770. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4771. if(zipcode==nil)
  4772. zipcode="";
  4773. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4774. char *state = (char*)sqlite3_column_text(statement, 5);
  4775. if(state==nil)
  4776. state="";
  4777. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4778. char *city = (char*)sqlite3_column_text(statement, 6);
  4779. if(city==nil)
  4780. city="";
  4781. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4782. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4783. // NSString *nscontact_name = nil;
  4784. // if(contact_name==nil)
  4785. // nscontact_name=@"";
  4786. // else
  4787. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4788. char *phone = (char*)sqlite3_column_text(statement, 8);
  4789. NSString *nsphone = nil;
  4790. if(phone==nil)
  4791. nsphone=@"";
  4792. else
  4793. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4794. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4795. // if(contact_id==nil)
  4796. // contact_id="";
  4797. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4798. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4799. if(addr_1==nil)
  4800. addr_1="";
  4801. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4802. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4803. if(addr_2==nil)
  4804. addr_2="";
  4805. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4806. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4807. if(addr_3==nil)
  4808. addr_3="";
  4809. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4810. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4811. if(addr_4==nil)
  4812. addr_4="";
  4813. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4814. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4815. if(first_name==nil)
  4816. first_name="";
  4817. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4818. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4819. if(last_name==nil)
  4820. last_name="";
  4821. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4822. char *fax = (char*)sqlite3_column_text(statement, 16);
  4823. NSString *nsfax = nil;
  4824. if(fax==nil)
  4825. nsfax=@"";
  4826. else
  4827. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4828. char *email = (char*)sqlite3_column_text(statement, 17);
  4829. NSString *nsemail = nil;
  4830. if(email==nil)
  4831. nsemail=@"";
  4832. else
  4833. nsemail= [[NSString alloc]initWithUTF8String:email];
  4834. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4835. NSString *nsimg_0 = nil;
  4836. if(img_0==nil)
  4837. nsimg_0=@"";
  4838. else
  4839. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4840. [self copy_bcardImg:nsimg_0];
  4841. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4842. NSString *nsimg_1 = nil;
  4843. if(img_1==nil)
  4844. nsimg_1=@"";
  4845. else
  4846. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4847. [self copy_bcardImg:nsimg_1];
  4848. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4849. NSString *nsimg_2 = nil;
  4850. if(img_2==nil)
  4851. nsimg_2=@"";
  4852. else
  4853. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4854. [self copy_bcardImg:nsimg_2];
  4855. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4856. NSString *nsprice_type = nil;
  4857. if(price_type==nil)
  4858. nsprice_type=@"";
  4859. else
  4860. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4861. char *notes = (char*)sqlite3_column_text(statement, 22);
  4862. NSString *nsnotes = nil;
  4863. if(notes==nil)
  4864. nsnotes=@"";
  4865. else
  4866. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4867. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4868. NSString *nssalesrep = nil;
  4869. if(salesrep==nil)
  4870. nssalesrep=@"";
  4871. else
  4872. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4873. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4874. carrier = sqlite3_column_int(statement, 25);
  4875. {
  4876. // decrypt
  4877. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4878. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4879. nsphone=[AESCrypt fastdecrypt:nsphone];
  4880. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4881. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4882. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4883. }
  4884. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4885. [arr_name addObject:nsfirst_name];
  4886. [arr_name addObject:nslast_name];
  4887. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4888. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4889. [arr_ext addObject:nsaddr_1];
  4890. [arr_ext addObject:nsaddr_2];
  4891. [arr_ext addObject:nsaddr_3];
  4892. [arr_ext addObject:nsaddr_4];
  4893. [arr_ext addObject:@"\r\n"];
  4894. [arr_ext addObject:nscity];
  4895. [arr_ext addObject:nsstate];
  4896. [arr_ext addObject:nszipcode];
  4897. [arr_ext addObject:nscountry];
  4898. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4899. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4900. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4901. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4902. [item setValue:nscountry forKey:@"customer_country"];
  4903. [item setValue:nsphone forKey:@"customer_phone"];
  4904. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4905. [item setValue:nscompany_name forKey:@"customer_name"];
  4906. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4907. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4908. [item setValue:nsext forKey:@"customer_contact_ext"];
  4909. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4910. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4911. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4912. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4913. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4914. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4915. [item setValue:nslast_name forKey:@"customer_last_name"];
  4916. [item setValue:nscity forKey:@"customer_city"];
  4917. [item setValue:nsstate forKey:@"customer_state"];
  4918. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4919. [item setValue:contactId forKey:@"customer_cid"];
  4920. [item setValue:nscontact_name forKey:@"customer_contact"];
  4921. [item setValue:nsfax forKey:@"customer_fax"];
  4922. [item setValue:nsemail forKey:@"customer_email"];
  4923. [item setValue:contact_type forKey:@"customer_contact_type"];
  4924. // i++;
  4925. }
  4926. // UIApplication * app = [UIApplication sharedApplication];
  4927. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4928. [ret setValue:params[@"mode"] forKey:@"mode"];
  4929. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4930. sqlite3_finalize(statement);
  4931. }
  4932. [iSalesDB close_db:db];
  4933. NSString* scarrier = [self offline_getCarrier:carrier];
  4934. item[@"customer_truck_carrier"] = scarrier;
  4935. [ret setObject:item forKey:@"customerInfo"];
  4936. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4937. return ret;
  4938. }
  4939. + (bool) copy_bcardImg:(NSString*) filename
  4940. {
  4941. if(filename.length==0)
  4942. return false;
  4943. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4944. bool ret=false;
  4945. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4946. NSString *cachefolder = [paths objectAtIndex:0];
  4947. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4948. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4949. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4950. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4951. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4952. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4953. //
  4954. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4955. NSFileManager* fileManager = [NSFileManager defaultManager];
  4956. BOOL bdir=NO;
  4957. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4958. {
  4959. NSError *error = nil;
  4960. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4961. {
  4962. ret=false;
  4963. }
  4964. else
  4965. {
  4966. ret=true;
  4967. }
  4968. // NSError *error = nil;
  4969. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4970. //
  4971. // if(!bsuccess)
  4972. // {
  4973. // DebugLog(@"Create offline_createimg folder failed");
  4974. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4975. // return [RAConvertor dict2data:ret];
  4976. // }
  4977. // if(bsuccess)
  4978. // {
  4979. // sqlite3 *db = [self get_db];
  4980. //
  4981. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4982. // [iSalesDB close_db:db];
  4983. // }
  4984. }
  4985. return ret;
  4986. //
  4987. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4988. // if(bsuccess)
  4989. // {
  4990. // NSError *error = nil;
  4991. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4992. // {
  4993. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4994. // }
  4995. // else
  4996. // {
  4997. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4998. //
  4999. // ret[@"img_url_aname"]=filename;
  5000. // ret[@"img_url"]=savedImagePath;
  5001. // }
  5002. // }
  5003. }
  5004. +(NSData *) offline_saveBusinesscard:(NSData *) image
  5005. {
  5006. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5007. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  5008. NSString *cachefolder = [paths objectAtIndex:0];
  5009. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  5010. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  5011. NSFileManager* fileManager = [NSFileManager defaultManager];
  5012. BOOL bdir=YES;
  5013. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  5014. {
  5015. NSError *error = nil;
  5016. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  5017. if(!bsuccess)
  5018. {
  5019. DebugLog(@"Create offline_createimg folder failed");
  5020. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5021. return [RAConvertor dict2data:ret];
  5022. }
  5023. // if(bsuccess)
  5024. // {
  5025. // sqlite3 *db = [self get_db];
  5026. //
  5027. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5028. // [iSalesDB close_db:db];
  5029. // }
  5030. }
  5031. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  5032. {
  5033. NSError *error = nil;
  5034. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  5035. if(!bsuccess)
  5036. {
  5037. DebugLog(@"Create img_cache folder failed");
  5038. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5039. return [RAConvertor dict2data:ret];
  5040. }
  5041. // if(bsuccess)
  5042. // {
  5043. // sqlite3 *db = [self get_db];
  5044. //
  5045. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5046. // [iSalesDB close_db:db];
  5047. // }
  5048. }
  5049. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  5050. //JEPG格式
  5051. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  5052. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  5053. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  5054. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  5055. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  5056. if(bsuccess)
  5057. {
  5058. NSError *error = nil;
  5059. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5060. {
  5061. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5062. }
  5063. else
  5064. {
  5065. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5066. ret[@"img_url_aname"]=filename;
  5067. ret[@"img_url"]=filename;
  5068. }
  5069. }
  5070. else
  5071. {
  5072. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5073. }
  5074. return [RAConvertor dict2data:ret];
  5075. }
  5076. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5077. {
  5078. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5079. category = [category substringToIndex:3];
  5080. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5081. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5082. params[@"category"]= category;
  5083. ret[@"params"]= params;
  5084. [ret setValue:@"detail" forKey:@"target"];
  5085. [ret setValue:@"popup" forKey:@"action"];
  5086. [ret setValue:@"content" forKey:@"type"];
  5087. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5088. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5089. [ret setValue:@"true" forKey:@"single_row"];
  5090. [ret setValue:@"true" forKey:@"partial_refresh"];
  5091. // sqlite3 *db = [iSalesDB get_db];
  5092. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5093. NSString *sqlQuery =nil;
  5094. #ifdef BUILD_NPD
  5095. 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 ;
  5096. #else
  5097. 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];
  5098. #endif
  5099. sqlite3_stmt * statement;
  5100. int count = 0;
  5101. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5102. // int count=0;
  5103. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5104. {
  5105. int i=0;
  5106. while (sqlite3_step(statement) == SQLITE_ROW)
  5107. {
  5108. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5109. // char *name = (char*)sqlite3_column_text(statement, 1);
  5110. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5111. char *name = (char*)sqlite3_column_text(statement, 0);
  5112. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5113. int product_id = sqlite3_column_int(statement, 1);
  5114. char *url = (char*)sqlite3_column_text(statement, 2);
  5115. if(url==nil)
  5116. url="";
  5117. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5118. [item setValue:nsurl forKey:@"picture_path"];
  5119. [item setValue:nsname forKey:@"fash_name"];
  5120. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5121. [item setValue:category forKey:@"category"];
  5122. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5123. i++;
  5124. }
  5125. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5126. sqlite3_finalize(statement);
  5127. }
  5128. DebugLog(@"count:%d",count);
  5129. // [iSalesDB close_db:db];
  5130. return ret;
  5131. }
  5132. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5133. {
  5134. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5135. [ret setValue:key forKey:@"key"];
  5136. [ret setValue:value forKey:@"val"];
  5137. [ret setValue:@"price" forKey:@"type"];
  5138. return ret;
  5139. }
  5140. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5141. {
  5142. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5143. [ret setValue:key forKey:@"key"];
  5144. [ret setValue:value forKey:@"val"];
  5145. return ret;
  5146. }
  5147. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5148. {
  5149. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5150. [ret setValue:@"0" forKey:@"img_count"];
  5151. // sqlite3 *db = [iSalesDB get_db];
  5152. 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 ;
  5153. sqlite3_stmt * statement;
  5154. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5155. {
  5156. int i=0;
  5157. if (sqlite3_step(statement) == SQLITE_ROW)
  5158. {
  5159. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5160. // char *name = (char*)sqlite3_column_text(statement, 1);
  5161. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5162. char *value = (char*)sqlite3_column_text(statement, 0);
  5163. if(value==nil)
  5164. value="";
  5165. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5166. char *key = (char*)sqlite3_column_text(statement, 1);
  5167. if(key==nil)
  5168. key="";
  5169. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5170. [item setValue:nsvalue forKey:@"val"];
  5171. [item setValue:nskey forKey:@"key"];
  5172. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5173. [ret setValue:@"1" forKey:@"count"];
  5174. i++;
  5175. }
  5176. sqlite3_finalize(statement);
  5177. }
  5178. // [iSalesDB close_db:db];
  5179. return ret;
  5180. }
  5181. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5182. {
  5183. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5184. [ret setValue:@"0" forKey:@"count"];
  5185. // sqlite3 *db = [iSalesDB get_db];
  5186. 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;
  5187. sqlite3_stmt * statement;
  5188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5189. {
  5190. int i=0;
  5191. while (sqlite3_step(statement) == SQLITE_ROW)
  5192. {
  5193. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5194. // char *name = (char*)sqlite3_column_text(statement, 1);
  5195. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5196. char *value = (char*)sqlite3_column_text(statement, 0);
  5197. if(value==nil)
  5198. value="";
  5199. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5200. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5201. if(selector_display==nil)
  5202. selector_display="";
  5203. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5204. int product_id = sqlite3_column_int(statement, 2);
  5205. char *category = (char*)sqlite3_column_text(statement, 3);
  5206. if(category==nil)
  5207. category="";
  5208. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5209. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5210. [item setValue:nsvalue forKey:@"title"];
  5211. [item setValue:url forKey:@"pic_url"];
  5212. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5213. [params setValue:@"2" forKey:@"count"];
  5214. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5215. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5216. [param0 setValue:@"product_id" forKey:@"name"];
  5217. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5218. [param1 setValue:nscategory forKey:@"val"];
  5219. [param1 setValue:@"category" forKey:@"name"];
  5220. [params setObject:param0 forKey:@"param_0"];
  5221. [params setObject:param1 forKey:@"param_1"];
  5222. [item setObject:params forKey:@"params"];
  5223. [ret setValue:nsselector_display forKey:@"name"];
  5224. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5225. i++;
  5226. }
  5227. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5228. [ret setValue:@"switch" forKey:@"action"];
  5229. sqlite3_finalize(statement);
  5230. }
  5231. // [iSalesDB close_db:db];
  5232. return ret;
  5233. }
  5234. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5235. {
  5236. // model 在 category search 显示的图片。
  5237. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5238. // sqlite3 *db = [iSalesDB get_db];
  5239. 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];
  5240. sqlite3_stmt * statement;
  5241. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5242. {
  5243. while (sqlite3_step(statement) == SQLITE_ROW)
  5244. {
  5245. char *url = (char*)sqlite3_column_text(statement, 0);
  5246. if(url==nil)
  5247. url="";
  5248. int type = sqlite3_column_int(statement, 1);
  5249. if(type==0)
  5250. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5251. else
  5252. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5253. }
  5254. sqlite3_finalize(statement);
  5255. }
  5256. // [iSalesDB close_db:db];
  5257. return ret;
  5258. }
  5259. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5260. {
  5261. int item_id=-1;
  5262. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5263. sqlite3_stmt * statement;
  5264. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5265. {
  5266. if (sqlite3_step(statement) == SQLITE_ROW)
  5267. {
  5268. item_id = sqlite3_column_int(statement, 0);
  5269. }
  5270. sqlite3_finalize(statement);
  5271. }
  5272. return item_id;
  5273. }
  5274. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5275. {
  5276. // NSString* ret = @"";
  5277. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5278. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5279. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5280. sqlite3_stmt * statement;
  5281. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5282. {
  5283. while (sqlite3_step(statement) == SQLITE_ROW)
  5284. {
  5285. int bitem_id = sqlite3_column_int(statement, 0);
  5286. int bitem_qty = sqlite3_column_int(statement, 1);
  5287. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5288. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5289. }
  5290. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5291. sqlite3_finalize(statement);
  5292. }
  5293. // if(ret==nil)
  5294. // ret=@"";
  5295. *count=arr_count;
  5296. return arr_bundle;
  5297. }
  5298. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5299. {
  5300. // get default sold qty, return -1 if model not found;
  5301. int ret = -1;
  5302. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5303. sqlite3_stmt * statement;
  5304. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5305. {
  5306. if (sqlite3_step(statement) == SQLITE_ROW)
  5307. {
  5308. ret = sqlite3_column_int(statement, 0);
  5309. }
  5310. sqlite3_finalize(statement);
  5311. }
  5312. return ret;
  5313. }
  5314. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5315. {
  5316. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5317. [ret setValue:@"0" forKey:@"img_count"];
  5318. // sqlite3 *db = [iSalesDB get_db];
  5319. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5320. sqlite3_stmt * statement;
  5321. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5322. {
  5323. int i=0;
  5324. while (sqlite3_step(statement) == SQLITE_ROW)
  5325. {
  5326. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5327. // char *name = (char*)sqlite3_column_text(statement, 1);
  5328. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5329. char *url = (char*)sqlite3_column_text(statement, 0);
  5330. if(url==nil)
  5331. url="";
  5332. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5333. [item setValue:nsurl forKey:@"s"];
  5334. [item setValue:nsurl forKey:@"l"];
  5335. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5336. i++;
  5337. }
  5338. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5339. sqlite3_finalize(statement);
  5340. }
  5341. // [iSalesDB close_db:db];
  5342. return ret;
  5343. }
  5344. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5345. // UIApplication * app = [UIApplication sharedApplication];
  5346. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5347. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5348. sqlite3 *db = [iSalesDB get_db];
  5349. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5350. NSString* product_id=params[@"product_id"];
  5351. NSString *item_count_str = params[@"item_count"];
  5352. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5353. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5354. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5355. // NSString *sql = @"";
  5356. for(int i=0;i<arr.count;i++)
  5357. {
  5358. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5359. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5360. __block int cart_count = 0;
  5361. if (!item_count_str) {
  5362. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5363. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5364. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5365. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5366. cart_count = [[model_set_components lastObject] intValue];
  5367. }];
  5368. }
  5369. if(count==0)
  5370. {
  5371. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5372. sqlite3_stmt *stmt;
  5373. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5374. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5375. if (item_count_arr) {
  5376. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5377. } else {
  5378. sqlite3_bind_int(stmt,2,cart_count);
  5379. }
  5380. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5381. [iSalesDB execSql:@"ROLLBACK" db:db];
  5382. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5383. [iSalesDB close_db:db];
  5384. DebugLog(@"add to wishlist error");
  5385. return ret;
  5386. }
  5387. } else {
  5388. int qty = 0;
  5389. if (item_count_arr) {
  5390. qty = [item_count_arr[i] intValue];
  5391. } else {
  5392. qty = cart_count;
  5393. }
  5394. 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]];
  5395. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5396. [iSalesDB execSql:@"ROLLBACK" db:db];
  5397. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5398. [iSalesDB close_db:db];
  5399. DebugLog(@"add to wishlist error");
  5400. return ret;
  5401. }
  5402. }
  5403. }
  5404. // [iSalesDB execSql:sql db:db];
  5405. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5406. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5407. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5408. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5409. [iSalesDB close_db:db];
  5410. ret[@"wish_count"]=@(count);
  5411. ret[@"result"]= [NSNumber numberWithInt:2];
  5412. return ret;
  5413. }
  5414. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5415. // 0 category
  5416. // 1 search
  5417. // 2 itemsearch
  5418. NSData *ret = nil;
  5419. NSDictionary *items = nil;
  5420. switch (from) {
  5421. case 0:{
  5422. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5423. }
  5424. break;
  5425. case 1:{
  5426. items = [[self search:params limited:NO] objectForKey:@"items"];
  5427. }
  5428. break;
  5429. case 2:{
  5430. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5431. }
  5432. break;
  5433. default:
  5434. break;
  5435. }
  5436. if (!items) {
  5437. return ret;
  5438. }
  5439. int count = [[items objectForKey:@"count"] intValue];
  5440. NSMutableString *product_id_str = [@"" mutableCopy];
  5441. for (int i = 0; i < count; i++) {
  5442. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5443. NSDictionary *item = [items objectForKey:key];
  5444. NSString *product_id = [item objectForKey:@"product_id"];
  5445. if (i == 0) {
  5446. [product_id_str appendString:product_id];
  5447. } else {
  5448. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5449. }
  5450. }
  5451. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5452. if ([add_to isEqualToString:@"cart"]) {
  5453. NSString *order_code = [params objectForKey:@"orderCode"];
  5454. if (order_code.length) {
  5455. NSDictionary *newParams = @{
  5456. @"product_id" : product_id_str,
  5457. @"orderCode" : order_code,
  5458. @"can_create_backorder":params[@"can_create_backorder"]
  5459. };
  5460. ret = [self offline_add2cart:newParams.mutableCopy];
  5461. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5462. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5463. ret = [RAConvertor dict2data:retDic];
  5464. }
  5465. } else if([add_to isEqualToString:@"wishlist"]) {
  5466. NSDictionary *newParams = @{
  5467. @"product_id" : product_id_str
  5468. };
  5469. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5470. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5471. ret = [RAConvertor dict2data:retDic];
  5472. } else if([add_to isEqualToString:@"portfolio"]) {
  5473. NSDictionary *newParams = @{
  5474. @"product_id" : product_id_str
  5475. };
  5476. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5477. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5478. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5479. ret = [RAConvertor dict2data:retDic];
  5480. }
  5481. return ret;
  5482. }
  5483. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5484. {
  5485. return [self addAll:params from:0];
  5486. }
  5487. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5488. {
  5489. return [self addAll:params from:1];
  5490. }
  5491. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5492. {
  5493. return [self addAll:params from:2];
  5494. }
  5495. #pragma mark - Jack
  5496. #warning 做SQL操作时转义!!
  5497. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5498. // "val_227" : {
  5499. // "check" : 1,
  5500. // "value" : "US United States",
  5501. // "value_id" : "228"
  5502. // },
  5503. if (!countryCode) {
  5504. countryCode = @"US";
  5505. }
  5506. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5507. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5508. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5509. int code_id = sqlite3_column_int(stmt, 3); // id
  5510. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5511. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5512. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5513. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5514. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5515. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5516. }
  5517. long n = *count;
  5518. *count = n + 1;
  5519. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5520. [container setValue:countryDic forKey:key];
  5521. }] mutableCopy];
  5522. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5523. return ret;
  5524. }
  5525. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5526. // "val_227" : {
  5527. // "check" : 1,
  5528. // "value" : "US United States",
  5529. // "value_id" : "228"
  5530. // },
  5531. __block NSString* OrderCarrier =@"";
  5532. // sqlite3 *db = [iSalesDB get_db];
  5533. // sqlite3_stmt * statement;
  5534. [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) {
  5535. OrderCarrier = [self textAtColumn:0 statement:statment];
  5536. // sqlite3_finalize(statement);
  5537. }];
  5538. // [iSalesDB close_db:db];
  5539. __block int c=0;
  5540. 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) {
  5541. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5542. int code_id = sqlite3_column_int(stmt, 2); // id
  5543. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5544. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5545. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5546. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5547. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5548. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5549. }
  5550. c++;
  5551. long n = *count;
  5552. *count = n + 1;
  5553. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5554. [container setValue:carrierDic forKey:key];
  5555. }] mutableCopy];
  5556. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5557. return ret;
  5558. }
  5559. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5560. // "val_227" : {
  5561. // "check" : 1,
  5562. // "value" : "US United States",
  5563. // "value_id" : "228"
  5564. // },
  5565. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5566. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5567. int code_id = sqlite3_column_int(stmt, 2); // id
  5568. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5569. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5570. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5571. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5572. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5573. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5574. }
  5575. long n = *count;
  5576. *count = n + 1;
  5577. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5578. [container setValue:carrierDic forKey:key];
  5579. }] mutableCopy];
  5580. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5581. return ret;
  5582. }
  5583. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5584. // // "val_227" : {
  5585. // // "check" : 1,
  5586. // // "value" : "US United States",
  5587. // // "value_id" : "228"
  5588. // // },
  5589. //
  5590. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5591. // NSString * code = [iSalesDB jk_queryText:Sql];
  5592. //
  5593. // return [self offline_getCarrier:code];
  5594. //
  5595. //}
  5596. + (NSString *)offline_getCarrier:(int)code {
  5597. // "val_227" : {
  5598. // "check" : 1,
  5599. // "value" : "US United States",
  5600. // "value_id" : "228"
  5601. // },
  5602. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5603. NSString * ret = [iSalesDB jk_queryText:Sql];
  5604. return ret;
  5605. }
  5606. + (int )offline_getCarrierCode:(NSString*)name {
  5607. // "val_227" : {
  5608. // "check" : 1,
  5609. // "value" : "US United States",
  5610. // "value_id" : "228"
  5611. // },
  5612. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5613. NSString * ret = [iSalesDB jk_queryText:Sql];
  5614. return [ret intValue];
  5615. }
  5616. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5617. countryCode = [self translateSingleQuote:countryCode];
  5618. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5619. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5620. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5621. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5622. if (name == NULL) {
  5623. name = "";
  5624. }
  5625. if (code == NULL) {
  5626. code = "";
  5627. }
  5628. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5629. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5630. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5631. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5632. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5633. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5634. }
  5635. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5636. [container setValue:stateDic forKey:key];
  5637. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5638. DebugLog(@"query all state error: %@",err_msg);
  5639. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5640. // [stateDic setValue:@"Other" forKey:@"value"];
  5641. // [stateDic setValue:@"" forKey:@"value_id"];
  5642. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5643. //
  5644. // if (state_code && [@"" isEqualToString:state_code]) {
  5645. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5646. // }
  5647. //
  5648. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5649. // [container setValue:stateDic forKey:key];
  5650. }] mutableCopy];
  5651. [ret removeObjectForKey:@"result"];
  5652. // failure 可以不用了,一样的
  5653. if (ret.allKeys.count == 0) {
  5654. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5655. [stateDic setValue:@"Other" forKey:@"value"];
  5656. [stateDic setValue:@"" forKey:@"value_id"];
  5657. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5658. if (state_code && [@"" isEqualToString:state_code]) {
  5659. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5660. }
  5661. NSString *key = [NSString stringWithFormat:@"val_0"];
  5662. [ret setValue:stateDic forKey:key];
  5663. }
  5664. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5665. return ret;
  5666. }
  5667. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5668. codeId = [self translateSingleQuote:codeId];
  5669. 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];
  5670. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5671. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5672. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5673. if (name == NULL) {
  5674. name = "";
  5675. }
  5676. if (code == NULL) {
  5677. code = "";
  5678. }
  5679. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5680. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5681. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5682. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5683. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5684. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5685. }
  5686. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5687. [container setValue:stateDic forKey:key];
  5688. }] mutableCopy];
  5689. [ret removeObjectForKey:@"result"];
  5690. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5691. return ret;
  5692. }
  5693. + (NSDictionary *)offline_getPrice {
  5694. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5695. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5696. char *name = (char *) sqlite3_column_text(stmt, 1);
  5697. int type = sqlite3_column_int(stmt, 2);
  5698. int orderBy = sqlite3_column_int(stmt, 3);
  5699. if (name == NULL) {
  5700. name = "";
  5701. }
  5702. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5703. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5704. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5705. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5706. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5707. if (orderBy == 0) {
  5708. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5709. }
  5710. [container setValue:priceDic forKey:key];
  5711. }] mutableCopy];
  5712. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5713. return ret;
  5714. }
  5715. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5716. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5717. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5718. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5719. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5720. // int _id = sqlite3_column_int(stmt, 0);
  5721. NSString *name = [self textAtColumn:1 statement:stmt];
  5722. NSDictionary *typeDic = @{
  5723. @"value_id" : name,
  5724. @"value" : name,
  5725. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5726. };
  5727. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5728. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5729. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5730. }];
  5731. return ret;
  5732. }
  5733. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5734. // 首先从offline_login表中取出sales_code
  5735. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5736. // NSString *user = app.user;
  5737. user = [self translateSingleQuote:user];
  5738. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5739. __block NSString *user_code = @"";
  5740. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5741. char *code = (char *)sqlite3_column_text(stmt, 0);
  5742. if (code == NULL) {
  5743. code = "";
  5744. }
  5745. user_code = [NSString stringWithUTF8String:code];
  5746. }];
  5747. // 再取所有salesRep
  5748. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5749. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5750. // 1 name 2 code 3 salesrep_id
  5751. char *name = (char *)sqlite3_column_text(stmt, 1);
  5752. char *code = (char *)sqlite3_column_text(stmt, 2);
  5753. int salesrep_id = sqlite3_column_int(stmt, 3);
  5754. if (name == NULL) {
  5755. name = "";
  5756. }
  5757. if (code == NULL) {
  5758. code = "";
  5759. }
  5760. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5761. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5762. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5763. // 比较code 相等则check
  5764. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5765. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5766. }
  5767. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5768. }] mutableCopy];
  5769. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5770. return ret;
  5771. }
  5772. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5773. zipcode = [self translateSingleQuote:zipcode];
  5774. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5775. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5776. char *country = (char *)sqlite3_column_text(stmt, 0);
  5777. char *state = (char *)sqlite3_column_text(stmt, 1);
  5778. char *city = (char *)sqlite3_column_text(stmt, 2);
  5779. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5780. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5781. if (country == NULL) {
  5782. country = "";
  5783. }
  5784. if (state == NULL) {
  5785. state = "";
  5786. }
  5787. if (city == NULL) {
  5788. city = "";
  5789. }
  5790. if (country_code == NULL) {
  5791. country_code = "";
  5792. }
  5793. if (state_code == NULL) {
  5794. state_code = "";
  5795. }
  5796. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5797. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5798. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5799. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5800. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5801. }] mutableCopy];
  5802. return ret;
  5803. }
  5804. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5805. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5806. [item setValue:value forKey:valueKey];
  5807. [dic setValue:item forKey:itemKey];
  5808. }
  5809. + (NSString *)countryCodeByid:(NSString *)code_id {
  5810. NSString *ret = nil;
  5811. code_id = [self translateSingleQuote:code_id];
  5812. sqlite3 *db = [iSalesDB get_db];
  5813. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5814. sqlite3_stmt * statement;
  5815. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5816. while (sqlite3_step(statement) == SQLITE_ROW) {
  5817. char *code = (char *)sqlite3_column_text(statement, 0);
  5818. if (code == NULL) {
  5819. code = "";
  5820. }
  5821. ret = [NSString stringWithUTF8String:code];
  5822. }
  5823. sqlite3_finalize(statement);
  5824. }
  5825. [iSalesDB close_db:db];
  5826. return ret;
  5827. }
  5828. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5829. NSString *ret = nil;
  5830. code = [self translateSingleQuote:code];
  5831. sqlite3 *db = [iSalesDB get_db];
  5832. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5833. sqlite3_stmt * statement;
  5834. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5835. while (sqlite3_step(statement) == SQLITE_ROW) {
  5836. char *_id = (char *)sqlite3_column_text(statement, 0);
  5837. if (_id == NULL) {
  5838. _id = "";
  5839. }
  5840. ret = [NSString stringWithFormat:@"%s",_id];
  5841. }
  5842. sqlite3_finalize(statement);
  5843. }
  5844. [iSalesDB close_db:db];
  5845. return ret;
  5846. }
  5847. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5848. NSString *name = nil;
  5849. codeId = [self translateSingleQuote:codeId];
  5850. sqlite3 *db = [iSalesDB get_db];
  5851. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5852. sqlite3_stmt * statement;
  5853. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5854. while (sqlite3_step(statement) == SQLITE_ROW) {
  5855. char *value = (char *)sqlite3_column_text(statement, 0);
  5856. if (value == NULL) {
  5857. value = "";
  5858. }
  5859. name = [NSString stringWithUTF8String:value];
  5860. }
  5861. sqlite3_finalize(statement);
  5862. }
  5863. [iSalesDB close_db:db];
  5864. return name;
  5865. }
  5866. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5867. NSString *ret = nil;
  5868. sqlite3 *db = [iSalesDB get_db];
  5869. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5870. sqlite3_stmt * statement;
  5871. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5872. while (sqlite3_step(statement) == SQLITE_ROW) {
  5873. char *name = (char *)sqlite3_column_text(statement, 0);
  5874. if (name == NULL) {
  5875. name = "";
  5876. }
  5877. ret = [NSString stringWithUTF8String:name];
  5878. }
  5879. sqlite3_finalize(statement);
  5880. }
  5881. [iSalesDB close_db:db];
  5882. return ret;
  5883. }
  5884. + (NSString *)salesRepCodeById:(NSString *)_id {
  5885. NSString *ret = nil;
  5886. _id = [self translateSingleQuote:_id];
  5887. sqlite3 *db = [iSalesDB get_db];
  5888. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5889. sqlite3_stmt * statement;
  5890. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5891. while (sqlite3_step(statement) == SQLITE_ROW) {
  5892. char *rep = (char *)sqlite3_column_text(statement, 0);
  5893. if (rep == NULL) {
  5894. rep = "";
  5895. }
  5896. ret = [NSString stringWithUTF8String:rep];
  5897. }
  5898. sqlite3_finalize(statement);
  5899. }
  5900. [iSalesDB close_db:db];
  5901. return ret;
  5902. }
  5903. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5904. char *tx = (char *)sqlite3_column_text(stmt, col);
  5905. if (tx == NULL) {
  5906. tx = "";
  5907. }
  5908. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5909. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5910. if (!text) {
  5911. text = @"";
  5912. }
  5913. // 将字符全部为' '的字符串干掉
  5914. int spaceCount = 0;
  5915. for (int i = 0; i < text.length; i++) {
  5916. if ([text characterAtIndex:i] == ' ') {
  5917. spaceCount++;
  5918. }
  5919. }
  5920. if (spaceCount == text.length) {
  5921. text = @"";
  5922. }
  5923. return text;
  5924. }
  5925. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5926. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5927. NSData *data = [NSData dataWithContentsOfFile:path];
  5928. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5929. return ret;
  5930. }
  5931. + (NSString *)textFileName:(NSString *)name {
  5932. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5933. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5934. if (!text) {
  5935. text = @"";
  5936. }
  5937. return text;
  5938. }
  5939. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5940. return [[dic objectForKey:key] mutableCopy];
  5941. }
  5942. + (id)translateSingleQuote:(NSString *)string {
  5943. if ([string isKindOfClass:[NSString class]])
  5944. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5945. return string;
  5946. }
  5947. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5948. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5949. }
  5950. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5951. NSString* ret= nil;
  5952. NSString *sqlQuery = nil;
  5953. // 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
  5954. 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];
  5955. sqlite3_stmt * statement;
  5956. // int count=0;
  5957. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5958. {
  5959. if (sqlite3_step(statement) == SQLITE_ROW)
  5960. {
  5961. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5962. if(imgurl==nil)
  5963. imgurl="";
  5964. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5965. ret=nsimgurl;
  5966. }
  5967. sqlite3_finalize(statement);
  5968. }
  5969. else
  5970. {
  5971. [ret setValue:@"8" forKey:@"result"];
  5972. }
  5973. // [iSalesDB close_db:db];
  5974. // DebugLog(@"data string: %@",ret );
  5975. return ret;
  5976. }
  5977. #pragma mark contact Advanced search
  5978. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5979. {
  5980. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5981. return [RAConvertor dict2data:contactAdvanceDic];
  5982. }
  5983. #pragma mark create new contact
  5984. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5985. assert(params[@"user"]!=nil);
  5986. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5987. NSData *data = [NSData dataWithContentsOfFile:path];
  5988. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5989. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5990. NSString *countryCode = nil;
  5991. NSString *countryCode_id = nil;
  5992. NSString *stateCode = nil;
  5993. NSString *city = nil;
  5994. NSString *zipCode = nil;
  5995. NSString *carrierCode = nil;
  5996. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5997. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5998. NSString *code_id = params[@"country"];
  5999. countryCode_id = code_id;
  6000. countryCode = [self countryCodeByid:code_id];
  6001. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  6002. NSString *zip_code = params[@"zipcode"];
  6003. // 剔除全部为空格
  6004. int spaceCount = 0;
  6005. for (int i = 0; i < zip_code.length; i++) {
  6006. if ([zip_code characterAtIndex:i] == ' ') {
  6007. spaceCount++;
  6008. }
  6009. }
  6010. if (spaceCount == zip_code.length) {
  6011. zip_code = @"";
  6012. }
  6013. zipCode = zip_code;
  6014. if (zipCode.length > 0) {
  6015. countryCode_id = params[@"country"];
  6016. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6017. countryCode = [dic valueForKey:@"country_code"];
  6018. if (!countryCode) {
  6019. // countryCode = @"US";
  6020. NSString *code_id = params[@"country"];
  6021. countryCode = [self countryCodeByid:code_id];
  6022. }
  6023. stateCode = [dic valueForKey:@"state_code"];
  6024. if (!stateCode.length) {
  6025. stateCode = params[@"state"];
  6026. }
  6027. city = [dic valueForKey:@"city"];
  6028. if (!city.length) {
  6029. city = params[@"city"];
  6030. }
  6031. // zip code
  6032. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  6033. [zipDic setValue:zipCode forKey:@"value"];
  6034. [section_0 setValue:zipDic forKey:@"item_11"];
  6035. } else {
  6036. NSString *code_id = params[@"country"];
  6037. countryCode = [self countryCodeByid:code_id];
  6038. stateCode = params[@"state"];
  6039. city = params[@"city"];
  6040. }
  6041. }
  6042. } else {
  6043. // default: US United States
  6044. countryCode = @"US";
  6045. countryCode_id = @"228";
  6046. }
  6047. // country
  6048. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6049. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  6050. // state
  6051. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6052. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  6053. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  6054. // city
  6055. if (city) {
  6056. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  6057. [cityDic setValue:city forKey:@"value"];
  6058. [section_0 setValue:cityDic forKey:@"item_13"];
  6059. }
  6060. // price type
  6061. NSDictionary *priceDic = [self offline_getPrice];
  6062. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  6063. // contact type
  6064. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  6065. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  6066. // Sales Rep
  6067. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  6068. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6069. #ifdef BUILD_CONTRAST
  6070. //carrier
  6071. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  6072. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6073. #endif
  6074. [ret setValue:section_0 forKey:@"section_0"];
  6075. return [RAConvertor dict2data:ret];
  6076. }
  6077. #pragma mark save
  6078. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  6079. NSString *addr = nil;
  6080. NSString *contact_name = nil;
  6081. // int carrier = [params[@"carrier"] intValue];
  6082. NSString *companyName = [params objectForKey:@"company"];
  6083. if (companyName) {
  6084. companyName = [AESCrypt fastencrypt:companyName];
  6085. } else {
  6086. companyName = @"";
  6087. }
  6088. DebugLog(@"company");
  6089. companyName = [self translateSingleQuote:companyName];
  6090. NSString *addr1 = [params objectForKey:@"address"];
  6091. NSString *addr2 = [params objectForKey:@"address2"];
  6092. NSString *addr3 = [params objectForKey:@"address_3"];
  6093. NSString *addr4 = [params objectForKey:@"address_4"];
  6094. if (!addr2) {
  6095. addr2 = @"";
  6096. }
  6097. if (!addr3) {
  6098. addr3 = @"";
  6099. }
  6100. if (!addr4) {
  6101. addr4 = @"";
  6102. }
  6103. if (!addr1) {
  6104. addr1 = @"";
  6105. }
  6106. DebugLog(@"addr");
  6107. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  6108. addr = [AESCrypt fastencrypt:addr];
  6109. addr = [self translateSingleQuote:addr];
  6110. if (addr1 && ![addr1 isEqualToString:@""]) {
  6111. addr1 = [AESCrypt fastencrypt:addr1];
  6112. }
  6113. addr1 = [self translateSingleQuote:addr1];
  6114. addr2 = [self translateSingleQuote:addr2];
  6115. addr3 = [self translateSingleQuote:addr3];
  6116. addr4 = [self translateSingleQuote:addr4];
  6117. NSString *country = [params objectForKey:@"country"];
  6118. if (country) {
  6119. country = [self countryNameByCountryCodeId:country];
  6120. } else {
  6121. country = @"";
  6122. }
  6123. int carrier = [params[@"carrier"] intValue];
  6124. DebugLog(@"country");
  6125. country = [self translateSingleQuote:country];
  6126. NSString *state = [params objectForKey:@"state"];
  6127. if (!state) {
  6128. state = @"";
  6129. }
  6130. DebugLog(@"state");
  6131. state = [self translateSingleQuote:state];
  6132. NSString *city = [params objectForKey:@"city"];
  6133. if (!city) {
  6134. city = @"";
  6135. }
  6136. city = [self translateSingleQuote:city];
  6137. NSString *zipcode = [params objectForKey:@"zipcode"];
  6138. if (!zipcode) {
  6139. zipcode = @"";
  6140. }
  6141. DebugLog(@"zip");
  6142. zipcode = [self translateSingleQuote:zipcode];
  6143. NSString *fistName = [params objectForKey:@"firstname"];
  6144. if (!fistName) {
  6145. fistName = @"";
  6146. }
  6147. NSString *lastName = [params objectForKey:@"lastname"];
  6148. if (!lastName) {
  6149. lastName = @"";
  6150. }
  6151. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  6152. DebugLog(@"contact_name");
  6153. contact_name = [self translateSingleQuote:contact_name];
  6154. fistName = [self translateSingleQuote:fistName];
  6155. lastName = [self translateSingleQuote:lastName];
  6156. NSString *phone = [params objectForKey:@"phone"];
  6157. if (phone) {
  6158. phone = [AESCrypt fastencrypt:phone];
  6159. } else {
  6160. phone = @"";
  6161. }
  6162. DebugLog(@"PHONE");
  6163. phone = [self translateSingleQuote:phone];
  6164. NSString *fax = [params objectForKey:@"fax"];
  6165. if (!fax) {
  6166. fax = @"";
  6167. }
  6168. DebugLog(@"FAX");
  6169. fax = [self translateSingleQuote:fax];
  6170. NSString *email = [params objectForKey:@"email"];
  6171. if (!email) {
  6172. email = @"";
  6173. }
  6174. DebugLog(@"EMAIL:%@",email);
  6175. email = [self translateSingleQuote:email];
  6176. NSString *notes = [params objectForKey:@"contact_notes"];
  6177. if (!notes) {
  6178. notes = @"";
  6179. }
  6180. DebugLog(@"NOTE:%@",notes);
  6181. notes = [self translateSingleQuote:notes];
  6182. NSString *price = [params objectForKey:@"price_name"];
  6183. if (price) {
  6184. price = [self priceNameByPriceId:price];
  6185. } else {
  6186. price = @"";
  6187. }
  6188. DebugLog(@"PRICE");
  6189. price = [self translateSingleQuote:price];
  6190. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6191. if (salesRep) {
  6192. salesRep = [self salesRepCodeById:salesRep];
  6193. } else {
  6194. salesRep = @"";
  6195. }
  6196. salesRep = [self translateSingleQuote:salesRep];
  6197. NSString *img = [params objectForKey:@"business_card"];
  6198. NSArray *array = [img componentsSeparatedByString:@","];
  6199. NSString *img_0 = array[0];
  6200. if (!img_0) {
  6201. img_0 = @"";
  6202. }
  6203. img_0 = [self translateSingleQuote:img_0];
  6204. NSString *img_1 = array[1];
  6205. if (!img_1) {
  6206. img_1 = @"";
  6207. }
  6208. img_1 = [self translateSingleQuote:img_1];
  6209. NSString *img_2 = array[2];
  6210. if (!img_2) {
  6211. img_2 = @"";
  6212. }
  6213. img_2 = [self translateSingleQuote:img_2];
  6214. NSString *contact_id = [NSUUID UUID].UUIDString;
  6215. NSString *contact_type = [params objectForKey:@"type_name"];
  6216. if (!contact_type) {
  6217. contact_type = @"";
  6218. }
  6219. contact_type = [self translateSingleQuote:contact_type];
  6220. // 判断更新时是否为customer
  6221. if (update) {
  6222. contact_id = [params objectForKey:@"contact_id"];
  6223. if (!contact_id) {
  6224. contact_id = @"";
  6225. }
  6226. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6227. __block int customer = 0;
  6228. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6229. customer = sqlite3_column_int(stmt, 0);
  6230. }];
  6231. isCustomer = customer ? YES : NO;
  6232. }
  6233. NSMutableDictionary *sync_dic = [params mutableCopy];
  6234. if (isCustomer) {
  6235. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6236. } else {
  6237. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6238. }
  6239. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  6240. NSString *sync_data = nil;
  6241. NSString *sql = nil;
  6242. if (update){
  6243. contact_id = [params objectForKey:@"contact_id"];
  6244. if (!contact_id) {
  6245. contact_id = @"";
  6246. }
  6247. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6248. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6249. sync_data = [RAConvertor dict2string:sync_dic];
  6250. sync_data = [self translateSingleQuote:sync_data];
  6251. 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];
  6252. } else {
  6253. contact_id = [self translateSingleQuote:contact_id];
  6254. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6255. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6256. sync_data = [RAConvertor dict2string:sync_dic];
  6257. sync_data = [self translateSingleQuote:sync_data];
  6258. 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];
  6259. }
  6260. int result = [iSalesDB execSql:sql];
  6261. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6262. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6263. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6264. }
  6265. #pragma mark save new contact
  6266. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6267. {
  6268. return [self offline_saveContact:params update:NO isCustomer:YES];
  6269. }
  6270. #pragma mark edit contact
  6271. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6272. {
  6273. assert(params[@"user"]!=nil);
  6274. // {
  6275. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6276. // password = 123456;
  6277. // user = EvanK;
  6278. // }
  6279. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6280. NSData *data = [NSData dataWithContentsOfFile:path];
  6281. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6282. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6283. NSString *countryCode = nil;
  6284. NSString *countryCode_id = nil;
  6285. NSString *stateCode = nil;
  6286. __block int carrierCode = -1;
  6287. /*------contact infor------*/
  6288. __block NSString *country = nil;
  6289. __block NSString *company_name = nil;
  6290. __block NSString *contact_id = params[@"contact_id"];
  6291. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6292. __block NSString *zipcode = nil;
  6293. __block NSString *state = nil; // state_code
  6294. __block NSString *city = nil; //
  6295. __block NSString *firt_name,*last_name;
  6296. __block NSString *phone,*fax,*email;
  6297. __block NSString *notes,*price_type,*sales_rep;
  6298. __block NSString *img_0,*img_1,*img_2;
  6299. __block NSString *contact_type;
  6300. contact_id = [self translateSingleQuote:contact_id];
  6301. 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];
  6302. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6303. country = [self textAtColumn:0 statement:stmt]; // country name
  6304. company_name = [self textAtColumn:1 statement:stmt];
  6305. addr_1 = [self textAtColumn:2 statement:stmt];
  6306. addr_2 = [self textAtColumn:3 statement:stmt];
  6307. addr_3 = [self textAtColumn:4 statement:stmt];
  6308. addr_4 = [self textAtColumn:5 statement:stmt];
  6309. zipcode = [self textAtColumn:6 statement:stmt];
  6310. state = [self textAtColumn:7 statement:stmt]; // state code
  6311. city = [self textAtColumn:8 statement:stmt];
  6312. firt_name = [self textAtColumn:9 statement:stmt];
  6313. last_name = [self textAtColumn:10 statement:stmt];
  6314. phone = [self textAtColumn:11 statement:stmt];
  6315. fax = [self textAtColumn:12 statement:stmt];
  6316. email = [self textAtColumn:13 statement:stmt];
  6317. notes = [self textAtColumn:14 statement:stmt];
  6318. price_type = [self textAtColumn:15 statement:stmt]; // name
  6319. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6320. img_0 = [self textAtColumn:17 statement:stmt];
  6321. img_1 = [self textAtColumn:18 statement:stmt];
  6322. img_2 = [self textAtColumn:19 statement:stmt];
  6323. contact_type = [self textAtColumn:20 statement:stmt];
  6324. carrierCode = sqlite3_column_int(stmt, 21);
  6325. }];
  6326. // decrypt
  6327. if (company_name) {
  6328. company_name = [AESCrypt fastdecrypt:company_name];
  6329. }
  6330. if (addr_1) {
  6331. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6332. }
  6333. if (phone) {
  6334. phone = [AESCrypt fastdecrypt:phone];
  6335. }
  6336. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6337. countryCode = [iSalesDB jk_queryText:countrySql];
  6338. stateCode = state;
  6339. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6340. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6341. NSString *code_id = params[@"country"];
  6342. countryCode_id = code_id;
  6343. countryCode = [self countryCodeByid:code_id];
  6344. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6345. NSString *zip_code = params[@"zipcode"];
  6346. // 剔除全部为空格
  6347. int spaceCount = 0;
  6348. for (int i = 0; i < zip_code.length; i++) {
  6349. if ([zip_code characterAtIndex:i] == ' ') {
  6350. spaceCount++;
  6351. }
  6352. }
  6353. if (spaceCount == zip_code.length) {
  6354. zip_code = @"";
  6355. }
  6356. if (zipcode.length > 0) {
  6357. zipcode = zip_code;
  6358. countryCode_id = params[@"country"];
  6359. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6360. countryCode = [dic valueForKey:@"country_code"];
  6361. if (!countryCode) {
  6362. NSString *code_id = params[@"country"];
  6363. countryCode = [self countryCodeByid:code_id];
  6364. }
  6365. stateCode = [dic valueForKey:@"state_code"];
  6366. if (!stateCode.length) {
  6367. stateCode = params[@"state"];
  6368. }
  6369. city = [dic valueForKey:@"city"];
  6370. if (!city.length) {
  6371. city = params[@"city"];
  6372. }
  6373. // zip code
  6374. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6375. [zipDic setValue:zipcode forKey:@"value"];
  6376. [section_0 setValue:zipDic forKey:@"item_8"];
  6377. } else {
  6378. NSString *code_id = params[@"country"];
  6379. countryCode = [self countryCodeByid:code_id];
  6380. stateCode = params[@"state"];
  6381. city = params[@"city"];
  6382. }
  6383. }
  6384. }
  6385. // 0 Country
  6386. // 1 Company Name
  6387. // 2 Contact ID
  6388. // 3 Picture
  6389. // 4 Address 1
  6390. // 5 Address 2
  6391. // 6 Address 3
  6392. // 7 Address 4
  6393. // 8 Zip Code
  6394. // 9 State/Province
  6395. // 10 City
  6396. // 11 Contact First Name
  6397. // 12 Contact Last Name
  6398. // 13 Phone
  6399. // 14 Fax
  6400. // 15 Email
  6401. // 16 Contact Notes
  6402. // 17 Price Type
  6403. // 18 Contact Type
  6404. // 19 Sales Rep
  6405. // country
  6406. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6407. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6408. // company
  6409. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6410. // contact_id
  6411. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6412. // picture
  6413. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6414. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6415. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6416. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6417. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6418. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6419. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6420. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6421. // addr 1 2 3 4
  6422. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6423. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6424. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6425. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6426. // zip code
  6427. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6428. // state
  6429. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6430. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6431. // city
  6432. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6433. // first last
  6434. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6435. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6436. // phone fax email
  6437. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6438. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6439. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6440. // notes
  6441. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6442. // price
  6443. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6444. for (NSString *key in priceDic.allKeys) {
  6445. if ([key containsString:@"val_"]) {
  6446. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6447. if ([dic[@"value"] isEqualToString:price_type]) {
  6448. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6449. [priceDic setValue:dic forKey:key];
  6450. }
  6451. }
  6452. }
  6453. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6454. // Contact Rep
  6455. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6456. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6457. // Sales Rep
  6458. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6459. for (NSString *key in repDic.allKeys) {
  6460. if ([key containsString:@"val_"]) {
  6461. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6462. NSString *value = dic[@"value"];
  6463. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6464. if (code && [code isEqualToString:sales_rep]) {
  6465. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6466. [repDic setValue:dic forKey:key];
  6467. }
  6468. }
  6469. }
  6470. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6471. #ifdef BUILD_CONTRAST
  6472. //carrier
  6473. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6474. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6475. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6476. #endif
  6477. [ret setValue:section_0 forKey:@"section_0"];
  6478. return [RAConvertor dict2data:ret];
  6479. }
  6480. #pragma mark save contact
  6481. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6482. {
  6483. return [self offline_saveContact:params update:YES isCustomer:YES];
  6484. }
  6485. #pragma mark category
  6486. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6487. if (ck) {
  6488. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6489. for (NSString *key in res.allKeys) {
  6490. if (![key isEqualToString:@"count"]) {
  6491. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6492. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6493. if ([val[@"value"] isEqualToString:ck]) {
  6494. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6495. }
  6496. [res setValue:val forKey:key];
  6497. }
  6498. }
  6499. [dic setValue:res forKey:valueKey];
  6500. }
  6501. }
  6502. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6503. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6504. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6505. NSString* category = [params valueForKey:@"category"];
  6506. if (!category || [category isEqualToString:@""]) {
  6507. category = @"%";
  6508. }
  6509. category = [self translateSingleQuote:category];
  6510. int limit = [[params valueForKey:@"limit"] intValue];
  6511. int offset = [[params valueForKey:@"offset"] intValue];
  6512. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6513. NSString *limit_str = @"";
  6514. if (limited) {
  6515. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6516. }
  6517. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6518. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6519. sqlite3 *db = [iSalesDB get_db];
  6520. // [iSalesDB AddExFunction:db];
  6521. int count;
  6522. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6523. 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];
  6524. double price_min = 0;
  6525. double price_max = 0;
  6526. if ([params.allKeys containsObject:@"alert"]) {
  6527. // alert
  6528. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6529. NSString *alert = params[@"alert"];
  6530. if ([alert isEqualToString:@"Display All"]) {
  6531. alert = [NSString stringWithFormat:@""];
  6532. } else {
  6533. alert = [self translateSingleQuote:alert];
  6534. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6535. }
  6536. // available
  6537. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6538. NSString *available = params[@"available"];
  6539. NSString *available_condition;
  6540. if ([available isEqualToString:@"Display All"]) {
  6541. available_condition = @"";
  6542. } else if ([available isEqualToString:@"Available Now"]) {
  6543. available_condition = @"and availability > 0";
  6544. } else {
  6545. available_condition = @"and availability == 0";
  6546. }
  6547. // best seller
  6548. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6549. NSString *best_seller = @"";
  6550. NSString *order_best_seller = @"m.name asc";
  6551. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6552. best_seller = @"and best_seller > 0";
  6553. order_best_seller = @"m.best_seller desc,m.name asc";
  6554. }
  6555. // price
  6556. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6557. NSString *price = params[@"price"];
  6558. price_min = 0;
  6559. price_max = MAXFLOAT;
  6560. if (params[@"user"] && price != nil) {
  6561. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6562. NSMutableString *priceName = [NSMutableString string];
  6563. for (int i = 0; i < priceTypeArray.count; i++) {
  6564. NSString *pricetype = priceTypeArray[i];
  6565. pricetype = [self translateSingleQuote:pricetype];
  6566. if (i == 0) {
  6567. [priceName appendFormat:@"'%@'",pricetype];
  6568. } else {
  6569. [priceName appendFormat:@",'%@'",pricetype];
  6570. }
  6571. }
  6572. if ([price isEqualToString:@"Display All"]) {
  6573. price = [NSString stringWithFormat:@""];
  6574. } else if([price containsString:@"+"]){
  6575. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6576. price_min = [price doubleValue];
  6577. 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];
  6578. } else {
  6579. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6580. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6581. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6582. 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];
  6583. }
  6584. } else {
  6585. price = @"";
  6586. }
  6587. // sold_by_qty : Sold in quantities of %@
  6588. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6589. NSString *qty = params[@"sold_by_qty"];
  6590. if ([qty isEqualToString:@"Display All"]) {
  6591. qty = @"";
  6592. } else {
  6593. qty = [self translateSingleQuote:qty];
  6594. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6595. }
  6596. // model name;
  6597. NSString* modelname =params[@"modelName"];
  6598. if(modelname.length==0)
  6599. {
  6600. modelname=@"";
  6601. }
  6602. else
  6603. {
  6604. modelname = modelname.lowercaseString;
  6605. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6606. }
  6607. //modelDescription
  6608. NSString* modelDescription =params[@"modelDescription"];
  6609. if(modelDescription.length==0)
  6610. {
  6611. modelDescription=@"";
  6612. }
  6613. else
  6614. {
  6615. modelDescription = modelDescription.lowercaseString;
  6616. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6617. }
  6618. // cate
  6619. // category = [self translateSingleQuote:category];
  6620. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6621. // cate mutiple selection
  6622. NSString *category_id = params[@"category"];
  6623. NSMutableArray *cate_id_array = nil;
  6624. NSMutableString *cateWhere = [NSMutableString string];
  6625. if ([category_id isEqualToString:@""] || !category_id) {
  6626. [cateWhere appendString:@"1 = 1"];
  6627. } else {
  6628. if ([category_id containsString:@","]) {
  6629. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6630. } else {
  6631. cate_id_array = [@[category_id] mutableCopy];
  6632. }
  6633. [cateWhere appendString:@"("];
  6634. for (int i = 0; i < cate_id_array.count; i++) {
  6635. if (i == 0) {
  6636. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6637. } else {
  6638. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6639. }
  6640. }
  6641. [cateWhere appendString:@")"];
  6642. }
  6643. // where bestseller > 0 order by bestseller desc
  6644. // sql query: alert availability(int) best_seller(int) price qty
  6645. 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];
  6646. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6647. }
  6648. DebugLog(@"offline category where: %@",where);
  6649. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6650. if (!params[@"user"]) {
  6651. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6652. }
  6653. [ret setValue:filter forKey:@"filter"];
  6654. DebugLog(@"offline_category sql:%@",sqlQuery);
  6655. sqlite3_stmt * statement;
  6656. [ret setValue:@"2" forKey:@"result"];
  6657. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6658. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6659. // int count=0;
  6660. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6661. {
  6662. int i=0;
  6663. while (sqlite3_step(statement) == SQLITE_ROW)
  6664. {
  6665. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6666. char *name = (char*)sqlite3_column_text(statement, 0);
  6667. if(name==nil)
  6668. name="";
  6669. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6670. char *description = (char*)sqlite3_column_text(statement, 1);
  6671. if(description==nil)
  6672. description="";
  6673. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6674. int product_id = sqlite3_column_int(statement, 2);
  6675. int wid = sqlite3_column_int(statement, 3);
  6676. int closeout = sqlite3_column_int(statement, 4);
  6677. int cid = sqlite3_column_int(statement, 5);
  6678. int wisdelete = sqlite3_column_int(statement, 6);
  6679. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6680. int more_color = sqlite3_column_int(statement, 8);
  6681. // Defaul Category ID
  6682. __block NSString *categoryID = nil;
  6683. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6684. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6685. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6686. if(default_category==nil)
  6687. default_category="";
  6688. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6689. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6690. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6691. categoryID = nsdefault_category;
  6692. }];
  6693. if (!categoryID.length) {
  6694. NSString *cateIDs = params[@"category"];
  6695. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6696. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6697. for (NSString *cateID in requestCategoryArr) {
  6698. BOOL needBreak = NO;
  6699. for (NSString *itemCateIDBox in itemCategoryArr) {
  6700. if (itemCateIDBox.length > 4) {
  6701. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6702. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6703. if ([itemCategoryID isEqualToString:cateID]) {
  6704. needBreak = YES;
  6705. categoryID = itemCategoryID;
  6706. break;
  6707. }
  6708. }
  6709. }
  6710. if (needBreak) {
  6711. break;
  6712. }
  6713. }
  6714. }
  6715. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6716. if(wid !=0 && wisdelete != 1)
  6717. [item setValue:@"true" forKey:@"wish_exists"];
  6718. else
  6719. [item setValue:@"false" forKey:@"wish_exists"];
  6720. if(closeout==0)
  6721. [item setValue:@"false" forKey:@"is_closeout"];
  6722. else
  6723. [item setValue:@"true" forKey:@"is_closeout"];
  6724. if(cid==0)
  6725. [item setValue:@"false" forKey:@"cart_exists"];
  6726. else
  6727. [item setValue:@"true" forKey:@"cart_exists"];
  6728. if (more_color == 0) {
  6729. [item setObject:@(false) forKey:@"more_color"];
  6730. } else if (more_color == 1) {
  6731. [item setObject:@(true) forKey:@"more_color"];
  6732. }
  6733. [item addEntriesFromDictionary:imgjson];
  6734. // [item setValue:nsurl forKey:@"img"];
  6735. [item setValue:nsname forKey:@"name"];
  6736. [item setValue:nsdescription forKey:@"description"];
  6737. if (categoryID) {
  6738. [item setValue:categoryID forKey:@"item_category_id"];
  6739. }
  6740. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6741. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6742. i++;
  6743. }
  6744. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6745. [ret setObject:items forKey:@"items"];
  6746. sqlite3_finalize(statement);
  6747. } else {
  6748. DebugLog(@"nothing...");
  6749. }
  6750. DebugLog(@"count:%d",count);
  6751. [iSalesDB close_db:db];
  6752. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6753. return ret;
  6754. }
  6755. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6756. {
  6757. return [self categoryList:params limited:YES];
  6758. }
  6759. # pragma mark item search
  6760. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6761. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6762. // assert(params[@"order_code"]);
  6763. // params[@"user"]
  6764. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6765. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6766. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6767. // category
  6768. NSDictionary *category_menu = [self offline_category_menu];
  6769. [filter setValue:category_menu forKey:@"category"];
  6770. NSString* where= nil;
  6771. NSString* orderby= @"m.name";
  6772. if (!filterSearch) {
  6773. int covertype = [[params valueForKey:@"covertype"] intValue];
  6774. switch (covertype) {
  6775. case 0:
  6776. {
  6777. where=@"m.category like'%%#005#%%'";
  6778. break;
  6779. }
  6780. case 1:
  6781. {
  6782. where=@"m.alert like '%QS%'";
  6783. break;
  6784. }
  6785. case 2:
  6786. {
  6787. where=@"m.availability>0";
  6788. break;
  6789. }
  6790. case 3:
  6791. {
  6792. where=@"m.best_seller>0";
  6793. orderby=@"m.best_seller desc,m.name asc";
  6794. break;
  6795. }
  6796. default:
  6797. where=@"1=1";
  6798. break;
  6799. }
  6800. }
  6801. int limit = [[params valueForKey:@"limit"] intValue];
  6802. int offset = [[params valueForKey:@"offset"] intValue];
  6803. NSString *limit_str = @"";
  6804. if (limited) {
  6805. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6806. }
  6807. sqlite3 *db = [iSalesDB get_db];
  6808. // [iSalesDB AddExFunction:db];
  6809. int count;
  6810. NSString *sqlQuery = nil;
  6811. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6812. 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];
  6813. double price_min = 0;
  6814. double price_max = 0;
  6815. if (filterSearch) {
  6816. // alert
  6817. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6818. NSString *alert = params[@"alert"];
  6819. if ([alert isEqualToString:@"Display All"]) {
  6820. alert = [NSString stringWithFormat:@""];
  6821. } else {
  6822. alert = [self translateSingleQuote:alert];
  6823. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6824. }
  6825. // available
  6826. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6827. NSString *available = params[@"available"];
  6828. NSString *available_condition;
  6829. if ([available isEqualToString:@"Display All"]) {
  6830. available_condition = @"";
  6831. } else if ([available isEqualToString:@"Available Now"]) {
  6832. available_condition = @"and availability > 0";
  6833. } else {
  6834. available_condition = @"and availability == 0";
  6835. }
  6836. // best seller
  6837. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6838. NSString *best_seller = @"";
  6839. NSString *order_best_seller = @"m.name asc";
  6840. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6841. best_seller = @"and best_seller > 0";
  6842. order_best_seller = @"m.best_seller desc,m.name asc";
  6843. }
  6844. // price
  6845. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6846. NSString *price = params[@"price"];
  6847. price_min = 0;
  6848. price_max = MAXFLOAT;
  6849. if (params[@"user"]) {
  6850. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6851. NSMutableString *priceName = [NSMutableString string];
  6852. for (int i = 0; i < priceTypeArray.count; i++) {
  6853. NSString *pricetype = priceTypeArray[i];
  6854. pricetype = [self translateSingleQuote:pricetype];
  6855. if (i == 0) {
  6856. [priceName appendFormat:@"'%@'",pricetype];
  6857. } else {
  6858. [priceName appendFormat:@",'%@'",pricetype];
  6859. }
  6860. }
  6861. if ([price isEqualToString:@"Display All"]) {
  6862. price = [NSString stringWithFormat:@""];
  6863. } else if([price containsString:@"+"]){
  6864. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6865. price_min = [price doubleValue];
  6866. 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];
  6867. } else {
  6868. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6869. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6870. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6871. 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];
  6872. }
  6873. } else {
  6874. price = @"";
  6875. }
  6876. // sold_by_qty : Sold in quantities of %@
  6877. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6878. NSString *qty = params[@"sold_by_qty"];
  6879. if ([qty isEqualToString:@"Display All"]) {
  6880. qty = @"";
  6881. } else {
  6882. qty = [self translateSingleQuote:qty];
  6883. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6884. }
  6885. // model name;
  6886. NSString* modelname =params[@"modelName"];
  6887. if(modelname.length==0)
  6888. {
  6889. modelname=@"";
  6890. }
  6891. else
  6892. {
  6893. modelname = modelname.lowercaseString;
  6894. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6895. }
  6896. //modelDescription
  6897. NSString* modelDescription =params[@"modelDescription"];
  6898. if(modelDescription.length==0)
  6899. {
  6900. modelDescription=@"";
  6901. }
  6902. else
  6903. {
  6904. modelDescription = modelDescription.lowercaseString;
  6905. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6906. }
  6907. //lower(description) like'%%%@%%'
  6908. // category
  6909. NSString *category_id = params[@"ctgId"];
  6910. NSMutableArray *cate_id_array = nil;
  6911. NSMutableString *cateWhere = [NSMutableString string];
  6912. if ([category_id isEqualToString:@""] || !category_id) {
  6913. [cateWhere appendString:@"1 = 1"];
  6914. } else {
  6915. if ([category_id containsString:@","]) {
  6916. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6917. } else {
  6918. cate_id_array = [@[category_id] mutableCopy];
  6919. }
  6920. /*-----------*/
  6921. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6922. [cateWhere appendString:@"("];
  6923. for (int i = 0; i < cate_id_array.count; i++) {
  6924. for (NSString *key0 in cateDic.allKeys) {
  6925. if ([key0 containsString:@"category_"]) {
  6926. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6927. for (NSString *key1 in category0.allKeys) {
  6928. if ([key1 containsString:@"category_"]) {
  6929. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6930. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6931. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6932. cate_id_array[i] = [category1 objectForKey:@"id"];
  6933. if (i == 0) {
  6934. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6935. } else {
  6936. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6937. }
  6938. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6939. [category0 setValue:category1 forKey:key1];
  6940. [cateDic setValue:category0 forKey:key0];
  6941. }
  6942. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6943. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6944. cate_id_array[i] = [category0 objectForKey:@"id"];
  6945. if (i == 0) {
  6946. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6947. } else {
  6948. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6949. }
  6950. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6951. [cateDic setValue:category0 forKey:key0];
  6952. }
  6953. }
  6954. }
  6955. }
  6956. }
  6957. }
  6958. [cateWhere appendString:@")"];
  6959. [filter setValue:cateDic forKey:@"category"];
  6960. }
  6961. // where bestseller > 0 order by bestseller desc
  6962. // sql query: alert availability(int) best_seller(int) price qty
  6963. 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];
  6964. // count
  6965. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6966. }
  6967. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6968. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6969. if (!params[@"user"]) {
  6970. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6971. }
  6972. [ret setValue:filter forKey:@"filter"];
  6973. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6974. sqlite3_stmt * statement;
  6975. [ret setValue:@"2" forKey:@"result"];
  6976. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6977. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6978. // int count=0;
  6979. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6980. {
  6981. int i=0;
  6982. while (sqlite3_step(statement) == SQLITE_ROW)
  6983. {
  6984. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6985. char *name = (char*)sqlite3_column_text(statement, 0);
  6986. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6987. char *description = (char*)sqlite3_column_text(statement, 1);
  6988. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6989. int product_id = sqlite3_column_int(statement, 2);
  6990. int wid = sqlite3_column_int(statement, 3);
  6991. int closeout = sqlite3_column_int(statement, 4);
  6992. int cid = sqlite3_column_int(statement, 5);
  6993. int wisdelete = sqlite3_column_int(statement, 6);
  6994. int more_color = sqlite3_column_int(statement, 7);
  6995. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6996. if(wid !=0 && wisdelete != 1)
  6997. [item setValue:@"true" forKey:@"wish_exists"];
  6998. else
  6999. [item setValue:@"false" forKey:@"wish_exists"];
  7000. if(closeout==0)
  7001. [item setValue:@"false" forKey:@"is_closeout"];
  7002. else
  7003. [item setValue:@"true" forKey:@"is_closeout"];
  7004. if(cid==0)
  7005. [item setValue:@"false" forKey:@"cart_exists"];
  7006. else
  7007. [item setValue:@"true" forKey:@"cart_exists"];
  7008. if (more_color == 0) {
  7009. [item setObject:@(false) forKey:@"more_color"];
  7010. } else if (more_color == 1) {
  7011. [item setObject:@(true) forKey:@"more_color"];
  7012. }
  7013. [item addEntriesFromDictionary:imgjson];
  7014. // [item setValue:nsurl forKey:@"img"];
  7015. [item setValue:nsname forKey:@"fash_name"];
  7016. [item setValue:nsdescription forKey:@"description"];
  7017. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  7018. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  7019. i++;
  7020. }
  7021. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  7022. [ret setObject:items forKey:@"items"];
  7023. sqlite3_finalize(statement);
  7024. }
  7025. [iSalesDB close_db:db];
  7026. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  7027. return ret;
  7028. }
  7029. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  7030. {
  7031. return [self itemsearch:params limited:YES];
  7032. }
  7033. #pragma mark order detail
  7034. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  7035. if (str1.length == 0) {
  7036. str1 = @"&nbsp";
  7037. }
  7038. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  7039. return str;
  7040. }
  7041. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  7042. {
  7043. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  7044. [fromformatter setDateFormat:from];
  7045. NSDate *date = [fromformatter dateFromString:datetime];
  7046. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  7047. [toformatter setDateFormat:to];
  7048. NSString * ret = [toformatter stringFromDate:date];
  7049. return ret;
  7050. }
  7051. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  7052. // 把毫秒去掉
  7053. if ([dateTime containsString:@"."]) {
  7054. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  7055. }
  7056. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  7057. formatter.dateFormat = formate;
  7058. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  7059. NSDate *date = [formatter dateFromString:dateTime];
  7060. formatter.dateFormat = newFormate;
  7061. NSString *result = [formatter stringFromDate:date];
  7062. return result ? result : @"";
  7063. }
  7064. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  7065. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  7066. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7067. }
  7068. + (NSString *)changeDateFormate:(NSString *)dateTime {
  7069. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  7070. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7071. }
  7072. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  7073. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  7074. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7075. }
  7076. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  7077. {
  7078. assert(params[@"mode"]!=nil);
  7079. assert(params[@"user"]!=nil);
  7080. DebugLog(@"offline oderdetail params: %@",params);
  7081. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7082. int orderId = [params[@"orderId"] intValue];
  7083. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  7084. // decrypt card number and card security code
  7085. // 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 ];
  7086. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country,o.must_call from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  7087. sqlite3 *db = [iSalesDB get_db];
  7088. sqlite3_stmt * statement;
  7089. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  7090. NSString *nssoid = @"";
  7091. NSString* orderinfo = @"";
  7092. NSString *moreInfo = @"";
  7093. double handlingFee = 0;
  7094. double payments_and_credist = 0;
  7095. double totalPrice = 0;
  7096. double shippingFee = 0;
  7097. double lift_gate = 0;
  7098. NSString *customer_contact = @"";
  7099. NSString *customer_email = @"";
  7100. NSString *customer_fax = @"";
  7101. NSString *customer_phone = @"";
  7102. NSString *customer_city = @"";
  7103. NSString *customer_state = @"";
  7104. NSString *customer_zipcode = @"";
  7105. NSString *customer_country = @"";
  7106. BOOL must_call = NO;
  7107. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  7108. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  7109. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  7110. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  7111. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  7112. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  7113. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  7114. {
  7115. if (sqlite3_step(statement) == SQLITE_ROW)
  7116. {
  7117. // int order_id = sqlite3_column_int(statement, 0);
  7118. NSString *sign_url = [self textAtColumn:51 statement:statement];
  7119. // ret[@"sign_url"] = sign_url;
  7120. section_1 = @{
  7121. @"data":sign_url,
  7122. @"title":@"Signature",
  7123. @"type":@"sign_url"
  7124. }.mutableCopy;
  7125. [ret setObject:section_1 forKey:@"section_1"];
  7126. customer_contact = [self textAtColumn:52 statement:statement];
  7127. customer_email = [self textAtColumn:53 statement:statement];
  7128. customer_phone = [self textAtColumn:54 statement:statement];
  7129. customer_fax = [self textAtColumn:55 statement:statement];
  7130. customer_city = [self textAtColumn:60 statement:statement];
  7131. customer_state = [self textAtColumn:61 statement:statement];
  7132. customer_zipcode = [self textAtColumn:62 statement:statement];
  7133. customer_country = [self textAtColumn:63 statement:statement];
  7134. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  7135. int offline_edit=sqlite3_column_int(statement, 56);
  7136. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  7137. char *soid = (char*)sqlite3_column_text(statement, 1);
  7138. if(soid==nil)
  7139. soid= "";
  7140. nssoid= [[NSString alloc]initWithUTF8String:soid];
  7141. // so#
  7142. ret[@"so#"] = nssoid;
  7143. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  7144. if(poNumber==nil)
  7145. poNumber= "";
  7146. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  7147. char *create_time = (char*)sqlite3_column_text(statement, 3);
  7148. if(create_time==nil)
  7149. create_time= "";
  7150. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7151. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  7152. int status = sqlite3_column_int(statement, 4);
  7153. int erpStatus = sqlite3_column_int(statement, 49);
  7154. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7155. // status
  7156. if (status > 1 && status != 3) {
  7157. status = erpStatus;
  7158. } else if (status == 3) {
  7159. status = 15;
  7160. }
  7161. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  7162. ret[@"order_status"] = nsstatus;
  7163. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7164. if(company_name==nil)
  7165. company_name= "";
  7166. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7167. // company name
  7168. ret[@"company_name"] = nscompany_name;
  7169. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7170. if(customer_contact==nil)
  7171. customer_contact= "";
  7172. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7173. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7174. if(addr_1==nil)
  7175. addr_1="";
  7176. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7177. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7178. if(addr_2==nil)
  7179. addr_2="";
  7180. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7181. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7182. if(addr_3==nil)
  7183. addr_3="";
  7184. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7185. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7186. if(addr_4==nil)
  7187. addr_4="";
  7188. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7189. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7190. [arr_addr addObject:nsaddr_1];
  7191. [arr_addr addObject:nsaddr_2];
  7192. [arr_addr addObject:nsaddr_3];
  7193. [arr_addr addObject:nsaddr_4];
  7194. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7195. [arr_addr addObject:customer_state];
  7196. [arr_addr addObject:customer_zipcode];
  7197. [arr_addr addObject:customer_country];
  7198. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7199. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7200. char *logist = (char*)sqlite3_column_text(statement, 11);
  7201. if(logist==nil)
  7202. logist= "";
  7203. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7204. if (/*status == -11 || */status == 10 || status == 11) {
  7205. nslogist = [self textAtColumn:59 statement:statement];
  7206. };
  7207. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7208. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7209. shipping = @"Shipping To Be Quoted";
  7210. } else {
  7211. shippingFee = sqlite3_column_double(statement, 12);
  7212. }
  7213. // Shipping
  7214. // ret[@"Shipping"] = shipping;
  7215. NSDictionary *shipping_item = @{
  7216. @"title":@"Shipping",
  7217. @"value":shipping
  7218. };
  7219. [price_data setObject:shipping_item forKey:@"item_1"];
  7220. int have_lift_gate = sqlite3_column_int(statement, 17);
  7221. lift_gate = sqlite3_column_double(statement, 13);
  7222. // Liftgate Fee(No loading dock)
  7223. if (!have_lift_gate) {
  7224. lift_gate = 0;
  7225. }
  7226. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7227. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7228. if (sqlite3_column_int(statement, 57)) {
  7229. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7230. liftgate_value = @"Shipping To Be Quoted";
  7231. }
  7232. NSDictionary *liftgate_item = @{
  7233. @"title":@"Liftgate Fee(No loading dock)",
  7234. @"value":liftgate_value
  7235. };
  7236. [price_data setObject:liftgate_item forKey:@"item_2"];
  7237. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7238. // [ret removeObjectForKey:@"Shipping"];
  7239. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7240. // }
  7241. //
  7242. // if (have_lift_gate) {
  7243. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7244. // }
  7245. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7246. if(general_notes==nil)
  7247. general_notes= "";
  7248. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7249. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7250. if(internal_notes==nil)
  7251. internal_notes= "";
  7252. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7253. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7254. if(payment_type==nil)
  7255. payment_type= "";
  7256. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7257. // order info
  7258. orderinfo = [self textFileName:@"order_info.html"];
  7259. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7260. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7261. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7262. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7263. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7264. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7265. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7266. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7267. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7268. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7269. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7270. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7271. NSString *payment = nil;
  7272. // id -> show
  7273. __block NSString *show_pay_type = nspayment_type;
  7274. [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) {
  7275. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7276. if (show_typ_ch != NULL) {
  7277. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7278. }
  7279. }];
  7280. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7281. {
  7282. payment = [self textFileName:@"creditcardpayment.html"];
  7283. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7284. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7285. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7286. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7287. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7288. NSString *card_type = [self textAtColumn:42 statement:statement];
  7289. if (card_type.length > 0) { // 显示星号
  7290. card_type = @"****";
  7291. }
  7292. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7293. if (card_number.length > 0 && card_number.length > 4) {
  7294. for (int i = 0; i < card_number.length - 4; i++) {
  7295. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7296. }
  7297. } else {
  7298. card_number = @"";
  7299. }
  7300. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7301. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7302. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7303. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7304. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7305. card_expiration = @"****";
  7306. }
  7307. NSString *card_city = [self textAtColumn:46 statement:statement];
  7308. NSString *card_state = [self textAtColumn:47 statement:statement];
  7309. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7310. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7311. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7312. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7313. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7314. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7315. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7316. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7317. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7318. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7319. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7320. }
  7321. else
  7322. {
  7323. payment=[self textFileName:@"normalpayment.html"];
  7324. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7325. }
  7326. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7327. ret[@"result"]= [NSNumber numberWithInt:2];
  7328. // more info
  7329. moreInfo = [self textFileName:@"more_info.html"];
  7330. /*****ship to******/
  7331. // ShipToCompany_or_&nbsp
  7332. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7333. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7334. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7335. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7336. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7337. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7338. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7339. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7340. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7341. /*****ship from******/
  7342. // ShipFromCompany_or_&nbsp
  7343. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7344. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7345. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7346. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7347. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7348. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7349. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7350. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7351. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7352. /*****freight to******/
  7353. // FreightBillToCompany_or_&nbsp
  7354. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7355. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7356. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7357. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7358. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7359. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7360. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7361. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7362. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7363. /*****merchandise to******/
  7364. // MerchandiseBillToCompany_or_&nbsp
  7365. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7366. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7367. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7368. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7369. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7370. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7371. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7372. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7373. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7374. /*****return to******/
  7375. // ReturnToCompany_or_&nbsp
  7376. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7377. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7378. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7379. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7380. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7381. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7382. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7383. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7384. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7385. //
  7386. // DebugLog(@"more info : %@",moreInfo);
  7387. // handling fee
  7388. handlingFee = sqlite3_column_double(statement, 33);
  7389. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7390. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7391. if (sqlite3_column_int(statement, 58)) {
  7392. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7393. handling_fee_value = @"Shipping To Be Quoted";
  7394. }
  7395. NSDictionary *handling_fee_item = @{
  7396. @"title":@"Handling Fee",
  7397. @"value":handling_fee_value
  7398. };
  7399. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7400. //
  7401. // customer info
  7402. customerID = [self textAtColumn:36 statement:statement];
  7403. // mode
  7404. ret[@"mode"] = params[@"mode"];
  7405. // model_count
  7406. ret[@"model_count"] = @(0);
  7407. }
  7408. sqlite3_finalize(statement);
  7409. }
  7410. [iSalesDB close_db:db];
  7411. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7412. // "customer_email" = "Shui Hu";
  7413. // "customer_fax" = "";
  7414. // "customer_first_name" = F;
  7415. // "customer_last_name" = L;
  7416. // "customer_name" = ",da He Xiang Dong Liu A";
  7417. // "customer_phone" = "Hey Xuan Feng";
  7418. contactInfo[@"customer_phone"] = customer_phone;
  7419. contactInfo[@"customer_fax"] = customer_fax;
  7420. contactInfo[@"customer_email"] = customer_email;
  7421. NSString *first_name = @"";
  7422. NSString *last_name = @"";
  7423. if ([customer_contact isEqualToString:@""]) {
  7424. } else if ([customer_contact containsString:@" "]) {
  7425. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7426. first_name = [customer_contact substringToIndex:first_space_index];
  7427. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7428. }
  7429. contactInfo[@"customer_first_name"] = first_name;
  7430. contactInfo[@"customer_last_name"] = last_name;
  7431. ret[@"customerInfo"] = contactInfo;
  7432. // models
  7433. if (nssoid) {
  7434. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7435. __block double TotalCuft = 0;
  7436. __block double TotalWeight = 0;
  7437. __block int TotalCarton = 0;
  7438. __block double allItemPrice = 0;
  7439. 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];
  7440. sqlite3 *db1 = [iSalesDB get_db];
  7441. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7442. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7443. int product_id = sqlite3_column_int(stmt, 0);
  7444. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7445. int item_id = sqlite3_column_int(stmt, 7);
  7446. NSString* Price=nil;
  7447. if(str_price==nil)
  7448. {
  7449. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7450. if(price==nil)
  7451. Price=@"No Price.";
  7452. else
  7453. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7454. }
  7455. else
  7456. {
  7457. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7458. }
  7459. double discount = sqlite3_column_double(stmt, 2);
  7460. int item_count = sqlite3_column_int(stmt, 3);
  7461. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7462. NSString *nsline_note=nil;
  7463. if(line_note!=nil)
  7464. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7465. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7466. // NSString *nsname = nil;
  7467. // if(name!=nil)
  7468. // nsname= [[NSString alloc]initWithUTF8String:name];
  7469. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7470. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7471. // NSString *nsdescription=nil;
  7472. // if(description!=nil)
  7473. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7474. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7475. // int stockUom = sqlite3_column_int(stmt, 8);
  7476. // int _id = sqlite3_column_int(stmt, 9);
  7477. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7478. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7479. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7480. int carton=[bsubtotaljson[@"carton"] intValue];
  7481. TotalCuft += cuft;
  7482. TotalWeight += weight;
  7483. TotalCarton += carton;
  7484. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7485. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7486. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7487. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7488. itemjson[@"note"]=nsline_note;
  7489. itemjson[@"origin_price"] = Price;
  7490. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7491. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7492. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7493. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7494. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7495. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7496. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7497. if(itemjson[@"combine"] != nil)
  7498. {
  7499. // int citem=0;
  7500. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7501. for(int bc=0;bc<bcount;bc++)
  7502. {
  7503. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7504. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7505. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7506. subTotal += uprice * modulus * item_count;
  7507. }
  7508. }
  7509. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7510. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7511. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7512. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7513. itemjson[@"type"] = @"order_item";
  7514. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7515. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7516. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7517. allItemPrice += subTotal;
  7518. }];
  7519. section_3[@"data"] = model_data;
  7520. section_3[@"type"] = @"sub_order";
  7521. section_3[@"title"] = @"Models";
  7522. section_3[@"switch"] = @(false);
  7523. ret[@"section_3"] = section_3;
  7524. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7525. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7526. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7527. // payments/Credits
  7528. // payments_and_credist = sqlite3_column_double(statement, 34);
  7529. payments_and_credist = allItemPrice;
  7530. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7531. NSDictionary *sub_total_item = @{
  7532. @"title":@"Sub-Total",
  7533. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7534. };
  7535. [price_data setObject:sub_total_item forKey:@"item_0"];
  7536. // // total
  7537. // totalPrice = sqlite3_column_double(statement, 35);
  7538. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7539. } else {
  7540. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7541. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7542. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7543. // payments/Credits
  7544. payments_and_credist = 0;
  7545. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7546. NSDictionary *sub_total_item = @{
  7547. @"title":@"Sub-Total",
  7548. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7549. };
  7550. [price_data setObject:sub_total_item forKey:@"item_0"];
  7551. }
  7552. // total
  7553. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7554. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7555. NSDictionary *total_item = @{
  7556. @"title":@"Total",
  7557. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7558. };
  7559. [price_data setObject:total_item forKey:@"item_4"];
  7560. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7561. section_4 = @{
  7562. @"data":price_data,
  7563. @"title":@"Price Info",
  7564. @"type":@"price_info"
  7565. }.mutableCopy;
  7566. ret[@"section_4"] = section_4;
  7567. // ret[@"order_info"]= orderinfo;
  7568. section_0 = @{
  7569. @"data":orderinfo,
  7570. @"title":@"Order Info",
  7571. @"type":@"order_info"
  7572. }.mutableCopy;
  7573. ret[@"section_0"] = section_0;
  7574. // ret[@"more_order_info"] = moreInfo;
  7575. section_2 = @{
  7576. @"data":moreInfo,
  7577. @"title":@"More Info",
  7578. @"type":@"more_order_info"
  7579. }.mutableCopy;
  7580. ret[@"section_2"] = section_2;
  7581. ret[@"count"] = @(5);
  7582. return [RAConvertor dict2data:ret];
  7583. }
  7584. #pragma mark order list
  7585. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7586. NSString *nsstatus = @"";
  7587. switch (status) {
  7588. case 0:
  7589. {
  7590. nsstatus=@"Temp Order";
  7591. break;
  7592. }
  7593. case 1:
  7594. {
  7595. nsstatus=@"Saved Order";
  7596. break;
  7597. }
  7598. case 2: {
  7599. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7600. break;
  7601. }
  7602. case 3:
  7603. case 15:
  7604. {
  7605. nsstatus=@"Cancelled";
  7606. break;
  7607. }
  7608. case 10:
  7609. {
  7610. nsstatus=@"Quote Submitted";
  7611. break;
  7612. }
  7613. case 11:
  7614. {
  7615. nsstatus=@"Sales Order Submitted";
  7616. break;
  7617. }
  7618. case 12:
  7619. {
  7620. nsstatus=@"Processing";
  7621. break;
  7622. }
  7623. case 13:
  7624. {
  7625. nsstatus=@"Shipped";
  7626. break;
  7627. }
  7628. case 14:
  7629. {
  7630. nsstatus=@"Closed";
  7631. break;
  7632. }
  7633. case -11:
  7634. {
  7635. nsstatus = @"Ready For Submit";
  7636. break;
  7637. }
  7638. default:
  7639. break;
  7640. }
  7641. return nsstatus;
  7642. }
  7643. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7644. {
  7645. double total = 0;
  7646. __block double payments_and_credist = 0;
  7647. __block double allItemPrice = 0;
  7648. // sqlite3 *db1 = [iSalesDB get_db];
  7649. if (so_id) {
  7650. // 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];
  7651. 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];
  7652. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7653. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7654. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7655. int product_id = sqlite3_column_int(stmt, 0);
  7656. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7657. int discount = sqlite3_column_int(stmt, 2);
  7658. int item_count = sqlite3_column_int(stmt, 3);
  7659. // int item_id = sqlite3_column_int(stmt, 7);
  7660. int item_id = sqlite3_column_int(stmt, 4);
  7661. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7662. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7663. NSString* Price=nil;
  7664. if(str_price==nil)
  7665. {
  7666. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7667. if(price==nil)
  7668. Price=@"No Price.";
  7669. else
  7670. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7671. }
  7672. else
  7673. {
  7674. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7675. }
  7676. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7677. itemjson[@"The unit price"]=Price;
  7678. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7679. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7680. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7681. if(itemjson[@"combine"] != nil)
  7682. {
  7683. // int citem=0;
  7684. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7685. for(int bc=0;bc<bcount;bc++)
  7686. {
  7687. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7688. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7689. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7690. subTotal += uprice * modulus * item_count;
  7691. }
  7692. }
  7693. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7694. allItemPrice += subTotal;
  7695. }];
  7696. payments_and_credist = allItemPrice;
  7697. } else {
  7698. // payments/Credits
  7699. payments_and_credist = 0;
  7700. }
  7701. // lift_gate handlingFee shippingFee
  7702. __block double lift_gate = 0;
  7703. __block double handlingFee = 0;
  7704. __block double shippingFee = 0;
  7705. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7706. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7707. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7708. if (have_lift_gate) {
  7709. lift_gate = sqlite3_column_double(stmt, 1);
  7710. }
  7711. handlingFee = sqlite3_column_double(stmt, 2);
  7712. shippingFee = sqlite3_column_double(stmt, 3);
  7713. }];
  7714. // total
  7715. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7716. if (close) {
  7717. [iSalesDB close_db:db1];
  7718. }
  7719. return total;
  7720. }
  7721. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7722. {
  7723. assert(params[@"user"]!=nil);
  7724. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7725. int limit = [[params valueForKey:@"limit"] intValue];
  7726. int offset = [[params valueForKey:@"offset"] intValue];
  7727. NSString* keyword = [params valueForKey:@"keyWord"];
  7728. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7729. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7730. NSString* where=@"1 = 1";
  7731. if(keyword.length>0)
  7732. 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]];
  7733. if (orderStatus.length > 0) {
  7734. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7735. if (order_status_array.count == 1) {
  7736. int status_value = [[order_status_array firstObject] intValue];
  7737. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7738. if (status_value == 15 || status_value == 3) {
  7739. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7740. } else {
  7741. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7742. }
  7743. } else {
  7744. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7745. }
  7746. } else if (order_status_array.count > 1) {
  7747. for (int i = 0; i < order_status_array.count;i++) {
  7748. NSString *status = order_status_array[i];
  7749. NSString *condition = @" or";
  7750. if (i == 0) {
  7751. condition = @" and (";
  7752. }
  7753. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7754. int status_value = [status intValue];
  7755. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7756. if (status_value == 15 || status_value == 3) {
  7757. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7758. } else {
  7759. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7760. }
  7761. } else {
  7762. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7763. }
  7764. }
  7765. where = [where stringByAppendingString:@" )"];
  7766. }
  7767. }
  7768. 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];
  7769. // DebugLog(@"order list sql: %@",sqlQuery);
  7770. sqlite3 *db = [iSalesDB get_db];
  7771. sqlite3_stmt * statement;
  7772. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7773. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7774. {
  7775. int count=0;
  7776. while (sqlite3_step(statement) == SQLITE_ROW)
  7777. {
  7778. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7779. int order_id = sqlite3_column_double(statement, 0);
  7780. char *soid = (char*)sqlite3_column_text(statement, 1);
  7781. if(soid==nil)
  7782. soid= "";
  7783. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7784. int status = sqlite3_column_double(statement, 2);
  7785. int erpStatus = sqlite3_column_double(statement, 9);
  7786. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7787. if(sales_rep==nil)
  7788. sales_rep= "";
  7789. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7790. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7791. if(create_by==nil)
  7792. create_by= "";
  7793. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7794. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7795. if(company_name==nil)
  7796. company_name= "";
  7797. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7798. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7799. if(create_time==nil)
  7800. create_time= "";
  7801. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7802. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7803. // double total_price = sqlite3_column_double(statement, 7);
  7804. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7805. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7806. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7807. int offline_edit = sqlite3_column_int(statement, 10);
  7808. // ": "JH",
  7809. // "": "$8307.00",
  7810. // "": "MOB1608050001",
  7811. // "": "ArpithaT",
  7812. // "": "1st Stage Property Transformations",
  7813. // "": "JANICE SUTTON",
  7814. // "": 2255,
  7815. // "": "08/02/2016 09:49:18",
  7816. // "": 1,
  7817. // "": "Saved Order"
  7818. // "": "1470384050483",
  7819. // "model_count": "6 / 28"
  7820. item[@"sales_rep"]= nssales_rep;
  7821. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7822. item[@"so#"]= nssoid;
  7823. item[@"create_by"]= nscreate_by;
  7824. item[@"customer_name"]= nscompany_name;
  7825. item[@"customer_contact"] = customer_contact;
  7826. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7827. item[@"purchase_time"]= nscreate_time;
  7828. int statusCode = status;
  7829. if (statusCode == 2) {
  7830. statusCode = erpStatus;
  7831. } else if (statusCode == 3) {
  7832. statusCode = 15;
  7833. }
  7834. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7835. item[@"order_status"]= nsstatus;
  7836. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7837. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7838. // item[@"model_count"]
  7839. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7840. count++;
  7841. }
  7842. ret[@"count"]= [NSNumber numberWithInt:count];
  7843. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7844. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7845. ret[@"result"]= [NSNumber numberWithInt:2];
  7846. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7847. ret[@"time_zone"] = @"PST";
  7848. sqlite3_finalize(statement);
  7849. }
  7850. 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];
  7851. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7852. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7853. [iSalesDB close_db:db];
  7854. return [RAConvertor dict2data:ret];
  7855. }
  7856. #pragma mark update gnotes
  7857. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7858. {
  7859. DebugLog(@"params: %@",params);
  7860. // comments = Meyoyoyoyoyoyoy;
  7861. // orderCode = MOB1608110001;
  7862. // password = 123456;
  7863. // user = EvanK;
  7864. 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]];
  7865. int ret = [iSalesDB execSql:sql];
  7866. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7867. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7868. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7869. // [dic setValue:@"160409" forKey:@"min_ver"];
  7870. return [RAConvertor dict2data:dic];
  7871. }
  7872. #pragma mark move to wishlist
  7873. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7874. {
  7875. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7876. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7877. _id = [NSString stringWithFormat:@"(%@)",_id];
  7878. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7879. sqlite3 *db = [iSalesDB get_db];
  7880. __block NSString *product_id = @"";
  7881. __block NSString *item_count_str = @"";
  7882. // __block NSString *item_id = nil;
  7883. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7884. // product_id = [self textAtColumn:0 statement:stmt];
  7885. int item_count = sqlite3_column_int(stmt, 1);
  7886. // item_id = [self textAtColumn:2 statement:stmt];
  7887. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7888. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7889. if (p_id.length) {
  7890. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7891. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7892. }
  7893. }];
  7894. [iSalesDB close_db:db];
  7895. // 去除第一个,
  7896. if (product_id.length > 1) {
  7897. product_id = [product_id substringFromIndex:1];
  7898. }
  7899. if (item_count_str.length > 1) {
  7900. item_count_str = [item_count_str substringFromIndex:1];
  7901. }
  7902. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7903. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7904. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7905. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7906. sqlite3 *db1 = [iSalesDB get_db];
  7907. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7908. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7909. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7910. // 删除
  7911. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7912. int ret = [iSalesDB execSql:deleteSql db:db1];
  7913. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7914. [iSalesDB close_db:db1];
  7915. return [RAConvertor dict2data:dic];
  7916. }
  7917. #pragma mark cart delete
  7918. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7919. {
  7920. // cartItemId = 548;
  7921. // orderCode = MOB1608110001;
  7922. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7923. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7924. _id = [NSString stringWithFormat:@"(%@)",_id];
  7925. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7926. int ret = [iSalesDB execSql:sql];
  7927. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7928. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7929. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7930. // [dic setValue:@"160409" forKey:@"min_ver"];
  7931. return [RAConvertor dict2data:dic];
  7932. }
  7933. #pragma mark set price
  7934. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7935. {
  7936. DebugLog(@"cart set price params: %@",params);
  7937. // "cartitem_id" = 1;
  7938. // discount = "0.000000";
  7939. // "item_note" = "";
  7940. // price = "269.000000";
  7941. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7942. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7943. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7944. // NSString *price = [self valueInParams:params key:@"price"];
  7945. // bool badd_price_changed=false;
  7946. // sqlite3* db=[iSalesDB get_db];
  7947. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7948. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7949. // NSRange range;
  7950. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7951. //
  7952. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7953. // badd_price_changed=true;
  7954. // [iSalesDB close_db:db];
  7955. //
  7956. // if(badd_price_changed)
  7957. // {
  7958. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7959. // }
  7960. //
  7961. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7962. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7963. int ret = [iSalesDB execSql:sql];
  7964. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7965. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7966. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7967. // [dic setValue:@"160409" forKey:@"min_ver"];
  7968. // return [RAConvertor dict2data:dic];
  7969. return dic;
  7970. }
  7971. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7972. {
  7973. DebugLog(@"cart set price params: %@",params);
  7974. // "cartitem_id" = 1;
  7975. // discount = "0.000000";
  7976. // "item_note" = "";
  7977. // price = "269.000000";
  7978. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7979. NSString *notes = [self valueInParams:params key:@"item_note"];
  7980. NSString *discount = [self valueInParams:params key:@"discount"];
  7981. NSString *price = [self valueInParams:params key:@"price"];
  7982. // bool badd_price_changed=false;
  7983. // sqlite3* db=[iSalesDB get_db];
  7984. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7985. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7986. // NSRange range;
  7987. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7988. //
  7989. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7990. // badd_price_changed=true;
  7991. // [iSalesDB close_db:db];
  7992. //
  7993. // if(badd_price_changed)
  7994. // {
  7995. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7996. // }
  7997. //
  7998. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7999. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  8000. int ret = [iSalesDB execSql:sql];
  8001. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8002. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8003. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8004. // [dic setValue:@"160409" forKey:@"min_ver"];
  8005. return [RAConvertor dict2data:dic];
  8006. }
  8007. #pragma mark set line notes
  8008. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  8009. {
  8010. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8011. NSString *notes = [self valueInParams:params key:@"notes"];
  8012. notes = [self translateSingleQuote:notes];
  8013. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  8014. int ret = [iSalesDB execSql:sql];
  8015. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8016. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8017. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8018. // [dic setValue:@"160409" forKey:@"min_ver"];
  8019. return [RAConvertor dict2data:dic];
  8020. }
  8021. #pragma mark set qty
  8022. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  8023. {
  8024. assert(params[@"can_create_backorder"]!=nil);
  8025. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8026. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8027. int item_count = [params[@"inputInt"] intValue];
  8028. // 购买检查数量大于库存
  8029. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8030. if (!params[@"can_create_backorder"]) {
  8031. 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];
  8032. __block BOOL out_of_stock = NO;
  8033. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8034. int availability = sqlite3_column_int(stmt, 0);
  8035. if (availability < item_count) {
  8036. out_of_stock = YES;
  8037. }
  8038. }];
  8039. if (out_of_stock) { // 缺货
  8040. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  8041. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  8042. return [RAConvertor dict2data:dic];
  8043. }
  8044. }
  8045. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  8046. int ret = [iSalesDB execSql:sql];
  8047. __block int item_id = 0;
  8048. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8049. item_id = sqlite3_column_int(stmt, 0);
  8050. }];
  8051. sqlite3 *db = [iSalesDB get_db];
  8052. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8053. [iSalesDB close_db:db];
  8054. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8055. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8056. // [dic setValue:@"160409" forKey:@"min_ver"];
  8057. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  8058. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  8059. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  8060. return [RAConvertor dict2data:dic];
  8061. }
  8062. #pragma mark place order
  8063. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  8064. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8065. if (aname) {
  8066. [dic setValue:aname forKey:@"aname"];
  8067. }
  8068. if (control) {
  8069. [dic setValue:control forKey:@"control"];
  8070. }
  8071. if (keyboard) {
  8072. [dic setValue:keyboard forKey:@"keyboard"];
  8073. }
  8074. if (name) {
  8075. [dic setValue:name forKey:@"name"];
  8076. }
  8077. if (value) {
  8078. [dic setValue:value forKey:@"value"];
  8079. }
  8080. return dic;
  8081. }
  8082. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  8083. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8084. orderCode = [self translateSingleQuote:orderCode];
  8085. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  8086. 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];
  8087. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8088. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  8089. NSString *img0 = [self textAtColumn:1 statement:stmt];
  8090. NSString *img1 = [self textAtColumn:2 statement:stmt];
  8091. NSString *img2 = [self textAtColumn:3 statement:stmt];
  8092. NSString *company_name = [self textAtColumn:4 statement:stmt];
  8093. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  8094. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  8095. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  8096. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  8097. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  8098. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  8099. NSString *email = [self textAtColumn:11 statement:stmt];
  8100. NSString *phone = [self textAtColumn:12 statement:stmt];
  8101. NSString *fax = [self textAtColumn:13 statement:stmt];
  8102. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  8103. NSString *city = [self textAtColumn:15 statement:stmt];
  8104. NSString *state = [self textAtColumn:16 statement:stmt];
  8105. NSString *country = [self textAtColumn:17 statement:stmt];
  8106. NSString *name = [self textAtColumn:18 statement:stmt];
  8107. // contact id
  8108. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  8109. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  8110. [contact_id_dic setValue:@"text" forKey:@"control"];
  8111. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  8112. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  8113. [contact_id_dic setValue:@"true" forKey:@"required"];
  8114. [contact_id_dic setValue:contact_id forKey:@"value"];
  8115. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  8116. // business card
  8117. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  8118. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  8119. [business_card_dic setValue:@"img" forKey:@"control"];
  8120. [business_card_dic setValue:@"1" forKey:@"disable"];
  8121. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  8122. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  8123. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  8124. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  8125. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  8126. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  8127. [business_card_dic setValue:@"business_card" forKey:@"name"];
  8128. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  8129. // fax
  8130. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8131. [customer_dic setValue:fax_dic forKey:@"item_10"];
  8132. // zipcode
  8133. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  8134. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  8135. // city
  8136. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  8137. [customer_dic setValue:city_dic forKey:@"item_12"];
  8138. // state
  8139. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  8140. [customer_dic setValue:state_dic forKey:@"item_13"];
  8141. // country
  8142. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  8143. [customer_dic setValue:country_dic forKey:@"item_14"];
  8144. // company name
  8145. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  8146. [customer_dic setValue:company_dic forKey:@"item_2"];
  8147. // addr_1
  8148. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  8149. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  8150. // addr_2
  8151. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  8152. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  8153. // addr_3
  8154. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  8155. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  8156. // addr_4
  8157. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  8158. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  8159. // Contact
  8160. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  8161. [customer_dic setValue:contact_dic forKey:@"item_7"];
  8162. // email
  8163. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8164. [customer_dic setValue:email_dic forKey:@"item_8"];
  8165. // phone
  8166. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8167. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8168. // title
  8169. [customer_dic setValue:@"Customer" forKey:@"title"];
  8170. // count
  8171. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8172. }];
  8173. // setting
  8174. NSDictionary *setting = params[@"setting"];
  8175. NSNumber *hide = setting[@"CustomerHide"];
  8176. [customer_dic setValue:hide forKey:@"hide"];
  8177. return customer_dic;
  8178. }
  8179. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8180. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8181. // setting
  8182. NSDictionary *setting = params[@"setting"];
  8183. NSNumber *hide = setting[@"ShipToHide"];
  8184. [dic setValue:hide forKey:@"hide"];
  8185. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8186. orderCode = [self translateSingleQuote:orderCode];
  8187. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  8188. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8189. NSString *cid = [self textAtColumn:0 statement:stmt];
  8190. NSString *name = [self textAtColumn:1 statement:stmt];
  8191. NSString *ext = [self textAtColumn:2 statement:stmt];
  8192. NSString *contact = [self textAtColumn:3 statement:stmt];
  8193. NSString *email = [self textAtColumn:4 statement:stmt];
  8194. NSString *fax = [self textAtColumn:5 statement:stmt];
  8195. NSString *phone = [self textAtColumn:6 statement:stmt];
  8196. // count
  8197. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8198. // title
  8199. [dic setValue:@"Ship To" forKey:@"title"];
  8200. // choose
  8201. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8202. [choose_dic setValue:@"choose" forKey:@"aname"];
  8203. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8204. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8205. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8206. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8207. @"receive_contact" : @"customer_contact",
  8208. @"receive_email" : @"customer_email",
  8209. @"receive_ext" : @"customer_contact_ext",
  8210. @"receive_fax" : @"customer_fax",
  8211. @"receive_name" : @"customer_name",
  8212. @"receive_phone" : @"customer_phone"},
  8213. @"refresh" : [NSNumber numberWithInteger:1],
  8214. @"type" : @"pull"};
  8215. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8216. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8217. @"name" : @"Add new address",
  8218. @"refresh" : [NSNumber numberWithInteger:1],
  8219. @"value" : @"new_addr"
  8220. };
  8221. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8222. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8223. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8224. @"receive_contact" : @"customer_contact",
  8225. @"receive_email" : @"customer_email",
  8226. @"receive_ext" : @"customer_contact_ext",
  8227. @"receive_fax" : @"customer_fax",
  8228. @"receive_name" : @"customer_name",
  8229. @"receive_phone" : @"customer_phone"},
  8230. @"name" : @"select_ship_to",
  8231. @"refresh" : [NSNumber numberWithInteger:1],
  8232. @"value" : @"Sales_Order_Ship_To"};
  8233. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8234. [dic setValue:choose_dic forKey:@"item_0"];
  8235. // contact id
  8236. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8237. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8238. [contact_id_dic setValue:@"true" forKey:@"required"];
  8239. [dic setValue:contact_id_dic forKey:@"item_1"];
  8240. // company name
  8241. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8242. [dic setValue:company_name_dic forKey:@"item_2"];
  8243. // address
  8244. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8245. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8246. if ([required integerValue]) {
  8247. [ext_dic setValue:@"true" forKey:@"required"];
  8248. }
  8249. [dic setValue:ext_dic forKey:@"item_3"];
  8250. // contact
  8251. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8252. [dic setValue:contact_dic forKey:@"item_4"];
  8253. // phone
  8254. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8255. [dic setValue:phone_dic forKey:@"item_5"];
  8256. // fax
  8257. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8258. [dic setValue:fax_dic forKey:@"item_6"];
  8259. // email
  8260. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8261. [dic setValue:email_dic forKey:@"item_7"];
  8262. }];
  8263. return dic;
  8264. }
  8265. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8266. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8267. // setting
  8268. NSDictionary *setting = params[@"setting"];
  8269. NSNumber *hide = setting[@"ShipFromHide"];
  8270. [dic setValue:hide forKey:@"hide"];
  8271. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8272. orderCode = [self translateSingleQuote:orderCode];
  8273. 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];
  8274. 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';";
  8275. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8276. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8277. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8278. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8279. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8280. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8281. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8282. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8283. if (!cid.length) {
  8284. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8285. cid = [self textAtColumn:0 statement:statment];
  8286. name = [self textAtColumn:1 statement:statment];
  8287. ext = [self textAtColumn:2 statement:statment];
  8288. contact = [self textAtColumn:3 statement:statment];
  8289. email = [self textAtColumn:4 statement:statment];
  8290. fax = [self textAtColumn:5 statement:statment];
  8291. phone = [self textAtColumn:6 statement:statment];
  8292. }];
  8293. }
  8294. // count
  8295. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8296. // title
  8297. [dic setValue:@"Ship From" forKey:@"title"];
  8298. // hide
  8299. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8300. // choose
  8301. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8302. [choose_dic setValue:@"choose" forKey:@"aname"];
  8303. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8304. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8305. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8306. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8307. @"sender_contact" : @"customer_contact",
  8308. @"sender_email" : @"customer_email",
  8309. @"sender_ext" : @"customer_contact_ext",
  8310. @"sender_fax" : @"customer_fax",
  8311. @"sender_name" : @"customer_name",
  8312. @"sender_phone" : @"customer_phone"},
  8313. @"name" : @"select_cid",
  8314. @"refresh" : [NSNumber numberWithInteger:0],
  8315. @"value" : @"Sales_Order_Ship_From"};
  8316. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8317. [dic setValue:choose_dic forKey:@"item_0"];
  8318. // contact id
  8319. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8320. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8321. [contact_id_dic setValue:@"true" forKey:@"required"];
  8322. [dic setValue:contact_id_dic forKey:@"item_1"];
  8323. // company name
  8324. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8325. [dic setValue:company_name_dic forKey:@"item_2"];
  8326. // address
  8327. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8328. [dic setValue:ext_dic forKey:@"item_3"];
  8329. // contact
  8330. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8331. [dic setValue:contact_dic forKey:@"item_4"];
  8332. // phone
  8333. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8334. [dic setValue:phone_dic forKey:@"item_5"];
  8335. // fax
  8336. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8337. [dic setValue:fax_dic forKey:@"item_6"];
  8338. // email
  8339. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8340. [dic setValue:email_dic forKey:@"item_7"];
  8341. }];
  8342. return dic;
  8343. }
  8344. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8345. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8346. // setting
  8347. NSDictionary *setting = params[@"setting"];
  8348. NSNumber *hide = setting[@"FreightBillToHide"];
  8349. [dic setValue:hide forKey:@"hide"];
  8350. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8351. orderCode = [self translateSingleQuote:orderCode];
  8352. 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];
  8353. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8354. NSString *cid = [self textAtColumn:0 statement:stmt];
  8355. NSString *name = [self textAtColumn:1 statement:stmt];
  8356. NSString *ext = [self textAtColumn:2 statement:stmt];
  8357. NSString *contact = [self textAtColumn:3 statement:stmt];
  8358. NSString *email = [self textAtColumn:4 statement:stmt];
  8359. NSString *fax = [self textAtColumn:5 statement:stmt];
  8360. NSString *phone = [self textAtColumn:6 statement:stmt];
  8361. // count
  8362. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8363. // title
  8364. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8365. // hide
  8366. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8367. // choose
  8368. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8369. [choose_dic setValue:@"choose" forKey:@"aname"];
  8370. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8371. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8372. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8373. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8374. @"shipping_billto_contact" : @"receive_contact",
  8375. @"shipping_billto_email" : @"receive_email",
  8376. @"shipping_billto_ext" : @"receive_ext",
  8377. @"shipping_billto_fax" : @"receive_fax",
  8378. @"shipping_billto_name" : @"receive_name",
  8379. @"shipping_billto_phone" : @"receive_phone"},
  8380. @"type" : @"pull"};
  8381. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8382. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8383. @"type" : @"pull",
  8384. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8385. @"shipping_billto_contact" : @"customer_contact",
  8386. @"shipping_billto_email" : @"customer_email",
  8387. @"shipping_billto_ext" : @"customer_contact_ext",
  8388. @"shipping_billto_fax" : @"customer_fax",
  8389. @"shipping_billto_name" : @"customer_name",
  8390. @"shipping_billto_phone" : @"customer_phone"}
  8391. };
  8392. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8393. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8394. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8395. @"shipping_billto_contact" : @"sender_contact",
  8396. @"shipping_billto_email" : @"sender_email",
  8397. @"shipping_billto_ext" : @"sender_ext",
  8398. @"shipping_billto_fax" : @"sender_fax",
  8399. @"shipping_billto_name" : @"sender_name",
  8400. @"shipping_billto_phone" : @"sender_phone"},
  8401. @"type" : @"pull"
  8402. };
  8403. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8404. NSDictionary *select_freight_bill_to_dic = @{
  8405. @"aname" : @"Select freight bill to",
  8406. @"name" : @"select_cid",
  8407. @"refresh" : [NSNumber numberWithInteger:0],
  8408. @"value" : @"Sales_Order_Freight_Bill_To",
  8409. @"key_map" : @{
  8410. @"shipping_billto_cid" : @"customer_cid",
  8411. @"shipping_billto_contact" : @"customer_contact",
  8412. @"shipping_billto_email" : @"customer_email",
  8413. @"shipping_billto_ext" : @"customer_contact_ext",
  8414. @"shipping_billto_fax" : @"customer_fax",
  8415. @"shipping_billto_name" : @"customer_name",
  8416. @"shipping_billto_phone" : @"customer_phone"
  8417. }
  8418. };
  8419. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8420. [dic setValue:choose_dic forKey:@"item_0"];
  8421. // contact id
  8422. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8423. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8424. [contact_id_dic setValue:@"true" forKey:@"required"];
  8425. [dic setValue:contact_id_dic forKey:@"item_1"];
  8426. // company name
  8427. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8428. [dic setValue:company_name_dic forKey:@"item_2"];
  8429. // address
  8430. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8431. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8432. if ([ext_required integerValue]) {
  8433. [ext_dic setValue:@"true" forKey:@"required"];
  8434. }
  8435. [dic setValue:ext_dic forKey:@"item_3"];
  8436. // contact
  8437. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8438. [dic setValue:contact_dic forKey:@"item_4"];
  8439. // phone
  8440. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8441. [dic setValue:phone_dic forKey:@"item_5"];
  8442. // fax
  8443. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8444. [dic setValue:fax_dic forKey:@"item_6"];
  8445. // email
  8446. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8447. [dic setValue:email_dic forKey:@"item_7"];
  8448. }];
  8449. return dic;
  8450. }
  8451. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8452. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8453. // setting
  8454. NSDictionary *setting = params[@"setting"];
  8455. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8456. [dic setValue:hide forKey:@"hide"];
  8457. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8458. orderCode = [self translateSingleQuote:orderCode];
  8459. 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];
  8460. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8461. NSString *cid = [self textAtColumn:0 statement:stmt];
  8462. NSString *name = [self textAtColumn:1 statement:stmt];
  8463. NSString *ext = [self textAtColumn:2 statement:stmt];
  8464. NSString *contact = [self textAtColumn:3 statement:stmt];
  8465. NSString *email = [self textAtColumn:4 statement:stmt];
  8466. NSString *fax = [self textAtColumn:5 statement:stmt];
  8467. NSString *phone = [self textAtColumn:6 statement:stmt];
  8468. // count
  8469. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8470. // title
  8471. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8472. // hide
  8473. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8474. // choose
  8475. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8476. [choose_dic setValue:@"choose" forKey:@"aname"];
  8477. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8478. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8479. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8480. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8481. @"billing_contact" : @"receive_contact",
  8482. @"billing_email" : @"receive_email",
  8483. @"billing_ext" : @"receive_ext",
  8484. @"billing_fax" : @"receive_fax",
  8485. @"billing_name" : @"receive_name",
  8486. @"billing_phone" : @"receive_phone"},
  8487. @"type" : @"pull"};
  8488. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8489. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8490. @"type" : @"pull",
  8491. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8492. @"billing_contact" : @"customer_contact",
  8493. @"billing_email" : @"customer_email",
  8494. @"billing_ext" : @"customer_contact_ext",
  8495. @"billing_fax" : @"customer_fax",
  8496. @"billing_name" : @"customer_name",
  8497. @"billing_phone" : @"customer_phone"}
  8498. };
  8499. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8500. NSDictionary *select_bill_to_dic = @{
  8501. @"aname" : @"Select bill to",
  8502. @"name" : @"select_cid",
  8503. @"refresh" : [NSNumber numberWithInteger:0],
  8504. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8505. @"key_map" : @{
  8506. @"billing_cid" : @"customer_cid",
  8507. @"billing_contact" : @"customer_contact",
  8508. @"billing_email" : @"customer_email",
  8509. @"billing_ext" : @"customer_contact_ext",
  8510. @"billing_fax" : @"customer_fax",
  8511. @"billing_name" : @"customer_name",
  8512. @"billing_phone" : @"customer_phone"
  8513. }
  8514. };
  8515. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8516. [dic setValue:choose_dic forKey:@"item_0"];
  8517. // contact id
  8518. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8519. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8520. [contact_id_dic setValue:@"true" forKey:@"required"];
  8521. [dic setValue:contact_id_dic forKey:@"item_1"];
  8522. // company name
  8523. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8524. [dic setValue:company_name_dic forKey:@"item_2"];
  8525. // address
  8526. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8527. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8528. if ([ext_required integerValue]) {
  8529. [ext_dic setValue:@"true" forKey:@"required"];
  8530. }
  8531. [dic setValue:ext_dic forKey:@"item_3"];
  8532. // contact
  8533. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8534. [dic setValue:contact_dic forKey:@"item_4"];
  8535. // phone
  8536. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8537. [dic setValue:phone_dic forKey:@"item_5"];
  8538. // fax
  8539. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8540. [dic setValue:fax_dic forKey:@"item_6"];
  8541. // email
  8542. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8543. [dic setValue:email_dic forKey:@"item_7"];
  8544. }];
  8545. return dic;
  8546. }
  8547. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8548. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8549. // setting
  8550. NSDictionary *setting = params[@"setting"];
  8551. NSNumber *hide = setting[@"ReturnToHide"];
  8552. [dic setValue:hide forKey:@"hide"];
  8553. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8554. orderCode = [self translateSingleQuote:orderCode];
  8555. 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];
  8556. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8557. NSString *cid = [self textAtColumn:0 statement:stmt];
  8558. NSString *name = [self textAtColumn:1 statement:stmt];
  8559. NSString *ext = [self textAtColumn:2 statement:stmt];
  8560. NSString *contact = [self textAtColumn:3 statement:stmt];
  8561. NSString *email = [self textAtColumn:4 statement:stmt];
  8562. NSString *fax = [self textAtColumn:5 statement:stmt];
  8563. NSString *phone = [self textAtColumn:6 statement:stmt];
  8564. // count
  8565. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8566. // title
  8567. [dic setValue:@"Return To" forKey:@"title"];
  8568. // hide
  8569. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8570. // choose
  8571. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8572. [choose_dic setValue:@"choose" forKey:@"aname"];
  8573. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8574. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8575. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8576. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8577. @"returnto_contact" : @"sender_contact",
  8578. @"returnto_email" : @"sender_email",
  8579. @"returnto_ext" : @"sender_ext",
  8580. @"returnto_fax" : @"sender_fax",
  8581. @"returnto_name" : @"sender_name",
  8582. @"returnto_phone" : @"sender_phone"},
  8583. @"type" : @"pull"};
  8584. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8585. NSDictionary *select_return_to_dic = @{
  8586. @"aname" : @"Select return to",
  8587. @"name" : @"select_cid",
  8588. @"refresh" : [NSNumber numberWithInteger:0],
  8589. @"value" : @"Contact_Return_To",
  8590. @"key_map" : @{
  8591. @"returnto_cid" : @"customer_cid",
  8592. @"returnto_contact" : @"customer_contact",
  8593. @"returnto_email" : @"customer_email",
  8594. @"returnto_ext" : @"customer_contact_ext",
  8595. @"returnto_fax" : @"customer_fax",
  8596. @"returnto_name" : @"customer_name",
  8597. @"returnto_phone" : @"customer_phone"
  8598. }
  8599. };
  8600. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8601. [dic setValue:choose_dic forKey:@"item_0"];
  8602. // contact id
  8603. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8604. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8605. [contact_id_dic setValue:@"true" forKey:@"required"];
  8606. [dic setValue:contact_id_dic forKey:@"item_1"];
  8607. // company name
  8608. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8609. [dic setValue:company_name_dic forKey:@"item_2"];
  8610. // address
  8611. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8612. [dic setValue:ext_dic forKey:@"item_3"];
  8613. // contact
  8614. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8615. [dic setValue:contact_dic forKey:@"item_4"];
  8616. // phone
  8617. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8618. [dic setValue:phone_dic forKey:@"item_5"];
  8619. // fax
  8620. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8621. [dic setValue:fax_dic forKey:@"item_6"];
  8622. // email
  8623. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8624. [dic setValue:email_dic forKey:@"item_7"];
  8625. }];
  8626. return dic;
  8627. }
  8628. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8629. {
  8630. assert(params[@"user"]!=nil);
  8631. assert(params[@"contact_id"]!=nil);
  8632. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8633. orderCode = [self translateSingleQuote:orderCode];
  8634. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8635. __block double TotalCuft = 0;
  8636. __block double TotalWeight = 0;
  8637. __block int TotalCarton = 0;
  8638. __block double payments = 0;
  8639. // setting
  8640. NSDictionary *setting = params[@"setting"];
  8641. NSNumber *hide = setting[@"ModelInformationHide"];
  8642. [dic setValue:hide forKey:@"hide"];
  8643. 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];
  8644. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8645. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8646. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8647. // item id
  8648. int item_id = sqlite3_column_int(stmt, 0);
  8649. // count
  8650. int item_count = sqlite3_column_int(stmt, 1);
  8651. // stockUom
  8652. int stockUom = sqlite3_column_int(stmt, 2);
  8653. // unit price
  8654. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8655. NSString* Price=nil;
  8656. if([str_price isEqualToString:@""])
  8657. {
  8658. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8659. if(price==nil)
  8660. Price=@"No Price.";
  8661. else
  8662. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8663. }
  8664. else
  8665. {
  8666. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8667. }
  8668. // discount
  8669. double discount = sqlite3_column_double(stmt, 4);
  8670. // name
  8671. NSString *name = [self textAtColumn:5 statement:stmt];
  8672. // description
  8673. NSString *description = [self textAtColumn:6 statement:stmt];
  8674. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8675. // line note
  8676. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8677. int avaulability = sqlite3_column_int(stmt, 8);
  8678. // img
  8679. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8680. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8681. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8682. if(combine != nil)
  8683. {
  8684. // int citem=0;
  8685. int bcount=[[combine valueForKey:@"count"] intValue];
  8686. for(int bc=0;bc<bcount;bc++)
  8687. {
  8688. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8689. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8690. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8691. subTotal += uprice * modulus * item_count;
  8692. }
  8693. }
  8694. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8695. [model_dic setValue:@"model" forKey:@"control"];
  8696. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8697. [model_dic setValue:description forKey:@"description"];
  8698. [model_dic setValue:line_note forKey:@"note"];
  8699. [model_dic setValue:img forKey:@"img_url"];
  8700. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8701. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8702. [model_dic setValue:Price forKey:@"unit_price"];
  8703. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8704. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8705. if (combine) {
  8706. [model_dic setValue:combine forKey:@"combine"];
  8707. }
  8708. // well,what under the row is the info for total
  8709. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8710. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8711. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8712. int carton=[bsubtotaljson[@"carton"] intValue];
  8713. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8714. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8715. TotalCuft += cuft;
  8716. TotalWeight += weight;
  8717. TotalCarton += carton;
  8718. payments += subTotal;
  8719. //---------------------------
  8720. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8721. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8722. }];
  8723. [dic setValue:@"Model Information" forKey:@"title"];
  8724. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8725. return dic;
  8726. }
  8727. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8729. [dic setValue:@"Remarks Content" forKey:@"title"];
  8730. // setting
  8731. NSDictionary *setting = params[@"setting"];
  8732. NSNumber *hide = setting[@"RemarksContentHide"];
  8733. [dic setValue:hide forKey:@"hide"];
  8734. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8735. orderCode = [self translateSingleQuote:orderCode];
  8736. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8737. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8738. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8739. int mustCall = sqlite3_column_int(stmt, 1);
  8740. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8741. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8742. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8743. NSDictionary *po_dic = @{
  8744. @"aname" : @"PO#",
  8745. @"control" : @"edit",
  8746. @"keyboard" : @"text",
  8747. @"name" : @"poNumber",
  8748. @"value" : poNumber
  8749. };
  8750. #ifdef BUILD_CONTRAST
  8751. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8752. NSDictionary *schedule_dict = @{
  8753. @"aname" : @"Schdule Date",
  8754. @"control" : @"datepicker",
  8755. @"type": @"date",
  8756. @"required": @"true",
  8757. @"name" : @"schedule_date_str",
  8758. @"value" : nsdate
  8759. };
  8760. #endif
  8761. NSDictionary *must_call_dic = @{
  8762. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8763. @"control" : @"switch",
  8764. @"name" : @"must_call",
  8765. @"value" : mustCall ? @"true" : @"false"
  8766. };
  8767. NSDictionary *general_notes_dic = @{
  8768. @"aname" : @"General notes",
  8769. @"control" : @"text_view",
  8770. @"keyboard" : @"text",
  8771. @"name" : @"comments",
  8772. @"value" : generalNotes
  8773. };
  8774. // NSDictionary *internal_notes_dic = @{
  8775. // @"aname" : @"Internal notes",
  8776. // @"control" : @"text_view",
  8777. // @"keyboard" : @"text",
  8778. // @"name" : @"internal_notes",
  8779. // @"value" : internalNotes
  8780. // };
  8781. #ifdef BUILD_CONTRAST
  8782. [dic setValue:po_dic forKey:@"item_0"];
  8783. [dic setValue:schedule_dict forKey:@"item_1"];
  8784. [dic setValue:must_call_dic forKey:@"item_2"];
  8785. [dic setValue:general_notes_dic forKey:@"item_3"];
  8786. #else
  8787. [dic setValue:po_dic forKey:@"item_0"];
  8788. [dic setValue:must_call_dic forKey:@"item_1"];
  8789. [dic setValue:general_notes_dic forKey:@"item_2"];
  8790. #endif
  8791. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8792. }];
  8793. #ifdef BUILD_CONTRAST
  8794. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8795. #else
  8796. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8797. #endif
  8798. return dic;
  8799. }
  8800. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8801. // params
  8802. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8803. orderCode = [self translateSingleQuote:orderCode];
  8804. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8805. // setting
  8806. NSDictionary *setting = params[@"setting"];
  8807. NSNumber *hide = setting[@"OrderTotalHide"];
  8808. [dic setValue:hide forKey:@"hide"];
  8809. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8810. __block double lift_gate_value = 0;
  8811. __block double handling_fee = 0;
  8812. __block double shipping = 0;
  8813. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8814. int lift_gate = sqlite3_column_int(stmt, 0);
  8815. lift_gate_value = sqlite3_column_double(stmt, 1);
  8816. shipping = sqlite3_column_double(stmt, 2);
  8817. handling_fee = sqlite3_column_double(stmt, 3);
  8818. if (!lift_gate) {
  8819. lift_gate_value = 0;
  8820. }
  8821. }];
  8822. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8823. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8824. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8825. double payments = [[total valueForKey:@"payments"] doubleValue];
  8826. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8827. double totalPrice = payments;
  8828. [dic setValue:@"Order Total" forKey:@"title"];
  8829. NSDictionary *payments_dic = @{
  8830. @"align" : @"right",
  8831. @"aname" : @"Payments/Credits",
  8832. @"control" : @"text",
  8833. @"name" : @"paymentsAndCredits",
  8834. @"type" : @"price",
  8835. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8836. };
  8837. [dic setValue:payments_dic forKey:@"item_0"];
  8838. // version 1.71 remove
  8839. // NSDictionary *handling_fee_dic = @{
  8840. // @"align" : @"right",
  8841. // @"aname" : @"Handling Fee",
  8842. // @"control" : @"text",
  8843. // @"name" : @"handling_fee_value",
  8844. // @"required" : @"true",
  8845. // @"type" : @"price",
  8846. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8847. // };
  8848. NSDictionary *shipping_dic = @{
  8849. @"align" : @"right",
  8850. @"aname" : @"Shipping*",
  8851. @"control" : @"text",
  8852. @"name" : @"shipping",
  8853. @"required" : @"true",
  8854. @"type" : @"price",
  8855. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8856. };
  8857. NSDictionary *lift_gate_dic = @{
  8858. @"align" : @"right",
  8859. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8860. @"control" : @"text",
  8861. @"name" : @"lift_gate_value",
  8862. @"required" : @"true",
  8863. @"type" : @"price",
  8864. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8865. };
  8866. int item_count = 1;
  8867. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8868. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8869. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8870. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8871. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8872. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8873. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8874. } else {
  8875. }
  8876. }
  8877. // version 1.71 remove
  8878. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8879. NSDictionary *total_price_dic = @{
  8880. @"align" : @"right",
  8881. @"aname" : @"Total",
  8882. @"control" : @"text",
  8883. @"name" : @"totalPrice",
  8884. @"type" : @"price",
  8885. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8886. };
  8887. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8888. NSDictionary *total_cuft_dic = @{
  8889. @"align" : @"right",
  8890. @"aname" : @"Total Cuft",
  8891. @"control" : @"text",
  8892. @"name" : @"",
  8893. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8894. };
  8895. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8896. NSDictionary *total_weight_dic = @{
  8897. @"align" : @"right",
  8898. @"aname" : @"Total Weight",
  8899. @"control" : @"text",
  8900. @"name" : @"",
  8901. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8902. };
  8903. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8904. NSDictionary *total_carton_dic = @{
  8905. @"align" : @"right",
  8906. @"aname" : @"Total Carton",
  8907. @"control" : @"text",
  8908. @"name" : @"",
  8909. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8910. };
  8911. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8912. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8913. return dic;
  8914. }
  8915. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8916. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8917. orderCode = [self translateSingleQuote:orderCode];
  8918. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8919. // setting
  8920. NSDictionary *setting = params[@"setting"];
  8921. NSNumber *hide = setting[@"SignatureHide"];
  8922. [dic setValue:hide forKey:@"hide"];
  8923. [dic setValue:@"Signature" forKey:@"title"];
  8924. __block NSString *pic_path = @"";
  8925. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8926. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8927. pic_path = [self textAtColumn:0 statement:stmt];
  8928. }];
  8929. NSDictionary *pic_dic = @{
  8930. @"aname" : @"Signature",
  8931. @"avalue" :pic_path,
  8932. @"control" : @"signature",
  8933. @"name" : @"sign_picpath",
  8934. @"value" : pic_path
  8935. };
  8936. [dic setValue:pic_dic forKey:@"item_0"];
  8937. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8938. return dic;
  8939. }
  8940. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8941. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8942. // setting
  8943. NSDictionary *setting = params[@"setting"];
  8944. NSNumber *hide = setting[@"ShippingMethodHide"];
  8945. [dic setValue:hide forKey:@"hide"];
  8946. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8947. orderCode = [self translateSingleQuote:orderCode];
  8948. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8949. __block NSString *logist = @"";
  8950. __block NSString *carrier = @"";
  8951. __block NSString *lift_gate = @"";
  8952. __block int lift_gate_integer = 0;
  8953. __block NSString *logistic_note = @"";
  8954. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8955. logist = [self textAtColumn:0 statement:stmt];
  8956. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8957. logistic_note = [self textAtColumn:2 statement:stmt];
  8958. carrier = [self textAtColumn:3 statement:stmt];
  8959. }];
  8960. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8961. [dic setValue:@"Shipping Method" forKey:@"title"];
  8962. // val_0
  8963. int PERSONAL_PICK_UP_check = 0;
  8964. int USE_MY_CARRIER_check = 0;
  8965. NSString *logistic_note_text = @"";
  8966. int will_call_check = 0;
  8967. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8968. will_call_check = 1;
  8969. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8970. PERSONAL_PICK_UP_check = 1;
  8971. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8972. USE_MY_CARRIER_check = 1;
  8973. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8974. logistic_note = [logistic_note_array firstObject];
  8975. if (logistic_note_array.count > 1) {
  8976. logistic_note_text = [logistic_note_array lastObject];
  8977. }
  8978. }
  8979. }
  8980. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8981. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8982. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8983. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8984. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8985. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8986. NSDictionary *val0_subItem_item0 = @{
  8987. @"aname" : @"Option",
  8988. @"cadedate" : @{
  8989. @"count" : [NSNumber numberWithInteger:2],
  8990. @"val_0" : @{
  8991. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8992. @"refresh" : [NSNumber numberWithInteger:0],
  8993. @"value" : @"PERSONAL PICK UP",
  8994. @"value_id" : @"PERSONAL PICK UP"
  8995. },
  8996. @"val_1" : @{
  8997. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8998. @"refresh" : [NSNumber numberWithInteger:0],
  8999. @"sub_item" : @{
  9000. @"count" : [NSNumber numberWithInteger:1],
  9001. @"item_0" : @{
  9002. @"aname" : @"BOL",
  9003. @"control" : @"edit",
  9004. @"keyboard" : @"text",
  9005. @"name" : @"logist_note_text",
  9006. @"refresh" : [NSNumber numberWithInteger:0],
  9007. @"required" : @"false",
  9008. @"value" : logistic_note_text
  9009. }
  9010. },
  9011. @"value" : @"USE MY CARRIER",
  9012. @"value_id" : @"USE MY CARRIER"
  9013. }
  9014. },
  9015. @"control" : @"enum",
  9016. @"name" : @"logistic_note",
  9017. @"required" : @"true",
  9018. @"single_select" : @"true"
  9019. };
  9020. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  9021. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  9022. // val_1
  9023. lift_gate = [self valueInParams:params key:@"lift_gate"];
  9024. if([lift_gate isEqualToString:@""]) {
  9025. if (lift_gate_integer == 1) {
  9026. lift_gate = @"true";
  9027. } else {
  9028. lift_gate = @"false";
  9029. }
  9030. }
  9031. // carrier = [self valueInParams:params key:@"carrier"];
  9032. // if([carrier isEqualToString:@""]) {
  9033. //
  9034. // if (lift_gate_integer == 1) {
  9035. // lift_gate = @"true";
  9036. // } else {
  9037. // lift_gate = @"false";
  9038. // }
  9039. //
  9040. // }
  9041. int common_carrier_check = 0;
  9042. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  9043. common_carrier_check = 1;
  9044. [params setValue:lift_gate forKey:@"lift_gate"];
  9045. #ifdef BUILD_CONTRAST
  9046. [params setValue:carrier forKey:@"carrier"];
  9047. #endif
  9048. }
  9049. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  9050. // NSString * code = [iSalesDB jk_queryText:Sql];
  9051. // [self offline_getCarrierCode:offline_getCarrierCode];
  9052. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  9053. NSDictionary *val_1 = @{
  9054. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  9055. @"sub_item" : @{
  9056. @"item_0" : @{
  9057. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  9058. @"control" : @"switch",
  9059. @"name" : @"lift_gate",
  9060. @"refresh" : [NSNumber numberWithInteger:1],
  9061. @"required" : @"true",
  9062. @"value" : lift_gate
  9063. },
  9064. #ifdef BUILD_CONTRAST
  9065. @"count" : [NSNumber numberWithInteger:2],
  9066. @"item_1" : @{
  9067. @"aname" : @"Carrier",
  9068. @"cadedate" :carrierDic,
  9069. @"control" : @"enum",
  9070. @"name" : @"carrier",
  9071. @"required" : @"false",
  9072. @"value" : @"",
  9073. @"single_select" : @"true"
  9074. }
  9075. #else
  9076. @"count" : [NSNumber numberWithInteger:1]
  9077. #endif
  9078. },
  9079. @"value" : @"COMMON CARRIER",
  9080. @"value_id" : @"COMMON CARRIER"
  9081. };
  9082. // cadedate
  9083. NSDictionary *cadedate = @{
  9084. @"count" : [NSNumber numberWithInteger:2],
  9085. @"val_0" : val_0,
  9086. @"val_1" : val_1
  9087. };
  9088. // item_0
  9089. NSMutableDictionary *item_0 = @{
  9090. @"aname" : @"Shipping",
  9091. @"cadedate" : cadedate,
  9092. @"control" : @"enum",
  9093. @"name" : @"logist",
  9094. @"refresh" : [NSNumber numberWithInteger:1],
  9095. @"single_select" : @"true",
  9096. }.mutableCopy;
  9097. NSNumber *required = setting[@"ShippingMethodRequire"];
  9098. if ([required integerValue]) {
  9099. [item_0 setValue:@"true" forKey:@"required"];
  9100. }
  9101. [dic setValue:item_0 forKey:@"item_0"];
  9102. if ([logist isEqualToString:@"WILL CALL"]) {
  9103. [dic removeObjectForKey:@"lift_gate"];
  9104. }
  9105. return dic;
  9106. }
  9107. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  9108. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9109. orderCode = [self translateSingleQuote:orderCode];
  9110. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  9111. __block int submit_as_integer = 0;
  9112. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9113. submit_as_integer = sqlite3_column_int(stmt, 0);
  9114. }];
  9115. int check11 = 0;
  9116. int check10 = 0;
  9117. if (submit_as_integer == 11) {
  9118. check11 = 1;
  9119. }
  9120. if (submit_as_integer == 10) {
  9121. check10 = 1;
  9122. }
  9123. // section_0
  9124. NSMutableDictionary *dic = @{
  9125. @"count" : @(1),
  9126. @"item_0" : @{
  9127. @"aname" : @"Submit Order As",
  9128. @"cadedate" : @{
  9129. @"count" : @(2),
  9130. @"val_0" : @{
  9131. @"check" : [NSNumber numberWithInteger:check11],
  9132. @"value" : @"Sales Order",
  9133. @"value_id" : @(11)
  9134. },
  9135. @"val_1" : @{
  9136. @"check" : [NSNumber numberWithInteger:check10],
  9137. @"value" : @"Quote",
  9138. @"value_id" : @(10)
  9139. }
  9140. },
  9141. @"control" : @"enum",
  9142. @"name" : @"erpOrderStatus",
  9143. @"required" : @"true",
  9144. @"single_select" : @"true"
  9145. },
  9146. @"title" : @"Order Type"
  9147. }.mutableCopy;
  9148. // setting
  9149. NSDictionary *setting = params[@"setting"];
  9150. NSNumber *hide = setting[@"OrderTypeHide"];
  9151. [dic setValue:hide forKey:@"hide"];
  9152. return dic;
  9153. }
  9154. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  9155. countryCode = [self translateSingleQuote:countryCode];
  9156. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  9157. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9158. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  9159. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  9160. if (name == NULL) {
  9161. name = "";
  9162. }
  9163. if (code == NULL) {
  9164. code = "";
  9165. }
  9166. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9167. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  9168. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  9169. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9170. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  9171. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9172. }
  9173. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  9174. [container setValue:stateDic forKey:key];
  9175. }] mutableCopy];
  9176. [ret removeObjectForKey:@"result"];
  9177. // failure 可以不用了,一样的
  9178. if (ret.allKeys.count == 0) {
  9179. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9180. [stateDic setValue:@"Other" forKey:@"value"];
  9181. [stateDic setValue:@"" forKey:@"value_id"];
  9182. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9183. if (state_code && [@"" isEqualToString:state_code]) {
  9184. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9185. }
  9186. NSString *key = [NSString stringWithFormat:@"val_0"];
  9187. [ret setValue:stateDic forKey:key];
  9188. }
  9189. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  9190. return ret;
  9191. }
  9192. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  9193. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9194. orderCode = [self translateSingleQuote:orderCode];
  9195. 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];
  9196. __block NSString *payType = @"";
  9197. __block NSString *firstName = @"";
  9198. __block NSString *lastName = @"";
  9199. __block NSString *addr1 = @"";
  9200. __block NSString *addr2 = @"";
  9201. __block NSString *zipcode = @"";
  9202. __block NSString *cardType = @"";
  9203. __block NSString *cardNumber = @"";
  9204. __block NSString *securityCode = @"";
  9205. __block NSString *month = @"";
  9206. __block NSString *year = @"";
  9207. __block NSString *city = @"";
  9208. __block NSString *state = @"";
  9209. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9210. payType = [self textAtColumn:0 statement:stmt];
  9211. firstName = [self textAtColumn:1 statement:stmt];
  9212. lastName = [self textAtColumn:2 statement:stmt];
  9213. addr1 = [self textAtColumn:3 statement:stmt];
  9214. addr2 = [self textAtColumn:4 statement:stmt];
  9215. zipcode = [self textAtColumn:5 statement:stmt];
  9216. cardType = [self textAtColumn:6 statement:stmt];
  9217. cardNumber = [self textAtColumn:7 statement:stmt];
  9218. securityCode = [self textAtColumn:8 statement:stmt];
  9219. month = [self textAtColumn:9 statement:stmt];
  9220. year = [self textAtColumn:10 statement:stmt];
  9221. city = [self textAtColumn:11 statement:stmt];
  9222. state = [self textAtColumn:12 statement:stmt];
  9223. }];
  9224. NSString *required = @"true";
  9225. // setting
  9226. NSDictionary *setting = params[@"setting"];
  9227. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9228. if ([requiredNumber integerValue]) {
  9229. required = @"true";
  9230. } else {
  9231. required = @"false";
  9232. }
  9233. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9234. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9235. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9236. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9237. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9238. NSString *type = [self textAtColumn:1 statement:stmt];
  9239. NSMutableDictionary *val = @{
  9240. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9241. @"value" : type,
  9242. @"value_id" : type_id
  9243. }.mutableCopy;
  9244. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9245. NSDictionary *sub_item =nil;
  9246. #ifdef BUILD_CONTRAST
  9247. sub_item = @{
  9248. @"count" : @(3),
  9249. @"item_0" : @{
  9250. @"aname" : @"Number",
  9251. @"control" : @"edit",
  9252. @"keyboard" : @"int",
  9253. @"length" : @"16",
  9254. @"name" : @"credit_card_number",
  9255. @"required" : @"false",
  9256. @"value" : cardNumber
  9257. },
  9258. @"item_1" : @{
  9259. @"aname" : @"Expiration Date",
  9260. @"control" : @"monthpicker",
  9261. @"name" : @"credit_card_expiration",
  9262. @"required" : @"false",
  9263. @"type" : @"date",
  9264. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9265. },
  9266. @"item_2" : @{
  9267. @"aname" : @"Security Code",
  9268. @"control" : @"edit",
  9269. @"keyboard" : @"int",
  9270. @"length" : @"4",
  9271. @"name" : @"credit_card_security_code",
  9272. @"required" : @"false",
  9273. @"value" : securityCode
  9274. },
  9275. };
  9276. #else
  9277. sub_item= @{
  9278. @"count" : @(3),
  9279. @"item_0" : @{
  9280. @"aname" : @"choose",
  9281. @"control" : @"multi_action",
  9282. @"count" : @(1),
  9283. @"item_0" : @{
  9284. @"aname" : @"Same as customer",
  9285. @"key_map" : @{
  9286. @"credit_card_address1" : @"customer_address1",
  9287. @"credit_card_address2" : @"customer_address2",
  9288. @"credit_card_city" : @"customer_city",
  9289. @"credit_card_first_name" : @"customer_first_name",
  9290. @"credit_card_last_name" : @"customer_last_name",
  9291. @"credit_card_state" : @"customer_state",
  9292. @"credit_card_zipcode" : @"customer_zipcode"
  9293. },
  9294. @"type" : @"pull"
  9295. }
  9296. },
  9297. @"item_1" : @{
  9298. @"aname" : @"",
  9299. @"color" : @"red",
  9300. @"control" : @"text",
  9301. @"name" : @"",
  9302. @"value" : @"USA Credit cards only"
  9303. },
  9304. @"item_2" : @{
  9305. @"aname" : @"Fill",
  9306. @"cadedate" : @{
  9307. @"count" : @(2),
  9308. @"val_0" : @{
  9309. @"check" : @(1),
  9310. @"sub_item" : @{
  9311. @"count" : @(11),
  9312. @"item_0" : @{
  9313. @"aname" : @"Type",
  9314. @"cadedate" : @{
  9315. @"count" : @(2),
  9316. @"val_0" : @{
  9317. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9318. @"value" : @"VISA",
  9319. @"value_id" : @"VISA"/*@(0)*/
  9320. },
  9321. @"val_1" : @{
  9322. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9323. @"value" : @"MASTER CARD",
  9324. @"value_id" : @"MASTER CARD"/*@(1)*/
  9325. }
  9326. },
  9327. @"control" : @"enum",
  9328. @"name" : @"credit_card_type",
  9329. @"required" : @"true",
  9330. @"single_select" : @"true"
  9331. },
  9332. @"item_1" : @{
  9333. @"aname" : @"Number",
  9334. @"control" : @"edit",
  9335. @"keyboard" : @"int",
  9336. @"length" : @"16",
  9337. @"name" : @"credit_card_number",
  9338. @"required" : @"true",
  9339. @"value" : cardNumber
  9340. },
  9341. @"item_10" : @{
  9342. @"aname" : @"State",
  9343. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9344. @"control" : @"enum",
  9345. @"enum" : @"true",
  9346. @"name" : @"credit_card_state",
  9347. @"required" : @"true",
  9348. @"single_select" : @"true"
  9349. },
  9350. @"item_2" : @{
  9351. @"aname" : @"Expiration Date",
  9352. @"control" : @"monthpicker",
  9353. @"name" : @"credit_card_expiration",
  9354. @"required" : @"true",
  9355. @"type" : @"date",
  9356. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9357. },
  9358. @"item_3" : @{
  9359. @"aname" : @"Security Code",
  9360. @"control" : @"edit",
  9361. @"keyboard" : @"int",
  9362. @"length" : @"3",
  9363. @"name" : @"credit_card_security_code",
  9364. @"required" : @"true",
  9365. @"value" : securityCode
  9366. },
  9367. @"item_4" : @{
  9368. @"aname" : @"First Name",
  9369. @"control" : @"edit",
  9370. @"keyboard" : @"text",
  9371. @"name" : @"credit_card_first_name",
  9372. @"required" : @"true",
  9373. @"value" : firstName
  9374. },
  9375. @"item_5" : @{
  9376. @"aname" : @"Last Name",
  9377. @"control" : @"edit",
  9378. @"keyboard" : @"text",
  9379. @"name" : @"credit_card_last_name",
  9380. @"required" : @"true",
  9381. @"value" : lastName
  9382. },
  9383. @"item_6" : @{
  9384. @"aname" : @"Address 1",
  9385. @"control" : @"edit",
  9386. @"keyboard" : @"text",
  9387. @"name" : @"credit_card_address1",
  9388. @"required" : @"true",
  9389. @"value" : addr1
  9390. },
  9391. @"item_7" : @{
  9392. @"aname" : @"Address 2",
  9393. @"control" : @"edit",
  9394. @"keyboard" : @"text",
  9395. @"name" : @"credit_card_address2",
  9396. @"value" : addr2
  9397. },
  9398. @"item_8" : @{
  9399. @"aname" : @"zip code",
  9400. @"control" : @"edit",
  9401. @"keyboard" : @"text",
  9402. @"name" : @"credit_card_zipcode",
  9403. @"required" : @"true",
  9404. @"value" : zipcode
  9405. },
  9406. @"item_9" : @{
  9407. @"aname" : @"City",
  9408. @"control" : @"edit",
  9409. @"keyboard" : @"text",
  9410. @"name" : @"credit_card_city",
  9411. @"required" : @"true",
  9412. @"value" : city
  9413. }
  9414. },
  9415. @"value" : @"Fill Now",
  9416. @"value_id" : @""
  9417. },
  9418. @"val_1" : @{
  9419. @"check" : @(0),
  9420. @"value" : @"Fill Later",
  9421. @"value_id" : @""
  9422. }
  9423. },
  9424. @"control" : @"enum",
  9425. @"name" : @"",
  9426. @"single_select" : @"true"
  9427. }
  9428. };
  9429. #endif;
  9430. [val setObject:sub_item forKey:@"sub_item"];
  9431. }
  9432. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9433. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9434. }];
  9435. // "section_2"
  9436. NSMutableDictionary *dic = @{
  9437. @"count" : @(1),
  9438. @"item_0" : @{
  9439. @"aname" : @"Payment",
  9440. @"required" : required,
  9441. @"cadedate" : cadedate,
  9442. // @{
  9443. // @"count" : @(6),
  9444. // @"val_3" : @{
  9445. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9446. // @"value" : @"Check",
  9447. // @"value_id" : @"Check"
  9448. // },
  9449. // @"val_2" : @{
  9450. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9451. // @"value" : @"Cash",
  9452. // @"value_id" : @"Cash"
  9453. // },
  9454. // @"val_1" : @{
  9455. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9456. // @"value" : @"NET 60",
  9457. // @"value_id" : @"NET 30"
  9458. // },
  9459. // @"val_4" : @{
  9460. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9461. // @"value" : @"Wire Transfer",
  9462. // @"value_id" : @"Wire Transfer"
  9463. // },
  9464. // @"val_0" : @{
  9465. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9466. // @"sub_item" : @{
  9467. // @"count" : @(3),
  9468. // @"item_0" : @{
  9469. // @"aname" : @"choose",
  9470. // @"control" : @"multi_action",
  9471. // @"count" : @(1),
  9472. // @"item_0" : @{
  9473. // @"aname" : @"Same as customer",
  9474. // @"key_map" : @{
  9475. // @"credit_card_address1" : @"customer_address1",
  9476. // @"credit_card_address2" : @"customer_address2",
  9477. // @"credit_card_city" : @"customer_city",
  9478. // @"credit_card_first_name" : @"customer_first_name",
  9479. // @"credit_card_last_name" : @"customer_last_name",
  9480. // @"credit_card_state" : @"customer_state",
  9481. // @"credit_card_zipcode" : @"customer_zipcode"
  9482. // },
  9483. // @"type" : @"pull"
  9484. // }
  9485. // },
  9486. // @"item_1" : @{
  9487. // @"aname" : @"",
  9488. // @"color" : @"red",
  9489. // @"control" : @"text",
  9490. // @"name" : @"",
  9491. // @"value" : @"USA Credit cards only"
  9492. // },
  9493. // @"item_2" : @{
  9494. // @"aname" : @"Fill",
  9495. // @"cadedate" : @{
  9496. // @"count" : @(2),
  9497. // @"val_0" : @{
  9498. // @"check" : @(1),
  9499. // @"sub_item" : @{
  9500. // @"count" : @(11),
  9501. // @"item_0" : @{
  9502. // @"aname" : @"Type",
  9503. // @"cadedate" : @{
  9504. // @"count" : @(2),
  9505. // @"val_0" : @{
  9506. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9507. // @"value" : @"VISA",
  9508. // @"value_id" : @(0)
  9509. // },
  9510. // @"val_1" : @{
  9511. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9512. // @"value" : @"MASTER CARD",
  9513. // @"value_id" : @(1)
  9514. // }
  9515. // },
  9516. // @"control" : @"enum",
  9517. // @"name" : @"credit_card_type",
  9518. // @"required" : @"true",
  9519. // @"single_select" : @"true"
  9520. // },
  9521. // @"item_1" : @{
  9522. // @"aname" : @"Number",
  9523. // @"control" : @"edit",
  9524. // @"keyboard" : @"int",
  9525. // @"length" : @"16",
  9526. // @"name" : @"credit_card_number",
  9527. // @"required" : @"true",
  9528. // @"value" : cardNumber
  9529. // },
  9530. // @"item_10" : @{
  9531. // @"aname" : @"State",
  9532. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9533. // @"control" : @"enum",
  9534. // @"enum" : @"true",
  9535. // @"name" : @"credit_card_state",
  9536. // @"required" : @"true",
  9537. // @"single_select" : @"true"
  9538. // },
  9539. // @"item_2" : @{
  9540. // @"aname" : @"Expiration Date",
  9541. // @"control" : @"monthpicker",
  9542. // @"name" : @"credit_card_expiration",
  9543. // @"required" : @"true",
  9544. // @"type" : @"date",
  9545. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9546. // },
  9547. // @"item_3" : @{
  9548. // @"aname" : @"Security Code",
  9549. // @"control" : @"edit",
  9550. // @"keyboard" : @"int",
  9551. // @"length" : @"3",
  9552. // @"name" : @"credit_card_security_code",
  9553. // @"required" : @"true",
  9554. // @"value" : securityCode
  9555. // },
  9556. // @"item_4" : @{
  9557. // @"aname" : @"First Name",
  9558. // @"control" : @"edit",
  9559. // @"keyboard" : @"text",
  9560. // @"name" : @"credit_card_first_name",
  9561. // @"required" : @"true",
  9562. // @"value" : firstName
  9563. // },
  9564. // @"item_5" : @{
  9565. // @"aname" : @"Last Name",
  9566. // @"control" : @"edit",
  9567. // @"keyboard" : @"text",
  9568. // @"name" : @"credit_card_last_name",
  9569. // @"required" : @"true",
  9570. // @"value" : lastName
  9571. // },
  9572. // @"item_6" : @{
  9573. // @"aname" : @"Address 1",
  9574. // @"control" : @"edit",
  9575. // @"keyboard" : @"text",
  9576. // @"name" : @"credit_card_address1",
  9577. // @"required" : @"true",
  9578. // @"value" : addr1
  9579. // },
  9580. // @"item_7" : @{
  9581. // @"aname" : @"Address 2",
  9582. // @"control" : @"edit",
  9583. // @"keyboard" : @"text",
  9584. // @"name" : @"credit_card_address2",
  9585. // @"value" : addr2
  9586. // },
  9587. // @"item_8" : @{
  9588. // @"aname" : @"zip code",
  9589. // @"control" : @"edit",
  9590. // @"keyboard" : @"text",
  9591. // @"name" : @"credit_card_zipcode",
  9592. // @"required" : @"true",
  9593. // @"value" : zipcode
  9594. // },
  9595. // @"item_9" : @{
  9596. // @"aname" : @"City",
  9597. // @"control" : @"edit",
  9598. // @"keyboard" : @"text",
  9599. // @"name" : @"credit_card_city",
  9600. // @"required" : @"true",
  9601. // @"value" : city
  9602. // }
  9603. // },
  9604. // @"value" : @"Fill Now",
  9605. // @"value_id" : @""
  9606. // },
  9607. // @"val_1" : @{
  9608. // @"check" : @(0),
  9609. // @"value" : @"Fill Later",
  9610. // @"value_id" : @""
  9611. // }
  9612. // },
  9613. // @"control" : @"enum",
  9614. // @"name" : @"",
  9615. // @"single_select" : @"true"
  9616. // }
  9617. // },
  9618. // @"value" : @"Visa/Master",
  9619. // @"value_id" : @"Credit Card"
  9620. // },
  9621. // @"val_5" : @{
  9622. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9623. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9624. // @"value_id" : @"FOLLOW EXISTING"
  9625. // }
  9626. // },
  9627. @"control" : @"enum",
  9628. @"name" : @"paymentType",
  9629. @"single_select" : @"true"
  9630. },
  9631. @"title" : @"Payment Information"
  9632. }.mutableCopy;
  9633. NSNumber *hide = setting[@"PaymentInformationHide"];
  9634. [dic setValue:hide forKey:@"hide"];
  9635. return dic;
  9636. }
  9637. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9638. // params
  9639. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9640. orderCode = [self translateSingleQuote:orderCode];
  9641. // 缺货检查
  9642. 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];
  9643. __block BOOL outOfStock = NO;
  9644. sqlite3 *database = db;
  9645. if (!db) {
  9646. database = [iSalesDB get_db];
  9647. }
  9648. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9649. int availability = sqlite3_column_int(stmt, 0);
  9650. int item_qty = sqlite3_column_int(stmt, 1);
  9651. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9652. }];
  9653. if (!db) {
  9654. [iSalesDB close_db:database];
  9655. }
  9656. return outOfStock;
  9657. }
  9658. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9659. {
  9660. assert(params[@"user"]!=nil);
  9661. assert(params[@"contact_id"]!=nil);
  9662. assert(params[@"can_create_backorder"]!=nil);
  9663. sqlite3 *db = [iSalesDB get_db];
  9664. // params
  9665. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9666. orderCode = [self translateSingleQuote:orderCode];
  9667. // 缺货检查
  9668. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9669. if (![params[@"can_create_backorder"] boolValue]) {
  9670. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9671. if (out_of_stock) {
  9672. [iSalesDB close_db:db];
  9673. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9674. [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"];
  9675. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9676. return [RAConvertor dict2data:resultDic];
  9677. }
  9678. }
  9679. // UISetting
  9680. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9681. NSString *cachefolder = [paths objectAtIndex:0];
  9682. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9683. NSData* json =nil;
  9684. json=[NSData dataWithContentsOfFile:img_cache];
  9685. NSError *error=nil;
  9686. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9687. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9688. [params setObject:setting forKey:@"setting"];
  9689. int section_count = 0;
  9690. // 0 Order Type 1 Shipping Method 2 Payment Information
  9691. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9692. // 0 Order Type
  9693. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9694. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9695. [ret setValue:order_type_dic forKey:key0];
  9696. // 1 Shipping Method
  9697. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9698. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9699. [ret setValue:shipping_method_dic forKey:key1];
  9700. // 2 Payment Information
  9701. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9702. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9703. [ret setValue:payment_info_dic forKey:key2];
  9704. // 3 Customer
  9705. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9706. NSDictionary *customer_dic = [self customerDic:params db:db];
  9707. [ret setValue:customer_dic forKey:key3];
  9708. // 4 Ship To
  9709. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9710. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9711. [ret setValue:ship_to_dic forKey:key4];
  9712. // 5 Ship From
  9713. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9714. if (![shipFromDisable integerValue]) {
  9715. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9716. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9717. [ret setValue:ship_from_dic forKey:key5];
  9718. }
  9719. // 6 Freight Bill To
  9720. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9721. if (![freightBillToDisable integerValue]) {
  9722. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9723. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9724. [ret setValue:freight_bill_to forKey:key6];
  9725. }
  9726. // 7 Merchandise Bill To
  9727. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9728. if (![merchandiseBillToDisable integerValue]) {
  9729. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9730. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9731. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9732. }
  9733. // 8 Return To
  9734. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9735. if (![returnToDisable integerValue]) {
  9736. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9737. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9738. [ret setValue:return_to_dic forKey:key8];
  9739. }
  9740. // 9 Model Information
  9741. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9742. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9743. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9744. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9745. [ret setValue:model_info_dic forKey:key9];
  9746. // 10 Remarks Content
  9747. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9748. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9749. [ret setValue:remarks_content_dic forKey:key10];
  9750. // 11 Order Total
  9751. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9752. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9753. [ret setValue:order_total_dic forKey:key11];
  9754. // 12 Signature
  9755. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9756. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9757. [ret setValue:sign_dic forKey:key12];
  9758. [ret setValue:@(section_count) forKey:@"section_count"];
  9759. [iSalesDB close_db:db];
  9760. return [RAConvertor dict2data:ret];
  9761. // return nil;
  9762. }
  9763. #pragma mark addr editor
  9764. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9765. NSMutableDictionary *new_item = [item mutableCopy];
  9766. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9767. return new_item;
  9768. }
  9769. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9770. {
  9771. // "is_subaction" = true;
  9772. // orderCode = MOB1608240002;
  9773. // password = 123456;
  9774. // "subaction_tag" = 1;
  9775. // user = EvanK;
  9776. // {
  9777. // "is_subaction" = true;
  9778. // orderCode = MOB1608240002;
  9779. // password = 123456;
  9780. // "refresh_trigger" = zipcode;
  9781. // "subaction_tag" = 1;
  9782. // user = EvanK;
  9783. // }
  9784. NSString *country_code = nil;
  9785. NSString *zipCode = nil;
  9786. NSString *stateCode = nil;
  9787. NSString *city = nil;
  9788. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9789. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9790. NSString *code_id = params[@"country"];
  9791. country_code = [self countryCodeByid:code_id];
  9792. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9793. NSString *zip_code = params[@"zipcode"];
  9794. // 剔除全部为空格
  9795. int spaceCount = 0;
  9796. for (int i = 0; i < zip_code.length; i++) {
  9797. if ([zip_code characterAtIndex:i] == ' ') {
  9798. spaceCount++;
  9799. }
  9800. }
  9801. if (spaceCount == zip_code.length) {
  9802. zip_code = @"";
  9803. }
  9804. zipCode = zip_code;
  9805. if (zipCode.length > 0) {
  9806. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9807. country_code = [dic valueForKey:@"country_code"];
  9808. if (!country_code) {
  9809. // country_code = @"US";
  9810. NSString *code_id = params[@"country"];
  9811. country_code = [self countryCodeByid:code_id];
  9812. }
  9813. stateCode = [dic valueForKey:@"state_code"];
  9814. if(!stateCode.length) {
  9815. stateCode = params[@"state"];
  9816. }
  9817. city = [dic valueForKey:@"city"];
  9818. if (!city.length) {
  9819. city = params[@"city"];
  9820. }
  9821. // zip code
  9822. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9823. // [zipDic setValue:zipCode forKey:@"value"];
  9824. // [section_0 setValue:zipDic forKey:@"item_11"];
  9825. } else {
  9826. NSString *code_id = params[@"country"];
  9827. country_code = [self countryCodeByid:code_id];
  9828. stateCode = params[@"state"];
  9829. city = params[@"city"];
  9830. }
  9831. }
  9832. }
  9833. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9834. // [ret removeObjectForKey:@"up_params"];
  9835. [ret setObject:@"New Address" forKey:@"title"];
  9836. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9837. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9838. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9839. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9840. // [country_dic removeObjectForKey:@"refresh"];
  9841. // [country_dic removeObjectForKey:@"restore"];
  9842. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9843. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9844. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9845. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9846. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9847. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9848. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9849. // [zip_code_dic removeObjectForKey:@"refresh"];
  9850. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9851. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9852. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9853. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9854. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9855. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9856. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9857. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9858. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9859. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9860. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9861. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9862. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9863. // country
  9864. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9865. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9866. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9867. // state
  9868. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9869. // NSDictionary *tmpDic = @{
  9870. // @"value" : @"Other",
  9871. // @"value_id" : @"",
  9872. // @"check" : [NSNumber numberWithInteger:0]
  9873. // };
  9874. //
  9875. // for (int i = 0; i < allState.allKeys.count; i++) {
  9876. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9877. //
  9878. // NSDictionary *tmp = allState[key];
  9879. // [allState setValue:tmpDic forKey:key];
  9880. // tmpDic = tmp;
  9881. // }
  9882. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9883. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9884. [ret setValue:new_section_0 forKey:@"section_0"];
  9885. return [RAConvertor dict2data:ret];
  9886. }
  9887. #pragma mark save addr
  9888. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9889. {
  9890. // NSString *companyName = [self valueInParams:params key:@"company"];
  9891. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9892. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9893. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9894. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9895. // NSString *countryId = [self valueInParams:params key:@"country"];
  9896. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9897. // NSString *city = [self valueInParams:params key:@"city"];
  9898. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9899. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9900. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9901. // NSString *phone = [self valueInParams:params key:@"phone"];
  9902. // NSString *fax = [self valueInParams:params key:@"fax"];
  9903. // NSString *email = [self valueInParams:params key:@"email"];
  9904. return [self offline_saveContact:params update:NO isCustomer:NO];
  9905. }
  9906. #pragma mark cancel order
  9907. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9908. {
  9909. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9910. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9911. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9912. if (order_id.length) {
  9913. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9914. }
  9915. int ret = [iSalesDB execSql:sql];
  9916. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9917. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9918. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9919. // [dic setValue:@"160409" forKey:@"min_ver"];
  9920. return [RAConvertor dict2data:dic];
  9921. }
  9922. #pragma mark sign order
  9923. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9924. {
  9925. //参考 offline_saveBusinesscard
  9926. DebugLog(@"sign order params: %@",params);
  9927. // orderCode = MOB1608240002;
  9928. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9929. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9930. orderCode = [self translateSingleQuote:orderCode];
  9931. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9932. picPath = [self translateSingleQuote:picPath];
  9933. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9934. int ret = [iSalesDB execSql:sql];
  9935. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9936. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9937. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9938. // [dic setValue:@"160409" forKey:@"min_ver"];
  9939. return [RAConvertor dict2data:dic];
  9940. }
  9941. #pragma mark save order
  9942. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9943. NSString *ret = [self valueInParams:params key:key];
  9944. if (translate) {
  9945. ret = [self translateSingleQuote:ret];
  9946. }
  9947. return ret;
  9948. }
  9949. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9950. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9951. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9952. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9953. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9954. if (schedule_date.length==0 ) {
  9955. schedule_date = @"";
  9956. } else {
  9957. schedule_date = [self rchangeDateFormate:schedule_date];
  9958. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9959. }
  9960. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9961. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9962. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9963. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9964. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9965. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9966. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9967. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9968. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9969. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9970. if (![total_price isEqualToString:@""]) {
  9971. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9972. } else {
  9973. total_price = @"";
  9974. }
  9975. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9976. if ([paymentsAndCredits isEqualToString:@""]) {
  9977. paymentsAndCredits = @"";
  9978. } else {
  9979. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9980. }
  9981. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9982. if ([handling_fee_value isEqualToString:@""]) {
  9983. handling_fee_value = @"";
  9984. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9985. handling_fee_value = @"";
  9986. } else {
  9987. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9988. }
  9989. NSString *handling_fee_placeholder = handling_fee_value;
  9990. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9991. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9992. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9993. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9994. NSString *lift_gate_placeholder = @"";
  9995. if ([lift_gate_value isEqualToString:@""]) {
  9996. lift_gate_placeholder = @"";
  9997. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9998. lift_gate_placeholder = @"";
  9999. } else {
  10000. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  10001. }
  10002. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  10003. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  10004. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  10005. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  10006. 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];
  10007. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  10008. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  10009. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  10010. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  10011. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  10012. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  10013. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  10014. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  10015. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  10016. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  10017. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  10018. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  10019. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  10020. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  10021. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  10022. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  10023. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  10024. BOOL number_nil = [credit_card_number isEqualToString:@""];
  10025. if (!number_nil) {
  10026. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  10027. }
  10028. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  10029. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  10030. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  10031. if (!security_code_nil) {
  10032. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  10033. }
  10034. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  10035. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  10036. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  10037. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  10038. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  10039. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  10040. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  10041. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  10042. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  10043. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  10044. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  10045. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  10046. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  10047. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  10048. NSString *contact_id = customer_cid;
  10049. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  10050. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  10051. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  10052. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  10053. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  10054. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  10055. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  10056. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  10057. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  10058. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  10059. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  10060. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  10061. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  10062. //
  10063. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  10064. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  10065. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  10066. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  10067. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  10068. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  10069. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  10070. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  10071. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  10072. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  10073. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  10074. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  10075. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  10076. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  10077. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  10078. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  10079. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  10080. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  10081. 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];
  10082. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  10083. if (receive_cid.length) {
  10084. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  10085. }
  10086. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  10087. if (receive_name.length) {
  10088. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  10089. }
  10090. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  10091. if (receive_ext.length) {
  10092. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  10093. }
  10094. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  10095. if (receive_contact.length) {
  10096. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  10097. }
  10098. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  10099. if (receive_phone.length) {
  10100. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  10101. }
  10102. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  10103. if (receive_email.length) {
  10104. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  10105. }
  10106. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  10107. if (receive_fax.length) {
  10108. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  10109. }
  10110. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  10111. if (sender_cid.length) {
  10112. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  10113. }
  10114. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  10115. if (sender_name.length) {
  10116. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  10117. }
  10118. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  10119. if (sender_ext.length) {
  10120. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  10121. }
  10122. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  10123. if(sender_contact.length) {
  10124. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  10125. }
  10126. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  10127. if (sender_phone.length) {
  10128. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  10129. }
  10130. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  10131. if (sender_fax.length) {
  10132. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  10133. }
  10134. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  10135. if (sender_email.length) {
  10136. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  10137. }
  10138. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  10139. if (shipping_billto_cid.length) {
  10140. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  10141. }
  10142. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  10143. if (shipping_billto_name.length) {
  10144. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  10145. }
  10146. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  10147. if (shipping_billto_ext.length) {
  10148. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  10149. }
  10150. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  10151. if (shipping_billto_contact.length) {
  10152. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  10153. }
  10154. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  10155. if (shipping_billto_phone.length) {
  10156. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  10157. }
  10158. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  10159. if (shipping_billto_fax.length) {
  10160. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  10161. }
  10162. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  10163. if (shipping_billto_email.length) {
  10164. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  10165. }
  10166. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  10167. if (billing_cid.length) {
  10168. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  10169. }
  10170. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  10171. if (billing_name.length) {
  10172. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  10173. }
  10174. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  10175. if (billing_ext.length) {
  10176. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  10177. }
  10178. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  10179. if (billing_contact.length) {
  10180. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  10181. }
  10182. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  10183. if (billing_phone.length) {
  10184. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  10185. }
  10186. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  10187. if (billing_fax.length) {
  10188. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  10189. }
  10190. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  10191. if (billing_email.length) {
  10192. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  10193. }
  10194. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  10195. if (returnto_cid.length) {
  10196. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  10197. }
  10198. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10199. if (returnto_name.length) {
  10200. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10201. }
  10202. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10203. if (returnto_ext.length) {
  10204. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10205. }
  10206. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10207. if (returnto_contact.length) {
  10208. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10209. }
  10210. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10211. if (returnto_phone.length) {
  10212. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10213. }
  10214. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10215. if (returnto_fax.length) {
  10216. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10217. }
  10218. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10219. if (returnto_email.length) {
  10220. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10221. }
  10222. NSString *order_status = @"status = 1,";
  10223. if (submit) {
  10224. order_status = @"status = -11,";
  10225. }
  10226. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10227. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10228. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10229. NSString *sync_sql = @"";
  10230. if (submit) {
  10231. param[@"truck_carrier"]=carrier;
  10232. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10233. [param setValue:sales_rep forKey:@"sales_rep"];
  10234. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10235. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10236. }
  10237. 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];
  10238. DebugLog(@"save order contactSql: %@",contactSql);
  10239. DebugLog(@"save order orderSql: %@",orderSql);
  10240. // int contact_ret = [iSalesDB execSql:contactSql];
  10241. int order_ret = [iSalesDB execSql:orderSql];
  10242. int ret = order_ret;
  10243. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10244. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10245. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10246. // [dic setValue:@"160409" forKey:@"min_ver"];
  10247. [dic setObject:so_id forKey:@"so#"];
  10248. return [RAConvertor dict2data:dic];
  10249. }
  10250. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10251. {
  10252. // id foo = nil;
  10253. // NSMutableArray *a = @[].mutableCopy;
  10254. // [a addObject:foo];
  10255. return [self saveorder:param submit:NO];
  10256. }
  10257. #pragma mark add to cart by name
  10258. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10259. {
  10260. NSString *orderCode = [params objectForKey:@"orderCode"];
  10261. NSDictionary *newParams = @{
  10262. @"product_id" : params[@"product_id_string"],
  10263. @"orderCode" : orderCode,
  10264. @"can_create_backorder":params[@"can_create_backorder"]
  10265. };
  10266. return [self offline_add2cart:[newParams mutableCopy]];
  10267. }
  10268. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10269. {
  10270. NSString *orderCode = [params objectForKey:@"orderCode"];
  10271. NSString *upccode = [params objectForKey:@"upc_code"];
  10272. // product_name = [self translateSingleQuote:product_name];
  10273. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10274. // bool search_upc = [params[@"search_upc"] boolValue];
  10275. sqlite3 *db = [iSalesDB get_db];
  10276. __block NSMutableString *product_id_string = [NSMutableString string];
  10277. // NSString *name = [product_name_array objectAtIndex:i];
  10278. NSString *sql =nil;
  10279. 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];
  10280. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10281. int product_id = sqlite3_column_int(stmt, 0);
  10282. [product_id_string appendFormat:@"%d",product_id];
  10283. }];
  10284. if (!orderCode) {
  10285. orderCode = @"";
  10286. }
  10287. NSDictionary *newParams = @{
  10288. @"product_id" : product_id_string,
  10289. @"orderCode" : orderCode,
  10290. @"can_create_backorder":params[@"can_create_backorder"]
  10291. };
  10292. [iSalesDB close_db:db];
  10293. return [self offline_add2cart:[newParams mutableCopy]];
  10294. }
  10295. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10296. {
  10297. NSString *orderCode = [params objectForKey:@"orderCode"];
  10298. NSString *product_name = [params objectForKey:@"product_name"];
  10299. product_name = [self translateSingleQuote:product_name];
  10300. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10301. bool search_upc = [params[@"search_upc"] boolValue];
  10302. sqlite3 *db = [iSalesDB get_db];
  10303. __block NSMutableString *product_id_string = [NSMutableString string];
  10304. for (int i = 0; i < product_name_array.count; i++) {
  10305. NSString *name = [product_name_array objectAtIndex:i];
  10306. NSString *sql =nil;
  10307. if(search_upc)
  10308. 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];
  10309. else
  10310. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10311. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10312. int product_id = sqlite3_column_int(stmt, 0);
  10313. if (i == product_name_array.count - 1) {
  10314. [product_id_string appendFormat:@"%d",product_id];
  10315. } else {
  10316. [product_id_string appendFormat:@"%d,",product_id];
  10317. }
  10318. }];
  10319. }
  10320. if (!orderCode) {
  10321. orderCode = @"";
  10322. }
  10323. NSDictionary *newParams = @{
  10324. @"product_id" : product_id_string,
  10325. @"orderCode" : orderCode,
  10326. @"can_create_backorder":params[@"can_create_backorder"]
  10327. };
  10328. [iSalesDB close_db:db];
  10329. return [self offline_add2cart:[newParams mutableCopy]];
  10330. }
  10331. #pragma mark reset order
  10332. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10333. {
  10334. // UIApplication * app = [UIApplication sharedApplication];
  10335. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10336. [iSalesDB disable_trigger];
  10337. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10338. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10339. [iSalesDB enable_trigger];
  10340. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10341. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10342. return [RAConvertor dict2data:dic];
  10343. }
  10344. #pragma mark submit order
  10345. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10346. {
  10347. return [self saveorder:params submit:YES];
  10348. }
  10349. #pragma mark copy order
  10350. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10351. {
  10352. NSMutableDictionary *ret = @{}.mutableCopy;
  10353. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10354. sqlite3 *db = [iSalesDB get_db];
  10355. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10356. // 首先查看联系人是否active
  10357. 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];
  10358. __block int customer_is_active = 1;
  10359. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10360. customer_is_active = sqlite3_column_int(stmt, 0);
  10361. }];
  10362. if (!customer_is_active) {
  10363. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10364. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10365. [iSalesDB close_db:db];
  10366. return [RAConvertor dict2data:ret];
  10367. }
  10368. // new order
  10369. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10370. NSString *new_order_code = [self get_offline_soid:db];
  10371. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10372. user = [self translateSingleQuote:user];
  10373. 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
  10374. __block int result = 1;
  10375. result = [iSalesDB execSql:insert_order_sql db:db];
  10376. if (!result) {
  10377. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10378. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10379. [iSalesDB close_db:db];
  10380. return [RAConvertor dict2data:ret];
  10381. }
  10382. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10383. // __block NSString *product_id = @"";
  10384. __weak typeof(self) weakSelf = self;
  10385. 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];
  10386. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10387. int is_active = sqlite3_column_int(stmt, 1);
  10388. if (is_active) {
  10389. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10390. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10391. 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];
  10392. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10393. }
  10394. }];
  10395. // product_id = [product_id substringFromIndex:1];
  10396. //
  10397. // [self offline_add2cart:@{}.mutableCopy];
  10398. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10399. [iSalesDB close_db:db];
  10400. if (result) {
  10401. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10402. } else {
  10403. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10404. }
  10405. [ret setObject:new_order_code forKey:@"so_id"];
  10406. return [RAConvertor dict2data:ret];
  10407. }
  10408. #pragma mark move wish list to cart
  10409. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10410. assert(params[@"can_create_backorder"]!=nil);
  10411. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10412. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10413. NSString *collectId = params[@"collectId"];
  10414. 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];
  10415. __block NSString *product_id = @"";
  10416. __block NSString *qty = @"";
  10417. __block int number_of_outOfStock = 0;
  10418. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10419. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10420. int productId = sqlite3_column_int(stmt, 0);
  10421. int item_qty = sqlite3_column_int(stmt, 1);
  10422. int availability = sqlite3_column_int(stmt, 2);
  10423. int _id = sqlite3_column_int(stmt, 3);
  10424. if (![params[@"can_create_backorder"] boolValue]) {
  10425. // 库存小于购买量为缺货
  10426. if (availability >= item_qty) {
  10427. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10428. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10429. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10430. } else {
  10431. number_of_outOfStock++;
  10432. }
  10433. } else {
  10434. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10435. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10436. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10437. }
  10438. }];
  10439. NSMutableDictionary *retDic = nil;
  10440. if (![params[@"can_create_backorder"] boolValue]) {
  10441. if (delete_collectId.count == 0) {
  10442. retDic = [NSMutableDictionary dictionary];
  10443. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10444. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10445. return [RAConvertor dict2data:retDic];
  10446. }
  10447. }
  10448. if (product_id.length > 1) {
  10449. product_id = [product_id substringFromIndex:1];
  10450. }
  10451. if (qty.length > 1) {
  10452. qty = [qty substringFromIndex:1];
  10453. }
  10454. NSString *orderCode = params[@"orderCode"];
  10455. if (!orderCode) {
  10456. orderCode = @"";
  10457. }
  10458. NSDictionary *newParams = @{
  10459. @"product_id" : product_id,
  10460. @"orderCode" : orderCode,
  10461. @"qty" : qty,
  10462. @"can_create_backorder":params[@"can_create_backorder"]
  10463. };
  10464. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10465. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10466. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10467. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10468. NSInteger ret = [wish_return[@"result"] intValue];
  10469. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10470. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10471. }
  10472. if (![params[@"can_create_backorder"] boolValue]) {
  10473. if (number_of_outOfStock > 0) {
  10474. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10475. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10476. }
  10477. }
  10478. return [RAConvertor dict2data:retDic];
  10479. }
  10480. #pragma mark - portfolio
  10481. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10482. // assert(params[@"contact_id"]!=nil);
  10483. assert(params[@"user"]!=nil);
  10484. assert(params[@"can_see_price"]!=nil);
  10485. int sort = [[params valueForKey:@"sort"] intValue];
  10486. int offset = [[params valueForKey:@"offset"] intValue];
  10487. int limit = [[params valueForKey:@"limit"] intValue];
  10488. NSString *orderBy = @"";
  10489. switch (sort) {
  10490. case 0:{
  10491. orderBy = @"p.modify_time desc";
  10492. }
  10493. break;
  10494. case 1:{
  10495. orderBy = @"modify_time asc";
  10496. }
  10497. break;
  10498. case 2:{
  10499. orderBy = @"p.name asc";
  10500. }
  10501. break;
  10502. case 3:{
  10503. orderBy = @"p.name desc";
  10504. }
  10505. break;
  10506. case 4:{
  10507. orderBy = @"p.description asc";
  10508. }
  10509. break;
  10510. case 5: {
  10511. orderBy = @"m.default_category asc";
  10512. }
  10513. default:
  10514. break;
  10515. }
  10516. // 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];
  10517. 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];
  10518. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10519. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10520. sqlite3 *db = [iSalesDB get_db];
  10521. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10522. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10523. int product_id = sqlite3_column_int(stmt, 0);
  10524. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10525. NSString *name = [self textAtColumn:1 statement:stmt];
  10526. NSString *description = [self textAtColumn:2 statement:stmt];
  10527. double price = sqlite3_column_double(stmt, 3);
  10528. double discount = sqlite3_column_double(stmt,4);
  10529. int qty = sqlite3_column_int(stmt, 5);
  10530. int percentage = sqlite3_column_int(stmt, 6);
  10531. int item_id = sqlite3_column_int(stmt, 7);
  10532. // int fashion_id = sqlite3_column_int(stmt, 8);
  10533. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10534. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10535. double percent = sqlite3_column_double(stmt, 11);
  10536. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10537. if ([price_null isEqualToString:@"null"]) {
  10538. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10539. }
  10540. NSMutableDictionary *item = @{
  10541. @"linenotes": line_note,
  10542. @"check": @(1),
  10543. @"product_id": product_id_string,
  10544. @"available_qty": @(qty),
  10545. @"available_percent" : @(percent),
  10546. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10547. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10548. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10549. @"img": img_path,
  10550. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10551. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10552. }.mutableCopy;
  10553. if (percentage) {
  10554. [item removeObjectForKey:@"available_qty"];
  10555. } else {
  10556. [item removeObjectForKey:@"available_percent"];
  10557. }
  10558. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10559. if ([qty_null isEqualToString:@"null"]) {
  10560. [item removeObjectForKey:@"available_qty"];
  10561. }
  10562. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10563. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10564. }];
  10565. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10566. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10567. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10568. }
  10569. [iSalesDB close_db:db];
  10570. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10571. [dic setValue:@"" forKey:@"email_content"];
  10572. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10573. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10574. return [RAConvertor dict2data:dic];
  10575. }
  10576. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10577. __block int qty = 0;
  10578. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10579. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10580. qty = sqlite3_column_int(stmt, 0);
  10581. }];
  10582. return qty;
  10583. }
  10584. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10585. {
  10586. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10587. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10588. // NSMutableDictionary * values = params[@"replaceValue"];
  10589. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10590. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10591. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10592. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10593. NSString *pdf_path = @"";
  10594. if (direct) {
  10595. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10596. } else {
  10597. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10598. }
  10599. NSString *create_user = [self valueInParams:params key:@"user"];
  10600. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10601. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10602. // model info
  10603. // 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];
  10604. // V1.90 more color
  10605. 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];
  10606. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10607. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10608. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10609. sqlite3 *db = [iSalesDB get_db];
  10610. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10611. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10612. int product_id = sqlite3_column_int(stmt, 0);
  10613. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10614. double price = sqlite3_column_double(stmt, 1);
  10615. double discount = sqlite3_column_double(stmt,2);
  10616. int qty = sqlite3_column_int(stmt, 3);
  10617. int percentage = sqlite3_column_int(stmt, 4);
  10618. int item_id = sqlite3_column_int(stmt, 5);
  10619. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10620. double percent = sqlite3_column_double(stmt, 7);
  10621. int more_color = sqlite3_column_int(stmt, 8);
  10622. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10623. /* if ([price_null isEqualToString:@"null"]) {
  10624. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10625. }
  10626. */
  10627. [product_ids_string appendFormat:@"%@,",product_id_string];
  10628. // Regular Price
  10629. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10630. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10631. // QTY
  10632. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10633. if ([qty_null isEqualToString:@"null"]) {
  10634. // 查available
  10635. qty = [self model_QTY:product_id_string db:db];
  10636. }
  10637. if (percentage) {
  10638. qty = qty * percent / 100;
  10639. }
  10640. // Special Price
  10641. if ([price_null isEqualToString:@"null"]) {
  10642. // price = regular price
  10643. price = [regular_price_str doubleValue];
  10644. }
  10645. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10646. if (![discount_null isEqualToString:@"null"]) {
  10647. price = price * (1 - discount / 100.0);
  10648. }
  10649. NSMutableDictionary *item = @{
  10650. @"line_note": line_note,
  10651. @"product_id": product_id_string,
  10652. @"available_qty": @(qty),
  10653. @"more_color":@(more_color),
  10654. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10655. @"regular_price" : regular_price_str,
  10656. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10657. }.mutableCopy;
  10658. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10659. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10660. }];
  10661. [iSalesDB close_db:db];
  10662. if (product_ids_string.length > 0) {
  10663. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10664. }
  10665. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10666. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10667. return [RAConvertor dict2data:resultDictionary];
  10668. }
  10669. NSString *model_info = [RAConvertor dict2string:dic];
  10670. // 创建PDF
  10671. // 在preview情况下保存,则不需要新建了
  10672. if (direct) {
  10673. NSMutableDictionary *tear_sheet_params = params;
  10674. // if (tear_sheet_id) {
  10675. // tear_sheet_params = values;
  10676. // }
  10677. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10678. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10679. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10680. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10681. resultDictionary = pdfInfo.mutableCopy;
  10682. } else { // 创建PDF失败
  10683. return pdfData;
  10684. }
  10685. } else {
  10686. // pdf_path 就是本地路径
  10687. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10688. }
  10689. // 将文件移动到PDF Cache文件夹
  10690. NSString *newPath = [pdf_path lastPathComponent];
  10691. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10692. NSString *cachefolder = [paths objectAtIndex:0];
  10693. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10694. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10695. NSFileManager *fileManager = [NSFileManager defaultManager];
  10696. NSError *error = nil;
  10697. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10698. if (error) { // 移动文件失败
  10699. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10700. return [RAConvertor dict2data:resultDictionary];
  10701. }
  10702. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10703. pdf_path = [newPath lastPathComponent];
  10704. // 保存信息
  10705. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10706. NSString *off_params = [RAConvertor dict2string:params];
  10707. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10708. pdf_path = [self translateSingleQuote:pdf_path];
  10709. create_user = [self translateSingleQuote:create_user];
  10710. tear_note = [self translateSingleQuote:tear_note];
  10711. tear_name = [self translateSingleQuote:tear_name];
  10712. model_info = [self translateSingleQuote:model_info];
  10713. configureParams = [self translateSingleQuote:configureParams];
  10714. off_params = [self translateSingleQuote:off_params];
  10715. 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];
  10716. if (tear_sheet_id) {
  10717. int _id = [tear_sheet_id intValue];
  10718. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10719. }
  10720. int result = [iSalesDB execSql:save_pdf_sql];
  10721. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10722. //
  10723. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10724. if (remove_Item) {
  10725. // portfolioId
  10726. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10727. }
  10728. return [RAConvertor dict2data:resultDictionary];
  10729. }
  10730. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10731. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10732. }
  10733. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10734. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10735. }
  10736. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10737. int offset = [[params valueForKey:@"offset"] intValue];
  10738. int limit = [[params valueForKey:@"limit"] intValue];
  10739. NSString *keyword = [params valueForKey:@"keyWord"];
  10740. NSString *where = @"where is_delete is null or is_delete = 0";
  10741. if (keyword.length) {
  10742. keyword = [self translateSingleQuote:keyword];
  10743. 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];
  10744. }
  10745. 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
  10746. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10747. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10748. NSString *cachefolder = [paths objectAtIndex:0];
  10749. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10750. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10751. NSString *name = [self textAtColumn:0 statement:stmt];
  10752. NSString *note = [self textAtColumn:1 statement:stmt];
  10753. NSString *time = [self textAtColumn:2 statement:stmt];
  10754. NSString *user = [self textAtColumn:3 statement:stmt];
  10755. NSString *path = [self textAtColumn:4 statement:stmt];
  10756. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10757. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10758. path = [pdfFolder stringByAppendingPathComponent:path];
  10759. BOOL bdir=NO;
  10760. NSFileManager* fileManager = [NSFileManager defaultManager];
  10761. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10762. {
  10763. //pdf文件不存在
  10764. path=nil;
  10765. }
  10766. time = [self changeDateTimeFormate:time];
  10767. time = [time stringByAppendingString:@" PST"];
  10768. int sheet_id = sqlite3_column_int(stmt, 5);
  10769. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10770. item[@"tearsheetsId"]=@(sheet_id);
  10771. item[@"pdf_path"]=path;
  10772. item[@"create_time"]=time;
  10773. item[@"create_user"]=user;
  10774. item[@"tear_note"]=note;
  10775. item[@"tear_name"]=name;
  10776. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10777. item[@"model_info"]=model_info;
  10778. item[@"off_params"]=off_params;
  10779. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10780. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10781. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10782. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10783. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10784. }];
  10785. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10786. return [RAConvertor dict2data:dic];
  10787. }
  10788. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10789. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10790. // NSString *user = [params objectForKey:@"user"];
  10791. // 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];
  10792. 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];
  10793. int result = [iSalesDB execSql:sql];
  10794. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10795. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10796. if (result == RESULT_TRUE) {
  10797. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10798. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10799. sqlite3 *db = [iSalesDB get_db];
  10800. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10801. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10802. [iSalesDB close_db:db];
  10803. }
  10804. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10805. return [RAConvertor dict2data:dic];
  10806. }
  10807. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10808. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10809. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10810. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10811. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10812. NSDictionary *company_item = @{
  10813. @"control": @"text",
  10814. @"keyboard": @"text",
  10815. @"name": @"company_name",
  10816. @"value": COMPANY_FULL_NAME,
  10817. @"aname": @"Company Name"
  10818. };
  10819. [section_0 setObject:company_item forKey:@"item_0"];
  10820. [dic setObject:section_0 forKey:@"section_0"];
  10821. // Regurlar Price
  10822. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10823. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10824. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10825. __block long val_count = 0;
  10826. NSString *sql = @"select name,type,order_by from price order by order_by";
  10827. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10828. NSString *name = [self textAtColumn:0 statement:stmt];
  10829. int type = sqlite3_column_int(stmt, 1);
  10830. int order_by = sqlite3_column_int(stmt, 2);
  10831. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10832. NSDictionary *price_dic = @{
  10833. @"value" : name,
  10834. @"value_id" : [NSNumber numberWithInteger:type],
  10835. @"check" : order_by == 0 ? @(1) : @(0)
  10836. };
  10837. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10838. val_count = ++(*count);
  10839. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10840. val_count = 0;
  10841. }];
  10842. [cadedate setObject:@{@"value" : @"None",
  10843. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10844. val_count++;
  10845. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10846. [price setObject:cadedate forKey:@"cadedate"];
  10847. [section1 setObject:price forKey:@"item_2"];
  10848. [dic setObject:section1 forKey:@"section_1"];
  10849. return [RAConvertor dict2data:dic];
  10850. }
  10851. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10852. NSString *product_id = [params objectForKey:@"fashionId"];
  10853. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10854. __block int result = RESULT_TRUE;
  10855. __block int qty = 0;
  10856. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10857. qty = sqlite3_column_int(stmt, 0);
  10858. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10859. result = RESULT_FALSE;
  10860. }];
  10861. NSMutableDictionary *dic = @{
  10862. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10863. @"mode" : @"Regular Mode",
  10864. @"quantity_available" : @(qty),
  10865. @"result" : @(result),
  10866. }.mutableCopy;
  10867. return [RAConvertor dict2data:dic];
  10868. }
  10869. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10870. NSString *item_ids = [params objectForKey:@"item_id"];
  10871. NSString *line_notes = [params objectForKey:@"notes"];
  10872. NSString *price_str = [params objectForKey:@"price"];
  10873. NSString *discount_str = [params objectForKey:@"discount"];
  10874. NSString *percent = [params objectForKey:@"available_percent"];
  10875. NSString *qty = [params objectForKey:@"available_qty"];
  10876. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10877. int dot = 0;
  10878. if (line_notes) {
  10879. line_notes = [self translateSingleQuote:line_notes];
  10880. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10881. sql = [sql stringByAppendingString:line_notes];
  10882. dot = 1;
  10883. } else {
  10884. line_notes = @"";
  10885. }
  10886. if (price_str) {
  10887. if (dot) {
  10888. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10889. } else {
  10890. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10891. dot = 1;
  10892. }
  10893. sql = [sql stringByAppendingString:price_str];
  10894. } else {
  10895. price_str = @"";
  10896. }
  10897. if (discount_str) {
  10898. if (dot) {
  10899. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10900. } else {
  10901. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10902. dot = 1;
  10903. }
  10904. sql = [sql stringByAppendingString:discount_str];
  10905. } else {
  10906. discount_str = @"";
  10907. }
  10908. if (percent) {
  10909. if (dot) {
  10910. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10911. } else {
  10912. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10913. dot = 1;
  10914. }
  10915. sql = [sql stringByAppendingString:percent];
  10916. } else {
  10917. percent = @"";
  10918. }
  10919. if (qty) {
  10920. if (dot) {
  10921. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10922. } else {
  10923. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10924. dot = 1;
  10925. }
  10926. sql = [sql stringByAppendingString:qty];
  10927. } else {
  10928. qty = @"";
  10929. }
  10930. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10931. sql = [sql stringByAppendingString:where];
  10932. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10933. int result = [iSalesDB execSql:sql];
  10934. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10935. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10936. return [RAConvertor dict2data:dic];
  10937. }
  10938. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10939. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10940. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10941. sqlite3 *db = [iSalesDB get_db];
  10942. 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];
  10943. int result = [iSalesDB execSql:sql db:db];
  10944. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10945. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10946. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10947. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10948. [iSalesDB close_db:db];
  10949. return [RAConvertor dict2data:dic];
  10950. }
  10951. + (void)offline_removePDFWithName:(NSString *)name {
  10952. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10953. NSString *cachefolder = [paths objectAtIndex:0];
  10954. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10955. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10956. NSFileManager *fileManager = [NSFileManager defaultManager];
  10957. [fileManager removeItemAtPath:path error:nil];
  10958. }
  10959. + (void)offline_clear_PDFCache {
  10960. NSFileManager *fileManager = [NSFileManager defaultManager];
  10961. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10962. NSString *cachefolder = [paths objectAtIndex:0];
  10963. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10964. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10965. for (NSString *path in pdf_files) {
  10966. [self offline_removePDFWithName:[path lastPathComponent]];
  10967. }
  10968. }
  10969. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10970. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10971. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10972. NSString *user = [params objectForKey:@"user"];
  10973. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10974. if (![create_user isEqualToString:user]) {
  10975. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10976. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10977. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10978. return [RAConvertor dict2data:dic];
  10979. }
  10980. 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]];
  10981. __block int is_local = 0;
  10982. __block NSString *path = @"";
  10983. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10984. is_local = sqlite3_column_int(stmt, 0);
  10985. path = [self textAtColumn:1 statement:stmt];
  10986. }];
  10987. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10988. if (is_local == 1) {
  10989. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10990. // 删除文件
  10991. [self offline_removePDFWithName:path];
  10992. }
  10993. int result = [iSalesDB execSql:sql];
  10994. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10995. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10996. return [RAConvertor dict2data:dic];
  10997. }
  10998. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10999. {
  11000. // assert(add_params[@"contact_id"]!=nil);
  11001. assert(add_params[@"user"]!=nil);
  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,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];
  11010. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11011. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11012. sqlite3 *db = [iSalesDB get_db];
  11013. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11014. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11015. NSInteger _id = sqlite3_column_int(stmt, 0);
  11016. NSInteger product_id = sqlite3_column_int(stmt, 1);
  11017. NSString *name = [self textAtColumn:2 statement:stmt];
  11018. NSString *desc = [self textAtColumn:3 statement:stmt];
  11019. NSInteger item_id = sqlite3_column_int(stmt, 4);
  11020. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  11021. NSInteger qty = sqlite3_column_int(stmt, 6);
  11022. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  11023. double percent = sqlite3_column_double(stmt, 8);
  11024. double price = sqlite3_column_double(stmt, 9);
  11025. double discount = sqlite3_column_double(stmt, 10);
  11026. NSString *img = [self textAtColumn:11 statement:stmt];
  11027. NSString *line_note = [self textAtColumn:12 statement:stmt];
  11028. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  11029. NSString *create_time = [self textAtColumn:14 statement:stmt];
  11030. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  11031. NSString *price_null = [self textAtColumn:9 statement:stmt];
  11032. if ([price_null isEqualToString:@"null"]) {
  11033. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  11034. }
  11035. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  11036. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  11037. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  11038. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11039. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  11040. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  11041. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  11042. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  11043. if ([qty_null isEqualToString:@"null"]) {
  11044. // [item setValue:@"null" forKey:@"available_qty"];
  11045. } else {
  11046. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  11047. }
  11048. if ([is_percent_null isEqualToString:@"null"]) {
  11049. // [item setValue:@"null" forKey:@"percentage"];
  11050. } else {
  11051. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  11052. }
  11053. if ([percent_null isEqualToString:@"null"]) {
  11054. // [item setValue:@"null" forKey:@"percent"];
  11055. } else {
  11056. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  11057. }
  11058. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  11059. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  11060. [item setValue:name forKey:@"name"];
  11061. [item setValue:desc forKey:@"description"];
  11062. [item setValue:img forKey:@"img"];
  11063. [item setValue:line_note forKey:@"line_note"];
  11064. [item setValue:create_time forKey:@"createtime"];
  11065. [item setValue:modify_time forKey:@"modifytime"];
  11066. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11067. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11068. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11069. }];
  11070. [iSalesDB close_db:db];
  11071. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  11072. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11073. }
  11074. return ret;
  11075. }
  11076. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  11077. {
  11078. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11079. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11080. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11081. NSString* where=@"1=1";
  11082. if (ver!=nil) {
  11083. where=@"is_dirty=1";
  11084. }
  11085. 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];
  11086. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11087. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11088. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11089. NSInteger _id = sqlite3_column_int(stmt, 0);
  11090. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  11091. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  11092. NSString *create_user = [self textAtColumn:3 statement:stmt];
  11093. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  11094. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  11095. NSString *model_info = [self textAtColumn:6 statement:stmt];
  11096. NSString *createtime = [self textAtColumn:7 statement:stmt];
  11097. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  11098. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  11099. NSString *off_params = [self textAtColumn:10 statement:stmt];
  11100. NSString *uuid = [NSUUID UUID].UUIDString;
  11101. int is_delete = sqlite3_column_int(stmt, 11);
  11102. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  11103. int is_local = sqlite3_column_int(stmt, 12);
  11104. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11105. if (![tearsheet_id_null isEqualToString:@"null"]) {
  11106. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  11107. }
  11108. [item setObject:pdf_path forKey:@"pdf_path"];
  11109. [item setObject:create_user forKey:@"create_user"];
  11110. [item setObject:tear_note forKey:@"tear_note"];
  11111. [item setObject:tear_name forKey:@"tear_name"];
  11112. [item setObject:model_info forKey:@"model_info"];
  11113. [item setObject:createtime forKey:@"createtime"];
  11114. [item setObject:modifytime forKey:@"modifytime"];
  11115. [item setObject:urlParams forKey:@"urlParams"];
  11116. [item setObject:off_params forKey:@"off_params"];
  11117. [item setObject:uuid forKey:@"pdf_token"];
  11118. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  11119. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  11120. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  11121. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  11122. } else {
  11123. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11124. }
  11125. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11126. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11127. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  11128. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11129. }];
  11130. return ret;
  11131. }
  11132. #pragma mark 2020
  11133. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11134. {
  11135. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11136. sqlite3 *db = [iSalesDB get_db];
  11137. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  11138. if (contactType) {
  11139. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  11140. } else {
  11141. contactType = @"1 = 1";
  11142. }
  11143. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  11144. DebugLog(@"offline contact list keyword: %@",keyword);
  11145. // advanced search
  11146. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  11147. if (contact_name) {
  11148. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11149. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  11150. } else {
  11151. contact_name = @"";
  11152. }
  11153. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  11154. if (customer_phone) {
  11155. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11156. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  11157. } else {
  11158. customer_phone = @"";
  11159. }
  11160. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  11161. if (customer_fax) {
  11162. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11163. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  11164. } else {
  11165. customer_fax = @"";
  11166. }
  11167. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  11168. if (customer_zipcode) {
  11169. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11170. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  11171. } else {
  11172. customer_zipcode = @"";
  11173. }
  11174. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  11175. if (customer_sales_rep) {
  11176. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11177. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  11178. } else {
  11179. customer_sales_rep = @"";
  11180. }
  11181. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  11182. if (customer_state) {
  11183. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11184. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  11185. } else {
  11186. customer_state = @"";
  11187. }
  11188. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  11189. if (customer_name) {
  11190. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11191. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  11192. } else {
  11193. customer_name = @"";
  11194. }
  11195. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  11196. if (customer_country) {
  11197. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11198. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  11199. } else {
  11200. customer_country = @"";
  11201. }
  11202. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11203. if (customer_cid) {
  11204. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11205. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11206. } else {
  11207. customer_cid = @"";
  11208. }
  11209. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11210. if (customer_city) {
  11211. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11212. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11213. } else {
  11214. customer_city = @"";
  11215. }
  11216. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11217. if (customer_address) {
  11218. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11219. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11220. } else {
  11221. customer_address = @"";
  11222. }
  11223. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11224. if (customer_email) {
  11225. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11226. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11227. } else {
  11228. customer_email = @"";
  11229. }
  11230. NSString *price_name = [params valueForKey:@"price_name"];
  11231. if (price_name) {
  11232. if ([price_name containsString:@","]) {
  11233. // 首先从 price表中查处name
  11234. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11235. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11236. for (int i = 1;i < pArray.count;i++) {
  11237. NSString *p = pArray[i];
  11238. [mutablePStr appendFormat:@" or type = %@ ",p];
  11239. }
  11240. [mutablePStr appendString:@";"];
  11241. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11242. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11243. char *name = (char *)sqlite3_column_text(stmt, 0);
  11244. if (!name)
  11245. name = "";
  11246. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11247. }];
  11248. // 再根据name 拼sql
  11249. NSMutableString *mutable_price_name = [NSMutableString string];
  11250. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11251. for (int i = 1; i < price_name_array.count; i++) {
  11252. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11253. }
  11254. [mutable_price_name appendString:@")"];
  11255. price_name = mutable_price_name;
  11256. } else {
  11257. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11258. if ([price_name isEqualToString:@""]) {
  11259. price_name = @"";
  11260. } else {
  11261. __block NSString *price;
  11262. price_name = [self translateSingleQuote:price_name];
  11263. [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) {
  11264. char *p = (char *)sqlite3_column_text(stmt, 0);
  11265. if (p == NULL) {
  11266. p = "";
  11267. }
  11268. price = [NSString stringWithUTF8String:p];
  11269. }];
  11270. if ([price isEqualToString:@""]) {
  11271. price_name = @"";
  11272. } else {
  11273. price = [self translateSingleQuote:price];
  11274. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11275. }
  11276. }
  11277. }
  11278. } else {
  11279. price_name = @"";
  11280. }
  11281. int limit = [[params valueForKey:@"limit"] intValue];
  11282. int offset = [[params valueForKey:@"offset"] intValue];
  11283. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11284. 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];
  11285. 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];
  11286. // int result= [iSalesDB AddExFunction:db];
  11287. int count =0;
  11288. NSString *sqlQuery = nil;
  11289. if(keyword.length==0)
  11290. {
  11291. // 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];
  11292. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11293. sqlQuery = sql;
  11294. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11295. }
  11296. else
  11297. {
  11298. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11299. keyword = keyword.lowercaseString;
  11300. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11301. 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];
  11302. 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]];
  11303. }
  11304. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11305. sqlite3_stmt * statement;
  11306. [ret setValue:@"2" forKey:@"result"];
  11307. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11308. // 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";
  11309. int i = 0;
  11310. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11311. {
  11312. while (sqlite3_step(statement) == SQLITE_ROW)
  11313. {
  11314. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11315. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11316. int editable = sqlite3_column_int(statement, 0);
  11317. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11318. NSString *nscompany_name =nil;
  11319. if(company_name==nil)
  11320. nscompany_name=@"";
  11321. else
  11322. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11323. char *country = (char*)sqlite3_column_text(statement, 2);
  11324. if(country==nil)
  11325. country="";
  11326. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11327. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11328. // if(addr==nil)
  11329. // addr="";
  11330. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11331. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11332. if(zipcode==nil)
  11333. zipcode="";
  11334. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11335. char *state = (char*)sqlite3_column_text(statement, 5);
  11336. if(state==nil)
  11337. state="";
  11338. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11339. char *city = (char*)sqlite3_column_text(statement, 6);
  11340. if(city==nil)
  11341. city="";
  11342. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11343. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11344. // NSString *nscontact_name = nil;
  11345. // if(contact_name==nil)
  11346. // nscontact_name=@"";
  11347. // else
  11348. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11349. char *phone = (char*)sqlite3_column_text(statement, 8);
  11350. NSString *nsphone = nil;
  11351. if(phone==nil)
  11352. nsphone=@"";
  11353. else
  11354. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11355. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11356. if(contact_id==nil)
  11357. contact_id="";
  11358. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11359. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11360. if(addr_1==nil)
  11361. addr_1="";
  11362. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11363. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11364. if(addr_2==nil)
  11365. addr_2="";
  11366. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11367. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11368. if(addr_3==nil)
  11369. addr_3="";
  11370. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11371. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11372. if(addr_4==nil)
  11373. addr_4="";
  11374. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11375. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11376. if(first_name==nil)
  11377. first_name="";
  11378. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11379. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11380. if(last_name==nil)
  11381. last_name="";
  11382. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11383. char *fax = (char*)sqlite3_column_text(statement, 16);
  11384. NSString *nsfax = nil;
  11385. if(fax==nil)
  11386. nsfax=@"";
  11387. else
  11388. {
  11389. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11390. if(nsfax.length>0)
  11391. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11392. }
  11393. char *email = (char*)sqlite3_column_text(statement, 17);
  11394. NSString *nsemail = nil;
  11395. if(email==nil)
  11396. nsemail=@"";
  11397. else
  11398. {
  11399. nsemail= [[NSString alloc]initWithUTF8String:email];
  11400. if(nsemail.length>0)
  11401. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11402. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11403. }
  11404. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11405. [arr_name addObject:nsfirst_name];
  11406. [arr_name addObject:nslast_name];
  11407. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11408. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11409. {
  11410. // decrypt
  11411. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11412. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11413. nsphone=[AESCrypt fastdecrypt:nsphone];
  11414. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11415. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11416. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11417. }
  11418. [arr_addr addObject:nscompany_name];
  11419. [arr_addr addObject:nscontact_name];
  11420. [arr_addr addObject:@"<br>"];
  11421. [arr_addr addObject:nsaddr_1];
  11422. [arr_addr addObject:nsaddr_2];
  11423. [arr_addr addObject:nsaddr_3];
  11424. [arr_addr addObject:nsaddr_4];
  11425. //[arr_addr addObject:nsaddr];
  11426. [arr_addr addObject:nszipcode];
  11427. [arr_addr addObject:nscity];
  11428. [arr_addr addObject:nsstate];
  11429. [arr_addr addObject:nscountry];
  11430. [arr_addr addObject:@"<br>"];
  11431. [arr_addr addObject:nsphone];
  11432. [arr_addr addObject:nsfax];
  11433. [arr_addr addObject:nsemail];
  11434. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11435. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11436. [item setValue:name forKey:@"name"];
  11437. [item setValue:nscontact_id forKey:@"contact_id"];
  11438. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11439. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11440. i++;
  11441. }
  11442. sqlite3_finalize(statement);
  11443. }
  11444. [iSalesDB close_db:db];
  11445. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11446. dispatch_async(dispatch_get_main_queue(), ^{
  11447. UIApplication * app = [UIApplication sharedApplication];
  11448. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11449. [ret setValue:appDelegate.mode forKey:@"mode"];
  11450. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11451. result(ret);
  11452. });
  11453. return ;
  11454. });
  11455. }
  11456. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11457. //{
  11458. //
  11459. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11460. //
  11461. // NSString* contactId = [params valueForKey:@"contactId"];
  11462. //
  11463. //
  11464. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11465. //
  11466. //
  11467. //
  11468. //
  11469. //
  11470. //
  11471. // sqlite3 *db = [iSalesDB get_db];
  11472. //
  11473. //
  11474. //
  11475. //
  11476. //
  11477. // NSString *sqlQuery = nil;
  11478. //
  11479. //
  11480. // {
  11481. // 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];
  11482. //
  11483. // }
  11484. //
  11485. //
  11486. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11487. // sqlite3_stmt * statement;
  11488. //
  11489. //
  11490. // [ret setValue:@"2" forKey:@"result"];
  11491. //
  11492. //
  11493. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11494. // {
  11495. //
  11496. // //int i = 0;
  11497. // if (sqlite3_step(statement) == SQLITE_ROW)
  11498. // {
  11499. //
  11500. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11501. //
  11502. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11503. //
  11504. //
  11505. // // int editable = sqlite3_column_int(statement, 0);
  11506. //
  11507. //
  11508. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11509. // NSString *nscompany_name =nil;
  11510. // if(company_name==nil)
  11511. // nscompany_name=@"";
  11512. // else
  11513. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11514. //
  11515. //
  11516. // char *country = (char*)sqlite3_column_text(statement, 2);
  11517. // if(country==nil)
  11518. // country="";
  11519. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11520. //
  11521. //
  11522. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11523. // // if(addr==nil)
  11524. // // addr="";
  11525. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11526. //
  11527. //
  11528. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11529. // if(zipcode==nil)
  11530. // zipcode="";
  11531. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11532. //
  11533. //
  11534. // char *state = (char*)sqlite3_column_text(statement, 5);
  11535. // if(state==nil)
  11536. // state="";
  11537. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11538. //
  11539. // char *city = (char*)sqlite3_column_text(statement, 6);
  11540. // if(city==nil)
  11541. // city="";
  11542. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11543. //
  11544. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11545. // // NSString *nscontact_name = nil;
  11546. // // if(contact_name==nil)
  11547. // // nscontact_name=@"";
  11548. // // else
  11549. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11550. //
  11551. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11552. // NSString *nsphone = nil;
  11553. // if(phone==nil)
  11554. // nsphone=@"";
  11555. // else
  11556. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11557. //
  11558. //
  11559. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11560. // // if(contact_id==nil)
  11561. // // contact_id="";
  11562. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11563. //
  11564. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11565. // if(addr_1==nil)
  11566. // addr_1="";
  11567. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11568. //
  11569. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11570. // if(addr_2==nil)
  11571. // addr_2="";
  11572. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11573. //
  11574. //
  11575. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11576. // if(addr_3==nil)
  11577. // addr_3="";
  11578. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11579. //
  11580. //
  11581. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11582. // if(addr_4==nil)
  11583. // addr_4="";
  11584. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11585. //
  11586. //
  11587. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11588. // if(first_name==nil)
  11589. // first_name="";
  11590. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11591. //
  11592. //
  11593. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11594. // if(last_name==nil)
  11595. // last_name="";
  11596. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11597. //
  11598. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11599. // NSString *nsfax = nil;
  11600. // if(fax==nil)
  11601. // nsfax=@"";
  11602. // else
  11603. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11604. //
  11605. // char *email = (char*)sqlite3_column_text(statement, 17);
  11606. // NSString *nsemail = nil;
  11607. // if(email==nil)
  11608. // nsemail=@"";
  11609. // else
  11610. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11611. //
  11612. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11613. // NSString *nsimg_0 = nil;
  11614. // if(img_0==nil)
  11615. // nsimg_0=@"";
  11616. // else
  11617. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11618. //
  11619. // [self copy_bcardImg:nsimg_0];
  11620. //
  11621. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11622. // NSString *nsimg_1 = nil;
  11623. // if(img_1==nil)
  11624. // nsimg_1=@"";
  11625. // else
  11626. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11627. // [self copy_bcardImg:nsimg_1];
  11628. //
  11629. //
  11630. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11631. // NSString *nsimg_2 = nil;
  11632. // if(img_2==nil)
  11633. // nsimg_2=@"";
  11634. // else
  11635. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11636. // [self copy_bcardImg:nsimg_2];
  11637. //
  11638. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11639. // NSString *nsprice_type = nil;
  11640. // if(price_type==nil)
  11641. // nsprice_type=@"";
  11642. // else
  11643. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11644. //
  11645. //
  11646. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11647. // NSString *nsnotes = nil;
  11648. // if(notes==nil)
  11649. // nsnotes=@"";
  11650. // else
  11651. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11652. //
  11653. //
  11654. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11655. // NSString *nssalesrep = nil;
  11656. // if(salesrep==nil)
  11657. // nssalesrep=@"";
  11658. // else
  11659. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11660. //
  11661. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11662. //
  11663. //
  11664. // {
  11665. // // decrypt
  11666. //
  11667. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11668. //
  11669. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11670. //
  11671. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11672. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11673. //
  11674. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11675. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11676. //
  11677. // }
  11678. //
  11679. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11680. //
  11681. // [arr_name addObject:nsfirst_name];
  11682. // [arr_name addObject:nslast_name];
  11683. //
  11684. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11685. //
  11686. //
  11687. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11688. //
  11689. // [arr_ext addObject:nsaddr_1];
  11690. // [arr_ext addObject:nsaddr_2];
  11691. // [arr_ext addObject:nsaddr_3];
  11692. // [arr_ext addObject:nsaddr_4];
  11693. // [arr_ext addObject:@"\r\n"];
  11694. //
  11695. // [arr_ext addObject:nscity];
  11696. // [arr_ext addObject:nsstate];
  11697. // [arr_ext addObject:nszipcode];
  11698. // [arr_ext addObject:nscountry];
  11699. //
  11700. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11701. //
  11702. //
  11703. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11704. //
  11705. //
  11706. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11707. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11708. // [item setValue:nscountry forKey:@"customer_country"];
  11709. // [item setValue:nsphone forKey:@"customer_phone"];
  11710. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11711. // [item setValue:nscompany_name forKey:@"customer_name"];
  11712. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11713. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11714. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11715. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11716. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11717. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11718. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11719. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11720. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11721. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11722. // [item setValue:nscity forKey:@"customer_city"];
  11723. // [item setValue:nsstate forKey:@"customer_state"];
  11724. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11725. // [item setValue:contactId forKey:@"customer_cid"];
  11726. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11727. // [item setValue:nsfax forKey:@"customer_fax"];
  11728. // [item setValue:nsemail forKey:@"customer_email"];
  11729. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11730. //
  11731. //
  11732. // [ret setObject:item forKey:@"customerInfo"];
  11733. // // i++;
  11734. //
  11735. //
  11736. //
  11737. // }
  11738. //
  11739. //
  11740. //
  11741. //
  11742. // sqlite3_finalize(statement);
  11743. // }
  11744. //
  11745. //
  11746. //
  11747. // [iSalesDB close_db:db];
  11748. //
  11749. //
  11750. //
  11751. //
  11752. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11753. // dispatch_async(dispatch_get_main_queue(), ^{
  11754. // UIApplication * app = [UIApplication sharedApplication];
  11755. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11756. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11757. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11758. // result(ret);
  11759. // });
  11760. //
  11761. // return ;
  11762. // });
  11763. //}
  11764. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11765. {
  11766. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11767. NSString* order_code= appDelegate.order_code;
  11768. NSString* user = appDelegate.user;
  11769. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11770. NSString* category = [params valueForKey:@"category"];
  11771. if (!category || [category isEqualToString:@""]) {
  11772. category = @"%";
  11773. }
  11774. category = [self translateSingleQuote:category];
  11775. int limit = [[params valueForKey:@"limit"] intValue];
  11776. int offset = [[params valueForKey:@"offset"] intValue];
  11777. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11778. NSString *limit_str = @"";
  11779. if (limited) {
  11780. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11781. }
  11782. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11783. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11784. sqlite3 *db = [iSalesDB get_db];
  11785. // [iSalesDB AddExFunction:db];
  11786. int count;
  11787. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11788. 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];
  11789. double price_min = 0;
  11790. double price_max = 0;
  11791. if ([params.allKeys containsObject:@"alert"]) {
  11792. // alert
  11793. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11794. NSString *alert = params[@"alert"];
  11795. if ([alert isEqualToString:@"Display All"]) {
  11796. alert = [NSString stringWithFormat:@""];
  11797. } else {
  11798. alert = [self translateSingleQuote:alert];
  11799. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11800. }
  11801. // available
  11802. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11803. NSString *available = params[@"available"];
  11804. NSString *available_condition;
  11805. if ([available isEqualToString:@"Display All"]) {
  11806. available_condition = @"";
  11807. } else if ([available isEqualToString:@"Available Now"]) {
  11808. available_condition = @"and availability > 0";
  11809. } else {
  11810. available_condition = @"and availability == 0";
  11811. }
  11812. // best seller
  11813. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11814. NSString *best_seller = @"";
  11815. NSString *order_best_seller = @"m.name asc";
  11816. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11817. best_seller = @"and best_seller > 0";
  11818. order_best_seller = @"m.best_seller desc,m.name asc";
  11819. }
  11820. // price
  11821. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11822. NSString *price = params[@"price"];
  11823. price_min = 0;
  11824. price_max = MAXFLOAT;
  11825. if (user && price != nil) {
  11826. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11827. NSMutableString *priceName = [NSMutableString string];
  11828. for (int i = 0; i < priceTypeArray.count; i++) {
  11829. NSString *pricetype = priceTypeArray[i];
  11830. pricetype = [self translateSingleQuote:pricetype];
  11831. if (i == 0) {
  11832. [priceName appendFormat:@"'%@'",pricetype];
  11833. } else {
  11834. [priceName appendFormat:@",'%@'",pricetype];
  11835. }
  11836. }
  11837. if ([price isEqualToString:@"Display All"]) {
  11838. price = [NSString stringWithFormat:@""];
  11839. } else if([price containsString:@"+"]){
  11840. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11841. price_min = [price doubleValue];
  11842. 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];
  11843. } else {
  11844. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11845. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11846. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11847. 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];
  11848. }
  11849. } else {
  11850. price = @"";
  11851. }
  11852. // sold_by_qty : Sold in quantities of %@
  11853. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11854. NSString *qty = params[@"sold_by_qty"];
  11855. if ([qty isEqualToString:@"Display All"]) {
  11856. qty = @"";
  11857. } else {
  11858. qty = [self translateSingleQuote:qty];
  11859. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11860. }
  11861. // cate
  11862. // category = [self translateSingleQuote:category];
  11863. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11864. // cate mutiple selection
  11865. NSString *category_id = params[@"category"];
  11866. NSMutableArray *cate_id_array = nil;
  11867. NSMutableString *cateWhere = [NSMutableString string];
  11868. if ([category_id isEqualToString:@""] || !category_id) {
  11869. [cateWhere appendString:@"1 = 1"];
  11870. } else {
  11871. if ([category_id containsString:@","]) {
  11872. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11873. } else {
  11874. cate_id_array = [@[category_id] mutableCopy];
  11875. }
  11876. [cateWhere appendString:@"("];
  11877. for (int i = 0; i < cate_id_array.count; i++) {
  11878. if (i == 0) {
  11879. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11880. } else {
  11881. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11882. }
  11883. }
  11884. [cateWhere appendString:@")"];
  11885. }
  11886. // where bestseller > 0 order by bestseller desc
  11887. // sql query: alert availability(int) best_seller(int) price qty
  11888. 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];
  11889. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11890. }
  11891. DebugLog(@"offline category where: %@",where);
  11892. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11893. if (!user) {
  11894. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11895. }
  11896. [ret setValue:filter forKey:@"filter"];
  11897. DebugLog(@"offline_category sql:%@",sqlQuery);
  11898. sqlite3_stmt * statement;
  11899. [ret setValue:@"2" forKey:@"result"];
  11900. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11901. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11902. // int count=0;
  11903. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11904. {
  11905. int i=0;
  11906. while (sqlite3_step(statement) == SQLITE_ROW)
  11907. {
  11908. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11909. char *name = (char*)sqlite3_column_text(statement, 0);
  11910. if(name==nil)
  11911. name="";
  11912. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11913. char *description = (char*)sqlite3_column_text(statement, 1);
  11914. if(description==nil)
  11915. description="";
  11916. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11917. int product_id = sqlite3_column_int(statement, 2);
  11918. int wid = sqlite3_column_int(statement, 3);
  11919. int closeout = sqlite3_column_int(statement, 4);
  11920. int cid = sqlite3_column_int(statement, 5);
  11921. int wisdelete = sqlite3_column_int(statement, 6);
  11922. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11923. int more_color = sqlite3_column_int(statement, 8);
  11924. // Defaul Category ID
  11925. __block NSString *categoryID = nil;
  11926. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11927. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11928. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11929. if(default_category==nil)
  11930. default_category="";
  11931. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11932. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11933. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11934. categoryID = nsdefault_category;
  11935. }];
  11936. if (!categoryID.length) {
  11937. NSString *cateIDs = params[@"category"];
  11938. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11939. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11940. for (NSString *cateID in requestCategoryArr) {
  11941. BOOL needBreak = NO;
  11942. for (NSString *itemCateIDBox in itemCategoryArr) {
  11943. if (itemCateIDBox.length > 4) {
  11944. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11945. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11946. if ([itemCategoryID isEqualToString:cateID]) {
  11947. needBreak = YES;
  11948. categoryID = itemCategoryID;
  11949. break;
  11950. }
  11951. }
  11952. }
  11953. if (needBreak) {
  11954. break;
  11955. }
  11956. }
  11957. }
  11958. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11959. if(wid !=0 && wisdelete != 1)
  11960. [item setValue:@"true" forKey:@"wish_exists"];
  11961. else
  11962. [item setValue:@"false" forKey:@"wish_exists"];
  11963. if(closeout==0)
  11964. [item setValue:@"false" forKey:@"is_closeout"];
  11965. else
  11966. [item setValue:@"true" forKey:@"is_closeout"];
  11967. if(cid==0)
  11968. [item setValue:@"false" forKey:@"cart_exists"];
  11969. else
  11970. [item setValue:@"true" forKey:@"cart_exists"];
  11971. if (more_color == 0) {
  11972. [item setObject:@(false) forKey:@"more_color"];
  11973. } else if (more_color == 1) {
  11974. [item setObject:@(true) forKey:@"more_color"];
  11975. }
  11976. [item addEntriesFromDictionary:imgjson];
  11977. // [item setValue:nsurl forKey:@"img"];
  11978. [item setValue:nsname forKey:@"name"];
  11979. [item setValue:nsdescription forKey:@"description"];
  11980. if (categoryID) {
  11981. [item setValue:categoryID forKey:@"item_category_id"];
  11982. }
  11983. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11984. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11985. i++;
  11986. }
  11987. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11988. [ret setObject:items forKey:@"items"];
  11989. sqlite3_finalize(statement);
  11990. } else {
  11991. DebugLog(@"nothing...");
  11992. }
  11993. DebugLog(@"count:%d",count);
  11994. [iSalesDB close_db:db];
  11995. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11996. dispatch_async(dispatch_get_main_queue(), ^{
  11997. // UIApplication * app = [UIApplication sharedApplication];
  11998. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11999. // [ret setValue:appDelegate.mode forKey:@"mode"];
  12000. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  12001. result(ret);
  12002. });
  12003. });
  12004. // NSString* orderCode = [params valueForKey:@"orderCode"];
  12005. }
  12006. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12007. {
  12008. [self categoryList:params limited:YES completionHandler:result];
  12009. }
  12010. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12011. {
  12012. UIApplication * app = [UIApplication sharedApplication];
  12013. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12014. NSString* order_code = appDelegate.order_code;
  12015. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12016. NSString* keyword = [params valueForKey:@"keyword"];
  12017. keyword=keyword.lowercaseString;
  12018. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  12019. int limit = [[params valueForKey:@"limit"] intValue];
  12020. int offset = [[params valueForKey:@"offset"] intValue];
  12021. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12022. NSString *limit_str = @"";
  12023. if (limited) {
  12024. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  12025. }
  12026. sqlite3 *db = [iSalesDB get_db];
  12027. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  12028. NSString *sqlQuery = nil;
  12029. if(exactMatch )
  12030. 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 ;
  12031. else
  12032. 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
  12033. DebugLog(@"offline_search sql:%@",sqlQuery);
  12034. sqlite3_stmt * statement;
  12035. [ret setValue:@"2" forKey:@"result"];
  12036. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12037. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12038. // int count=0;
  12039. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12040. {
  12041. int i=0;
  12042. while (sqlite3_step(statement) == SQLITE_ROW)
  12043. {
  12044. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12045. // char *name = (char*)sqlite3_column_text(statement, 1);
  12046. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12047. char *name = (char*)sqlite3_column_text(statement, 0);
  12048. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12049. char *description = (char*)sqlite3_column_text(statement, 1);
  12050. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12051. int product_id = sqlite3_column_int(statement, 2);
  12052. // char *url = (char*)sqlite3_column_text(statement, 3);
  12053. // if(url==nil)
  12054. // url="";
  12055. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12056. int wid = sqlite3_column_int(statement, 3);
  12057. int closeout = sqlite3_column_int(statement, 4);
  12058. int cid = sqlite3_column_int(statement, 5);
  12059. int wisdelete = sqlite3_column_int(statement, 6);
  12060. int more_color = sqlite3_column_int(statement, 7);
  12061. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12062. if(wid !=0 && wisdelete != 1)
  12063. [item setValue:@"true" forKey:@"wish_exists"];
  12064. else
  12065. [item setValue:@"false" forKey:@"wish_exists"];
  12066. if(closeout==0)
  12067. [item setValue:@"false" forKey:@"is_closeout"];
  12068. else
  12069. [item setValue:@"true" forKey:@"is_closeout"];
  12070. if(cid==0)
  12071. [item setValue:@"false" forKey:@"cart_exists"];
  12072. else
  12073. [item setValue:@"true" forKey:@"cart_exists"];
  12074. if (more_color == 0) {
  12075. [item setObject:@(false) forKey:@"more_color"];
  12076. } else if (more_color == 1) {
  12077. [item setObject:@(true) forKey:@"more_color"];
  12078. }
  12079. [item addEntriesFromDictionary:imgjson];
  12080. // [item setValue:nsurl forKey:@"img"];
  12081. [item setValue:nsname forKey:@"fash_name"];
  12082. [item setValue:nsdescription forKey:@"description"];
  12083. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12084. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12085. i++;
  12086. }
  12087. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12088. [ret setObject:items forKey:@"items"];
  12089. sqlite3_finalize(statement);
  12090. }
  12091. DebugLog(@"count:%d",count);
  12092. [iSalesDB close_db:db];
  12093. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12094. dispatch_async(dispatch_get_main_queue(), ^{
  12095. result(ret);
  12096. });
  12097. });
  12098. }
  12099. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12100. {
  12101. [self search:params limited:YES completionHandler:result];
  12102. }
  12103. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12104. //{
  12105. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  12106. // NSString* order_code = appDelegate.order_code;
  12107. // NSString* user = appDelegate.user;
  12108. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12109. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  12110. //
  12111. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  12112. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  12113. //
  12114. // // category
  12115. // NSDictionary *category_menu = [self offline_category_menu];
  12116. // [filter setValue:category_menu forKey:@"category"];
  12117. //
  12118. // NSString* where= nil;
  12119. // NSString* orderby= @"m.name";
  12120. // if (!filterSearch) {
  12121. // int covertype = [[params valueForKey:@"covertype"] intValue];
  12122. //
  12123. // switch (covertype) {
  12124. // case 0:
  12125. // {
  12126. // where=@"m.category like'%%#005#%%'";
  12127. // break;
  12128. // }
  12129. // case 1:
  12130. // {
  12131. // where=@"m.alert like '%QS%'";
  12132. // break;
  12133. // }
  12134. // case 2:
  12135. // {
  12136. // where=@"m.availability>0";
  12137. // break;
  12138. // }
  12139. // case 3:
  12140. // {
  12141. // where=@"m.best_seller>0";
  12142. // orderby=@"m.best_seller desc,m.name asc";
  12143. // break;
  12144. // }
  12145. // default:
  12146. // where=@"1=1";
  12147. // break;
  12148. // }
  12149. //
  12150. // }
  12151. //
  12152. //
  12153. // int limit = [[params valueForKey:@"limit"] intValue];
  12154. // int offset = [[params valueForKey:@"offset"] intValue];
  12155. //
  12156. // NSString *limit_str = @"";
  12157. // if (limited) {
  12158. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  12159. // }
  12160. //
  12161. //
  12162. // sqlite3 *db = [iSalesDB get_db];
  12163. // // [iSalesDB AddExFunction:db];
  12164. //
  12165. // int count;
  12166. //
  12167. // NSString *sqlQuery = nil;
  12168. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  12169. // 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];
  12170. //
  12171. //
  12172. // double price_min = 0;
  12173. // double price_max = 0;
  12174. // if (filterSearch) {
  12175. // // alert
  12176. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  12177. // NSString *alert = params[@"alert"];
  12178. // if ([alert isEqualToString:@"Display All"]) {
  12179. // alert = [NSString stringWithFormat:@""];
  12180. // } else {
  12181. // alert = [self translateSingleQuote:alert];
  12182. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  12183. // }
  12184. //
  12185. // // available
  12186. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  12187. // NSString *available = params[@"available"];
  12188. // NSString *available_condition;
  12189. // if ([available isEqualToString:@"Display All"]) {
  12190. // available_condition = @"";
  12191. // } else if ([available isEqualToString:@"Available Now"]) {
  12192. // available_condition = @"and availability > 0";
  12193. // } else {
  12194. // available_condition = @"and availability == 0";
  12195. // }
  12196. //
  12197. // // best seller
  12198. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  12199. // NSString *best_seller = @"";
  12200. // NSString *order_best_seller = @"m.name asc";
  12201. //
  12202. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12203. // best_seller = @"and best_seller > 0";
  12204. // order_best_seller = @"m.best_seller desc,m.name asc";
  12205. // }
  12206. //
  12207. // // price
  12208. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12209. // NSString *price = params[@"price"];
  12210. // price_min = 0;
  12211. // price_max = MAXFLOAT;
  12212. // if (user) {
  12213. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12214. // NSMutableString *priceName = [NSMutableString string];
  12215. // for (int i = 0; i < priceTypeArray.count; i++) {
  12216. // NSString *pricetype = priceTypeArray[i];
  12217. // pricetype = [self translateSingleQuote:pricetype];
  12218. // if (i == 0) {
  12219. // [priceName appendFormat:@"'%@'",pricetype];
  12220. // } else {
  12221. // [priceName appendFormat:@",'%@'",pricetype];
  12222. // }
  12223. // }
  12224. //
  12225. // if ([price isEqualToString:@"Display All"]) {
  12226. // price = [NSString stringWithFormat:@""];
  12227. // } else if([price containsString:@"+"]){
  12228. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12229. // price_min = [price doubleValue];
  12230. // 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];
  12231. // } else {
  12232. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12233. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12234. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12235. // 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];
  12236. // }
  12237. // } else {
  12238. // price = @"";
  12239. // }
  12240. //
  12241. // // sold_by_qty : Sold in quantities of %@
  12242. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12243. // NSString *qty = params[@"sold_by_qty"];
  12244. // if ([qty isEqualToString:@"Display All"]) {
  12245. // qty = @"";
  12246. // } else {
  12247. // qty = [self translateSingleQuote:qty];
  12248. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12249. // }
  12250. //
  12251. // // category
  12252. // NSString *category_id = params[@"ctgId"];
  12253. // NSMutableArray *cate_id_array = nil;
  12254. // NSMutableString *cateWhere = [NSMutableString string];
  12255. //
  12256. // if ([category_id isEqualToString:@""] || !category_id) {
  12257. // [cateWhere appendString:@"1 = 1"];
  12258. // } else {
  12259. //
  12260. // if ([category_id containsString:@","]) {
  12261. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12262. // } else {
  12263. // cate_id_array = [@[category_id] mutableCopy];
  12264. // }
  12265. // /*-----------*/
  12266. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12267. //
  12268. // [cateWhere appendString:@"("];
  12269. // for (int i = 0; i < cate_id_array.count; i++) {
  12270. //
  12271. // for (NSString *key0 in cateDic.allKeys) {
  12272. //
  12273. // if ([key0 containsString:@"category_"]) {
  12274. //
  12275. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12276. //
  12277. // for (NSString *key1 in category0.allKeys) {
  12278. //
  12279. // if ([key1 containsString:@"category_"]) {
  12280. //
  12281. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12282. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12283. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12284. //
  12285. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12286. // if (i == 0) {
  12287. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12288. // } else {
  12289. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12290. // }
  12291. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12292. // [category0 setValue:category1 forKey:key1];
  12293. // [cateDic setValue:category0 forKey:key0];
  12294. //
  12295. // }
  12296. //
  12297. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12298. //
  12299. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12300. //
  12301. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12302. // if (i == 0) {
  12303. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12304. // } else {
  12305. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12306. // }
  12307. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12308. // [cateDic setValue:category0 forKey:key0];
  12309. //
  12310. // }
  12311. //
  12312. // }
  12313. //
  12314. // }
  12315. //
  12316. // }
  12317. //
  12318. // }
  12319. //
  12320. // }
  12321. // [cateWhere appendString:@")"];
  12322. //
  12323. // [filter setValue:cateDic forKey:@"category"];
  12324. // }
  12325. //
  12326. // // where bestseller > 0 order by bestseller desc
  12327. // // sql query: alert availability(int) best_seller(int) price qty
  12328. //
  12329. // 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];
  12330. //
  12331. //
  12332. // // count
  12333. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12334. //
  12335. // }
  12336. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12337. //
  12338. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12339. //
  12340. //
  12341. //
  12342. //
  12343. // if (!user) {
  12344. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12345. // }
  12346. //
  12347. // [ret setValue:filter forKey:@"filter"];
  12348. //
  12349. //
  12350. //
  12351. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12352. //
  12353. // sqlite3_stmt * statement;
  12354. // [ret setValue:@"2" forKey:@"result"];
  12355. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12356. //
  12357. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12358. // // int count=0;
  12359. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12360. // {
  12361. //
  12362. // int i=0;
  12363. // while (sqlite3_step(statement) == SQLITE_ROW)
  12364. // {
  12365. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12366. //
  12367. //
  12368. // char *name = (char*)sqlite3_column_text(statement, 0);
  12369. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12370. //
  12371. // char *description = (char*)sqlite3_column_text(statement, 1);
  12372. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12373. //
  12374. //
  12375. //
  12376. // int product_id = sqlite3_column_int(statement, 2);
  12377. //
  12378. //
  12379. // int wid = sqlite3_column_int(statement, 3);
  12380. // int closeout = sqlite3_column_int(statement, 4);
  12381. // int cid = sqlite3_column_int(statement, 5);
  12382. // int wisdelete = sqlite3_column_int(statement, 6);
  12383. // int more_color = sqlite3_column_int(statement, 7);
  12384. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12385. //
  12386. // if(wid !=0 && wisdelete != 1)
  12387. // [item setValue:@"true" forKey:@"wish_exists"];
  12388. // else
  12389. // [item setValue:@"false" forKey:@"wish_exists"];
  12390. //
  12391. // if(closeout==0)
  12392. // [item setValue:@"false" forKey:@"is_closeout"];
  12393. // else
  12394. // [item setValue:@"true" forKey:@"is_closeout"];
  12395. //
  12396. // if(cid==0)
  12397. // [item setValue:@"false" forKey:@"cart_exists"];
  12398. // else
  12399. // [item setValue:@"true" forKey:@"cart_exists"];
  12400. //
  12401. // if (more_color == 0) {
  12402. // [item setObject:@(false) forKey:@"more_color"];
  12403. // } else if (more_color == 1) {
  12404. // [item setObject:@(true) forKey:@"more_color"];
  12405. // }
  12406. //
  12407. // [item addEntriesFromDictionary:imgjson];
  12408. //
  12409. //
  12410. // // [item setValue:nsurl forKey:@"img"];
  12411. // [item setValue:nsname forKey:@"fash_name"];
  12412. // [item setValue:nsdescription forKey:@"description"];
  12413. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12414. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12415. // i++;
  12416. // }
  12417. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12418. // [ret setObject:items forKey:@"items"];
  12419. // sqlite3_finalize(statement);
  12420. // }
  12421. //
  12422. // [iSalesDB close_db:db];
  12423. //
  12424. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12425. //
  12426. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12427. // dispatch_async(dispatch_get_main_queue(), ^{
  12428. //
  12429. // result(ret);
  12430. // });
  12431. //
  12432. // });
  12433. //
  12434. //
  12435. //
  12436. //}
  12437. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12438. //{
  12439. // [self itemsearch:params limited:YES completionHandler:result];
  12440. //}
  12441. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12442. {
  12443. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12444. NSString* offline_command=params[@"offline_Command"];
  12445. NSMutableDictionary* ret=nil;
  12446. if([offline_command isEqualToString:@"model_NIYMAL"])
  12447. {
  12448. NSString* category = params[@"category"];
  12449. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12450. }
  12451. dispatch_async(dispatch_get_main_queue(), ^{
  12452. result(ret);
  12453. });
  12454. });
  12455. }
  12456. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12457. {
  12458. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12459. int sort = [[params objectForKey:@"sort"] intValue];
  12460. NSString *sort_str = @"";
  12461. switch (sort) {
  12462. case 0:{
  12463. sort_str = @"order by w.modify_time desc";
  12464. }
  12465. break;
  12466. case 1:{
  12467. sort_str = @"order by w.modify_time asc";
  12468. }
  12469. break;
  12470. case 2:{
  12471. sort_str = @"order by m.name asc";
  12472. }
  12473. break;
  12474. case 3:{
  12475. sort_str = @"order by m.name desc";
  12476. }
  12477. break;
  12478. case 4:{
  12479. sort_str = @"order by m.description asc";
  12480. }
  12481. break;
  12482. default:
  12483. break;
  12484. }
  12485. // NSString* user = appDelegate.user;
  12486. sqlite3 *db = [iSalesDB get_db];
  12487. // order by w.create_time
  12488. 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];
  12489. // 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];
  12490. sqlite3_stmt * statement;
  12491. NSDate *date1 = [NSDate date];
  12492. // NSDate *date2 = nil;
  12493. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12494. int count=0;
  12495. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12496. {
  12497. while (sqlite3_step(statement) == SQLITE_ROW)
  12498. {
  12499. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12500. int product_id = sqlite3_column_double(statement, 0);
  12501. char *description = (char*)sqlite3_column_text(statement, 1);
  12502. if(description==nil)
  12503. description= "";
  12504. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12505. int item_id = sqlite3_column_double(statement, 2);
  12506. NSDate *date_image = [NSDate date];
  12507. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12508. printf("image : ");
  12509. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12510. // char *url = (char*)sqlite3_column_text(statement, 3);
  12511. // if(url==nil)
  12512. // url="";
  12513. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12514. int qty = sqlite3_column_int(statement, 3);
  12515. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12516. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12517. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12518. item[@"description"]= nsdescription;
  12519. item[@"img"]= nsurl;
  12520. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12521. count++;
  12522. }
  12523. printf("total time:");
  12524. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12525. ret[@"count"]= [NSNumber numberWithInt:count];
  12526. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12527. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12528. ret[@"result"]= [NSNumber numberWithInt:2];
  12529. sqlite3_finalize(statement);
  12530. }
  12531. [iSalesDB close_db:db];
  12532. dispatch_async(dispatch_get_main_queue(), ^{
  12533. UIApplication * app = [UIApplication sharedApplication];
  12534. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12535. appDelegate.wish_count =count;
  12536. [appDelegate update_count_mark];
  12537. result(ret);
  12538. });
  12539. });
  12540. }
  12541. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12542. {
  12543. // UIApplication * app = [UIApplication sharedApplication];
  12544. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12545. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12546. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12547. sqlite3 *db = [iSalesDB get_db];
  12548. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12549. NSString* product_id=params[@"product_id"];
  12550. NSString *item_count_str = params[@"item_count"];
  12551. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12552. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12553. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12554. // NSString *sql = @"";
  12555. for(int i=0;i<arr.count;i++)
  12556. {
  12557. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12558. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12559. __block int cart_count = 0;
  12560. if (!item_count_str) {
  12561. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12562. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12563. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12564. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12565. cart_count = [[model_set_components lastObject] intValue];
  12566. }];
  12567. }
  12568. if(count==0)
  12569. {
  12570. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12571. sqlite3_stmt *stmt;
  12572. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12573. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12574. if (item_count_arr) {
  12575. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12576. } else {
  12577. sqlite3_bind_int(stmt,2,cart_count);
  12578. }
  12579. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12580. [iSalesDB execSql:@"ROLLBACK" db:db];
  12581. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12582. [iSalesDB close_db:db];
  12583. DebugLog(@"add to wishlist error");
  12584. dispatch_async(dispatch_get_main_queue(), ^{
  12585. result(ret);
  12586. });
  12587. }
  12588. } else {
  12589. int qty = 0;
  12590. if (item_count_arr) {
  12591. qty = [item_count_arr[i] intValue];
  12592. } else {
  12593. qty = cart_count;
  12594. }
  12595. 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]];
  12596. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12597. [iSalesDB execSql:@"ROLLBACK" db:db];
  12598. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12599. [iSalesDB close_db:db];
  12600. DebugLog(@"add to wishlist error");
  12601. dispatch_async(dispatch_get_main_queue(), ^{
  12602. result(ret);
  12603. });
  12604. }
  12605. }
  12606. }
  12607. // [iSalesDB execSql:sql db:db];
  12608. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12609. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12610. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12611. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12612. [iSalesDB close_db:db];
  12613. ret[@"result"]= [NSNumber numberWithInt:2];
  12614. dispatch_async(dispatch_get_main_queue(), ^{
  12615. UIApplication * app = [UIApplication sharedApplication];
  12616. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12617. appDelegate.wish_count =count;
  12618. [appDelegate update_count_mark];
  12619. result(ret);
  12620. });
  12621. });
  12622. }
  12623. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12624. {
  12625. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12626. bool bLogin = appDelegate.bLogin;
  12627. __block NSString* contact_id = appDelegate.contact_id;
  12628. __block NSString* user = appDelegate.user;
  12629. __block NSString* order_code = appDelegate.order_code;
  12630. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12631. NSString* model_name = [params valueForKey:@"product_name"];
  12632. NSString* product_id = [params valueForKey:@"product_id"];
  12633. NSString* upc_code = [params valueForKey:@"upc_code"];
  12634. NSString* category = [params valueForKey:@"category"];
  12635. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12636. if(category==nil) {
  12637. category = default_category_id;
  12638. } else {
  12639. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12640. // 参数重有多个category id
  12641. if (arr_0.count > 1) {
  12642. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12643. // 取交集
  12644. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12645. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12646. [set_0 intersectSet:set_1];
  12647. if (set_0.count == 1) {
  12648. category = (NSString *)[set_0 anyObject];
  12649. } else {
  12650. if ([set_0 containsObject:default_category_id]) {
  12651. category = default_category_id;
  12652. } else {
  12653. category = (NSString *)[set_0 anyObject];
  12654. }
  12655. }
  12656. }
  12657. }
  12658. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12659. sqlite3 *db = [iSalesDB get_db];
  12660. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12661. NSString *sqlQuery = nil;
  12662. if(product_id==nil)
  12663. if(model_name==nil)
  12664. {
  12665. 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='%@';
  12666. }
  12667. else
  12668. {
  12669. 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='%@';
  12670. }
  12671. else
  12672. 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=%@;
  12673. sqlite3_stmt * statement;
  12674. [ret setValue:@"2" forKey:@"result"];
  12675. [ret setValue:@"3" forKey:@"detail_section_count"];
  12676. // int count=0;
  12677. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12678. {
  12679. if (sqlite3_step(statement) == SQLITE_ROW)
  12680. {
  12681. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12682. char *name = (char*)sqlite3_column_text(statement, 0);
  12683. if(name==nil)
  12684. name="";
  12685. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12686. char *description = (char*)sqlite3_column_text(statement, 1);
  12687. if(description==nil)
  12688. description="";
  12689. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12690. int product_id = sqlite3_column_int(statement, 2);
  12691. char *color = (char*)sqlite3_column_text(statement, 3);
  12692. if(color==nil)
  12693. color="";
  12694. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12695. //
  12696. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12697. // if(legcolor==nil)
  12698. // legcolor="";
  12699. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12700. //
  12701. //
  12702. int availability = sqlite3_column_int(statement, 5);
  12703. //
  12704. int incoming_stock = sqlite3_column_int(statement, 6);
  12705. char *demension = (char*)sqlite3_column_text(statement, 7);
  12706. if(demension==nil)
  12707. demension="";
  12708. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12709. // ,,,,,,,,,
  12710. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12711. if(seat_height==nil)
  12712. seat_height="";
  12713. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12714. char *material = (char*)sqlite3_column_text(statement, 9);
  12715. if(material==nil)
  12716. material="";
  12717. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12718. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12719. if(box_dim==nil)
  12720. box_dim="";
  12721. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12722. char *volume = (char*)sqlite3_column_text(statement, 11);
  12723. if(volume==nil)
  12724. volume="";
  12725. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12726. double weight = sqlite3_column_double(statement, 12);
  12727. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12728. if(model_set==nil)
  12729. model_set="";
  12730. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12731. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12732. if(load_ability==nil)
  12733. load_ability="";
  12734. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12735. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12736. // if(default_category==nil)
  12737. // default_category="";
  12738. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12739. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12740. if(fabric_content==nil)
  12741. fabric_content="";
  12742. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12743. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12744. if(assembling==nil)
  12745. assembling="";
  12746. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12747. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12748. if(made_in==nil)
  12749. made_in="";
  12750. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12751. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12752. if(special_remarks==nil)
  12753. special_remarks="";
  12754. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12755. int stockUcom = sqlite3_column_double(statement, 20);
  12756. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12757. if(product_group==nil)
  12758. product_group="";
  12759. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12760. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12761. // if(fashion_selector==nil)
  12762. // fashion_selector="";
  12763. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12764. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12765. if(selector_field==nil)
  12766. selector_field="";
  12767. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12768. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12769. if(property_field==nil)
  12770. property_field="";
  12771. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12772. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12773. if(packaging==nil)
  12774. packaging="";
  12775. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12776. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12777. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12778. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12779. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12780. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12781. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12782. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12783. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12784. NSString* Availability=nil;
  12785. if(availability>0)
  12786. Availability=[NSString stringWithFormat:@"%d",availability];
  12787. else
  12788. Availability = @"Out of Stock";
  12789. [img_section setValue:Availability forKey:@"Availability"];
  12790. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12791. char *eta = (char*)sqlite3_column_text(statement, 25);
  12792. if(eta==nil)
  12793. eta="";
  12794. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12795. if ([nseta isEqualToString:@"null"]) {
  12796. nseta = @"";
  12797. }
  12798. if (availability <= 0) {
  12799. [img_section setValue:nseta forKey:@"ETA"];
  12800. }
  12801. int item_id = sqlite3_column_int(statement, 26);
  12802. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12803. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12804. NSString* Price=nil;
  12805. if(bLogin==false)
  12806. Price=@"Must Sign in.";
  12807. else
  12808. {
  12809. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12810. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12811. if(price==nil)
  12812. Price=@"No Price.";
  12813. else
  12814. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12815. }
  12816. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12817. [img_section setValue:Price forKey:@"price"];
  12818. [img_section setValue:nsname forKey:@"model_name"];
  12819. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12820. if (order_code) { // 离线order code即so#
  12821. 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];
  12822. __block int cartQTY = 0;
  12823. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12824. cartQTY = sqlite3_column_int(stmt, 0);
  12825. }];
  12826. if (cartQTY > 0) {
  12827. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12828. }
  12829. }
  12830. [ret setObject:img_section forKey:@"img_section"];
  12831. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12832. int detail0_item_count=0;
  12833. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12834. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12835. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12836. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12837. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12838. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12839. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12840. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12841. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12842. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12843. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12844. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12845. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12846. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12847. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12848. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12849. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12850. {
  12851. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12852. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12853. }
  12854. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12855. [detail0_section setValue:@"kv" forKey:@"type"];
  12856. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12857. [ret setObject:detail0_section forKey:@"detail_0"];
  12858. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12859. // [detail1_section setValue:@"detail" forKey:@"target"];
  12860. // [detail1_section setValue:@"popup" forKey:@"action"];
  12861. // [detail1_section setValue:@"content" forKey:@"type"];
  12862. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12863. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12864. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12865. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12866. [ret setObject:detail1_section forKey:@"detail_1"];
  12867. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12868. [detail2_section setValue:@"detail" forKey:@"target"];
  12869. [detail2_section setValue:@"popup" forKey:@"action"];
  12870. [detail2_section setValue:@"content" forKey:@"type"];
  12871. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12872. [detail2_section setValue:@"true" forKey:@"single_row"];
  12873. [detail2_section setValue:@"local" forKey:@"data"];
  12874. [ret setObject:detail2_section forKey:@"detail_2"];
  12875. }
  12876. sqlite3_finalize(statement);
  12877. }
  12878. else
  12879. {
  12880. [ret setValue:@"8" forKey:@"result"];
  12881. }
  12882. // DebugLog(@"count:%d",count);
  12883. [iSalesDB close_db:db];
  12884. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12885. dispatch_async(dispatch_get_main_queue(), ^{
  12886. result(ret);
  12887. });
  12888. });
  12889. }
  12890. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12891. //{
  12892. //
  12893. // UIApplication * app = [UIApplication sharedApplication];
  12894. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12895. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12896. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12897. // sqlite3 *db = [iSalesDB get_db];
  12898. // NSString* collectId=params[@"collectId"];
  12899. //
  12900. //
  12901. //
  12902. //
  12903. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12904. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12905. // [iSalesDB execSql:sqlQuery db:db];
  12906. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12907. // [iSalesDB close_db:db];
  12908. //
  12909. //
  12910. // ret[@"result"]= [NSNumber numberWithInt:2];
  12911. // dispatch_async(dispatch_get_main_queue(), ^{
  12912. //
  12913. // appDelegate.wish_count =count;
  12914. //
  12915. // [appDelegate update_count_mark];
  12916. // result(ret);
  12917. // });
  12918. //
  12919. // });
  12920. //}
  12921. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12922. {
  12923. UIApplication * app = [UIApplication sharedApplication];
  12924. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12925. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12926. [iSalesDB disable_trigger];
  12927. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12928. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12929. [iSalesDB enable_trigger];
  12930. dispatch_async(dispatch_get_main_queue(), ^{
  12931. //
  12932. // NSString* user = [params valueForKey:@"user"];
  12933. //
  12934. // NSString* password = [params valueForKey:@"password"];
  12935. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12936. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12937. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12938. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12939. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12940. [appDelegate update_count_mark];
  12941. appDelegate.can_show_price =false;
  12942. appDelegate.can_see_price =false;
  12943. appDelegate.can_create_portfolio =false;
  12944. appDelegate.can_create_order =false;
  12945. appDelegate.can_cancel_order =false;
  12946. appDelegate.can_set_cart_price =false;
  12947. appDelegate.can_delete_order =false;
  12948. appDelegate.can_submit_order =false;
  12949. appDelegate.can_set_tearsheet_price =false;
  12950. appDelegate.can_update_contact_info = false;
  12951. appDelegate.save_order_logout = false;
  12952. appDelegate.submit_order_logout = false;
  12953. appDelegate.alert_sold_in_quantities = false;
  12954. appDelegate.ipad_perm =nil ;
  12955. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12956. appDelegate.OrderFilter= nil;
  12957. [appDelegate SetSo:nil];
  12958. [appDelegate set_main_button_panel];
  12959. result(ret);
  12960. });
  12961. });
  12962. }
  12963. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12964. {
  12965. // iSalesDB.
  12966. // UIApplication * app = [UIApplication sharedApplication];
  12967. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12968. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12969. [iSalesDB disable_trigger];
  12970. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12971. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12972. [iSalesDB enable_trigger];
  12973. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12974. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12975. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12976. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12977. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12978. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12979. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12980. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12981. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12982. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12983. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12984. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12985. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12986. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12987. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12988. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12989. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12990. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12991. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12992. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12993. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12994. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12995. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12996. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12997. [arr_name addObject:customer_first_name];
  12998. [arr_name addObject:customer_last_name];
  12999. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  13000. // default ship from
  13001. 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';";
  13002. __block NSString *cid = @"";
  13003. __block NSString *name = @"";
  13004. __block NSString *ext = @"";
  13005. __block NSString *contact = @"";
  13006. __block NSString *email = @"";
  13007. __block NSString *fax = @"";
  13008. __block NSString *phone = @"";
  13009. sqlite3 *db = [iSalesDB get_db];
  13010. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13011. cid = [self textAtColumn:0 statement:statment];
  13012. name = [self textAtColumn:1 statement:statment];
  13013. ext = [self textAtColumn:2 statement:statment];
  13014. contact = [self textAtColumn:3 statement:statment];
  13015. email = [self textAtColumn:4 statement:statment];
  13016. fax = [self textAtColumn:5 statement:statment];
  13017. phone = [self textAtColumn:6 statement:statment];
  13018. }];
  13019. // default carrier
  13020. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  13021. __block NSString *carrier = @"";
  13022. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13023. carrier = [self textAtColumn:0 statement:statment];
  13024. }];
  13025. NSString* so_id = [self get_offline_soid:db];
  13026. if(so_id==nil)
  13027. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  13028. 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];
  13029. int result_code =[iSalesDB execSql:sql_neworder db:db];
  13030. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  13031. //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'
  13032. //soId
  13033. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  13034. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  13035. sqlite3_stmt * statement;
  13036. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  13037. {
  13038. if (sqlite3_step(statement) == SQLITE_ROW)
  13039. {
  13040. // char *name = (char*)sqlite3_column_text(statement, 1);
  13041. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  13042. //ret = sqlite3_column_int(statement, 0);
  13043. char *soId = (char*)sqlite3_column_text(statement, 0);
  13044. if(soId==nil)
  13045. soId="";
  13046. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  13047. [ret setValue:nssoId forKey:@"soId"];
  13048. [ret setValue:nssoId forKey:@"orderCode"];
  13049. }
  13050. sqlite3_finalize(statement);
  13051. }
  13052. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  13053. [ret setValue:@"Regular Mode" forKey:@"mode"];
  13054. [iSalesDB close_db:db];
  13055. dispatch_async(dispatch_get_main_queue(), ^{
  13056. result(ret);
  13057. });
  13058. });
  13059. }
  13060. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  13061. {
  13062. UIApplication * app = [UIApplication sharedApplication];
  13063. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  13064. bool can_create_backorder= appDelegate.can_create_backorder;
  13065. NSString* user = appDelegate.user;
  13066. NSString* contact_id = appDelegate.contact_id;
  13067. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  13068. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  13069. sqlite3 *db = [iSalesDB get_db];
  13070. NSString* orderCode=params[@"orderCode"];
  13071. int sort = [[params objectForKey:@"sort"] intValue];
  13072. NSString *sort_str = @"";
  13073. switch (sort) {
  13074. case 0:{
  13075. sort_str = @"order by c.modify_time desc";
  13076. }
  13077. break;
  13078. case 1:{
  13079. sort_str = @"order by c.modify_time asc";
  13080. }
  13081. break;
  13082. case 2:{
  13083. sort_str = @"order by m.name asc";
  13084. }
  13085. break;
  13086. case 3:{
  13087. sort_str = @"order by m.name desc";
  13088. }
  13089. break;
  13090. case 4:{
  13091. sort_str = @"order by m.description asc";
  13092. }
  13093. break;
  13094. default:
  13095. break;
  13096. }
  13097. 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 ];
  13098. // 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 ];
  13099. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  13100. DebugLog(@"offline_login sql:%@",sqlQuery);
  13101. sqlite3_stmt * statement;
  13102. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  13103. NSDate *date1 = [NSDate date];
  13104. int count=0;
  13105. int cart_count=0;
  13106. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  13107. if ( dbresult== SQLITE_OK)
  13108. {
  13109. while (sqlite3_step(statement) == SQLITE_ROW)
  13110. {
  13111. // NSDate *row_date = [NSDate date];
  13112. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13113. int product_id = sqlite3_column_int(statement, 0);
  13114. char *str_price = (char*)sqlite3_column_text(statement, 1);
  13115. int item_id = sqlite3_column_int(statement, 7);
  13116. NSString* Price=nil;
  13117. if(str_price==nil)
  13118. {
  13119. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  13120. // NSDate *price_date = [NSDate date];
  13121. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  13122. // DebugLog(@"price time interval");
  13123. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  13124. if(price==nil)
  13125. Price=@"No Price.";
  13126. else
  13127. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  13128. }
  13129. else
  13130. {
  13131. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  13132. }
  13133. double discount = sqlite3_column_double(statement, 2);
  13134. int item_count = sqlite3_column_int(statement, 3);
  13135. char *line_note = (char*)sqlite3_column_text(statement, 4);
  13136. NSString *nsline_note=nil;
  13137. if(line_note!=nil)
  13138. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  13139. char *name = (char*)sqlite3_column_text(statement, 5);
  13140. NSString *nsname=nil;
  13141. if(name!=nil)
  13142. nsname= [[NSString alloc]initWithUTF8String:name];
  13143. char *description = (char*)sqlite3_column_text(statement, 6);
  13144. NSString *nsdescription=nil;
  13145. if(description!=nil)
  13146. nsdescription= [[NSString alloc]initWithUTF8String:description];
  13147. int stockUom = sqlite3_column_int(statement, 8);
  13148. int _id = sqlite3_column_int(statement, 9);
  13149. int availability = sqlite3_column_int(statement, 10);
  13150. // NSDate *subtotal_date = [NSDate date];
  13151. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  13152. // DebugLog(@"subtotal_date time interval");
  13153. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  13154. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  13155. double weight=[bsubtotaljson[@"weight"] doubleValue];
  13156. int carton=[bsubtotaljson[@"carton"] intValue];
  13157. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  13158. int mpack =[bsubtotaljson[@"mpack"] intValue];
  13159. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  13160. // NSDate *img_date = [NSDate date];
  13161. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  13162. // DebugLog(@"img_date time interval");
  13163. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  13164. itemjson[@"model"]=nsname;
  13165. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  13166. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  13167. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  13168. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  13169. itemjson[@"check"]=@"true";
  13170. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  13171. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  13172. itemjson[@"unit_price"]=Price;
  13173. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  13174. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  13175. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  13176. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  13177. itemjson[@"note"]=nsline_note;
  13178. if (!can_create_backorder) {
  13179. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  13180. }
  13181. // NSDate *date2 = [NSDate date];
  13182. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  13183. // DebugLog(@"model_bundle time interval");
  13184. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  13185. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  13186. count++;
  13187. // DebugLog(@"row time interval");
  13188. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  13189. }
  13190. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  13191. sqlite3_finalize(statement);
  13192. }
  13193. DebugLog(@"request cart total time interval");
  13194. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  13195. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  13196. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  13197. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  13198. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  13199. ret[@"count"]=[NSNumber numberWithInt:count ];
  13200. ret[@"mode"]=@"Regular Mode";
  13201. [iSalesDB close_db:db];
  13202. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  13203. DebugLog(@"general notes :%@",general_note);
  13204. ret[@"general_note"]= general_note;
  13205. dispatch_async(dispatch_get_main_queue(), ^{
  13206. result(ret);
  13207. });
  13208. });
  13209. }
  13210. @end