OLDataProvider.m 722 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168
  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*) getServerPath
  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* serverpath = [documents stringByAppendingPathComponent:servername];
  1887. // userpath = [userpath stringByAppendingPathComponent:user];
  1888. return serverpath;
  1889. }
  1890. +(NSString*) getUserPath
  1891. {
  1892. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1893. NSString *documents = [paths objectAtIndex:0];
  1894. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1895. NSString* servername = addressDic[@"name"];
  1896. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1897. // NSString* user = appDelegate.user;
  1898. NSString* user = RASingleton.sharedInstance.user;
  1899. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1900. userpath = [userpath stringByAppendingPathComponent:user];
  1901. return userpath;
  1902. }
  1903. +(bool) isSavedScanOrder:(NSString*) order_code
  1904. {
  1905. if(order_code == nil)
  1906. return false;
  1907. NSMutableDictionary * orderlist=[self getScanOrderList];
  1908. int count = [orderlist[@"count"] intValue];
  1909. for(int i=0;i<count;i++)
  1910. {
  1911. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1912. if([item[@"order_code"] isEqualToString: order_code ])
  1913. {
  1914. return true;
  1915. }
  1916. }
  1917. return false;
  1918. }
  1919. +(NSString*) getScanPath
  1920. {
  1921. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1922. if(appDelegate.order_code==nil)
  1923. {
  1924. appDelegate.order_code=[[NSUUID new] UUIDString];
  1925. }
  1926. return [self getScanOrderPath];
  1927. // else
  1928. // {
  1929. // [self createScanTempFolder];
  1930. // return [self getScanTempPath];
  1931. // }
  1932. }
  1933. +(void) scanCloseOrder
  1934. {
  1935. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1936. // if(appDelegate.user.length<=0)
  1937. // {
  1938. // [RAUtils message_alert:@"Please login first." title:@"Warring" controller:self];
  1939. // return;
  1940. // }
  1941. // if(appDelegate.order_code.length>0)
  1942. {
  1943. // 当前订单先关闭
  1944. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1945. appDelegate.order_code = nil;
  1946. // RASingleton.sharedInstance.scan_temp_code = nil;
  1947. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1948. { appDelegate.customerInfo = nil;
  1949. appDelegate.contact_id = nil;
  1950. }
  1951. RASingleton.sharedInstance.scan_cart = nil;
  1952. RASingleton.sharedInstance.price_type = 1;
  1953. // [appDelegate updateScanButton:false];
  1954. [appDelegate update_count_mark];
  1955. [appDelegate closeOrder];
  1956. }
  1957. }
  1958. +(NSString*) createScanTempCode
  1959. {
  1960. return [[NSUUID new] UUIDString];
  1961. }
  1962. +(NSString*) scanTemplatePath:(NSString*)file
  1963. {
  1964. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1965. NSString *documents = [paths objectAtIndex:0];
  1966. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1967. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1968. return templatefile;
  1969. }
  1970. +(void) realguestLogin
  1971. {
  1972. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1973. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1974. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1975. }
  1976. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1977. {
  1978. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1979. NSString *documents = [paths objectAtIndex:0];
  1980. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1981. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1982. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1983. return [dict mutableCopy];
  1984. }
  1985. +(void) deleteSubmittedOrder:(NSString*) order_code
  1986. {
  1987. NSString *userdir = [OLDataProvider getUserPath];
  1988. if(order_code.length==0)
  1989. return;
  1990. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1991. // NSString *documents = [paths objectAtIndex:0];
  1992. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1993. // if([appDelegate.order_code isEqualToString:order_code])
  1994. // {
  1995. // //要删除的订单是当前打开的订单;
  1996. // appDelegate.order_code = nil;
  1997. // appDelegate.customerInfo = nil;
  1998. // RASingleton.sharedInstance.scan_cart = nil;
  1999. // appDelegate.contact_id = nil;
  2000. // // [appDelegate updateScanButton:false];
  2001. //
  2002. // [appDelegate update_count_mark];
  2003. // }
  2004. //删除订单目录
  2005. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2006. //维护订单列表
  2007. NSMutableDictionary* submitlist = [self getScanSubmitList];
  2008. int count = [submitlist[@"count"] intValue];
  2009. if(count==0)
  2010. {
  2011. // order list 为空,不用维护
  2012. #ifdef RA_NOTIFICATION
  2013. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2014. #else
  2015. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2016. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2017. #endif
  2018. return;
  2019. }
  2020. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  2021. //
  2022. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2023. //
  2024. //
  2025. //
  2026. // // [orderlist addObject:appDelegate.order_code];
  2027. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2028. //
  2029. // }
  2030. // else
  2031. // {
  2032. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2033. //
  2034. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2035. //
  2036. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2037. // }
  2038. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2039. int newcount = 0;
  2040. for(int i=0;i<count;i++)
  2041. {
  2042. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  2043. if([item[@"order_code"] isEqualToString: order_code ])
  2044. continue;
  2045. else
  2046. {
  2047. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2048. newcount++;
  2049. }
  2050. }
  2051. newlist[@"count"] = @(newcount);
  2052. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2053. [self saveScanSubmitList:newlist];
  2054. #ifdef RA_NOTIFICATION
  2055. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2056. #else
  2057. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2058. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2059. #endif
  2060. }
  2061. +(void) updateScanOrder:(NSString*) order_code
  2062. {
  2063. #ifdef SCANNER_ORDER
  2064. NSString *userdir = [OLDataProvider getUserPath];
  2065. if(order_code.length==0)
  2066. return;
  2067. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2068. // NSString *documents = [paths objectAtIndex:0];
  2069. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2070. NSMutableDictionary* orderlist = [self getScanOrderList];
  2071. int count = [orderlist[@"count"] intValue];
  2072. if(count==0)
  2073. {
  2074. // order list 为空,不用维护
  2075. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2076. return;
  2077. }
  2078. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2079. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2080. //
  2081. //
  2082. //
  2083. // // [orderlist addObject:appDelegate.order_code];
  2084. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2085. //
  2086. // }
  2087. // else
  2088. // {
  2089. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2090. //
  2091. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2092. //
  2093. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2094. // }
  2095. //
  2096. //
  2097. // int count = [orderlist[@"count"] intValue];
  2098. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  2099. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2100. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2101. float p =0;
  2102. float s =0;
  2103. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  2104. {
  2105. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  2106. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  2107. int mcount = [cartitem[@"count"] intValue];
  2108. double msurcharge =0;
  2109. // remove net price
  2110. // if(RASingleton.sharedInstance.price_type==1)
  2111. // {
  2112. // if([cartitem[@"special_price"] boolValue])
  2113. // {
  2114. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2115. // }
  2116. // else
  2117. // {
  2118. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2119. // }
  2120. // }
  2121. s+=msurcharge;
  2122. }
  2123. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  2124. int order_qty=0;
  2125. for(int c=0;c<[section[@"count"] intValue];c++)
  2126. {
  2127. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  2128. order_qty+=[item[@"count"] intValue];
  2129. }
  2130. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  2131. // int newcount = 0;
  2132. for(int i=0;i<count;i++)
  2133. {
  2134. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2135. if([item[@"order_code"] isEqualToString: order_code ])
  2136. {
  2137. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  2138. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  2139. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  2140. }
  2141. }
  2142. // newlist[@"count"] = @(newcount);
  2143. [self saveScanOrderList:orderlist];
  2144. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2145. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2146. #endif
  2147. }
  2148. +(void) deleteScanOrder:(NSString*) order_code
  2149. {
  2150. NSString *userdir = [OLDataProvider getUserPath];
  2151. if(order_code.length==0)
  2152. return;
  2153. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2154. // NSString *documents = [paths objectAtIndex:0];
  2155. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2156. if([appDelegate.order_code isEqualToString:order_code])
  2157. {
  2158. //要删除的订单是当前打开的订单;
  2159. // appDelegate.order_code = nil;
  2160. // appDelegate.customerInfo = nil;
  2161. // RASingleton.sharedInstance.scan_cart = nil;
  2162. // appDelegate.contact_id = nil;
  2163. [self scanCloseOrder];
  2164. // [appDelegate updateScanButton:false];
  2165. [appDelegate update_count_mark];
  2166. }
  2167. //删除订单目录
  2168. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2169. //维护订单列表
  2170. NSMutableDictionary* orderlist = [self getScanOrderList];
  2171. int count = [orderlist[@"count"] intValue];
  2172. if(count==0)
  2173. {
  2174. // order list 为空,不用维护
  2175. #ifdef RA_NOTIFICATION
  2176. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2177. #else
  2178. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2179. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2180. #endif
  2181. return;
  2182. }
  2183. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2184. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2185. //
  2186. //
  2187. //
  2188. // // [orderlist addObject:appDelegate.order_code];
  2189. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2190. //
  2191. // }
  2192. // else
  2193. // {
  2194. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2195. //
  2196. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2197. //
  2198. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2199. // }
  2200. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2201. int newcount = 0;
  2202. for(int i=0;i<count;i++)
  2203. {
  2204. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2205. if([item[@"order_code"] isEqualToString: order_code ])
  2206. continue;
  2207. else
  2208. {
  2209. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2210. newcount++;
  2211. }
  2212. }
  2213. newlist[@"count"] = @(newcount);
  2214. [self saveScanOrderList:newlist];
  2215. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2216. #ifdef RA_NOTIFICATION
  2217. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2218. #else
  2219. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2220. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2221. #endif
  2222. }
  2223. #ifdef SCANNER_ORDER
  2224. +(void) scanRefreshCart
  2225. {
  2226. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2227. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2228. NSMutableDictionary * section = cart[@"section_0"];
  2229. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2230. int count = [section[@"count"] intValue];
  2231. int newcount = 0;
  2232. for(int i=0;i<count;i++)
  2233. {
  2234. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2235. NSString* model = item[@"model"];
  2236. NSDictionary* modeljson=appDelegate.scan_model[model];
  2237. if(modeljson==nil)
  2238. continue;
  2239. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2240. cartitem[@"note"]=item[@"note"];
  2241. cartitem[@"discount"]=item[@"discount"];
  2242. // [item removeObjectForKey:@"discount"];
  2243. // [item removeObjectForKey:@"note"];
  2244. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2245. newcount++;
  2246. }
  2247. newsection[@"count"]=@(newcount);
  2248. newsection[@"available"] = @(1);
  2249. newsection[@"title"] = @"Available List";
  2250. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2251. }
  2252. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2253. {
  2254. // _modelJson = modelJson;
  2255. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2256. // self.labelPrice0.text = appDelegate.price0_name;
  2257. // self.labelPrice1.text = appDelegate.price1_name;
  2258. // self.labelPrice2.text = appDelegate.price2_name;
  2259. // self.labelPrice3.text = appDelegate.price3_name;
  2260. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2261. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2262. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2263. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2264. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2265. NSLog( [RAConvertor dict2string:_modelJson]);
  2266. NSString * port=_modelJson[@"port"];
  2267. if(port.length==0)
  2268. port = @"N/A";
  2269. NSString * origin=_modelJson[@"origin"];
  2270. if(origin.length==0)
  2271. origin = @"N/A";
  2272. NSString * dimension=_modelJson[@"dimension"];
  2273. if(dimension.length==0)
  2274. dimension = @"N/A";
  2275. // _labelModel.text=_modelJson[@"model"];
  2276. // _labelDescription.text=_modelJson[@"description"];
  2277. // _labelDimension.text=dimension;
  2278. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2279. // _labelOrigin.text=origin;
  2280. // _labelPort.text=port;
  2281. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2282. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2283. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2284. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2285. if(![price0 isEqualToString: @"N/A"])
  2286. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2287. if(![price1 isEqualToString: @"N/A"])
  2288. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2289. if(![price2 isEqualToString: @"N/A"])
  2290. {
  2291. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2292. // if(RASingleton.sharedInstance.price_type==1)
  2293. _modelJson [@"special_price"] = @true;
  2294. }
  2295. if(![price3 isEqualToString: @"N/A"])
  2296. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2297. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2298. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2299. {
  2300. // if(RASingleton.sharedInstance.price_type==1)
  2301. _modelJson [@"net_price"] = @true;
  2302. // remove net price
  2303. _modelJson [@"net_price"] = @false;
  2304. }
  2305. //
  2306. // if(![price1 isEqualToString:@"N/A"])
  2307. // {
  2308. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2309. // _modelJson[@"price2"]= price2;
  2310. // }
  2311. // else
  2312. // {
  2313. // price2=@"N/A";
  2314. // }
  2315. //
  2316. // if([price0 isEqualToString:@"N/A"])
  2317. // _labelPriceCTNR.text= @"";//price0;
  2318. // else
  2319. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2320. //
  2321. // if([price3 isEqualToString:@"N/A"])
  2322. // _labelPriceNCA.text= price3;
  2323. // else
  2324. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2325. // _labelPrice25p.text=price2;
  2326. //
  2327. // if([_modelJson [@"special_price"] boolValue])
  2328. // {
  2329. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2330. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2331. // else
  2332. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2333. // }
  2334. // else
  2335. // {
  2336. // _labelPriceSpecial.text=@"";//@"N/A";
  2337. // }
  2338. //
  2339. // if([_modelJson [@"net_price"] boolValue])
  2340. // {
  2341. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2342. //// {
  2343. //// _labelPriceNet.text=_modelJson[@"price1"];
  2344. ////// _labelPriceNCA.text=@"N/A";
  2345. //// }
  2346. //// else
  2347. //// {
  2348. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2349. ////// _labelPriceNCA.text=@"N/A";
  2350. //// }
  2351. //
  2352. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2353. // {
  2354. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2355. //// _labelPriceNCA.text=@"N/A";
  2356. // }
  2357. // else
  2358. // {
  2359. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2360. //// _labelPriceNCA.text=@"N/A";
  2361. // }
  2362. //
  2363. // }
  2364. // else
  2365. // {
  2366. // _labelPriceNet.text=@"";//@"N/A";
  2367. // }
  2368. //
  2369. // {
  2370. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2371. // {
  2372. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2373. //// _labelPriceNCA.text=@"N/A";
  2374. // }
  2375. // else
  2376. // {
  2377. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2378. //// _labelPriceNet.text=@"N/A";
  2379. // }
  2380. // }
  2381. NSString * available=_modelJson[@"available"];
  2382. if(available.length==0)
  2383. available = @"N/A";
  2384. // _labelAvailable.text=available;
  2385. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2386. if(unit_cuft.length==0)
  2387. unit_cuft = @"N/A";
  2388. else
  2389. {
  2390. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2391. }
  2392. NSString * cell_price;
  2393. if(RASingleton.sharedInstance.price_type==1)
  2394. {
  2395. // if(_modelJson [@"net_price"])
  2396. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2397. // else if(_modelJson [@"special_price"])
  2398. // {
  2399. // unit_price= _modelJson[@"price2"];
  2400. // }
  2401. // else
  2402. // {
  2403. // unit_price= _modelJson[@"price1"];
  2404. // }
  2405. }
  2406. else
  2407. {
  2408. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2409. }
  2410. NSString * mpack=_modelJson[@"stockUom"];
  2411. if([mpack isEqualToString:@"N/A"])
  2412. mpack=@"1";
  2413. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2414. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2415. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2416. NSMutableDictionary* jitem = nil;
  2417. jitem = [_modelJson mutableCopy];
  2418. int stockUom =[jitem[@"stockUom"] intValue];
  2419. if(stockUom==0)
  2420. stockUom=1;
  2421. bool newitem = true;
  2422. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2423. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2424. // for(int i=0;i<count;i++)
  2425. // {
  2426. //
  2427. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2428. //
  2429. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2430. // {
  2431. //
  2432. //// int oldcount = [litem[@"stockUom"] intValue];
  2433. // newitem = false;
  2434. //
  2435. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2436. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2437. //
  2438. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2439. // break;
  2440. //
  2441. //
  2442. // }
  2443. //
  2444. //
  2445. //
  2446. // }
  2447. // if(true)
  2448. // {
  2449. jitem[@"count"]=@(stockUom);
  2450. jitem[@"check"]=@(true);
  2451. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2452. NSString * unit_price;
  2453. if(RASingleton.sharedInstance.price_type==0)
  2454. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2455. else if(_modelJson [@"special_price"])
  2456. {
  2457. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2458. }
  2459. else if(_modelJson [@"net_price"])
  2460. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2461. else
  2462. {
  2463. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2464. }
  2465. if([unit_price isEqualToString:@"N/A"])
  2466. unit_price = @"0";
  2467. else
  2468. {
  2469. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2470. }
  2471. jitem[@"unit_price"] = unit_price;
  2472. jitem[@"erp_unit_price"] = unit_price;
  2473. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2474. // section[@"count"]= @(count+1);
  2475. // count++;
  2476. // }
  2477. return jitem;
  2478. }
  2479. +(void) saveScanCart:(NSMutableDictionary*) cart
  2480. {
  2481. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2482. if(appDelegate.order_code.length<=0)
  2483. return;
  2484. if(cart==nil)
  2485. return;
  2486. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2487. // NSString *documents = [paths objectAtIndex:0];
  2488. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2489. if(RASingleton.sharedInstance.price_type==0)
  2490. {
  2491. NSMutableDictionary * section = cart[@"section_0"];
  2492. int count = [section[@"count"] intValue];
  2493. for(int i=0;i<count;i++)
  2494. {
  2495. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2496. [item removeObjectForKey:@"discount"];
  2497. [item removeObjectForKey:@"note"];
  2498. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2499. }
  2500. cart[@"section_0"]=section;
  2501. }
  2502. NSString *orderdir = [self getScanPath];
  2503. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2504. cart[@"price_group"] = appDelegate.price_group;
  2505. [RAUtils dicttofile:cartpath dict:cart];
  2506. }
  2507. #endif
  2508. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2509. {
  2510. assert(add_params[@"user"]!=nil);
  2511. // assert(add_params[@"contact_id"]!=nil);
  2512. // assert(add_params[@"password"]!=nil);
  2513. NSString* serial= [[NSUUID UUID] UUIDString];
  2514. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2515. NSString *cachefolder = [paths objectAtIndex:0];
  2516. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2517. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2518. NSFileManager* fileManager = [NSFileManager defaultManager];
  2519. BOOL bdir=YES;
  2520. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2521. {
  2522. NSError *error = nil;
  2523. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2524. if(!bsuccess)
  2525. DebugLog(@"Create UPLOAD folder failed");
  2526. }
  2527. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2528. ret[@"contact"]=[self prepareContact:serial];
  2529. ret[@"wishlist"]=[self prepareWishlist:serial];
  2530. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2531. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2532. ret[@"view_portfolio"] = [self preparePDF:serial];
  2533. NSString* str= [RAConvertor dict2string:ret];
  2534. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2535. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2536. NSError *error=nil;
  2537. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2538. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2539. ZipArchive* zip = [[ZipArchive alloc] init];
  2540. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2541. for(int i=0;i<arr_files.count;i++)
  2542. {
  2543. NSString* file=arr_files[i];
  2544. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2545. }
  2546. if( ![zip CloseZipFile2] )
  2547. {
  2548. zippath = @"";
  2549. }
  2550. ret[@"file"]=zippath;
  2551. return ret;
  2552. }
  2553. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2554. {
  2555. if(filename.length==0)
  2556. return false;
  2557. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2558. bool ret=false;
  2559. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2560. NSString *cachefolder = [paths objectAtIndex:0];
  2561. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2562. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2563. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2564. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2565. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2566. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2567. NSFileManager* fileManager = [NSFileManager defaultManager];
  2568. BOOL bdir=NO;
  2569. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2570. {
  2571. NSError *error = nil;
  2572. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2573. {
  2574. ret=false;
  2575. }
  2576. else
  2577. {
  2578. ret=true;
  2579. }
  2580. }
  2581. return ret;
  2582. }
  2583. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2584. {
  2585. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2586. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2587. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2588. NSString* where=@" 1=1";
  2589. if (ver!=nil) {
  2590. where=@"is_dirty=1";
  2591. }
  2592. sqlite3 *db = [iSalesDB get_db];
  2593. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2594. sqlite3_stmt * statement;
  2595. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2596. int count=0;
  2597. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2598. if ( dbresult== SQLITE_OK)
  2599. {
  2600. while (sqlite3_step(statement) == SQLITE_ROW)
  2601. {
  2602. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2603. int _id = sqlite3_column_int(statement, 0);
  2604. int product_id = sqlite3_column_int(statement, 1);
  2605. int item_id = sqlite3_column_int(statement, 2);
  2606. int qty = sqlite3_column_int(statement, 3);
  2607. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2608. NSString* nscreate_time=nil;
  2609. if(create_time!=nil)
  2610. {
  2611. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2612. }
  2613. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2614. NSString* nsmodify_time=nil;
  2615. if(modify_time!=nil)
  2616. {
  2617. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2618. }
  2619. int is_delete = sqlite3_column_int(statement, 6);
  2620. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2621. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2622. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2623. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2624. itemjson[@"create_time"]=nscreate_time;
  2625. itemjson[@"modify_time"]=nsmodify_time;
  2626. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2627. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2628. count++;
  2629. }
  2630. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2631. sqlite3_finalize(statement);
  2632. }
  2633. ret[@"count"]=[NSNumber numberWithInt:count ];
  2634. [iSalesDB close_db:db];
  2635. return ret;
  2636. }
  2637. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2638. {
  2639. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2640. // UIApplication * app = [UIApplication sharedApplication];
  2641. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2642. assert(add_params[@"user"]!=nil);
  2643. // assert(add_params[@"password"]!=nil);
  2644. 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 ];
  2645. // 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 ];
  2646. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2647. DebugLog(@"offline_login sql:%@",sqlQuery);
  2648. sqlite3_stmt * statement;
  2649. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2650. int count=0;
  2651. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2652. if ( dbresult== SQLITE_OK)
  2653. {
  2654. while (sqlite3_step(statement) == SQLITE_ROW)
  2655. {
  2656. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2657. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2658. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2659. int product_id = sqlite3_column_int(statement, 0);
  2660. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2661. int item_id = sqlite3_column_int(statement, 7);
  2662. NSString* Price=nil;
  2663. if(str_price==nil)
  2664. {
  2665. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2666. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2667. if(price==nil)
  2668. Price=@"No Price.";
  2669. else
  2670. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2671. }
  2672. else
  2673. {
  2674. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2675. }
  2676. double discount = sqlite3_column_double(statement, 2);
  2677. int item_count = sqlite3_column_int(statement, 3);
  2678. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2679. NSString *nsline_note=nil;
  2680. if(line_note!=nil)
  2681. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2682. char *name = (char*)sqlite3_column_text(statement, 5);
  2683. NSString *nsname=nil;
  2684. if(name!=nil)
  2685. nsname= [[NSString alloc]initWithUTF8String:name];
  2686. char *description = (char*)sqlite3_column_text(statement, 6);
  2687. NSString *nsdescription=nil;
  2688. if(description!=nil)
  2689. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2690. // int stockUom = sqlite3_column_int(statement, 8);
  2691. // int _id = sqlite3_column_int(statement, 9);
  2692. //
  2693. //
  2694. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2695. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2696. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2697. // int carton=[bsubtotaljson[@"carton"] intValue];
  2698. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2699. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2700. itemjson[@"model"]=nsname;
  2701. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2702. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2703. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2704. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2705. // itemjson[@"check"]=@"true";
  2706. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2707. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2708. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2709. itemjson[@"unit_price"]=Price;
  2710. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2711. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2712. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2713. itemjson[@"note"]=nsline_note;
  2714. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2715. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2716. count++;
  2717. }
  2718. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2719. sqlite3_finalize(statement);
  2720. }
  2721. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2722. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2723. return ret;
  2724. }
  2725. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2726. {
  2727. assert(add_params[@"user"]!=nil);
  2728. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2729. sqlite3 *db = [iSalesDB get_db];
  2730. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2731. // for(int i=0;i<arr_soid.count;i++)
  2732. sqlite3_stmt * statement;
  2733. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2734. //int count=0;
  2735. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2736. if ( dbresult== SQLITE_OK)
  2737. {
  2738. int count=0;
  2739. while (sqlite3_step(statement) == SQLITE_ROW)
  2740. {
  2741. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2742. int _id = sqlite3_column_int(statement, 0);
  2743. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2744. NSString* nssync_data=nil;
  2745. if(sync_data!=nil)
  2746. {
  2747. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2748. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2749. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2750. }
  2751. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2752. NSString* nsimg_1=nil;
  2753. if(img_1!=nil)
  2754. {
  2755. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2756. [self copy_upImg:serial file:nsimg_1];
  2757. }
  2758. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2759. NSString* nsso_no=nil;
  2760. if(so_no!=nil)
  2761. {
  2762. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2763. }
  2764. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2765. itemjson[@"order_type"]=@"submit order";
  2766. else
  2767. itemjson[@"order_type"]=@"archive order";
  2768. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2769. itemjson[@"json_data"]= nssync_data;
  2770. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2771. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2772. count++;
  2773. }
  2774. ret[@"count"]=[NSNumber numberWithInt:count ];
  2775. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2776. }
  2777. sqlite3_finalize(statement);
  2778. [iSalesDB close_db:db];
  2779. return ret;
  2780. }
  2781. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2782. {
  2783. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2784. sqlite3 *db = [iSalesDB get_db];
  2785. // UIApplication * app = [UIApplication sharedApplication];
  2786. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2787. 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";
  2788. sqlite3_stmt * statement;
  2789. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2790. int count=0;
  2791. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2792. if ( dbresult== SQLITE_OK)
  2793. {
  2794. while (sqlite3_step(statement) == SQLITE_ROW)
  2795. {
  2796. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2797. int _id = sqlite3_column_int(statement, 0);
  2798. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2799. NSString* nsimg_0=nil;
  2800. if(img_0!=nil)
  2801. {
  2802. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2803. [self copy_upImg:serial file:nsimg_0];
  2804. }
  2805. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2806. NSString* nsimg_1=nil;
  2807. if(img_1!=nil)
  2808. {
  2809. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2810. [self copy_upImg:serial file:nsimg_1];
  2811. }
  2812. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2813. NSString* nsimg_2=nil;
  2814. if(img_2!=nil)
  2815. {
  2816. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2817. [self copy_upImg:serial file:nsimg_2];
  2818. }
  2819. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2820. NSString* nssync_data=nil;
  2821. if(sync_data!=nil)
  2822. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2823. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2824. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2825. itemjson[@"json_data"]= nssync_data;
  2826. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2827. count++;
  2828. }
  2829. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2830. sqlite3_finalize(statement);
  2831. }
  2832. ret[@"count"]=[NSNumber numberWithInt:count ];
  2833. [iSalesDB close_db:db];
  2834. return ret;
  2835. }
  2836. +(bool) check_offlinedata
  2837. {
  2838. // UIApplication * app = [UIApplication sharedApplication];
  2839. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2840. sqlite3 *db = [iSalesDB get_db];
  2841. NSString * where=@"1=1";
  2842. // if(appDelegate.user!=nil)
  2843. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2844. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2845. [iSalesDB close_db:db];
  2846. if(count==0)
  2847. {
  2848. return false;
  2849. }
  2850. return true;
  2851. //
  2852. //[iSalesDB close_db:db];
  2853. }
  2854. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2855. {
  2856. // UIApplication * app = [UIApplication sharedApplication];
  2857. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2858. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2859. sqlite3 *db = [iSalesDB get_db];
  2860. NSString* collectId=params[@"collectId"];
  2861. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2862. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2863. [iSalesDB execSql:sqlQuery db:db];
  2864. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2865. [iSalesDB close_db:db];
  2866. // appDelegate.wish_count =count;
  2867. //
  2868. // [appDelegate update_count_mark];
  2869. ret[@"result"]= [NSNumber numberWithInt:2];
  2870. ret[@"wish_count"]=@(count);
  2871. return ret;
  2872. }
  2873. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2874. //{
  2875. //
  2876. // UIApplication * app = [UIApplication sharedApplication];
  2877. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2878. //
  2879. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2880. // sqlite3 *db = [iSalesDB get_db];
  2881. // NSString* product_id=params[@"product_id"];
  2882. //
  2883. //
  2884. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2885. //
  2886. //
  2887. // for(int i=0;i<arr.count;i++)
  2888. // {
  2889. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2890. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2891. // if(count==0)
  2892. // {
  2893. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2894. // [iSalesDB execSql:sqlQuery db:db];
  2895. // }
  2896. // }
  2897. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2898. // [iSalesDB close_db:db];
  2899. //
  2900. // appDelegate.wish_count =count;
  2901. //
  2902. // [appDelegate update_count_mark];
  2903. // ret[@"result"]= [NSNumber numberWithInt:2];
  2904. // return ret;
  2905. // //
  2906. // //return ret;
  2907. //}
  2908. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2909. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2910. DebugLog(@"time interval: %lf",interval);
  2911. }
  2912. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2913. //{
  2914. // UIApplication * app = [UIApplication sharedApplication];
  2915. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2916. //
  2917. // int sort = [[params objectForKey:@"sort"] intValue];
  2918. // NSString *sort_str = @"";
  2919. // switch (sort) {
  2920. // case 0:{
  2921. // sort_str = @"order by w.modify_time desc";
  2922. // }
  2923. // break;
  2924. // case 1:{
  2925. // sort_str = @"order by w.modify_time asc";
  2926. // }
  2927. // break;
  2928. // case 2:{
  2929. // sort_str = @"order by m.name asc";
  2930. // }
  2931. // break;
  2932. // case 3:{
  2933. // sort_str = @"order by m.name desc";
  2934. // }
  2935. // break;
  2936. // case 4:{
  2937. // sort_str = @"order by m.description asc";
  2938. // }
  2939. // break;
  2940. //
  2941. // default:
  2942. // break;
  2943. //
  2944. // }
  2945. //
  2946. //
  2947. //// NSString* user = appDelegate.user;
  2948. //
  2949. // sqlite3 *db = [iSalesDB get_db];
  2950. //
  2951. // // order by w.create_time
  2952. // 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];
  2953. //
  2954. //// 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];
  2955. //
  2956. //
  2957. // sqlite3_stmt * statement;
  2958. //
  2959. // NSDate *date1 = [NSDate date];
  2960. //// NSDate *date2 = nil;
  2961. //
  2962. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2963. // int count=0;
  2964. //
  2965. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2966. // {
  2967. //
  2968. //
  2969. // while (sqlite3_step(statement) == SQLITE_ROW)
  2970. // {
  2971. //
  2972. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2973. //
  2974. // int product_id = sqlite3_column_double(statement, 0);
  2975. //
  2976. //
  2977. //
  2978. //
  2979. //
  2980. // char *description = (char*)sqlite3_column_text(statement, 1);
  2981. // if(description==nil)
  2982. // description= "";
  2983. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2984. //
  2985. // int item_id = sqlite3_column_double(statement, 2);
  2986. //
  2987. // NSDate *date_image = [NSDate date];
  2988. //
  2989. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2990. //
  2991. // printf("image : ");
  2992. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2993. //
  2994. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2995. // // if(url==nil)
  2996. // // url="";
  2997. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2998. //
  2999. // int qty = sqlite3_column_int(statement, 3);
  3000. //
  3001. //
  3002. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  3003. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  3004. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  3005. // item[@"description"]= nsdescription;
  3006. // item[@"img"]= nsurl;
  3007. //
  3008. //
  3009. //
  3010. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3011. // count++;
  3012. //
  3013. // }
  3014. // printf("total time:");
  3015. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3016. //
  3017. // ret[@"count"]= [NSNumber numberWithInt:count];
  3018. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  3019. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  3020. // ret[@"result"]= [NSNumber numberWithInt:2];
  3021. //
  3022. //
  3023. // appDelegate.wish_count =count;
  3024. //
  3025. // [appDelegate update_count_mark];
  3026. // sqlite3_finalize(statement);
  3027. //
  3028. //
  3029. //
  3030. //
  3031. // }
  3032. //
  3033. // [iSalesDB close_db:db];
  3034. //
  3035. // return ret;
  3036. //}
  3037. +(NSDictionary*) offline_notimpl
  3038. {
  3039. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3040. ret[@"result"]=@"8";
  3041. ret[@"err_msg"]=@"offline mode does not support this function.";
  3042. return ret;
  3043. }
  3044. +(NSDictionary*) offline_home
  3045. {
  3046. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3047. NSString *cachefolder = [paths objectAtIndex:0];
  3048. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.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_category_menu
  3058. {
  3059. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3060. NSString *cachefolder = [paths objectAtIndex:0];
  3061. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  3062. NSData* json =nil;
  3063. json=[NSData dataWithContentsOfFile:img_cache];
  3064. if(json==nil)
  3065. return nil;
  3066. NSError *error=nil;
  3067. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3068. return menu;
  3069. }
  3070. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3071. {
  3072. NSString* offline_command=params[@"offline_Command"];
  3073. NSDictionary* ret=nil;
  3074. if([offline_command isEqualToString:@"model_NIYMAL"])
  3075. {
  3076. NSString* category = params[@"category"];
  3077. ret = [self refresh_model_NIYMAL:category];
  3078. }
  3079. return ret;
  3080. }
  3081. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3082. {
  3083. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3084. [ret setValue:@"2" forKey:@"result"];
  3085. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3086. sqlite3* db= [iSalesDB get_db];
  3087. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3088. [iSalesDB close_db:db];
  3089. [ret setObject:detail1_section forKey:@"detail_1"];
  3090. return ret;
  3091. }
  3092. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3093. {
  3094. //assert(user!=nil);
  3095. // UIApplication * app = [UIApplication sharedApplication];
  3096. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3097. // NSArray* arr1 = [self get_user_all_price_type];
  3098. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3099. // NSSet *set1 = [NSSet setWithArray:arr1];
  3100. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3101. // if(appDelegate.contact_id==nil)
  3102. // set2=[set1 mutableCopy];
  3103. // else
  3104. // [set2 intersectsSet:set1];
  3105. // NSArray *retarr = [set2 allObjects];
  3106. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3107. // sqlite3 *db = [iSalesDB get_db];
  3108. NSString* sqlQuery = nil;
  3109. if(contact_id==nil)
  3110. {
  3111. if(!blogin)
  3112. return nil;
  3113. 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];
  3114. }
  3115. else
  3116. 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];
  3117. sqlite3_stmt * statement;
  3118. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3119. int count=0;
  3120. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3121. {
  3122. while (sqlite3_step(statement) == SQLITE_ROW)
  3123. {
  3124. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3125. char *name = (char*)sqlite3_column_text(statement, 0);
  3126. if(name==nil)
  3127. name="";
  3128. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3129. // double price = sqlite3_column_double(statement, 1);
  3130. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3131. // if(isnull==nil)
  3132. // item[nsname]= @"No Price";
  3133. // else
  3134. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3135. char *price = (char*)sqlite3_column_text(statement, 1);
  3136. if(price!=nil)
  3137. {
  3138. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3139. nsprice=[AESCrypt fastdecrypt:nsprice];
  3140. if(nsprice.length>0)
  3141. {
  3142. double dp= [nsprice doubleValue];
  3143. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3144. }
  3145. }
  3146. else
  3147. {
  3148. item[nsname]= @"No Price";
  3149. }
  3150. // int type= sqlite3_column_int(statement, 2);
  3151. //item[@"type"]=@"price";
  3152. // item[nsname]= nsprice;
  3153. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3154. count++;
  3155. }
  3156. ret[@"count"]= [NSNumber numberWithInt:count];
  3157. sqlite3_finalize(statement);
  3158. }
  3159. // [iSalesDB close_db:db];
  3160. return ret;
  3161. }
  3162. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3163. //{
  3164. // NSArray* arr1 = [self get_user_all_price_type:db];
  3165. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3166. //
  3167. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3168. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3169. // // [set2 intersectsSet:set1];
  3170. // //
  3171. // //
  3172. // // NSArray *retarr = [set2 allObjects];
  3173. //
  3174. // NSString* whereprice=nil;
  3175. // if(contact_id==nil)
  3176. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3177. // else
  3178. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3179. //
  3180. //
  3181. // // sqlite3 *db = [iSalesDB get_db];
  3182. //
  3183. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3184. // sqlite3_stmt * statement;
  3185. //
  3186. //
  3187. // NSNumber* ret = nil;
  3188. // double dprice=DBL_MAX;
  3189. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3190. // {
  3191. //
  3192. //
  3193. // while (sqlite3_step(statement) == SQLITE_ROW)
  3194. // {
  3195. //
  3196. // // double val = sqlite3_column_double(statement, 0);
  3197. // char *price = (char*)sqlite3_column_text(statement, 0);
  3198. // if(price!=nil)
  3199. // {
  3200. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3201. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3202. // if(nsprice.length>0)
  3203. // {
  3204. // double dp= [nsprice doubleValue];
  3205. // if(dp<dprice)
  3206. // dprice=dp;
  3207. // }
  3208. // }
  3209. // }
  3210. //
  3211. //
  3212. //
  3213. //
  3214. // sqlite3_finalize(statement);
  3215. //
  3216. //
  3217. //
  3218. //
  3219. // }
  3220. //
  3221. // // [iSalesDB close_db:db];
  3222. //
  3223. // if(dprice==DBL_MAX)
  3224. // ret= nil;
  3225. // else
  3226. // ret= [NSNumber numberWithDouble:dprice];
  3227. // return ret;
  3228. //}
  3229. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3230. {
  3231. assert(user!=nil);
  3232. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3233. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3234. NSString* whereprice=nil;
  3235. if(contact_id==nil)
  3236. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3237. else
  3238. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3239. // sqlite3 *db = [iSalesDB get_db];
  3240. NSString *productIdCondition = @"1 = 1";
  3241. if (product_id) {
  3242. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3243. }
  3244. NSString *itemIdCondition = @"";
  3245. if (item_id) {
  3246. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3247. }
  3248. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3249. sqlite3_stmt * statement;
  3250. NSNumber* ret = nil;
  3251. double dprice=DBL_MAX;
  3252. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3253. {
  3254. while (sqlite3_step(statement) == SQLITE_ROW)
  3255. {
  3256. // double val = sqlite3_column_double(statement, 0);
  3257. char *price = (char*)sqlite3_column_text(statement, 0);
  3258. if(price!=nil)
  3259. {
  3260. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3261. nsprice=[AESCrypt fastdecrypt:nsprice];
  3262. if(nsprice.length>0)
  3263. {
  3264. double dp= [nsprice doubleValue];
  3265. if(dp<dprice)
  3266. dprice=dp;
  3267. }
  3268. }
  3269. }
  3270. sqlite3_finalize(statement);
  3271. }
  3272. // [iSalesDB close_db:db];
  3273. if(dprice==DBL_MAX)
  3274. ret= nil;
  3275. else
  3276. ret= [NSNumber numberWithDouble:dprice];
  3277. return ret;
  3278. }
  3279. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3280. {
  3281. assert(user!=nil);
  3282. NSArray* ret=nil;
  3283. // sqlite3 *db = [iSalesDB get_db];
  3284. // no customer assigned , use login user contact_id
  3285. // UIApplication * app = [UIApplication sharedApplication];
  3286. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3287. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3288. sqlite3_stmt * statement;
  3289. // int count=0;
  3290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3291. {
  3292. if (sqlite3_step(statement) == SQLITE_ROW)
  3293. {
  3294. char *val = (char*)sqlite3_column_text(statement, 0);
  3295. if(val==nil)
  3296. val="";
  3297. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3298. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3299. }
  3300. sqlite3_finalize(statement);
  3301. }
  3302. // [iSalesDB close_db:db];
  3303. return ret;
  3304. }
  3305. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3306. {
  3307. // sqlite3 *db = [iSalesDB get_db];
  3308. if(contact_id==nil)
  3309. {
  3310. // no customer assigned , use login user contact_id
  3311. // UIApplication * app = [UIApplication sharedApplication];
  3312. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3313. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3314. sqlite3_stmt * statement;
  3315. // int count=0;
  3316. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3317. {
  3318. if (sqlite3_step(statement) == SQLITE_ROW)
  3319. {
  3320. char *val = (char*)sqlite3_column_text(statement, 0);
  3321. if(val==nil)
  3322. val="";
  3323. contact_id = [[NSString alloc]initWithUTF8String:val];
  3324. }
  3325. sqlite3_finalize(statement);
  3326. }
  3327. if(contact_id.length<=0)
  3328. {
  3329. // [iSalesDB close_db:db];
  3330. return nil;
  3331. }
  3332. }
  3333. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3334. sqlite3_stmt * statement;
  3335. NSArray* ret=nil;
  3336. // int count=0;
  3337. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3338. {
  3339. if (sqlite3_step(statement) == SQLITE_ROW)
  3340. {
  3341. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3342. if(price_type==nil)
  3343. price_type="";
  3344. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3345. if(nsprice_type.length>0)
  3346. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3347. else
  3348. ret=nil;
  3349. }
  3350. sqlite3_finalize(statement);
  3351. }
  3352. // [iSalesDB close_db:db];
  3353. return ret;
  3354. }
  3355. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3356. {
  3357. NSString* ret= nil;
  3358. // sqlite3 *db = [iSalesDB get_db];
  3359. NSString *sqlQuery = nil;
  3360. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3361. // 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;
  3362. // 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
  3363. if(product_id==nil && model_name)
  3364. 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;
  3365. else if (product_id)
  3366. 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
  3367. sqlite3_stmt * statement;
  3368. // int count=0;
  3369. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3370. {
  3371. if (sqlite3_step(statement) == SQLITE_ROW)
  3372. {
  3373. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3374. if(imgurl==nil)
  3375. imgurl="";
  3376. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3377. ret=nsimgurl;
  3378. }
  3379. sqlite3_finalize(statement);
  3380. }
  3381. else
  3382. {
  3383. [ret setValue:@"8" forKey:@"result"];
  3384. }
  3385. // [iSalesDB close_db:db];
  3386. DebugLog(@"data string: %@",ret );
  3387. return ret;
  3388. }
  3389. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3390. {
  3391. NSString* ret= nil;
  3392. sqlite3 *db = [iSalesDB get_db];
  3393. NSString *sqlQuery = nil;
  3394. if(product_id==nil)
  3395. if(upc_code==nil)
  3396. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3397. else
  3398. 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='%@';
  3399. else
  3400. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3401. sqlite3_stmt * statement;
  3402. // int count=0;
  3403. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3404. {
  3405. if (sqlite3_step(statement) == SQLITE_ROW)
  3406. {
  3407. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3408. if(default_category==nil)
  3409. default_category="";
  3410. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3411. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3412. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3413. ret=nsdefault_category;
  3414. }
  3415. sqlite3_finalize(statement);
  3416. }
  3417. else
  3418. {
  3419. [ret setValue:@"8" forKey:@"result"];
  3420. }
  3421. [iSalesDB close_db:db];
  3422. DebugLog(@"data string: %@",ret );
  3423. return ret;
  3424. }
  3425. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3426. NSString *sql = nil;
  3427. if (product_id != nil) {
  3428. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3429. } else {
  3430. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3431. }
  3432. NSString *result_set = [iSalesDB jk_queryText:sql];
  3433. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3434. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3435. return result_set;
  3436. }
  3437. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3438. //{
  3439. //
  3440. //
  3441. // assert(params[@"user"]!=nil);
  3442. //
  3443. //
  3444. //
  3445. // NSString* model_name = [params valueForKey:@"product_name"];
  3446. //
  3447. // NSString* product_id = [params valueForKey:@"product_id"];
  3448. //
  3449. // NSString* category = [params valueForKey:@"category"];
  3450. //
  3451. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3452. // if(category==nil) {
  3453. // category = default_category_id;
  3454. // } else {
  3455. //
  3456. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3457. // // 参数重有多个category id
  3458. // if (arr_0.count > 1) {
  3459. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3460. //
  3461. // // 取交集
  3462. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3463. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3464. // [set_0 intersectSet:set_1];
  3465. //
  3466. // if (set_0.count == 1) {
  3467. // category = (NSString *)[set_0 anyObject];
  3468. // } else {
  3469. // if ([set_0 containsObject:default_category_id]) {
  3470. // category = default_category_id;
  3471. // } else {
  3472. // category = (NSString *)[set_0 anyObject];
  3473. // }
  3474. // }
  3475. // }
  3476. // }
  3477. //
  3478. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3479. //
  3480. //
  3481. //
  3482. //
  3483. //
  3484. //
  3485. // sqlite3 *db = [iSalesDB get_db];
  3486. //
  3487. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3488. //
  3489. //
  3490. // NSString *sqlQuery = nil;
  3491. //
  3492. // if(product_id==nil)
  3493. // 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='%@';
  3494. // else
  3495. //
  3496. // 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=%@;
  3497. //
  3498. //
  3499. // sqlite3_stmt * statement;
  3500. // [ret setValue:@"2" forKey:@"result"];
  3501. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3502. //
  3503. // // int count=0;
  3504. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3505. // {
  3506. //
  3507. //
  3508. // if (sqlite3_step(statement) == SQLITE_ROW)
  3509. // {
  3510. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3511. //
  3512. //
  3513. // char *name = (char*)sqlite3_column_text(statement, 0);
  3514. // if(name==nil)
  3515. // name="";
  3516. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3517. //
  3518. // char *description = (char*)sqlite3_column_text(statement, 1);
  3519. // if(description==nil)
  3520. // description="";
  3521. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3522. //
  3523. //
  3524. // int product_id = sqlite3_column_int(statement, 2);
  3525. //
  3526. //
  3527. // char *color = (char*)sqlite3_column_text(statement, 3);
  3528. // if(color==nil)
  3529. // color="";
  3530. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3531. // //
  3532. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3533. // // if(legcolor==nil)
  3534. // // legcolor="";
  3535. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3536. // //
  3537. // //
  3538. // int availability = sqlite3_column_int(statement, 5);
  3539. // //
  3540. // int incoming_stock = sqlite3_column_int(statement, 6);
  3541. //
  3542. //
  3543. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3544. // if(demension==nil)
  3545. // demension="";
  3546. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3547. //
  3548. //
  3549. //
  3550. // // ,,,,,,,,,
  3551. //
  3552. //
  3553. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3554. // if(seat_height==nil)
  3555. // seat_height="";
  3556. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3557. //
  3558. //
  3559. // char *material = (char*)sqlite3_column_text(statement, 9);
  3560. // if(material==nil)
  3561. // material="";
  3562. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3563. //
  3564. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3565. // if(box_dim==nil)
  3566. // box_dim="";
  3567. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3568. //
  3569. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3570. // if(volume==nil)
  3571. // volume="";
  3572. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3573. //
  3574. // double weight = sqlite3_column_double(statement, 12);
  3575. //
  3576. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3577. // if(model_set==nil)
  3578. // model_set="";
  3579. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3580. //
  3581. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3582. // if(load_ability==nil)
  3583. // load_ability="";
  3584. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3585. //
  3586. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3587. //// if(default_category==nil)
  3588. //// default_category="";
  3589. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3590. //
  3591. //
  3592. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3593. // if(fabric_content==nil)
  3594. // fabric_content="";
  3595. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3596. //
  3597. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3598. // if(assembling==nil)
  3599. // assembling="";
  3600. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3601. //
  3602. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3603. // if(made_in==nil)
  3604. // made_in="";
  3605. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3606. //
  3607. //
  3608. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3609. // if(special_remarks==nil)
  3610. // special_remarks="";
  3611. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3612. //
  3613. //
  3614. // int stockUcom = sqlite3_column_double(statement, 20);
  3615. //
  3616. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3617. // if(product_group==nil)
  3618. // product_group="";
  3619. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3620. //
  3621. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3622. // // if(fashion_selector==nil)
  3623. // // fashion_selector="";
  3624. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3625. //
  3626. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3627. // if(selector_field==nil)
  3628. // selector_field="";
  3629. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3630. //
  3631. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3632. // if(property_field==nil)
  3633. // property_field="";
  3634. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3635. //
  3636. //
  3637. //
  3638. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3639. // if(packaging==nil)
  3640. // packaging="";
  3641. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3642. //
  3643. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3644. //
  3645. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3646. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3647. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3648. //
  3649. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3650. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3651. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3652. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3653. //
  3654. //
  3655. //
  3656. //
  3657. //
  3658. // NSString* Availability=nil;
  3659. // if(availability>0)
  3660. // Availability=[NSString stringWithFormat:@"%d",availability];
  3661. // else
  3662. // Availability = @"Out of Stock";
  3663. //
  3664. // [img_section setValue:Availability forKey:@"Availability"];
  3665. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3666. //
  3667. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3668. //
  3669. //
  3670. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3671. // if(eta==nil)
  3672. // eta="";
  3673. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3674. // if ([nseta isEqualToString:@"null"]) {
  3675. // nseta = @"";
  3676. // }
  3677. // if (availability <= 0) {
  3678. // [img_section setValue:nseta forKey:@"ETA"];
  3679. // }
  3680. //
  3681. //
  3682. // int item_id = sqlite3_column_int(statement, 26);
  3683. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3684. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3685. //
  3686. // NSString* Price=nil;
  3687. // if(appDelegate.bLogin==false)
  3688. // Price=@"Must Sign in.";
  3689. // else
  3690. // {
  3691. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3692. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3693. // if(price==nil)
  3694. // Price=@"No Price.";
  3695. // else
  3696. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3697. // }
  3698. //
  3699. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3700. // [img_section setValue:Price forKey:@"price"];
  3701. // [img_section setValue:nsname forKey:@"model_name"];
  3702. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3703. //
  3704. // if (appDelegate.order_code) { // 离线order code即so#
  3705. //
  3706. // 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];
  3707. // __block int cartQTY = 0;
  3708. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3709. //
  3710. // cartQTY = sqlite3_column_int(stmt, 0);
  3711. //
  3712. // }];
  3713. //
  3714. // if (cartQTY > 0) {
  3715. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3716. // }
  3717. // }
  3718. //
  3719. // [ret setObject:img_section forKey:@"img_section"];
  3720. //
  3721. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3722. // int detail0_item_count=0;
  3723. //
  3724. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3725. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3726. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3727. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3728. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3729. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3730. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3731. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3732. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3733. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3734. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3735. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3736. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3737. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3738. //
  3739. //
  3740. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3741. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3742. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3743. // {
  3744. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3745. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3746. // }
  3747. //
  3748. //
  3749. //
  3750. //
  3751. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3752. // [detail0_section setValue:@"kv" forKey:@"type"];
  3753. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3754. //
  3755. // [ret setObject:detail0_section forKey:@"detail_0"];
  3756. //
  3757. //
  3758. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3759. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3760. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3761. // // [detail1_section setValue:@"content" forKey:@"type"];
  3762. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3763. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3764. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3765. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3766. // [ret setObject:detail1_section forKey:@"detail_1"];
  3767. //
  3768. //
  3769. //
  3770. //
  3771. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3772. // [detail2_section setValue:@"detail" forKey:@"target"];
  3773. // [detail2_section setValue:@"popup" forKey:@"action"];
  3774. // [detail2_section setValue:@"content" forKey:@"type"];
  3775. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3776. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3777. // [detail2_section setValue:@"local" forKey:@"data"];
  3778. // [ret setObject:detail2_section forKey:@"detail_2"];
  3779. // }
  3780. //
  3781. //
  3782. //
  3783. //
  3784. // sqlite3_finalize(statement);
  3785. // }
  3786. // else
  3787. // {
  3788. // [ret setValue:@"8" forKey:@"result"];
  3789. // }
  3790. //// DebugLog(@"count:%d",count);
  3791. //
  3792. //
  3793. // [iSalesDB close_db:db];
  3794. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3795. //
  3796. // return ret;
  3797. //}
  3798. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3799. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3800. NSString* keyword = [params valueForKey:@"keyword"];
  3801. keyword=keyword.lowercaseString;
  3802. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3803. int limit = [[params valueForKey:@"limit"] intValue];
  3804. int offset = [[params valueForKey:@"offset"] intValue];
  3805. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3806. NSString *limit_str = @"";
  3807. if (limited) {
  3808. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3809. }
  3810. sqlite3 *db = [iSalesDB get_db];
  3811. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3812. // UIApplication * app = [UIApplication sharedApplication];
  3813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3814. NSString *sqlQuery = nil;
  3815. if(exactMatch )
  3816. 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 ;
  3817. else
  3818. 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
  3819. DebugLog(@"offline_search sql:%@",sqlQuery);
  3820. sqlite3_stmt * statement;
  3821. [ret setValue:@"2" forKey:@"result"];
  3822. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3823. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3824. // int count=0;
  3825. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3826. {
  3827. int i=0;
  3828. while (sqlite3_step(statement) == SQLITE_ROW)
  3829. {
  3830. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3831. // char *name = (char*)sqlite3_column_text(statement, 1);
  3832. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3833. char *name = (char*)sqlite3_column_text(statement, 0);
  3834. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3835. char *description = (char*)sqlite3_column_text(statement, 1);
  3836. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3837. int product_id = sqlite3_column_int(statement, 2);
  3838. // char *url = (char*)sqlite3_column_text(statement, 3);
  3839. // if(url==nil)
  3840. // url="";
  3841. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3842. int wid = sqlite3_column_int(statement, 3);
  3843. int closeout = sqlite3_column_int(statement, 4);
  3844. int cid = sqlite3_column_int(statement, 5);
  3845. int wisdelete = sqlite3_column_int(statement, 6);
  3846. int more_color = sqlite3_column_int(statement, 7);
  3847. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3848. if(wid !=0 && wisdelete != 1)
  3849. [item setValue:@"true" forKey:@"wish_exists"];
  3850. else
  3851. [item setValue:@"false" forKey:@"wish_exists"];
  3852. if(closeout==0)
  3853. [item setValue:@"false" forKey:@"is_closeout"];
  3854. else
  3855. [item setValue:@"true" forKey:@"is_closeout"];
  3856. if(cid==0)
  3857. [item setValue:@"false" forKey:@"cart_exists"];
  3858. else
  3859. [item setValue:@"true" forKey:@"cart_exists"];
  3860. if (more_color == 0) {
  3861. [item setObject:@(false) forKey:@"more_color"];
  3862. } else if (more_color == 1) {
  3863. [item setObject:@(true) forKey:@"more_color"];
  3864. }
  3865. [item addEntriesFromDictionary:imgjson];
  3866. // [item setValue:nsurl forKey:@"img"];
  3867. [item setValue:nsname forKey:@"fash_name"];
  3868. [item setValue:nsdescription forKey:@"description"];
  3869. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3870. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3871. i++;
  3872. }
  3873. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3874. [ret setObject:items forKey:@"items"];
  3875. sqlite3_finalize(statement);
  3876. }
  3877. DebugLog(@"count:%d",count);
  3878. [iSalesDB close_db:db];
  3879. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3880. return ret;
  3881. }
  3882. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3883. {
  3884. return [self search:params limited:YES];
  3885. }
  3886. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3887. //{
  3888. // UIApplication * app = [UIApplication sharedApplication];
  3889. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3890. //
  3891. //// [iSalesDB disable_trigger]
  3892. // [iSalesDB disable_trigger];
  3893. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3894. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3895. //
  3896. // [iSalesDB enable_trigger];
  3897. //// [iSalesDB enable_trigger]
  3898. // //
  3899. // // NSString* user = [params valueForKey:@"user"];
  3900. // //
  3901. // // NSString* password = [params valueForKey:@"password"];
  3902. //
  3903. //
  3904. //
  3905. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3906. //
  3907. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3908. //
  3909. //
  3910. //
  3911. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3912. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3913. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3914. //
  3915. // [appDelegate update_count_mark];
  3916. //
  3917. //
  3918. // appDelegate.can_show_price =false;
  3919. // appDelegate.can_see_price =false;
  3920. // appDelegate.can_create_portfolio =false;
  3921. // appDelegate.can_create_order =false;
  3922. //
  3923. //
  3924. // appDelegate.can_cancel_order =false;
  3925. // appDelegate.can_set_cart_price =false;
  3926. // appDelegate.can_delete_order =false;
  3927. // appDelegate.can_submit_order =false;
  3928. // appDelegate.can_set_tearsheet_price =false;
  3929. // appDelegate.can_update_contact_info = false;
  3930. //
  3931. // appDelegate.save_order_logout = false;
  3932. // appDelegate.submit_order_logout = false;
  3933. // appDelegate.alert_sold_in_quantities = false;
  3934. //
  3935. // appDelegate.ipad_perm =nil ;
  3936. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3937. // appDelegate.OrderFilter= nil;
  3938. // [appDelegate SetSo:nil];
  3939. // [appDelegate set_main_button_panel];
  3940. //
  3941. //
  3942. // // sqlite3 *db = [iSalesDB get_db];
  3943. // //
  3944. // //
  3945. // //
  3946. // //
  3947. // //
  3948. // // 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"]];
  3949. // //
  3950. // //
  3951. // //
  3952. // //
  3953. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3954. // // sqlite3_stmt * statement;
  3955. // //
  3956. // //
  3957. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3958. // //
  3959. // //
  3960. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3961. // // {
  3962. // //
  3963. // //
  3964. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3965. // // {
  3966. // //
  3967. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3968. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3969. // //
  3970. // //
  3971. // //
  3972. // // int can_show_price = sqlite3_column_int(statement, 0);
  3973. // // int can_see_price = sqlite3_column_int(statement, 1);
  3974. // //
  3975. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3976. // // if(contact_id==nil)
  3977. // // contact_id="";
  3978. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3979. // //
  3980. // // int user_type = sqlite3_column_int(statement, 3);
  3981. // //
  3982. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3983. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3984. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3985. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3986. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3987. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3988. // // int can_create_order = sqlite3_column_int(statement, 10);
  3989. // //
  3990. // //
  3991. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3992. // // if(mode==nil)
  3993. // // mode="";
  3994. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3995. // //
  3996. // //
  3997. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3998. // // if(username==nil)
  3999. // // username="";
  4000. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4001. // //
  4002. // //
  4003. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4004. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4005. // // [header setValue:nscontact_id forKey:@"contact_id"];
  4006. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4007. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4008. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4009. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4010. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4011. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4012. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4013. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4014. // //
  4015. // // [header setValue:nsusername forKey:@"username"];
  4016. // //
  4017. // //
  4018. // // [ret setObject:header forKey:@"header"];
  4019. // // [ret setValue:nsmode forKey:@"mode"];
  4020. // //
  4021. // //
  4022. // // }
  4023. // //
  4024. // //
  4025. // //
  4026. // // sqlite3_finalize(statement);
  4027. // // }
  4028. // //
  4029. // //
  4030. // //
  4031. // // [iSalesDB close_db:db];
  4032. // //
  4033. // //
  4034. // //
  4035. // //
  4036. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4037. //
  4038. // return ret;
  4039. //}
  4040. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  4041. //{
  4042. //
  4043. // UIApplication * app = [UIApplication sharedApplication];
  4044. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4045. //
  4046. // [iSalesDB disable_trigger];
  4047. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4048. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4049. // [iSalesDB enable_trigger];
  4050. //
  4051. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4052. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4053. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  4054. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  4055. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  4056. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  4057. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  4058. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  4059. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  4060. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  4061. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  4062. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  4063. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  4064. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4065. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4066. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4067. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4068. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4069. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4070. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4071. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4072. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4073. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4074. //
  4075. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4076. //
  4077. // [arr_name addObject:customer_first_name];
  4078. // [arr_name addObject:customer_last_name];
  4079. //
  4080. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4081. //
  4082. // // default ship from
  4083. // 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';";
  4084. //
  4085. // __block NSString *cid = @"";
  4086. // __block NSString *name = @"";
  4087. // __block NSString *ext = @"";
  4088. // __block NSString *contact = @"";
  4089. // __block NSString *email = @"";
  4090. // __block NSString *fax = @"";
  4091. // __block NSString *phone = @"";
  4092. //
  4093. // sqlite3 *db = [iSalesDB get_db];
  4094. //
  4095. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4096. //
  4097. // cid = [self textAtColumn:0 statement:statment];
  4098. // name = [self textAtColumn:1 statement:statment];
  4099. // ext = [self textAtColumn:2 statement:statment];
  4100. // contact = [self textAtColumn:3 statement:statment];
  4101. // email = [self textAtColumn:4 statement:statment];
  4102. // fax = [self textAtColumn:5 statement:statment];
  4103. // phone = [self textAtColumn:6 statement:statment];
  4104. //
  4105. // }];
  4106. //
  4107. // NSString* so_id = [self get_offline_soid:db];
  4108. // if(so_id==nil)
  4109. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4110. //
  4111. // 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];
  4112. //
  4113. //
  4114. //
  4115. // int result =[iSalesDB execSql:sql_neworder db:db];
  4116. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4117. //
  4118. //
  4119. //
  4120. // //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'
  4121. // //soId
  4122. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4123. //
  4124. //
  4125. //
  4126. //
  4127. //
  4128. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4129. // sqlite3_stmt * statement;
  4130. //
  4131. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4132. // {
  4133. // if (sqlite3_step(statement) == SQLITE_ROW)
  4134. // {
  4135. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4136. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4137. //
  4138. // //ret = sqlite3_column_int(statement, 0);
  4139. //
  4140. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4141. // if(soId==nil)
  4142. // soId="";
  4143. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4144. // [ret setValue:nssoId forKey:@"soId"];
  4145. // [ret setValue:nssoId forKey:@"orderCode"];
  4146. //
  4147. // }
  4148. // sqlite3_finalize(statement);
  4149. // }
  4150. //
  4151. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4152. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4153. //
  4154. //
  4155. // [iSalesDB close_db:db];
  4156. //
  4157. // return [RAConvertor dict2data:ret];
  4158. //
  4159. //}
  4160. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4161. {
  4162. //assert(params[@"order_code"]);
  4163. // assert(params[@"order_code"]);
  4164. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4165. NSString* orderCode = [params valueForKey:@"orderCode"];
  4166. NSString* app_order_code= params[@"appDelegate.order_code"];
  4167. // UIApplication * app = [UIApplication sharedApplication];
  4168. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4169. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4170. {
  4171. [iSalesDB disable_trigger];
  4172. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4173. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4174. [iSalesDB enable_trigger];
  4175. }
  4176. sqlite3 *db = [iSalesDB get_db];
  4177. int cart_count=[self query_ordercartcount:orderCode db:db];
  4178. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4179. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4180. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4181. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4182. [iSalesDB close_db:db];
  4183. return [RAConvertor dict2data:ret];
  4184. }
  4185. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4186. {
  4187. assert(params[@"can_create_backorder"]!=nil);
  4188. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4189. sqlite3 *db = [iSalesDB get_db];
  4190. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4191. int count =0;
  4192. if(params[@"count"]!=nil)
  4193. {
  4194. count = [params[@"count"] intValue];
  4195. }
  4196. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4197. NSString* product_id=params[@"product_id"];
  4198. NSString* orderCode=params[@"orderCode"];
  4199. NSString *qty = params[@"qty"];
  4200. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4201. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4202. __block int number_of_outOfStock = 0;
  4203. for(int i=0;i<arr_id.count;i++)
  4204. {
  4205. NSInteger item_qty= count;
  4206. if (qty) {
  4207. item_qty = [qty_arr[i] integerValue];
  4208. }
  4209. if(item_qty==0)
  4210. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4211. // 检查新加Model数量是否大于库存
  4212. if (![params[@"can_create_backorder"] boolValue]) {
  4213. __block BOOL needContinue = NO;
  4214. [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) {
  4215. int availability = sqlite3_column_int(stmt, 0);
  4216. // 库存小于购买量为缺货
  4217. if (availability < item_qty || availability <= 0) {
  4218. number_of_outOfStock++;
  4219. needContinue = YES;
  4220. }
  4221. }];
  4222. if (needContinue) {
  4223. continue;
  4224. }
  4225. }
  4226. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4227. NSString* sql=nil;
  4228. sqlite3_stmt *stmt = nil;
  4229. BOOL shouldStep = NO;
  4230. if(_id<0)
  4231. {
  4232. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4233. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4234. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4235. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4236. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4237. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4238. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4239. shouldStep = YES;
  4240. }
  4241. else
  4242. {
  4243. if (qty) { // wish list move to cart
  4244. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4245. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4246. sqlite3_bind_int(stmt, 1, _id);
  4247. shouldStep = YES;
  4248. } else {
  4249. 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];
  4250. __block BOOL update = YES;
  4251. if (![params[@"can_create_backorder"] boolValue]) {
  4252. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4253. int newQTY = sqlite3_column_int(stmt, 0);
  4254. int availability = sqlite3_column_int(stmt, 1);
  4255. if (newQTY > availability) { // 库存不够
  4256. update = NO;
  4257. number_of_outOfStock++;
  4258. }
  4259. }];
  4260. }
  4261. if (update) {
  4262. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4263. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4264. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4265. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4266. shouldStep = YES;
  4267. }
  4268. }
  4269. }
  4270. if (shouldStep) {
  4271. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4272. [iSalesDB execSql:@"ROLLBACK" db:db];
  4273. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4274. [iSalesDB close_db:db];
  4275. DebugLog(@"add to cart error");
  4276. return [RAConvertor dict2data:ret];
  4277. }
  4278. }
  4279. }
  4280. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4281. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4282. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4283. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4284. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4285. if (![params[@"can_create_backorder"] boolValue]) {
  4286. if (number_of_outOfStock > 0) {
  4287. ret[@"result"]=[NSNumber numberWithInt:8];
  4288. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4289. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4290. }
  4291. }
  4292. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4293. [iSalesDB close_db:db];
  4294. return [RAConvertor dict2data:ret];
  4295. }
  4296. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4297. {
  4298. // UIApplication * app = [UIApplication sharedApplication];
  4299. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4300. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4301. 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];
  4302. sqlite3_stmt * statement;
  4303. int count=0;
  4304. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4305. {
  4306. while (sqlite3_step(statement) == SQLITE_ROW)
  4307. {
  4308. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4309. int bitem_id = sqlite3_column_int(statement, 0);
  4310. int bitem_qty = sqlite3_column_int(statement, 1);
  4311. char *name = (char*)sqlite3_column_text(statement, 2);
  4312. if(name==nil)
  4313. name="";
  4314. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4315. char *description = (char*)sqlite3_column_text(statement, 3);
  4316. if(description==nil)
  4317. description="";
  4318. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4319. double unit_price = sqlite3_column_double(statement, 4);
  4320. int use_unitprice = sqlite3_column_int(statement, 5);
  4321. if(use_unitprice!=1)
  4322. {
  4323. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4324. }
  4325. itemjson[@"model"]=nsname;
  4326. itemjson[@"description"]=nsdescription;
  4327. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4328. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4329. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4330. if(compute)
  4331. {
  4332. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4333. }
  4334. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4335. count++;
  4336. }
  4337. sqlite3_finalize(statement);
  4338. }
  4339. ret[@"count"]=@(count);
  4340. if(count==0)
  4341. return nil;
  4342. else
  4343. return ret;
  4344. }
  4345. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4346. {
  4347. //compute: add part to subtotal;
  4348. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4349. dict_item[@(item_id)]=@"1";
  4350. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4351. double cuft=0;
  4352. double weight=0;
  4353. int carton=0;
  4354. int impack=0;
  4355. 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];
  4356. sqlite3_stmt * statement;
  4357. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4358. {
  4359. if (sqlite3_step(statement) == SQLITE_ROW)
  4360. {
  4361. double ulength = sqlite3_column_double(statement, 0);
  4362. double uwidth = sqlite3_column_double(statement, 1);
  4363. double uheight = sqlite3_column_double(statement, 2);
  4364. double uweight = sqlite3_column_double(statement, 3);
  4365. double mlength = sqlite3_column_double(statement, 4);
  4366. double mwidth = sqlite3_column_double(statement, 5);
  4367. double mheight = sqlite3_column_double(statement, 6);
  4368. double mweight = sqlite3_column_double(statement, 7);
  4369. double ilength = sqlite3_column_double(statement, 8);
  4370. double iwidth = sqlite3_column_double(statement, 9);
  4371. double iheight = sqlite3_column_double(statement, 10);
  4372. double iweight = sqlite3_column_double(statement, 11);
  4373. // int pcs = sqlite3_column_int(statement,12);
  4374. int mpack = sqlite3_column_int(statement, 13);
  4375. impack = mpack;
  4376. int ipack = sqlite3_column_int(statement, 14);
  4377. double ucbf = sqlite3_column_double(statement, 15);
  4378. // double icbf = sqlite3_column_double(statement, 16);
  4379. // double mcbf = sqlite3_column_double(statement, 17);
  4380. if(ipack==0)
  4381. {
  4382. carton= count/mpack ;
  4383. weight = mweight*carton;
  4384. cuft= carton*(mlength*mwidth*mheight);
  4385. int remain=count%mpack;
  4386. if(remain==0)
  4387. {
  4388. //do nothing;
  4389. }
  4390. else
  4391. {
  4392. carton++;
  4393. weight += uweight*remain;
  4394. cuft += (ulength*uwidth*uheight)*remain;
  4395. }
  4396. }
  4397. else
  4398. {
  4399. carton = count/(mpack*ipack);
  4400. weight = mweight*carton;
  4401. cuft= carton*(mlength*mwidth*mheight);
  4402. int remain=count%(mpack*ipack);
  4403. if(remain==0)
  4404. {
  4405. // do nothing;
  4406. }
  4407. else
  4408. {
  4409. carton++;
  4410. int icarton =remain/ipack;
  4411. int iremain=remain%ipack;
  4412. weight += iweight*icarton;
  4413. cuft += (ilength*iwidth*iheight)*icarton;
  4414. if(iremain==0)
  4415. {
  4416. //do nothing;
  4417. }
  4418. else
  4419. {
  4420. weight += uweight*iremain;
  4421. cuft += (ulength*uwidth*uheight)*iremain;
  4422. }
  4423. }
  4424. }
  4425. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4426. cuft=ucbf*count;
  4427. weight= uweight*count;
  4428. #endif
  4429. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4430. cuft=ucbf*count;
  4431. weight= uweight*count;
  4432. #endif
  4433. }
  4434. sqlite3_finalize(statement);
  4435. }
  4436. if(compute)
  4437. {
  4438. NSArray * arr_count=nil;
  4439. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4440. for(int i=0;i<arr_bundle.count;i++)
  4441. {
  4442. dict_item[arr_bundle[i]]=@"1";
  4443. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4444. cuft+=[bundlejson[@"cuft"] doubleValue];
  4445. weight+=[bundlejson[@"weight"] doubleValue];
  4446. carton+=[bundlejson[@"carton"] intValue];
  4447. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4448. }
  4449. }
  4450. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4451. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4452. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4453. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4454. ret[@"items"]=dict_item;
  4455. return ret;
  4456. }
  4457. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4458. {
  4459. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4460. // 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 ];
  4461. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4462. DebugLog(@"offline_login sql:%@",sqlQuery);
  4463. sqlite3_stmt * statement;
  4464. int cart_count=0;
  4465. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4466. if ( dbresult== SQLITE_OK)
  4467. {
  4468. while (sqlite3_step(statement) == SQLITE_ROW)
  4469. {
  4470. int item_id = sqlite3_column_int(statement, 0);
  4471. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4472. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4473. }
  4474. sqlite3_finalize(statement);
  4475. }
  4476. return cart_count;
  4477. }
  4478. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4479. //{
  4480. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4481. // sqlite3 *db = [iSalesDB get_db];
  4482. // UIApplication * app = [UIApplication sharedApplication];
  4483. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4484. //
  4485. // NSString* orderCode=params[@"orderCode"];
  4486. //
  4487. // int sort = [[params objectForKey:@"sort"] intValue];
  4488. // NSString *sort_str = @"";
  4489. // switch (sort) {
  4490. // case 0:{
  4491. // sort_str = @"order by c.modify_time desc";
  4492. // }
  4493. // break;
  4494. // case 1:{
  4495. // sort_str = @"order by c.modify_time asc";
  4496. // }
  4497. // break;
  4498. // case 2:{
  4499. // sort_str = @"order by m.name asc";
  4500. // }
  4501. // break;
  4502. // case 3:{
  4503. // sort_str = @"order by m.name desc";
  4504. // }
  4505. // break;
  4506. // case 4:{
  4507. // sort_str = @"order by m.description asc";
  4508. // }
  4509. // break;
  4510. //
  4511. // default:
  4512. // break;
  4513. //
  4514. // }
  4515. //
  4516. //
  4517. //
  4518. // 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 ];
  4519. //
  4520. //
  4521. //// 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 ];
  4522. //
  4523. //
  4524. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4525. //
  4526. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4527. // sqlite3_stmt * statement;
  4528. //
  4529. //
  4530. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4531. //
  4532. // NSDate *date1 = [NSDate date];
  4533. //
  4534. // int count=0;
  4535. // int cart_count=0;
  4536. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4537. // if ( dbresult== SQLITE_OK)
  4538. // {
  4539. //
  4540. //
  4541. // while (sqlite3_step(statement) == SQLITE_ROW)
  4542. // {
  4543. //// NSDate *row_date = [NSDate date];
  4544. //
  4545. //
  4546. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4547. //
  4548. //
  4549. //
  4550. //
  4551. // int product_id = sqlite3_column_int(statement, 0);
  4552. //
  4553. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4554. //
  4555. // int item_id = sqlite3_column_int(statement, 7);
  4556. //
  4557. // NSString* Price=nil;
  4558. // if(str_price==nil)
  4559. // {
  4560. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4561. //// NSDate *price_date = [NSDate date];
  4562. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4563. //// DebugLog(@"price time interval");
  4564. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4565. //
  4566. // if(price==nil)
  4567. // Price=@"No Price.";
  4568. // else
  4569. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4570. // }
  4571. // else
  4572. // {
  4573. //
  4574. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4575. // }
  4576. //
  4577. //
  4578. // double discount = sqlite3_column_double(statement, 2);
  4579. // int item_count = sqlite3_column_int(statement, 3);
  4580. //
  4581. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4582. // NSString *nsline_note=nil;
  4583. // if(line_note!=nil)
  4584. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4585. //
  4586. //
  4587. // char *name = (char*)sqlite3_column_text(statement, 5);
  4588. // NSString *nsname=nil;
  4589. // if(name!=nil)
  4590. // nsname= [[NSString alloc]initWithUTF8String:name];
  4591. //
  4592. // char *description = (char*)sqlite3_column_text(statement, 6);
  4593. // NSString *nsdescription=nil;
  4594. // if(description!=nil)
  4595. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4596. //
  4597. //
  4598. //
  4599. // int stockUom = sqlite3_column_int(statement, 8);
  4600. // int _id = sqlite3_column_int(statement, 9);
  4601. // int availability = sqlite3_column_int(statement, 10);
  4602. //
  4603. //// NSDate *subtotal_date = [NSDate date];
  4604. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4605. //// DebugLog(@"subtotal_date time interval");
  4606. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4607. //
  4608. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4609. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4610. // int carton=[bsubtotaljson[@"carton"] intValue];
  4611. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4612. //
  4613. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4614. //// NSDate *img_date = [NSDate date];
  4615. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4616. //// DebugLog(@"img_date time interval");
  4617. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4618. //
  4619. // itemjson[@"model"]=nsname;
  4620. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4621. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4622. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4623. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4624. // itemjson[@"check"]=@"true";
  4625. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4626. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4627. // itemjson[@"unit_price"]=Price;
  4628. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4629. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4630. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4631. // itemjson[@"note"]=nsline_note;
  4632. // if (!appDelegate.can_create_backorder) {
  4633. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4634. // }
  4635. //// NSDate *date2 = [NSDate date];
  4636. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4637. //// DebugLog(@"model_bundle time interval");
  4638. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4639. //
  4640. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4641. // count++;
  4642. //
  4643. //// DebugLog(@"row time interval");
  4644. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4645. // }
  4646. //
  4647. //
  4648. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4649. // sqlite3_finalize(statement);
  4650. // }
  4651. //
  4652. //
  4653. // DebugLog(@"request cart total time interval");
  4654. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4655. //
  4656. //
  4657. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4658. //
  4659. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4660. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4661. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4662. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4663. //
  4664. // ret[@"mode"]=@"Regular Mode";
  4665. //
  4666. // [iSalesDB close_db:db];
  4667. //
  4668. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4669. //
  4670. // DebugLog(@"general notes :%@",general_note);
  4671. //
  4672. // ret[@"general_note"]= general_note;
  4673. //
  4674. // return [RAConvertor dict2data:ret];
  4675. //}
  4676. +(NSData*) offline_login :(NSMutableDictionary *) params
  4677. {
  4678. NSString* user = [params valueForKey:@"user"];
  4679. NSString* password = [params valueForKey:@"password"];
  4680. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4681. sqlite3 *db = [iSalesDB get_db];
  4682. 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"]];
  4683. DebugLog(@"offline_login sql:%@",sqlQuery);
  4684. sqlite3_stmt * statement;
  4685. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4686. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4687. {
  4688. if (sqlite3_step(statement) == SQLITE_ROW)
  4689. {
  4690. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4691. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4692. int can_show_price = sqlite3_column_int(statement, 0);
  4693. int can_see_price = sqlite3_column_int(statement, 1);
  4694. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4695. if(contact_id==nil)
  4696. contact_id="";
  4697. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4698. int user_type = sqlite3_column_int(statement, 3);
  4699. int can_cancel_order = sqlite3_column_int(statement, 4);
  4700. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4701. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4702. int can_delete_order = sqlite3_column_int(statement, 7);
  4703. int can_submit_order = sqlite3_column_int(statement, 8);
  4704. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4705. int can_create_order = sqlite3_column_int(statement, 10);
  4706. char *mode = (char*)sqlite3_column_text(statement, 11);
  4707. if(mode==nil)
  4708. mode="";
  4709. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4710. char *username = (char*)sqlite3_column_text(statement, 12);
  4711. if(username==nil)
  4712. username="";
  4713. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4714. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4715. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4716. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4717. [header setValue:nscontact_id forKey:@"contact_id"];
  4718. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4719. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4720. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4721. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4722. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4723. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4724. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4725. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4726. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4727. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4728. [header setValue:nsusername forKey:@"username"];
  4729. NSError* error=nil;
  4730. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4731. [header setValue:statusFilter forKey:@"statusFilter"];
  4732. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4733. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4734. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4735. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4736. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4737. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4738. [ret setObject:header forKey:@"header"];
  4739. [ret setValue:nsmode forKey:@"mode"];
  4740. }
  4741. sqlite3_finalize(statement);
  4742. }
  4743. [iSalesDB close_db:db];
  4744. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4745. return [RAConvertor dict2data:ret];
  4746. }
  4747. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4748. {
  4749. assert(params[@"mode"]!=nil);
  4750. NSString* contactId = [params valueForKey:@"contactId"];
  4751. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4752. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4753. sqlite3 *db = [iSalesDB get_db];
  4754. NSString *sqlQuery = nil;
  4755. {
  4756. 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];
  4757. }
  4758. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4759. sqlite3_stmt * statement;
  4760. [ret setValue:@"2" forKey:@"result"];
  4761. int carrier = -1;
  4762. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4763. {
  4764. //int i = 0;
  4765. if (sqlite3_step(statement) == SQLITE_ROW)
  4766. {
  4767. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4768. // int editable = sqlite3_column_int(statement, 0);
  4769. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4770. NSString *nscompany_name =nil;
  4771. if(company_name==nil)
  4772. nscompany_name=@"";
  4773. else
  4774. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4775. char *country = (char*)sqlite3_column_text(statement, 2);
  4776. if(country==nil)
  4777. country="";
  4778. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4779. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4780. // if(addr==nil)
  4781. // addr="";
  4782. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4783. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4784. if(zipcode==nil)
  4785. zipcode="";
  4786. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4787. char *state = (char*)sqlite3_column_text(statement, 5);
  4788. if(state==nil)
  4789. state="";
  4790. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4791. char *city = (char*)sqlite3_column_text(statement, 6);
  4792. if(city==nil)
  4793. city="";
  4794. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4795. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4796. // NSString *nscontact_name = nil;
  4797. // if(contact_name==nil)
  4798. // nscontact_name=@"";
  4799. // else
  4800. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4801. char *phone = (char*)sqlite3_column_text(statement, 8);
  4802. NSString *nsphone = nil;
  4803. if(phone==nil)
  4804. nsphone=@"";
  4805. else
  4806. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4807. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4808. // if(contact_id==nil)
  4809. // contact_id="";
  4810. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4811. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4812. if(addr_1==nil)
  4813. addr_1="";
  4814. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4815. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4816. if(addr_2==nil)
  4817. addr_2="";
  4818. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4819. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4820. if(addr_3==nil)
  4821. addr_3="";
  4822. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4823. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4824. if(addr_4==nil)
  4825. addr_4="";
  4826. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4827. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4828. if(first_name==nil)
  4829. first_name="";
  4830. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4831. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4832. if(last_name==nil)
  4833. last_name="";
  4834. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4835. char *fax = (char*)sqlite3_column_text(statement, 16);
  4836. NSString *nsfax = nil;
  4837. if(fax==nil)
  4838. nsfax=@"";
  4839. else
  4840. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4841. char *email = (char*)sqlite3_column_text(statement, 17);
  4842. NSString *nsemail = nil;
  4843. if(email==nil)
  4844. nsemail=@"";
  4845. else
  4846. nsemail= [[NSString alloc]initWithUTF8String:email];
  4847. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4848. NSString *nsimg_0 = nil;
  4849. if(img_0==nil)
  4850. nsimg_0=@"";
  4851. else
  4852. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4853. [self copy_bcardImg:nsimg_0];
  4854. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4855. NSString *nsimg_1 = nil;
  4856. if(img_1==nil)
  4857. nsimg_1=@"";
  4858. else
  4859. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4860. [self copy_bcardImg:nsimg_1];
  4861. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4862. NSString *nsimg_2 = nil;
  4863. if(img_2==nil)
  4864. nsimg_2=@"";
  4865. else
  4866. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4867. [self copy_bcardImg:nsimg_2];
  4868. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4869. NSString *nsprice_type = nil;
  4870. if(price_type==nil)
  4871. nsprice_type=@"";
  4872. else
  4873. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4874. char *notes = (char*)sqlite3_column_text(statement, 22);
  4875. NSString *nsnotes = nil;
  4876. if(notes==nil)
  4877. nsnotes=@"";
  4878. else
  4879. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4880. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4881. NSString *nssalesrep = nil;
  4882. if(salesrep==nil)
  4883. nssalesrep=@"";
  4884. else
  4885. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4886. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4887. carrier = sqlite3_column_int(statement, 25);
  4888. {
  4889. // decrypt
  4890. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4891. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4892. nsphone=[AESCrypt fastdecrypt:nsphone];
  4893. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4894. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4895. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4896. }
  4897. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4898. [arr_name addObject:nsfirst_name];
  4899. [arr_name addObject:nslast_name];
  4900. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4901. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4902. [arr_ext addObject:nsaddr_1];
  4903. [arr_ext addObject:nsaddr_2];
  4904. [arr_ext addObject:nsaddr_3];
  4905. [arr_ext addObject:nsaddr_4];
  4906. [arr_ext addObject:@"\r\n"];
  4907. [arr_ext addObject:nscity];
  4908. [arr_ext addObject:nsstate];
  4909. [arr_ext addObject:nszipcode];
  4910. [arr_ext addObject:nscountry];
  4911. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4912. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4913. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4914. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4915. [item setValue:nscountry forKey:@"customer_country"];
  4916. [item setValue:nsphone forKey:@"customer_phone"];
  4917. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4918. [item setValue:nscompany_name forKey:@"customer_name"];
  4919. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4920. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4921. [item setValue:nsext forKey:@"customer_contact_ext"];
  4922. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4923. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4924. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4925. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4926. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4927. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4928. [item setValue:nslast_name forKey:@"customer_last_name"];
  4929. [item setValue:nscity forKey:@"customer_city"];
  4930. [item setValue:nsstate forKey:@"customer_state"];
  4931. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4932. [item setValue:contactId forKey:@"customer_cid"];
  4933. [item setValue:nscontact_name forKey:@"customer_contact"];
  4934. [item setValue:nsfax forKey:@"customer_fax"];
  4935. [item setValue:nsemail forKey:@"customer_email"];
  4936. [item setValue:contact_type forKey:@"customer_contact_type"];
  4937. // i++;
  4938. }
  4939. // UIApplication * app = [UIApplication sharedApplication];
  4940. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4941. [ret setValue:params[@"mode"] forKey:@"mode"];
  4942. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4943. sqlite3_finalize(statement);
  4944. }
  4945. [iSalesDB close_db:db];
  4946. NSString* scarrier = [self offline_getCarrier:carrier];
  4947. item[@"customer_truck_carrier"] = scarrier;
  4948. [ret setObject:item forKey:@"customerInfo"];
  4949. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4950. return ret;
  4951. }
  4952. + (bool) copy_bcardImg:(NSString*) filename
  4953. {
  4954. if(filename.length==0)
  4955. return false;
  4956. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4957. bool ret=false;
  4958. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4959. NSString *cachefolder = [paths objectAtIndex:0];
  4960. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4961. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4962. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4963. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4964. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4965. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4966. //
  4967. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4968. NSFileManager* fileManager = [NSFileManager defaultManager];
  4969. BOOL bdir=NO;
  4970. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4971. {
  4972. NSError *error = nil;
  4973. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4974. {
  4975. ret=false;
  4976. }
  4977. else
  4978. {
  4979. ret=true;
  4980. }
  4981. // NSError *error = nil;
  4982. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4983. //
  4984. // if(!bsuccess)
  4985. // {
  4986. // DebugLog(@"Create offline_createimg folder failed");
  4987. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4988. // return [RAConvertor dict2data:ret];
  4989. // }
  4990. // if(bsuccess)
  4991. // {
  4992. // sqlite3 *db = [self get_db];
  4993. //
  4994. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4995. // [iSalesDB close_db:db];
  4996. // }
  4997. }
  4998. return ret;
  4999. //
  5000. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  5001. // if(bsuccess)
  5002. // {
  5003. // NSError *error = nil;
  5004. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5005. // {
  5006. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5007. // }
  5008. // else
  5009. // {
  5010. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5011. //
  5012. // ret[@"img_url_aname"]=filename;
  5013. // ret[@"img_url"]=savedImagePath;
  5014. // }
  5015. // }
  5016. }
  5017. +(NSData *) offline_saveBusinesscard:(NSData *) image
  5018. {
  5019. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5020. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  5021. NSString *cachefolder = [paths objectAtIndex:0];
  5022. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  5023. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  5024. NSFileManager* fileManager = [NSFileManager defaultManager];
  5025. BOOL bdir=YES;
  5026. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  5027. {
  5028. NSError *error = nil;
  5029. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  5030. if(!bsuccess)
  5031. {
  5032. DebugLog(@"Create offline_createimg folder failed");
  5033. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5034. return [RAConvertor dict2data:ret];
  5035. }
  5036. // if(bsuccess)
  5037. // {
  5038. // sqlite3 *db = [self get_db];
  5039. //
  5040. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5041. // [iSalesDB close_db:db];
  5042. // }
  5043. }
  5044. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  5045. {
  5046. NSError *error = nil;
  5047. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  5048. if(!bsuccess)
  5049. {
  5050. DebugLog(@"Create img_cache folder failed");
  5051. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5052. return [RAConvertor dict2data:ret];
  5053. }
  5054. // if(bsuccess)
  5055. // {
  5056. // sqlite3 *db = [self get_db];
  5057. //
  5058. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5059. // [iSalesDB close_db:db];
  5060. // }
  5061. }
  5062. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  5063. //JEPG格式
  5064. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  5065. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  5066. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  5067. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  5068. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  5069. if(bsuccess)
  5070. {
  5071. NSError *error = nil;
  5072. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5073. {
  5074. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5075. }
  5076. else
  5077. {
  5078. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5079. ret[@"img_url_aname"]=filename;
  5080. ret[@"img_url"]=filename;
  5081. }
  5082. }
  5083. else
  5084. {
  5085. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5086. }
  5087. return [RAConvertor dict2data:ret];
  5088. }
  5089. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5090. {
  5091. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5092. category = [category substringToIndex:3];
  5093. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5094. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5095. params[@"category"]= category;
  5096. ret[@"params"]= params;
  5097. [ret setValue:@"detail" forKey:@"target"];
  5098. [ret setValue:@"popup" forKey:@"action"];
  5099. [ret setValue:@"content" forKey:@"type"];
  5100. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5101. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5102. [ret setValue:@"true" forKey:@"single_row"];
  5103. [ret setValue:@"true" forKey:@"partial_refresh"];
  5104. // sqlite3 *db = [iSalesDB get_db];
  5105. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5106. NSString *sqlQuery =nil;
  5107. #ifdef BUILD_NPD
  5108. 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 ;
  5109. #else
  5110. 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];
  5111. #endif
  5112. sqlite3_stmt * statement;
  5113. int count = 0;
  5114. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5115. // int count=0;
  5116. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5117. {
  5118. int i=0;
  5119. while (sqlite3_step(statement) == SQLITE_ROW)
  5120. {
  5121. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5122. // char *name = (char*)sqlite3_column_text(statement, 1);
  5123. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5124. char *name = (char*)sqlite3_column_text(statement, 0);
  5125. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5126. int product_id = sqlite3_column_int(statement, 1);
  5127. char *url = (char*)sqlite3_column_text(statement, 2);
  5128. if(url==nil)
  5129. url="";
  5130. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5131. [item setValue:nsurl forKey:@"picture_path"];
  5132. [item setValue:nsname forKey:@"fash_name"];
  5133. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5134. [item setValue:category forKey:@"category"];
  5135. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5136. i++;
  5137. }
  5138. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5139. sqlite3_finalize(statement);
  5140. }
  5141. DebugLog(@"count:%d",count);
  5142. // [iSalesDB close_db:db];
  5143. return ret;
  5144. }
  5145. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5146. {
  5147. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5148. [ret setValue:key forKey:@"key"];
  5149. [ret setValue:value forKey:@"val"];
  5150. [ret setValue:@"price" forKey:@"type"];
  5151. return ret;
  5152. }
  5153. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5154. {
  5155. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5156. [ret setValue:key forKey:@"key"];
  5157. [ret setValue:value forKey:@"val"];
  5158. return ret;
  5159. }
  5160. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5161. {
  5162. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5163. [ret setValue:@"0" forKey:@"img_count"];
  5164. // sqlite3 *db = [iSalesDB get_db];
  5165. 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 ;
  5166. sqlite3_stmt * statement;
  5167. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5168. {
  5169. int i=0;
  5170. if (sqlite3_step(statement) == SQLITE_ROW)
  5171. {
  5172. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5173. // char *name = (char*)sqlite3_column_text(statement, 1);
  5174. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5175. char *value = (char*)sqlite3_column_text(statement, 0);
  5176. if(value==nil)
  5177. value="";
  5178. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5179. char *key = (char*)sqlite3_column_text(statement, 1);
  5180. if(key==nil)
  5181. key="";
  5182. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5183. [item setValue:nsvalue forKey:@"val"];
  5184. [item setValue:nskey forKey:@"key"];
  5185. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5186. [ret setValue:@"1" forKey:@"count"];
  5187. i++;
  5188. }
  5189. sqlite3_finalize(statement);
  5190. }
  5191. // [iSalesDB close_db:db];
  5192. return ret;
  5193. }
  5194. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5195. {
  5196. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5197. [ret setValue:@"0" forKey:@"count"];
  5198. // sqlite3 *db = [iSalesDB get_db];
  5199. 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;
  5200. sqlite3_stmt * statement;
  5201. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5202. {
  5203. int i=0;
  5204. while (sqlite3_step(statement) == SQLITE_ROW)
  5205. {
  5206. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5207. // char *name = (char*)sqlite3_column_text(statement, 1);
  5208. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5209. char *value = (char*)sqlite3_column_text(statement, 0);
  5210. if(value==nil)
  5211. value="";
  5212. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5213. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5214. if(selector_display==nil)
  5215. selector_display="";
  5216. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5217. int product_id = sqlite3_column_int(statement, 2);
  5218. char *category = (char*)sqlite3_column_text(statement, 3);
  5219. if(category==nil)
  5220. category="";
  5221. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5222. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5223. [item setValue:nsvalue forKey:@"title"];
  5224. [item setValue:url forKey:@"pic_url"];
  5225. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5226. [params setValue:@"2" forKey:@"count"];
  5227. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5228. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5229. [param0 setValue:@"product_id" forKey:@"name"];
  5230. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5231. [param1 setValue:nscategory forKey:@"val"];
  5232. [param1 setValue:@"category" forKey:@"name"];
  5233. [params setObject:param0 forKey:@"param_0"];
  5234. [params setObject:param1 forKey:@"param_1"];
  5235. [item setObject:params forKey:@"params"];
  5236. [ret setValue:nsselector_display forKey:@"name"];
  5237. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5238. i++;
  5239. }
  5240. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5241. [ret setValue:@"switch" forKey:@"action"];
  5242. sqlite3_finalize(statement);
  5243. }
  5244. // [iSalesDB close_db:db];
  5245. return ret;
  5246. }
  5247. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5248. {
  5249. // model 在 category search 显示的图片。
  5250. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5251. // sqlite3 *db = [iSalesDB get_db];
  5252. 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];
  5253. sqlite3_stmt * statement;
  5254. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5255. {
  5256. while (sqlite3_step(statement) == SQLITE_ROW)
  5257. {
  5258. char *url = (char*)sqlite3_column_text(statement, 0);
  5259. if(url==nil)
  5260. url="";
  5261. int type = sqlite3_column_int(statement, 1);
  5262. if(type==0)
  5263. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5264. else
  5265. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5266. }
  5267. sqlite3_finalize(statement);
  5268. }
  5269. // [iSalesDB close_db:db];
  5270. return ret;
  5271. }
  5272. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5273. {
  5274. int item_id=-1;
  5275. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5276. sqlite3_stmt * statement;
  5277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5278. {
  5279. if (sqlite3_step(statement) == SQLITE_ROW)
  5280. {
  5281. item_id = sqlite3_column_int(statement, 0);
  5282. }
  5283. sqlite3_finalize(statement);
  5284. }
  5285. return item_id;
  5286. }
  5287. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5288. {
  5289. // NSString* ret = @"";
  5290. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5291. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5292. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5293. sqlite3_stmt * statement;
  5294. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5295. {
  5296. while (sqlite3_step(statement) == SQLITE_ROW)
  5297. {
  5298. int bitem_id = sqlite3_column_int(statement, 0);
  5299. int bitem_qty = sqlite3_column_int(statement, 1);
  5300. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5301. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5302. }
  5303. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5304. sqlite3_finalize(statement);
  5305. }
  5306. // if(ret==nil)
  5307. // ret=@"";
  5308. *count=arr_count;
  5309. return arr_bundle;
  5310. }
  5311. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5312. {
  5313. // get default sold qty, return -1 if model not found;
  5314. int ret = -1;
  5315. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5316. sqlite3_stmt * statement;
  5317. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5318. {
  5319. if (sqlite3_step(statement) == SQLITE_ROW)
  5320. {
  5321. ret = sqlite3_column_int(statement, 0);
  5322. }
  5323. sqlite3_finalize(statement);
  5324. }
  5325. return ret;
  5326. }
  5327. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5328. {
  5329. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5330. [ret setValue:@"0" forKey:@"img_count"];
  5331. // sqlite3 *db = [iSalesDB get_db];
  5332. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5333. sqlite3_stmt * statement;
  5334. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5335. {
  5336. int i=0;
  5337. while (sqlite3_step(statement) == SQLITE_ROW)
  5338. {
  5339. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5340. // char *name = (char*)sqlite3_column_text(statement, 1);
  5341. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5342. char *url = (char*)sqlite3_column_text(statement, 0);
  5343. if(url==nil)
  5344. url="";
  5345. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5346. [item setValue:nsurl forKey:@"s"];
  5347. [item setValue:nsurl forKey:@"l"];
  5348. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5349. i++;
  5350. }
  5351. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5352. sqlite3_finalize(statement);
  5353. }
  5354. // [iSalesDB close_db:db];
  5355. return ret;
  5356. }
  5357. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5358. // UIApplication * app = [UIApplication sharedApplication];
  5359. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5360. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5361. sqlite3 *db = [iSalesDB get_db];
  5362. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5363. NSString* product_id=params[@"product_id"];
  5364. NSString *item_count_str = params[@"item_count"];
  5365. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5366. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5367. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5368. // NSString *sql = @"";
  5369. for(int i=0;i<arr.count;i++)
  5370. {
  5371. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5372. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5373. __block int cart_count = 0;
  5374. if (!item_count_str) {
  5375. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5376. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5377. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5378. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5379. cart_count = [[model_set_components lastObject] intValue];
  5380. }];
  5381. }
  5382. if(count==0)
  5383. {
  5384. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5385. sqlite3_stmt *stmt;
  5386. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5387. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5388. if (item_count_arr) {
  5389. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5390. } else {
  5391. sqlite3_bind_int(stmt,2,cart_count);
  5392. }
  5393. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5394. [iSalesDB execSql:@"ROLLBACK" db:db];
  5395. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5396. [iSalesDB close_db:db];
  5397. DebugLog(@"add to wishlist error");
  5398. return ret;
  5399. }
  5400. } else {
  5401. int qty = 0;
  5402. if (item_count_arr) {
  5403. qty = [item_count_arr[i] intValue];
  5404. } else {
  5405. qty = cart_count;
  5406. }
  5407. 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]];
  5408. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5409. [iSalesDB execSql:@"ROLLBACK" db:db];
  5410. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5411. [iSalesDB close_db:db];
  5412. DebugLog(@"add to wishlist error");
  5413. return ret;
  5414. }
  5415. }
  5416. }
  5417. // [iSalesDB execSql:sql db:db];
  5418. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5419. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5420. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5421. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5422. [iSalesDB close_db:db];
  5423. ret[@"wish_count"]=@(count);
  5424. ret[@"result"]= [NSNumber numberWithInt:2];
  5425. return ret;
  5426. }
  5427. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5428. // 0 category
  5429. // 1 search
  5430. // 2 itemsearch
  5431. NSData *ret = nil;
  5432. NSDictionary *items = nil;
  5433. switch (from) {
  5434. case 0:{
  5435. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5436. }
  5437. break;
  5438. case 1:{
  5439. items = [[self search:params limited:NO] objectForKey:@"items"];
  5440. }
  5441. break;
  5442. case 2:{
  5443. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5444. }
  5445. break;
  5446. default:
  5447. break;
  5448. }
  5449. if (!items) {
  5450. return ret;
  5451. }
  5452. int count = [[items objectForKey:@"count"] intValue];
  5453. NSMutableString *product_id_str = [@"" mutableCopy];
  5454. for (int i = 0; i < count; i++) {
  5455. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5456. NSDictionary *item = [items objectForKey:key];
  5457. NSString *product_id = [item objectForKey:@"product_id"];
  5458. if (i == 0) {
  5459. [product_id_str appendString:product_id];
  5460. } else {
  5461. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5462. }
  5463. }
  5464. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5465. if ([add_to isEqualToString:@"cart"]) {
  5466. NSString *order_code = [params objectForKey:@"orderCode"];
  5467. if (order_code.length) {
  5468. NSDictionary *newParams = @{
  5469. @"product_id" : product_id_str,
  5470. @"orderCode" : order_code,
  5471. @"can_create_backorder":params[@"can_create_backorder"]
  5472. };
  5473. ret = [self offline_add2cart:newParams.mutableCopy];
  5474. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5475. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5476. ret = [RAConvertor dict2data:retDic];
  5477. }
  5478. } else if([add_to isEqualToString:@"wishlist"]) {
  5479. NSDictionary *newParams = @{
  5480. @"product_id" : product_id_str
  5481. };
  5482. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5483. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5484. ret = [RAConvertor dict2data:retDic];
  5485. } else if([add_to isEqualToString:@"portfolio"]) {
  5486. NSDictionary *newParams = @{
  5487. @"product_id" : product_id_str
  5488. };
  5489. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5490. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5491. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5492. ret = [RAConvertor dict2data:retDic];
  5493. }
  5494. return ret;
  5495. }
  5496. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5497. {
  5498. return [self addAll:params from:0];
  5499. }
  5500. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5501. {
  5502. return [self addAll:params from:1];
  5503. }
  5504. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5505. {
  5506. return [self addAll:params from:2];
  5507. }
  5508. #pragma mark - Jack
  5509. #warning 做SQL操作时转义!!
  5510. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5511. // "val_227" : {
  5512. // "check" : 1,
  5513. // "value" : "US United States",
  5514. // "value_id" : "228"
  5515. // },
  5516. if (!countryCode) {
  5517. countryCode = @"US";
  5518. }
  5519. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5520. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5521. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5522. int code_id = sqlite3_column_int(stmt, 3); // id
  5523. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5524. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5525. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5526. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5527. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5528. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5529. }
  5530. long n = *count;
  5531. *count = n + 1;
  5532. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5533. [container setValue:countryDic forKey:key];
  5534. }] mutableCopy];
  5535. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5536. return ret;
  5537. }
  5538. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5539. // "val_227" : {
  5540. // "check" : 1,
  5541. // "value" : "US United States",
  5542. // "value_id" : "228"
  5543. // },
  5544. __block NSString* OrderCarrier =@"";
  5545. // sqlite3 *db = [iSalesDB get_db];
  5546. // sqlite3_stmt * statement;
  5547. [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) {
  5548. OrderCarrier = [self textAtColumn:0 statement:statment];
  5549. // sqlite3_finalize(statement);
  5550. }];
  5551. // [iSalesDB close_db:db];
  5552. __block int c=0;
  5553. 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) {
  5554. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5555. int code_id = sqlite3_column_int(stmt, 2); // id
  5556. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5557. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5558. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5559. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5560. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5561. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5562. }
  5563. c++;
  5564. long n = *count;
  5565. *count = n + 1;
  5566. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5567. [container setValue:carrierDic forKey:key];
  5568. }] mutableCopy];
  5569. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5570. return ret;
  5571. }
  5572. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5573. // "val_227" : {
  5574. // "check" : 1,
  5575. // "value" : "US United States",
  5576. // "value_id" : "228"
  5577. // },
  5578. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5579. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5580. int code_id = sqlite3_column_int(stmt, 2); // id
  5581. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5582. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5583. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5584. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5585. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5586. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5587. }
  5588. long n = *count;
  5589. *count = n + 1;
  5590. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5591. [container setValue:carrierDic forKey:key];
  5592. }] mutableCopy];
  5593. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5594. return ret;
  5595. }
  5596. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5597. // // "val_227" : {
  5598. // // "check" : 1,
  5599. // // "value" : "US United States",
  5600. // // "value_id" : "228"
  5601. // // },
  5602. //
  5603. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5604. // NSString * code = [iSalesDB jk_queryText:Sql];
  5605. //
  5606. // return [self offline_getCarrier:code];
  5607. //
  5608. //}
  5609. + (NSString *)offline_getCarrier:(int)code {
  5610. // "val_227" : {
  5611. // "check" : 1,
  5612. // "value" : "US United States",
  5613. // "value_id" : "228"
  5614. // },
  5615. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5616. NSString * ret = [iSalesDB jk_queryText:Sql];
  5617. return ret;
  5618. }
  5619. + (int )offline_getCarrierCode:(NSString*)name {
  5620. // "val_227" : {
  5621. // "check" : 1,
  5622. // "value" : "US United States",
  5623. // "value_id" : "228"
  5624. // },
  5625. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5626. NSString * ret = [iSalesDB jk_queryText:Sql];
  5627. return [ret intValue];
  5628. }
  5629. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5630. countryCode = [self translateSingleQuote:countryCode];
  5631. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5632. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5633. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5634. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5635. if (name == NULL) {
  5636. name = "";
  5637. }
  5638. if (code == NULL) {
  5639. code = "";
  5640. }
  5641. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5642. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5643. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5644. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5645. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5646. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5647. }
  5648. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5649. [container setValue:stateDic forKey:key];
  5650. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5651. DebugLog(@"query all state error: %@",err_msg);
  5652. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5653. // [stateDic setValue:@"Other" forKey:@"value"];
  5654. // [stateDic setValue:@"" forKey:@"value_id"];
  5655. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5656. //
  5657. // if (state_code && [@"" isEqualToString:state_code]) {
  5658. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5659. // }
  5660. //
  5661. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5662. // [container setValue:stateDic forKey:key];
  5663. }] mutableCopy];
  5664. [ret removeObjectForKey:@"result"];
  5665. // failure 可以不用了,一样的
  5666. if (ret.allKeys.count == 0) {
  5667. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5668. [stateDic setValue:@"Other" forKey:@"value"];
  5669. [stateDic setValue:@"" forKey:@"value_id"];
  5670. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5671. if (state_code && [@"" isEqualToString:state_code]) {
  5672. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5673. }
  5674. NSString *key = [NSString stringWithFormat:@"val_0"];
  5675. [ret setValue:stateDic forKey:key];
  5676. }
  5677. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5678. return ret;
  5679. }
  5680. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5681. codeId = [self translateSingleQuote:codeId];
  5682. 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];
  5683. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5684. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5685. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5686. if (name == NULL) {
  5687. name = "";
  5688. }
  5689. if (code == NULL) {
  5690. code = "";
  5691. }
  5692. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5693. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5694. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5695. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5696. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5697. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5698. }
  5699. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5700. [container setValue:stateDic forKey:key];
  5701. }] mutableCopy];
  5702. [ret removeObjectForKey:@"result"];
  5703. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5704. return ret;
  5705. }
  5706. + (NSDictionary *)offline_getPrice {
  5707. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5708. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5709. char *name = (char *) sqlite3_column_text(stmt, 1);
  5710. int type = sqlite3_column_int(stmt, 2);
  5711. int orderBy = sqlite3_column_int(stmt, 3);
  5712. if (name == NULL) {
  5713. name = "";
  5714. }
  5715. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5716. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5717. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5718. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5719. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5720. if (orderBy == 0) {
  5721. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5722. }
  5723. [container setValue:priceDic forKey:key];
  5724. }] mutableCopy];
  5725. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5726. return ret;
  5727. }
  5728. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5729. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5730. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5731. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5732. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5733. // int _id = sqlite3_column_int(stmt, 0);
  5734. NSString *name = [self textAtColumn:1 statement:stmt];
  5735. NSDictionary *typeDic = @{
  5736. @"value_id" : name,
  5737. @"value" : name,
  5738. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5739. };
  5740. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5741. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5742. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5743. }];
  5744. return ret;
  5745. }
  5746. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5747. // 首先从offline_login表中取出sales_code
  5748. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5749. // NSString *user = app.user;
  5750. user = [self translateSingleQuote:user];
  5751. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5752. __block NSString *user_code = @"";
  5753. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5754. char *code = (char *)sqlite3_column_text(stmt, 0);
  5755. if (code == NULL) {
  5756. code = "";
  5757. }
  5758. user_code = [NSString stringWithUTF8String:code];
  5759. }];
  5760. // 再取所有salesRep
  5761. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5762. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5763. // 1 name 2 code 3 salesrep_id
  5764. char *name = (char *)sqlite3_column_text(stmt, 1);
  5765. char *code = (char *)sqlite3_column_text(stmt, 2);
  5766. int salesrep_id = sqlite3_column_int(stmt, 3);
  5767. if (name == NULL) {
  5768. name = "";
  5769. }
  5770. if (code == NULL) {
  5771. code = "";
  5772. }
  5773. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5774. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5775. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5776. // 比较code 相等则check
  5777. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5778. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5779. }
  5780. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5781. }] mutableCopy];
  5782. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5783. return ret;
  5784. }
  5785. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5786. zipcode = [self translateSingleQuote:zipcode];
  5787. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5788. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5789. char *country = (char *)sqlite3_column_text(stmt, 0);
  5790. char *state = (char *)sqlite3_column_text(stmt, 1);
  5791. char *city = (char *)sqlite3_column_text(stmt, 2);
  5792. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5793. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5794. if (country == NULL) {
  5795. country = "";
  5796. }
  5797. if (state == NULL) {
  5798. state = "";
  5799. }
  5800. if (city == NULL) {
  5801. city = "";
  5802. }
  5803. if (country_code == NULL) {
  5804. country_code = "";
  5805. }
  5806. if (state_code == NULL) {
  5807. state_code = "";
  5808. }
  5809. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5810. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5811. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5812. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5813. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5814. }] mutableCopy];
  5815. return ret;
  5816. }
  5817. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5818. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5819. [item setValue:value forKey:valueKey];
  5820. [dic setValue:item forKey:itemKey];
  5821. }
  5822. + (NSString *)countryCodeByid:(NSString *)code_id {
  5823. NSString *ret = nil;
  5824. code_id = [self translateSingleQuote:code_id];
  5825. sqlite3 *db = [iSalesDB get_db];
  5826. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5827. sqlite3_stmt * statement;
  5828. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5829. while (sqlite3_step(statement) == SQLITE_ROW) {
  5830. char *code = (char *)sqlite3_column_text(statement, 0);
  5831. if (code == NULL) {
  5832. code = "";
  5833. }
  5834. ret = [NSString stringWithUTF8String:code];
  5835. }
  5836. sqlite3_finalize(statement);
  5837. }
  5838. [iSalesDB close_db:db];
  5839. return ret;
  5840. }
  5841. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5842. NSString *ret = nil;
  5843. code = [self translateSingleQuote:code];
  5844. sqlite3 *db = [iSalesDB get_db];
  5845. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5846. sqlite3_stmt * statement;
  5847. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5848. while (sqlite3_step(statement) == SQLITE_ROW) {
  5849. char *_id = (char *)sqlite3_column_text(statement, 0);
  5850. if (_id == NULL) {
  5851. _id = "";
  5852. }
  5853. ret = [NSString stringWithFormat:@"%s",_id];
  5854. }
  5855. sqlite3_finalize(statement);
  5856. }
  5857. [iSalesDB close_db:db];
  5858. return ret;
  5859. }
  5860. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5861. NSString *name = nil;
  5862. codeId = [self translateSingleQuote:codeId];
  5863. sqlite3 *db = [iSalesDB get_db];
  5864. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5865. sqlite3_stmt * statement;
  5866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5867. while (sqlite3_step(statement) == SQLITE_ROW) {
  5868. char *value = (char *)sqlite3_column_text(statement, 0);
  5869. if (value == NULL) {
  5870. value = "";
  5871. }
  5872. name = [NSString stringWithUTF8String:value];
  5873. }
  5874. sqlite3_finalize(statement);
  5875. }
  5876. [iSalesDB close_db:db];
  5877. return name;
  5878. }
  5879. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5880. NSString *ret = nil;
  5881. sqlite3 *db = [iSalesDB get_db];
  5882. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5883. sqlite3_stmt * statement;
  5884. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5885. while (sqlite3_step(statement) == SQLITE_ROW) {
  5886. char *name = (char *)sqlite3_column_text(statement, 0);
  5887. if (name == NULL) {
  5888. name = "";
  5889. }
  5890. ret = [NSString stringWithUTF8String:name];
  5891. }
  5892. sqlite3_finalize(statement);
  5893. }
  5894. [iSalesDB close_db:db];
  5895. return ret;
  5896. }
  5897. + (NSString *)salesRepCodeById:(NSString *)_id {
  5898. NSString *ret = nil;
  5899. _id = [self translateSingleQuote:_id];
  5900. sqlite3 *db = [iSalesDB get_db];
  5901. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5902. sqlite3_stmt * statement;
  5903. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5904. while (sqlite3_step(statement) == SQLITE_ROW) {
  5905. char *rep = (char *)sqlite3_column_text(statement, 0);
  5906. if (rep == NULL) {
  5907. rep = "";
  5908. }
  5909. ret = [NSString stringWithUTF8String:rep];
  5910. }
  5911. sqlite3_finalize(statement);
  5912. }
  5913. [iSalesDB close_db:db];
  5914. return ret;
  5915. }
  5916. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5917. char *tx = (char *)sqlite3_column_text(stmt, col);
  5918. if (tx == NULL) {
  5919. tx = "";
  5920. }
  5921. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5922. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5923. if (!text) {
  5924. text = @"";
  5925. }
  5926. // 将字符全部为' '的字符串干掉
  5927. int spaceCount = 0;
  5928. for (int i = 0; i < text.length; i++) {
  5929. if ([text characterAtIndex:i] == ' ') {
  5930. spaceCount++;
  5931. }
  5932. }
  5933. if (spaceCount == text.length) {
  5934. text = @"";
  5935. }
  5936. return text;
  5937. }
  5938. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5939. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5940. NSData *data = [NSData dataWithContentsOfFile:path];
  5941. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5942. return ret;
  5943. }
  5944. + (NSString *)textFileName:(NSString *)name {
  5945. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5946. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5947. if (!text) {
  5948. text = @"";
  5949. }
  5950. return text;
  5951. }
  5952. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5953. return [[dic objectForKey:key] mutableCopy];
  5954. }
  5955. + (id)translateSingleQuote:(NSString *)string {
  5956. if ([string isKindOfClass:[NSString class]])
  5957. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5958. return string;
  5959. }
  5960. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5961. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5962. }
  5963. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5964. NSString* ret= nil;
  5965. NSString *sqlQuery = nil;
  5966. // 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
  5967. 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];
  5968. sqlite3_stmt * statement;
  5969. // int count=0;
  5970. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5971. {
  5972. if (sqlite3_step(statement) == SQLITE_ROW)
  5973. {
  5974. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5975. if(imgurl==nil)
  5976. imgurl="";
  5977. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5978. ret=nsimgurl;
  5979. }
  5980. sqlite3_finalize(statement);
  5981. }
  5982. else
  5983. {
  5984. [ret setValue:@"8" forKey:@"result"];
  5985. }
  5986. // [iSalesDB close_db:db];
  5987. // DebugLog(@"data string: %@",ret );
  5988. return ret;
  5989. }
  5990. #pragma mark contact Advanced search
  5991. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5992. {
  5993. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5994. return [RAConvertor dict2data:contactAdvanceDic];
  5995. }
  5996. #pragma mark create new contact
  5997. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5998. assert(params[@"user"]!=nil);
  5999. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  6000. NSData *data = [NSData dataWithContentsOfFile:path];
  6001. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6002. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6003. NSString *countryCode = nil;
  6004. NSString *countryCode_id = nil;
  6005. NSString *stateCode = nil;
  6006. NSString *city = nil;
  6007. NSString *zipCode = nil;
  6008. NSString *carrierCode = nil;
  6009. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6010. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  6011. NSString *code_id = params[@"country"];
  6012. countryCode_id = code_id;
  6013. countryCode = [self countryCodeByid:code_id];
  6014. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  6015. NSString *zip_code = params[@"zipcode"];
  6016. // 剔除全部为空格
  6017. int spaceCount = 0;
  6018. for (int i = 0; i < zip_code.length; i++) {
  6019. if ([zip_code characterAtIndex:i] == ' ') {
  6020. spaceCount++;
  6021. }
  6022. }
  6023. if (spaceCount == zip_code.length) {
  6024. zip_code = @"";
  6025. }
  6026. zipCode = zip_code;
  6027. if (zipCode.length > 0) {
  6028. countryCode_id = params[@"country"];
  6029. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6030. countryCode = [dic valueForKey:@"country_code"];
  6031. if (!countryCode) {
  6032. // countryCode = @"US";
  6033. NSString *code_id = params[@"country"];
  6034. countryCode = [self countryCodeByid:code_id];
  6035. }
  6036. stateCode = [dic valueForKey:@"state_code"];
  6037. if (!stateCode.length) {
  6038. stateCode = params[@"state"];
  6039. }
  6040. city = [dic valueForKey:@"city"];
  6041. if (!city.length) {
  6042. city = params[@"city"];
  6043. }
  6044. // zip code
  6045. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  6046. [zipDic setValue:zipCode forKey:@"value"];
  6047. [section_0 setValue:zipDic forKey:@"item_11"];
  6048. } else {
  6049. NSString *code_id = params[@"country"];
  6050. countryCode = [self countryCodeByid:code_id];
  6051. stateCode = params[@"state"];
  6052. city = params[@"city"];
  6053. }
  6054. }
  6055. } else {
  6056. // default: US United States
  6057. countryCode = @"US";
  6058. countryCode_id = @"228";
  6059. }
  6060. // country
  6061. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6062. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  6063. // state
  6064. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6065. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  6066. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  6067. // city
  6068. if (city) {
  6069. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  6070. [cityDic setValue:city forKey:@"value"];
  6071. [section_0 setValue:cityDic forKey:@"item_13"];
  6072. }
  6073. // price type
  6074. NSDictionary *priceDic = [self offline_getPrice];
  6075. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  6076. // contact type
  6077. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  6078. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  6079. // Sales Rep
  6080. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  6081. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6082. #ifdef BUILD_CONTRAST
  6083. //carrier
  6084. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  6085. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6086. #endif
  6087. [ret setValue:section_0 forKey:@"section_0"];
  6088. return [RAConvertor dict2data:ret];
  6089. }
  6090. #pragma mark save
  6091. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  6092. NSString *addr = nil;
  6093. NSString *contact_name = nil;
  6094. // int carrier = [params[@"carrier"] intValue];
  6095. NSString *companyName = [params objectForKey:@"company"];
  6096. if (companyName) {
  6097. companyName = [AESCrypt fastencrypt:companyName];
  6098. } else {
  6099. companyName = @"";
  6100. }
  6101. DebugLog(@"company");
  6102. companyName = [self translateSingleQuote:companyName];
  6103. NSString *addr1 = [params objectForKey:@"address"];
  6104. NSString *addr2 = [params objectForKey:@"address2"];
  6105. NSString *addr3 = [params objectForKey:@"address_3"];
  6106. NSString *addr4 = [params objectForKey:@"address_4"];
  6107. if (!addr2) {
  6108. addr2 = @"";
  6109. }
  6110. if (!addr3) {
  6111. addr3 = @"";
  6112. }
  6113. if (!addr4) {
  6114. addr4 = @"";
  6115. }
  6116. if (!addr1) {
  6117. addr1 = @"";
  6118. }
  6119. DebugLog(@"addr");
  6120. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  6121. addr = [AESCrypt fastencrypt:addr];
  6122. addr = [self translateSingleQuote:addr];
  6123. if (addr1 && ![addr1 isEqualToString:@""]) {
  6124. addr1 = [AESCrypt fastencrypt:addr1];
  6125. }
  6126. addr1 = [self translateSingleQuote:addr1];
  6127. addr2 = [self translateSingleQuote:addr2];
  6128. addr3 = [self translateSingleQuote:addr3];
  6129. addr4 = [self translateSingleQuote:addr4];
  6130. NSString *country = [params objectForKey:@"country"];
  6131. if (country) {
  6132. country = [self countryNameByCountryCodeId:country];
  6133. } else {
  6134. country = @"";
  6135. }
  6136. int carrier = [params[@"carrier"] intValue];
  6137. DebugLog(@"country");
  6138. country = [self translateSingleQuote:country];
  6139. NSString *state = [params objectForKey:@"state"];
  6140. if (!state) {
  6141. state = @"";
  6142. }
  6143. DebugLog(@"state");
  6144. state = [self translateSingleQuote:state];
  6145. NSString *city = [params objectForKey:@"city"];
  6146. if (!city) {
  6147. city = @"";
  6148. }
  6149. city = [self translateSingleQuote:city];
  6150. NSString *zipcode = [params objectForKey:@"zipcode"];
  6151. if (!zipcode) {
  6152. zipcode = @"";
  6153. }
  6154. DebugLog(@"zip");
  6155. zipcode = [self translateSingleQuote:zipcode];
  6156. NSString *fistName = [params objectForKey:@"firstname"];
  6157. if (!fistName) {
  6158. fistName = @"";
  6159. }
  6160. NSString *lastName = [params objectForKey:@"lastname"];
  6161. if (!lastName) {
  6162. lastName = @"";
  6163. }
  6164. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  6165. DebugLog(@"contact_name");
  6166. contact_name = [self translateSingleQuote:contact_name];
  6167. fistName = [self translateSingleQuote:fistName];
  6168. lastName = [self translateSingleQuote:lastName];
  6169. NSString *phone = [params objectForKey:@"phone"];
  6170. if (phone) {
  6171. phone = [AESCrypt fastencrypt:phone];
  6172. } else {
  6173. phone = @"";
  6174. }
  6175. DebugLog(@"PHONE");
  6176. phone = [self translateSingleQuote:phone];
  6177. NSString *fax = [params objectForKey:@"fax"];
  6178. if (!fax) {
  6179. fax = @"";
  6180. }
  6181. DebugLog(@"FAX");
  6182. fax = [self translateSingleQuote:fax];
  6183. NSString *email = [params objectForKey:@"email"];
  6184. if (!email) {
  6185. email = @"";
  6186. }
  6187. DebugLog(@"EMAIL:%@",email);
  6188. email = [self translateSingleQuote:email];
  6189. NSString *notes = [params objectForKey:@"contact_notes"];
  6190. if (!notes) {
  6191. notes = @"";
  6192. }
  6193. DebugLog(@"NOTE:%@",notes);
  6194. notes = [self translateSingleQuote:notes];
  6195. NSString *price = [params objectForKey:@"price_name"];
  6196. if (price) {
  6197. price = [self priceNameByPriceId:price];
  6198. } else {
  6199. price = @"";
  6200. }
  6201. DebugLog(@"PRICE");
  6202. price = [self translateSingleQuote:price];
  6203. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6204. if (salesRep) {
  6205. salesRep = [self salesRepCodeById:salesRep];
  6206. } else {
  6207. salesRep = @"";
  6208. }
  6209. salesRep = [self translateSingleQuote:salesRep];
  6210. NSString *img = [params objectForKey:@"business_card"];
  6211. NSArray *array = [img componentsSeparatedByString:@","];
  6212. NSString *img_0 = array[0];
  6213. if (!img_0) {
  6214. img_0 = @"";
  6215. }
  6216. img_0 = [self translateSingleQuote:img_0];
  6217. NSString *img_1 = array[1];
  6218. if (!img_1) {
  6219. img_1 = @"";
  6220. }
  6221. img_1 = [self translateSingleQuote:img_1];
  6222. NSString *img_2 = array[2];
  6223. if (!img_2) {
  6224. img_2 = @"";
  6225. }
  6226. img_2 = [self translateSingleQuote:img_2];
  6227. NSString *contact_id = [NSUUID UUID].UUIDString;
  6228. NSString *contact_type = [params objectForKey:@"type_name"];
  6229. if (!contact_type) {
  6230. contact_type = @"";
  6231. }
  6232. contact_type = [self translateSingleQuote:contact_type];
  6233. // 判断更新时是否为customer
  6234. if (update) {
  6235. contact_id = [params objectForKey:@"contact_id"];
  6236. if (!contact_id) {
  6237. contact_id = @"";
  6238. }
  6239. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6240. __block int customer = 0;
  6241. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6242. customer = sqlite3_column_int(stmt, 0);
  6243. }];
  6244. isCustomer = customer ? YES : NO;
  6245. }
  6246. NSMutableDictionary *sync_dic = [params mutableCopy];
  6247. if (isCustomer) {
  6248. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6249. } else {
  6250. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6251. }
  6252. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  6253. NSString *sync_data = nil;
  6254. NSString *sql = nil;
  6255. if (update){
  6256. contact_id = [params objectForKey:@"contact_id"];
  6257. if (!contact_id) {
  6258. contact_id = @"";
  6259. }
  6260. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6261. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6262. sync_data = [RAConvertor dict2string:sync_dic];
  6263. sync_data = [self translateSingleQuote:sync_data];
  6264. 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];
  6265. } else {
  6266. contact_id = [self translateSingleQuote:contact_id];
  6267. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6268. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6269. sync_data = [RAConvertor dict2string:sync_dic];
  6270. sync_data = [self translateSingleQuote:sync_data];
  6271. 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];
  6272. }
  6273. int result = [iSalesDB execSql:sql];
  6274. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6275. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6276. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6277. }
  6278. #pragma mark save new contact
  6279. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6280. {
  6281. return [self offline_saveContact:params update:NO isCustomer:YES];
  6282. }
  6283. #pragma mark edit contact
  6284. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6285. {
  6286. assert(params[@"user"]!=nil);
  6287. // {
  6288. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6289. // password = 123456;
  6290. // user = EvanK;
  6291. // }
  6292. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6293. NSData *data = [NSData dataWithContentsOfFile:path];
  6294. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6295. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6296. NSString *countryCode = nil;
  6297. NSString *countryCode_id = nil;
  6298. NSString *stateCode = nil;
  6299. __block int carrierCode = -1;
  6300. /*------contact infor------*/
  6301. __block NSString *country = nil;
  6302. __block NSString *company_name = nil;
  6303. __block NSString *contact_id = params[@"contact_id"];
  6304. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6305. __block NSString *zipcode = nil;
  6306. __block NSString *state = nil; // state_code
  6307. __block NSString *city = nil; //
  6308. __block NSString *firt_name,*last_name;
  6309. __block NSString *phone,*fax,*email;
  6310. __block NSString *notes,*price_type,*sales_rep;
  6311. __block NSString *img_0,*img_1,*img_2;
  6312. __block NSString *contact_type;
  6313. contact_id = [self translateSingleQuote:contact_id];
  6314. 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];
  6315. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6316. country = [self textAtColumn:0 statement:stmt]; // country name
  6317. company_name = [self textAtColumn:1 statement:stmt];
  6318. addr_1 = [self textAtColumn:2 statement:stmt];
  6319. addr_2 = [self textAtColumn:3 statement:stmt];
  6320. addr_3 = [self textAtColumn:4 statement:stmt];
  6321. addr_4 = [self textAtColumn:5 statement:stmt];
  6322. zipcode = [self textAtColumn:6 statement:stmt];
  6323. state = [self textAtColumn:7 statement:stmt]; // state code
  6324. city = [self textAtColumn:8 statement:stmt];
  6325. firt_name = [self textAtColumn:9 statement:stmt];
  6326. last_name = [self textAtColumn:10 statement:stmt];
  6327. phone = [self textAtColumn:11 statement:stmt];
  6328. fax = [self textAtColumn:12 statement:stmt];
  6329. email = [self textAtColumn:13 statement:stmt];
  6330. notes = [self textAtColumn:14 statement:stmt];
  6331. price_type = [self textAtColumn:15 statement:stmt]; // name
  6332. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6333. img_0 = [self textAtColumn:17 statement:stmt];
  6334. img_1 = [self textAtColumn:18 statement:stmt];
  6335. img_2 = [self textAtColumn:19 statement:stmt];
  6336. contact_type = [self textAtColumn:20 statement:stmt];
  6337. carrierCode = sqlite3_column_int(stmt, 21);
  6338. }];
  6339. // decrypt
  6340. if (company_name) {
  6341. company_name = [AESCrypt fastdecrypt:company_name];
  6342. }
  6343. if (addr_1) {
  6344. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6345. }
  6346. if (phone) {
  6347. phone = [AESCrypt fastdecrypt:phone];
  6348. }
  6349. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6350. countryCode = [iSalesDB jk_queryText:countrySql];
  6351. stateCode = state;
  6352. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6353. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6354. NSString *code_id = params[@"country"];
  6355. countryCode_id = code_id;
  6356. countryCode = [self countryCodeByid:code_id];
  6357. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6358. NSString *zip_code = params[@"zipcode"];
  6359. // 剔除全部为空格
  6360. int spaceCount = 0;
  6361. for (int i = 0; i < zip_code.length; i++) {
  6362. if ([zip_code characterAtIndex:i] == ' ') {
  6363. spaceCount++;
  6364. }
  6365. }
  6366. if (spaceCount == zip_code.length) {
  6367. zip_code = @"";
  6368. }
  6369. if (zipcode.length > 0) {
  6370. zipcode = zip_code;
  6371. countryCode_id = params[@"country"];
  6372. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6373. countryCode = [dic valueForKey:@"country_code"];
  6374. if (!countryCode) {
  6375. NSString *code_id = params[@"country"];
  6376. countryCode = [self countryCodeByid:code_id];
  6377. }
  6378. stateCode = [dic valueForKey:@"state_code"];
  6379. if (!stateCode.length) {
  6380. stateCode = params[@"state"];
  6381. }
  6382. city = [dic valueForKey:@"city"];
  6383. if (!city.length) {
  6384. city = params[@"city"];
  6385. }
  6386. // zip code
  6387. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6388. [zipDic setValue:zipcode forKey:@"value"];
  6389. [section_0 setValue:zipDic forKey:@"item_8"];
  6390. } else {
  6391. NSString *code_id = params[@"country"];
  6392. countryCode = [self countryCodeByid:code_id];
  6393. stateCode = params[@"state"];
  6394. city = params[@"city"];
  6395. }
  6396. }
  6397. }
  6398. // 0 Country
  6399. // 1 Company Name
  6400. // 2 Contact ID
  6401. // 3 Picture
  6402. // 4 Address 1
  6403. // 5 Address 2
  6404. // 6 Address 3
  6405. // 7 Address 4
  6406. // 8 Zip Code
  6407. // 9 State/Province
  6408. // 10 City
  6409. // 11 Contact First Name
  6410. // 12 Contact Last Name
  6411. // 13 Phone
  6412. // 14 Fax
  6413. // 15 Email
  6414. // 16 Contact Notes
  6415. // 17 Price Type
  6416. // 18 Contact Type
  6417. // 19 Sales Rep
  6418. // country
  6419. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6420. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6421. // company
  6422. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6423. // contact_id
  6424. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6425. // picture
  6426. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6427. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6428. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6429. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6430. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6431. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6432. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6433. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6434. // addr 1 2 3 4
  6435. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6436. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6437. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6438. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6439. // zip code
  6440. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6441. // state
  6442. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6443. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6444. // city
  6445. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6446. // first last
  6447. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6448. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6449. // phone fax email
  6450. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6451. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6452. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6453. // notes
  6454. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6455. // price
  6456. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6457. for (NSString *key in priceDic.allKeys) {
  6458. if ([key containsString:@"val_"]) {
  6459. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6460. if ([dic[@"value"] isEqualToString:price_type]) {
  6461. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6462. [priceDic setValue:dic forKey:key];
  6463. }
  6464. }
  6465. }
  6466. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6467. // Contact Rep
  6468. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6469. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6470. // Sales Rep
  6471. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6472. for (NSString *key in repDic.allKeys) {
  6473. if ([key containsString:@"val_"]) {
  6474. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6475. NSString *value = dic[@"value"];
  6476. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6477. if (code && [code isEqualToString:sales_rep]) {
  6478. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6479. [repDic setValue:dic forKey:key];
  6480. }
  6481. }
  6482. }
  6483. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6484. #ifdef BUILD_CONTRAST
  6485. //carrier
  6486. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6487. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6488. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6489. #endif
  6490. [ret setValue:section_0 forKey:@"section_0"];
  6491. return [RAConvertor dict2data:ret];
  6492. }
  6493. #pragma mark save contact
  6494. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6495. {
  6496. return [self offline_saveContact:params update:YES isCustomer:YES];
  6497. }
  6498. #pragma mark category
  6499. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6500. if (ck) {
  6501. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6502. for (NSString *key in res.allKeys) {
  6503. if (![key isEqualToString:@"count"]) {
  6504. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6505. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6506. if ([val[@"value"] isEqualToString:ck]) {
  6507. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6508. }
  6509. [res setValue:val forKey:key];
  6510. }
  6511. }
  6512. [dic setValue:res forKey:valueKey];
  6513. }
  6514. }
  6515. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6516. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6517. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6518. NSString* category = [params valueForKey:@"category"];
  6519. if (!category || [category isEqualToString:@""]) {
  6520. category = @"%";
  6521. }
  6522. category = [self translateSingleQuote:category];
  6523. int limit = [[params valueForKey:@"limit"] intValue];
  6524. int offset = [[params valueForKey:@"offset"] intValue];
  6525. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6526. NSString *limit_str = @"";
  6527. if (limited) {
  6528. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6529. }
  6530. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6531. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6532. sqlite3 *db = [iSalesDB get_db];
  6533. // [iSalesDB AddExFunction:db];
  6534. int count;
  6535. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6536. 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];
  6537. double price_min = 0;
  6538. double price_max = 0;
  6539. if ([params.allKeys containsObject:@"alert"]) {
  6540. // alert
  6541. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6542. NSString *alert = params[@"alert"];
  6543. if ([alert isEqualToString:@"Display All"]) {
  6544. alert = [NSString stringWithFormat:@""];
  6545. } else {
  6546. alert = [self translateSingleQuote:alert];
  6547. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6548. }
  6549. // available
  6550. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6551. NSString *available = params[@"available"];
  6552. NSString *available_condition;
  6553. if ([available isEqualToString:@"Display All"]) {
  6554. available_condition = @"";
  6555. } else if ([available isEqualToString:@"Available Now"]) {
  6556. available_condition = @"and availability > 0";
  6557. } else {
  6558. available_condition = @"and availability == 0";
  6559. }
  6560. // best seller
  6561. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6562. NSString *best_seller = @"";
  6563. NSString *order_best_seller = @"m.name asc";
  6564. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6565. best_seller = @"and best_seller > 0";
  6566. order_best_seller = @"m.best_seller desc,m.name asc";
  6567. }
  6568. // price
  6569. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6570. NSString *price = params[@"price"];
  6571. price_min = 0;
  6572. price_max = MAXFLOAT;
  6573. if (params[@"user"] && price != nil) {
  6574. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6575. NSMutableString *priceName = [NSMutableString string];
  6576. for (int i = 0; i < priceTypeArray.count; i++) {
  6577. NSString *pricetype = priceTypeArray[i];
  6578. pricetype = [self translateSingleQuote:pricetype];
  6579. if (i == 0) {
  6580. [priceName appendFormat:@"'%@'",pricetype];
  6581. } else {
  6582. [priceName appendFormat:@",'%@'",pricetype];
  6583. }
  6584. }
  6585. if ([price isEqualToString:@"Display All"]) {
  6586. price = [NSString stringWithFormat:@""];
  6587. } else if([price containsString:@"+"]){
  6588. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6589. price_min = [price doubleValue];
  6590. 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];
  6591. } else {
  6592. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6593. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6594. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6595. 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];
  6596. }
  6597. } else {
  6598. price = @"";
  6599. }
  6600. // sold_by_qty : Sold in quantities of %@
  6601. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6602. NSString *qty = params[@"sold_by_qty"];
  6603. if ([qty isEqualToString:@"Display All"]) {
  6604. qty = @"";
  6605. } else {
  6606. qty = [self translateSingleQuote:qty];
  6607. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6608. }
  6609. // model name;
  6610. NSString* modelname =params[@"modelName"];
  6611. if(modelname.length==0)
  6612. {
  6613. modelname=@"";
  6614. }
  6615. else
  6616. {
  6617. modelname = modelname.lowercaseString;
  6618. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6619. }
  6620. //modelDescription
  6621. NSString* modelDescription =params[@"modelDescription"];
  6622. if(modelDescription.length==0)
  6623. {
  6624. modelDescription=@"";
  6625. }
  6626. else
  6627. {
  6628. modelDescription = modelDescription.lowercaseString;
  6629. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6630. }
  6631. // cate
  6632. // category = [self translateSingleQuote:category];
  6633. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6634. // cate mutiple selection
  6635. NSString *category_id = params[@"category"];
  6636. NSMutableArray *cate_id_array = nil;
  6637. NSMutableString *cateWhere = [NSMutableString string];
  6638. if ([category_id isEqualToString:@""] || !category_id) {
  6639. [cateWhere appendString:@"1 = 1"];
  6640. } else {
  6641. if ([category_id containsString:@","]) {
  6642. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6643. } else {
  6644. cate_id_array = [@[category_id] mutableCopy];
  6645. }
  6646. [cateWhere appendString:@"("];
  6647. for (int i = 0; i < cate_id_array.count; i++) {
  6648. if (i == 0) {
  6649. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6650. } else {
  6651. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6652. }
  6653. }
  6654. [cateWhere appendString:@")"];
  6655. }
  6656. // where bestseller > 0 order by bestseller desc
  6657. // sql query: alert availability(int) best_seller(int) price qty
  6658. 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];
  6659. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6660. }
  6661. DebugLog(@"offline category where: %@",where);
  6662. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6663. if (!params[@"user"]) {
  6664. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6665. }
  6666. [ret setValue:filter forKey:@"filter"];
  6667. DebugLog(@"offline_category sql:%@",sqlQuery);
  6668. sqlite3_stmt * statement;
  6669. [ret setValue:@"2" forKey:@"result"];
  6670. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6671. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6672. // int count=0;
  6673. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6674. {
  6675. int i=0;
  6676. while (sqlite3_step(statement) == SQLITE_ROW)
  6677. {
  6678. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6679. char *name = (char*)sqlite3_column_text(statement, 0);
  6680. if(name==nil)
  6681. name="";
  6682. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6683. char *description = (char*)sqlite3_column_text(statement, 1);
  6684. if(description==nil)
  6685. description="";
  6686. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6687. int product_id = sqlite3_column_int(statement, 2);
  6688. int wid = sqlite3_column_int(statement, 3);
  6689. int closeout = sqlite3_column_int(statement, 4);
  6690. int cid = sqlite3_column_int(statement, 5);
  6691. int wisdelete = sqlite3_column_int(statement, 6);
  6692. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6693. int more_color = sqlite3_column_int(statement, 8);
  6694. // Defaul Category ID
  6695. __block NSString *categoryID = nil;
  6696. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6697. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6698. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6699. if(default_category==nil)
  6700. default_category="";
  6701. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6702. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6703. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6704. categoryID = nsdefault_category;
  6705. }];
  6706. if (!categoryID.length) {
  6707. NSString *cateIDs = params[@"category"];
  6708. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6709. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6710. for (NSString *cateID in requestCategoryArr) {
  6711. BOOL needBreak = NO;
  6712. for (NSString *itemCateIDBox in itemCategoryArr) {
  6713. if (itemCateIDBox.length > 4) {
  6714. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6715. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6716. if ([itemCategoryID isEqualToString:cateID]) {
  6717. needBreak = YES;
  6718. categoryID = itemCategoryID;
  6719. break;
  6720. }
  6721. }
  6722. }
  6723. if (needBreak) {
  6724. break;
  6725. }
  6726. }
  6727. }
  6728. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6729. if(wid !=0 && wisdelete != 1)
  6730. [item setValue:@"true" forKey:@"wish_exists"];
  6731. else
  6732. [item setValue:@"false" forKey:@"wish_exists"];
  6733. if(closeout==0)
  6734. [item setValue:@"false" forKey:@"is_closeout"];
  6735. else
  6736. [item setValue:@"true" forKey:@"is_closeout"];
  6737. if(cid==0)
  6738. [item setValue:@"false" forKey:@"cart_exists"];
  6739. else
  6740. [item setValue:@"true" forKey:@"cart_exists"];
  6741. if (more_color == 0) {
  6742. [item setObject:@(false) forKey:@"more_color"];
  6743. } else if (more_color == 1) {
  6744. [item setObject:@(true) forKey:@"more_color"];
  6745. }
  6746. [item addEntriesFromDictionary:imgjson];
  6747. // [item setValue:nsurl forKey:@"img"];
  6748. [item setValue:nsname forKey:@"name"];
  6749. [item setValue:nsdescription forKey:@"description"];
  6750. if (categoryID) {
  6751. [item setValue:categoryID forKey:@"item_category_id"];
  6752. }
  6753. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6754. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6755. i++;
  6756. }
  6757. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6758. [ret setObject:items forKey:@"items"];
  6759. sqlite3_finalize(statement);
  6760. } else {
  6761. DebugLog(@"nothing...");
  6762. }
  6763. DebugLog(@"count:%d",count);
  6764. [iSalesDB close_db:db];
  6765. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6766. return ret;
  6767. }
  6768. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6769. {
  6770. return [self categoryList:params limited:YES];
  6771. }
  6772. # pragma mark item search
  6773. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6774. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6775. // assert(params[@"order_code"]);
  6776. // params[@"user"]
  6777. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6778. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6779. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6780. // category
  6781. NSDictionary *category_menu = [self offline_category_menu];
  6782. [filter setValue:category_menu forKey:@"category"];
  6783. NSString* where= nil;
  6784. NSString* orderby= @"m.name";
  6785. if (!filterSearch) {
  6786. int covertype = [[params valueForKey:@"covertype"] intValue];
  6787. switch (covertype) {
  6788. case 0:
  6789. {
  6790. where=@"m.category like'%%#005#%%'";
  6791. break;
  6792. }
  6793. case 1:
  6794. {
  6795. where=@"m.alert like '%QS%'";
  6796. break;
  6797. }
  6798. case 2:
  6799. {
  6800. where=@"m.availability>0";
  6801. break;
  6802. }
  6803. case 3:
  6804. {
  6805. where=@"m.best_seller>0";
  6806. orderby=@"m.best_seller desc,m.name asc";
  6807. break;
  6808. }
  6809. default:
  6810. where=@"1=1";
  6811. break;
  6812. }
  6813. }
  6814. int limit = [[params valueForKey:@"limit"] intValue];
  6815. int offset = [[params valueForKey:@"offset"] intValue];
  6816. NSString *limit_str = @"";
  6817. if (limited) {
  6818. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6819. }
  6820. sqlite3 *db = [iSalesDB get_db];
  6821. // [iSalesDB AddExFunction:db];
  6822. int count;
  6823. NSString *sqlQuery = nil;
  6824. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6825. 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];
  6826. double price_min = 0;
  6827. double price_max = 0;
  6828. if (filterSearch) {
  6829. // alert
  6830. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6831. NSString *alert = params[@"alert"];
  6832. if ([alert isEqualToString:@"Display All"]) {
  6833. alert = [NSString stringWithFormat:@""];
  6834. } else {
  6835. alert = [self translateSingleQuote:alert];
  6836. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6837. }
  6838. // available
  6839. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6840. NSString *available = params[@"available"];
  6841. NSString *available_condition;
  6842. if ([available isEqualToString:@"Display All"]) {
  6843. available_condition = @"";
  6844. } else if ([available isEqualToString:@"Available Now"]) {
  6845. available_condition = @"and availability > 0";
  6846. } else {
  6847. available_condition = @"and availability == 0";
  6848. }
  6849. // best seller
  6850. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6851. NSString *best_seller = @"";
  6852. NSString *order_best_seller = @"m.name asc";
  6853. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6854. best_seller = @"and best_seller > 0";
  6855. order_best_seller = @"m.best_seller desc,m.name asc";
  6856. }
  6857. // price
  6858. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6859. NSString *price = params[@"price"];
  6860. price_min = 0;
  6861. price_max = MAXFLOAT;
  6862. if (params[@"user"]) {
  6863. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6864. NSMutableString *priceName = [NSMutableString string];
  6865. for (int i = 0; i < priceTypeArray.count; i++) {
  6866. NSString *pricetype = priceTypeArray[i];
  6867. pricetype = [self translateSingleQuote:pricetype];
  6868. if (i == 0) {
  6869. [priceName appendFormat:@"'%@'",pricetype];
  6870. } else {
  6871. [priceName appendFormat:@",'%@'",pricetype];
  6872. }
  6873. }
  6874. if ([price isEqualToString:@"Display All"]) {
  6875. price = [NSString stringWithFormat:@""];
  6876. } else if([price containsString:@"+"]){
  6877. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6878. price_min = [price doubleValue];
  6879. 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];
  6880. } else {
  6881. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6882. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6883. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6884. 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];
  6885. }
  6886. } else {
  6887. price = @"";
  6888. }
  6889. // sold_by_qty : Sold in quantities of %@
  6890. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6891. NSString *qty = params[@"sold_by_qty"];
  6892. if ([qty isEqualToString:@"Display All"]) {
  6893. qty = @"";
  6894. } else {
  6895. qty = [self translateSingleQuote:qty];
  6896. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6897. }
  6898. // model name;
  6899. NSString* modelname =params[@"modelName"];
  6900. if(modelname.length==0)
  6901. {
  6902. modelname=@"";
  6903. }
  6904. else
  6905. {
  6906. modelname = modelname.lowercaseString;
  6907. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6908. }
  6909. //modelDescription
  6910. NSString* modelDescription =params[@"modelDescription"];
  6911. if(modelDescription.length==0)
  6912. {
  6913. modelDescription=@"";
  6914. }
  6915. else
  6916. {
  6917. modelDescription = modelDescription.lowercaseString;
  6918. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6919. }
  6920. //lower(description) like'%%%@%%'
  6921. // category
  6922. NSString *category_id = params[@"ctgId"];
  6923. NSMutableArray *cate_id_array = nil;
  6924. NSMutableString *cateWhere = [NSMutableString string];
  6925. if ([category_id isEqualToString:@""] || !category_id) {
  6926. [cateWhere appendString:@"1 = 1"];
  6927. } else {
  6928. if ([category_id containsString:@","]) {
  6929. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6930. } else {
  6931. cate_id_array = [@[category_id] mutableCopy];
  6932. }
  6933. /*-----------*/
  6934. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6935. [cateWhere appendString:@"("];
  6936. for (int i = 0; i < cate_id_array.count; i++) {
  6937. for (NSString *key0 in cateDic.allKeys) {
  6938. if ([key0 containsString:@"category_"]) {
  6939. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6940. for (NSString *key1 in category0.allKeys) {
  6941. if ([key1 containsString:@"category_"]) {
  6942. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6943. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6944. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6945. cate_id_array[i] = [category1 objectForKey:@"id"];
  6946. if (i == 0) {
  6947. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6948. } else {
  6949. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6950. }
  6951. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6952. [category0 setValue:category1 forKey:key1];
  6953. [cateDic setValue:category0 forKey:key0];
  6954. }
  6955. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6956. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6957. cate_id_array[i] = [category0 objectForKey:@"id"];
  6958. if (i == 0) {
  6959. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6960. } else {
  6961. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6962. }
  6963. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6964. [cateDic setValue:category0 forKey:key0];
  6965. }
  6966. }
  6967. }
  6968. }
  6969. }
  6970. }
  6971. [cateWhere appendString:@")"];
  6972. [filter setValue:cateDic forKey:@"category"];
  6973. }
  6974. // where bestseller > 0 order by bestseller desc
  6975. // sql query: alert availability(int) best_seller(int) price qty
  6976. 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];
  6977. // count
  6978. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6979. }
  6980. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6981. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6982. if (!params[@"user"]) {
  6983. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6984. }
  6985. [ret setValue:filter forKey:@"filter"];
  6986. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6987. sqlite3_stmt * statement;
  6988. [ret setValue:@"2" forKey:@"result"];
  6989. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6990. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6991. // int count=0;
  6992. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6993. {
  6994. int i=0;
  6995. while (sqlite3_step(statement) == SQLITE_ROW)
  6996. {
  6997. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6998. char *name = (char*)sqlite3_column_text(statement, 0);
  6999. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  7000. char *description = (char*)sqlite3_column_text(statement, 1);
  7001. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  7002. int product_id = sqlite3_column_int(statement, 2);
  7003. int wid = sqlite3_column_int(statement, 3);
  7004. int closeout = sqlite3_column_int(statement, 4);
  7005. int cid = sqlite3_column_int(statement, 5);
  7006. int wisdelete = sqlite3_column_int(statement, 6);
  7007. int more_color = sqlite3_column_int(statement, 7);
  7008. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  7009. if(wid !=0 && wisdelete != 1)
  7010. [item setValue:@"true" forKey:@"wish_exists"];
  7011. else
  7012. [item setValue:@"false" forKey:@"wish_exists"];
  7013. if(closeout==0)
  7014. [item setValue:@"false" forKey:@"is_closeout"];
  7015. else
  7016. [item setValue:@"true" forKey:@"is_closeout"];
  7017. if(cid==0)
  7018. [item setValue:@"false" forKey:@"cart_exists"];
  7019. else
  7020. [item setValue:@"true" forKey:@"cart_exists"];
  7021. if (more_color == 0) {
  7022. [item setObject:@(false) forKey:@"more_color"];
  7023. } else if (more_color == 1) {
  7024. [item setObject:@(true) forKey:@"more_color"];
  7025. }
  7026. [item addEntriesFromDictionary:imgjson];
  7027. // [item setValue:nsurl forKey:@"img"];
  7028. [item setValue:nsname forKey:@"fash_name"];
  7029. [item setValue:nsdescription forKey:@"description"];
  7030. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  7031. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  7032. i++;
  7033. }
  7034. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  7035. [ret setObject:items forKey:@"items"];
  7036. sqlite3_finalize(statement);
  7037. }
  7038. [iSalesDB close_db:db];
  7039. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  7040. return ret;
  7041. }
  7042. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  7043. {
  7044. return [self itemsearch:params limited:YES];
  7045. }
  7046. #pragma mark order detail
  7047. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  7048. if (str1.length == 0) {
  7049. str1 = @"&nbsp";
  7050. }
  7051. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  7052. return str;
  7053. }
  7054. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  7055. {
  7056. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  7057. [fromformatter setDateFormat:from];
  7058. NSDate *date = [fromformatter dateFromString:datetime];
  7059. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  7060. [toformatter setDateFormat:to];
  7061. NSString * ret = [toformatter stringFromDate:date];
  7062. return ret;
  7063. }
  7064. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  7065. // 把毫秒去掉
  7066. if ([dateTime containsString:@"."]) {
  7067. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  7068. }
  7069. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  7070. formatter.dateFormat = formate;
  7071. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  7072. NSDate *date = [formatter dateFromString:dateTime];
  7073. formatter.dateFormat = newFormate;
  7074. NSString *result = [formatter stringFromDate:date];
  7075. return result ? result : @"";
  7076. }
  7077. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  7078. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  7079. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7080. }
  7081. + (NSString *)changeDateFormate:(NSString *)dateTime {
  7082. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  7083. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7084. }
  7085. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  7086. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  7087. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7088. }
  7089. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  7090. {
  7091. assert(params[@"mode"]!=nil);
  7092. assert(params[@"user"]!=nil);
  7093. DebugLog(@"offline oderdetail params: %@",params);
  7094. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7095. int orderId = [params[@"orderId"] intValue];
  7096. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  7097. // decrypt card number and card security code
  7098. // 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 ];
  7099. 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];
  7100. sqlite3 *db = [iSalesDB get_db];
  7101. sqlite3_stmt * statement;
  7102. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  7103. NSString *nssoid = @"";
  7104. NSString* orderinfo = @"";
  7105. NSString *moreInfo = @"";
  7106. double handlingFee = 0;
  7107. double payments_and_credist = 0;
  7108. double totalPrice = 0;
  7109. double shippingFee = 0;
  7110. double lift_gate = 0;
  7111. NSString *customer_contact = @"";
  7112. NSString *customer_email = @"";
  7113. NSString *customer_fax = @"";
  7114. NSString *customer_phone = @"";
  7115. NSString *customer_city = @"";
  7116. NSString *customer_state = @"";
  7117. NSString *customer_zipcode = @"";
  7118. NSString *customer_country = @"";
  7119. BOOL must_call = NO;
  7120. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  7121. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  7122. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  7123. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  7124. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  7125. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  7126. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  7127. {
  7128. if (sqlite3_step(statement) == SQLITE_ROW)
  7129. {
  7130. // int order_id = sqlite3_column_int(statement, 0);
  7131. NSString *sign_url = [self textAtColumn:51 statement:statement];
  7132. // ret[@"sign_url"] = sign_url;
  7133. section_1 = @{
  7134. @"data":sign_url,
  7135. @"title":@"Signature",
  7136. @"type":@"sign_url"
  7137. }.mutableCopy;
  7138. [ret setObject:section_1 forKey:@"section_1"];
  7139. customer_contact = [self textAtColumn:52 statement:statement];
  7140. customer_email = [self textAtColumn:53 statement:statement];
  7141. customer_phone = [self textAtColumn:54 statement:statement];
  7142. customer_fax = [self textAtColumn:55 statement:statement];
  7143. customer_city = [self textAtColumn:60 statement:statement];
  7144. customer_state = [self textAtColumn:61 statement:statement];
  7145. customer_zipcode = [self textAtColumn:62 statement:statement];
  7146. customer_country = [self textAtColumn:63 statement:statement];
  7147. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  7148. int offline_edit=sqlite3_column_int(statement, 56);
  7149. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  7150. char *soid = (char*)sqlite3_column_text(statement, 1);
  7151. if(soid==nil)
  7152. soid= "";
  7153. nssoid= [[NSString alloc]initWithUTF8String:soid];
  7154. // so#
  7155. ret[@"so#"] = nssoid;
  7156. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  7157. if(poNumber==nil)
  7158. poNumber= "";
  7159. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  7160. char *create_time = (char*)sqlite3_column_text(statement, 3);
  7161. if(create_time==nil)
  7162. create_time= "";
  7163. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7164. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  7165. int status = sqlite3_column_int(statement, 4);
  7166. int erpStatus = sqlite3_column_int(statement, 49);
  7167. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7168. // status
  7169. if (status > 1 && status != 3) {
  7170. status = erpStatus;
  7171. } else if (status == 3) {
  7172. status = 15;
  7173. }
  7174. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  7175. ret[@"order_status"] = nsstatus;
  7176. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7177. if(company_name==nil)
  7178. company_name= "";
  7179. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7180. // company name
  7181. ret[@"company_name"] = nscompany_name;
  7182. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7183. if(customer_contact==nil)
  7184. customer_contact= "";
  7185. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7186. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7187. if(addr_1==nil)
  7188. addr_1="";
  7189. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7190. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7191. if(addr_2==nil)
  7192. addr_2="";
  7193. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7194. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7195. if(addr_3==nil)
  7196. addr_3="";
  7197. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7198. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7199. if(addr_4==nil)
  7200. addr_4="";
  7201. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7202. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7203. [arr_addr addObject:nsaddr_1];
  7204. [arr_addr addObject:nsaddr_2];
  7205. [arr_addr addObject:nsaddr_3];
  7206. [arr_addr addObject:nsaddr_4];
  7207. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7208. [arr_addr addObject:customer_state];
  7209. [arr_addr addObject:customer_zipcode];
  7210. [arr_addr addObject:customer_country];
  7211. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7212. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7213. char *logist = (char*)sqlite3_column_text(statement, 11);
  7214. if(logist==nil)
  7215. logist= "";
  7216. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7217. if (/*status == -11 || */status == 10 || status == 11) {
  7218. nslogist = [self textAtColumn:59 statement:statement];
  7219. };
  7220. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7221. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7222. shipping = @"Shipping To Be Quoted";
  7223. } else {
  7224. shippingFee = sqlite3_column_double(statement, 12);
  7225. }
  7226. // Shipping
  7227. // ret[@"Shipping"] = shipping;
  7228. NSDictionary *shipping_item = @{
  7229. @"title":@"Shipping",
  7230. @"value":shipping
  7231. };
  7232. [price_data setObject:shipping_item forKey:@"item_1"];
  7233. int have_lift_gate = sqlite3_column_int(statement, 17);
  7234. lift_gate = sqlite3_column_double(statement, 13);
  7235. // Liftgate Fee(No loading dock)
  7236. if (!have_lift_gate) {
  7237. lift_gate = 0;
  7238. }
  7239. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7240. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7241. if (sqlite3_column_int(statement, 57)) {
  7242. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7243. liftgate_value = @"Shipping To Be Quoted";
  7244. }
  7245. NSDictionary *liftgate_item = @{
  7246. @"title":@"Liftgate Fee(No loading dock)",
  7247. @"value":liftgate_value
  7248. };
  7249. [price_data setObject:liftgate_item forKey:@"item_2"];
  7250. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7251. // [ret removeObjectForKey:@"Shipping"];
  7252. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7253. // }
  7254. //
  7255. // if (have_lift_gate) {
  7256. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7257. // }
  7258. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7259. if(general_notes==nil)
  7260. general_notes= "";
  7261. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7262. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7263. if(internal_notes==nil)
  7264. internal_notes= "";
  7265. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7266. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7267. if(payment_type==nil)
  7268. payment_type= "";
  7269. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7270. // order info
  7271. orderinfo = [self textFileName:@"order_info.html"];
  7272. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7273. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7274. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7275. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7276. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7277. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7278. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7279. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7280. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7281. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7282. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7283. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7284. NSString *payment = nil;
  7285. // id -> show
  7286. __block NSString *show_pay_type = nspayment_type;
  7287. [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) {
  7288. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7289. if (show_typ_ch != NULL) {
  7290. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7291. }
  7292. }];
  7293. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7294. {
  7295. payment = [self textFileName:@"creditcardpayment.html"];
  7296. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7297. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7298. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7299. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7300. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7301. NSString *card_type = [self textAtColumn:42 statement:statement];
  7302. if (card_type.length > 0) { // 显示星号
  7303. card_type = @"****";
  7304. }
  7305. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7306. if (card_number.length > 0 && card_number.length > 4) {
  7307. for (int i = 0; i < card_number.length - 4; i++) {
  7308. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7309. }
  7310. } else {
  7311. card_number = @"";
  7312. }
  7313. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7314. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7315. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7316. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7317. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7318. card_expiration = @"****";
  7319. }
  7320. NSString *card_city = [self textAtColumn:46 statement:statement];
  7321. NSString *card_state = [self textAtColumn:47 statement:statement];
  7322. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7323. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7324. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7325. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7326. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7327. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7328. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7329. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7330. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7331. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7332. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7333. }
  7334. else
  7335. {
  7336. payment=[self textFileName:@"normalpayment.html"];
  7337. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7338. }
  7339. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7340. ret[@"result"]= [NSNumber numberWithInt:2];
  7341. // more info
  7342. moreInfo = [self textFileName:@"more_info.html"];
  7343. /*****ship to******/
  7344. // ShipToCompany_or_&nbsp
  7345. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7346. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7347. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7348. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7349. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7350. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7351. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7352. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7353. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7354. /*****ship from******/
  7355. // ShipFromCompany_or_&nbsp
  7356. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7357. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7358. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7359. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7360. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7361. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7362. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7363. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7364. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7365. /*****freight to******/
  7366. // FreightBillToCompany_or_&nbsp
  7367. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7368. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7369. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7370. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7371. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7372. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7373. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7374. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7375. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7376. /*****merchandise to******/
  7377. // MerchandiseBillToCompany_or_&nbsp
  7378. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7379. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7380. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7381. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7382. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7383. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7384. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7385. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7386. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7387. /*****return to******/
  7388. // ReturnToCompany_or_&nbsp
  7389. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7390. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7391. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7392. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7393. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7394. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7395. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7396. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7397. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7398. //
  7399. // DebugLog(@"more info : %@",moreInfo);
  7400. // handling fee
  7401. handlingFee = sqlite3_column_double(statement, 33);
  7402. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7403. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7404. if (sqlite3_column_int(statement, 58)) {
  7405. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7406. handling_fee_value = @"Shipping To Be Quoted";
  7407. }
  7408. NSDictionary *handling_fee_item = @{
  7409. @"title":@"Handling Fee",
  7410. @"value":handling_fee_value
  7411. };
  7412. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7413. //
  7414. // customer info
  7415. customerID = [self textAtColumn:36 statement:statement];
  7416. // mode
  7417. ret[@"mode"] = params[@"mode"];
  7418. // model_count
  7419. ret[@"model_count"] = @(0);
  7420. }
  7421. sqlite3_finalize(statement);
  7422. }
  7423. [iSalesDB close_db:db];
  7424. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7425. // "customer_email" = "Shui Hu";
  7426. // "customer_fax" = "";
  7427. // "customer_first_name" = F;
  7428. // "customer_last_name" = L;
  7429. // "customer_name" = ",da He Xiang Dong Liu A";
  7430. // "customer_phone" = "Hey Xuan Feng";
  7431. contactInfo[@"customer_phone"] = customer_phone;
  7432. contactInfo[@"customer_fax"] = customer_fax;
  7433. contactInfo[@"customer_email"] = customer_email;
  7434. NSString *first_name = @"";
  7435. NSString *last_name = @"";
  7436. if ([customer_contact isEqualToString:@""]) {
  7437. } else if ([customer_contact containsString:@" "]) {
  7438. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7439. first_name = [customer_contact substringToIndex:first_space_index];
  7440. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7441. }
  7442. contactInfo[@"customer_first_name"] = first_name;
  7443. contactInfo[@"customer_last_name"] = last_name;
  7444. ret[@"customerInfo"] = contactInfo;
  7445. // models
  7446. if (nssoid) {
  7447. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7448. __block double TotalCuft = 0;
  7449. __block double TotalWeight = 0;
  7450. __block int TotalCarton = 0;
  7451. __block double allItemPrice = 0;
  7452. 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];
  7453. sqlite3 *db1 = [iSalesDB get_db];
  7454. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7455. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7456. int product_id = sqlite3_column_int(stmt, 0);
  7457. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7458. int item_id = sqlite3_column_int(stmt, 7);
  7459. NSString* Price=nil;
  7460. if(str_price==nil)
  7461. {
  7462. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7463. if(price==nil)
  7464. Price=@"No Price.";
  7465. else
  7466. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7467. }
  7468. else
  7469. {
  7470. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7471. }
  7472. double discount = sqlite3_column_double(stmt, 2);
  7473. int item_count = sqlite3_column_int(stmt, 3);
  7474. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7475. NSString *nsline_note=nil;
  7476. if(line_note!=nil)
  7477. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7478. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7479. // NSString *nsname = nil;
  7480. // if(name!=nil)
  7481. // nsname= [[NSString alloc]initWithUTF8String:name];
  7482. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7483. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7484. // NSString *nsdescription=nil;
  7485. // if(description!=nil)
  7486. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7487. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7488. // int stockUom = sqlite3_column_int(stmt, 8);
  7489. // int _id = sqlite3_column_int(stmt, 9);
  7490. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7491. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7492. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7493. int carton=[bsubtotaljson[@"carton"] intValue];
  7494. TotalCuft += cuft;
  7495. TotalWeight += weight;
  7496. TotalCarton += carton;
  7497. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7498. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7499. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7500. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7501. itemjson[@"note"]=nsline_note;
  7502. itemjson[@"origin_price"] = Price;
  7503. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7504. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7505. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7506. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7507. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7508. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7509. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7510. if(itemjson[@"combine"] != nil)
  7511. {
  7512. // int citem=0;
  7513. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7514. for(int bc=0;bc<bcount;bc++)
  7515. {
  7516. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7517. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7518. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7519. subTotal += uprice * modulus * item_count;
  7520. }
  7521. }
  7522. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7523. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7524. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7525. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7526. itemjson[@"type"] = @"order_item";
  7527. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7528. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7529. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7530. allItemPrice += subTotal;
  7531. }];
  7532. section_3[@"data"] = model_data;
  7533. section_3[@"type"] = @"sub_order";
  7534. section_3[@"title"] = @"Models";
  7535. section_3[@"switch"] = @(false);
  7536. ret[@"section_3"] = section_3;
  7537. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7538. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7539. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7540. // payments/Credits
  7541. // payments_and_credist = sqlite3_column_double(statement, 34);
  7542. payments_and_credist = allItemPrice;
  7543. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7544. NSDictionary *sub_total_item = @{
  7545. @"title":@"Sub-Total",
  7546. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7547. };
  7548. [price_data setObject:sub_total_item forKey:@"item_0"];
  7549. // // total
  7550. // totalPrice = sqlite3_column_double(statement, 35);
  7551. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7552. } else {
  7553. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7554. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7555. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7556. // payments/Credits
  7557. payments_and_credist = 0;
  7558. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7559. NSDictionary *sub_total_item = @{
  7560. @"title":@"Sub-Total",
  7561. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7562. };
  7563. [price_data setObject:sub_total_item forKey:@"item_0"];
  7564. }
  7565. // total
  7566. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7567. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7568. NSDictionary *total_item = @{
  7569. @"title":@"Total",
  7570. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7571. };
  7572. [price_data setObject:total_item forKey:@"item_4"];
  7573. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7574. section_4 = @{
  7575. @"data":price_data,
  7576. @"title":@"Price Info",
  7577. @"type":@"price_info"
  7578. }.mutableCopy;
  7579. ret[@"section_4"] = section_4;
  7580. // ret[@"order_info"]= orderinfo;
  7581. section_0 = @{
  7582. @"data":orderinfo,
  7583. @"title":@"Order Info",
  7584. @"type":@"order_info"
  7585. }.mutableCopy;
  7586. ret[@"section_0"] = section_0;
  7587. // ret[@"more_order_info"] = moreInfo;
  7588. section_2 = @{
  7589. @"data":moreInfo,
  7590. @"title":@"More Info",
  7591. @"type":@"more_order_info"
  7592. }.mutableCopy;
  7593. ret[@"section_2"] = section_2;
  7594. ret[@"count"] = @(5);
  7595. return [RAConvertor dict2data:ret];
  7596. }
  7597. #pragma mark order list
  7598. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7599. NSString *nsstatus = @"";
  7600. switch (status) {
  7601. case 0:
  7602. {
  7603. nsstatus=@"Temp Order";
  7604. break;
  7605. }
  7606. case 1:
  7607. {
  7608. nsstatus=@"Saved Order";
  7609. break;
  7610. }
  7611. case 2: {
  7612. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7613. break;
  7614. }
  7615. case 3:
  7616. case 15:
  7617. {
  7618. nsstatus=@"Cancelled";
  7619. break;
  7620. }
  7621. case 10:
  7622. {
  7623. nsstatus=@"Quote Submitted";
  7624. break;
  7625. }
  7626. case 11:
  7627. {
  7628. nsstatus=@"Sales Order Submitted";
  7629. break;
  7630. }
  7631. case 12:
  7632. {
  7633. nsstatus=@"Processing";
  7634. break;
  7635. }
  7636. case 13:
  7637. {
  7638. nsstatus=@"Shipped";
  7639. break;
  7640. }
  7641. case 14:
  7642. {
  7643. nsstatus=@"Closed";
  7644. break;
  7645. }
  7646. case -11:
  7647. {
  7648. nsstatus = @"Ready For Submit";
  7649. break;
  7650. }
  7651. default:
  7652. break;
  7653. }
  7654. return nsstatus;
  7655. }
  7656. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7657. {
  7658. double total = 0;
  7659. __block double payments_and_credist = 0;
  7660. __block double allItemPrice = 0;
  7661. // sqlite3 *db1 = [iSalesDB get_db];
  7662. if (so_id) {
  7663. // 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];
  7664. 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];
  7665. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7666. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7667. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7668. int product_id = sqlite3_column_int(stmt, 0);
  7669. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7670. int discount = sqlite3_column_int(stmt, 2);
  7671. int item_count = sqlite3_column_int(stmt, 3);
  7672. // int item_id = sqlite3_column_int(stmt, 7);
  7673. int item_id = sqlite3_column_int(stmt, 4);
  7674. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7675. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7676. NSString* Price=nil;
  7677. if(str_price==nil)
  7678. {
  7679. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7680. if(price==nil)
  7681. Price=@"No Price.";
  7682. else
  7683. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7684. }
  7685. else
  7686. {
  7687. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7688. }
  7689. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7690. itemjson[@"The unit price"]=Price;
  7691. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7692. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7693. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7694. if(itemjson[@"combine"] != nil)
  7695. {
  7696. // int citem=0;
  7697. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7698. for(int bc=0;bc<bcount;bc++)
  7699. {
  7700. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7701. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7702. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7703. subTotal += uprice * modulus * item_count;
  7704. }
  7705. }
  7706. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7707. allItemPrice += subTotal;
  7708. }];
  7709. payments_and_credist = allItemPrice;
  7710. } else {
  7711. // payments/Credits
  7712. payments_and_credist = 0;
  7713. }
  7714. // lift_gate handlingFee shippingFee
  7715. __block double lift_gate = 0;
  7716. __block double handlingFee = 0;
  7717. __block double shippingFee = 0;
  7718. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7719. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7720. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7721. if (have_lift_gate) {
  7722. lift_gate = sqlite3_column_double(stmt, 1);
  7723. }
  7724. handlingFee = sqlite3_column_double(stmt, 2);
  7725. shippingFee = sqlite3_column_double(stmt, 3);
  7726. }];
  7727. // total
  7728. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7729. if (close) {
  7730. [iSalesDB close_db:db1];
  7731. }
  7732. return total;
  7733. }
  7734. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7735. {
  7736. assert(params[@"user"]!=nil);
  7737. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7738. int limit = [[params valueForKey:@"limit"] intValue];
  7739. int offset = [[params valueForKey:@"offset"] intValue];
  7740. NSString* keyword = [params valueForKey:@"keyWord"];
  7741. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7742. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7743. NSString* where=@"1 = 1";
  7744. if(keyword.length>0)
  7745. 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]];
  7746. if (orderStatus.length > 0) {
  7747. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7748. if (order_status_array.count == 1) {
  7749. int status_value = [[order_status_array firstObject] intValue];
  7750. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7751. if (status_value == 15 || status_value == 3) {
  7752. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7753. } else {
  7754. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7755. }
  7756. } else {
  7757. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7758. }
  7759. } else if (order_status_array.count > 1) {
  7760. for (int i = 0; i < order_status_array.count;i++) {
  7761. NSString *status = order_status_array[i];
  7762. NSString *condition = @" or";
  7763. if (i == 0) {
  7764. condition = @" and (";
  7765. }
  7766. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7767. int status_value = [status intValue];
  7768. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7769. if (status_value == 15 || status_value == 3) {
  7770. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7771. } else {
  7772. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7773. }
  7774. } else {
  7775. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7776. }
  7777. }
  7778. where = [where stringByAppendingString:@" )"];
  7779. }
  7780. }
  7781. 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];
  7782. // DebugLog(@"order list sql: %@",sqlQuery);
  7783. sqlite3 *db = [iSalesDB get_db];
  7784. sqlite3_stmt * statement;
  7785. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7786. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7787. {
  7788. int count=0;
  7789. while (sqlite3_step(statement) == SQLITE_ROW)
  7790. {
  7791. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7792. int order_id = sqlite3_column_double(statement, 0);
  7793. char *soid = (char*)sqlite3_column_text(statement, 1);
  7794. if(soid==nil)
  7795. soid= "";
  7796. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7797. int status = sqlite3_column_double(statement, 2);
  7798. int erpStatus = sqlite3_column_double(statement, 9);
  7799. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7800. if(sales_rep==nil)
  7801. sales_rep= "";
  7802. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7803. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7804. if(create_by==nil)
  7805. create_by= "";
  7806. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7807. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7808. if(company_name==nil)
  7809. company_name= "";
  7810. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7811. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7812. if(create_time==nil)
  7813. create_time= "";
  7814. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7815. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7816. // double total_price = sqlite3_column_double(statement, 7);
  7817. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7818. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7819. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7820. int offline_edit = sqlite3_column_int(statement, 10);
  7821. // ": "JH",
  7822. // "": "$8307.00",
  7823. // "": "MOB1608050001",
  7824. // "": "ArpithaT",
  7825. // "": "1st Stage Property Transformations",
  7826. // "": "JANICE SUTTON",
  7827. // "": 2255,
  7828. // "": "08/02/2016 09:49:18",
  7829. // "": 1,
  7830. // "": "Saved Order"
  7831. // "": "1470384050483",
  7832. // "model_count": "6 / 28"
  7833. item[@"sales_rep"]= nssales_rep;
  7834. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7835. item[@"so#"]= nssoid;
  7836. item[@"create_by"]= nscreate_by;
  7837. item[@"customer_name"]= nscompany_name;
  7838. item[@"customer_contact"] = customer_contact;
  7839. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7840. item[@"purchase_time"]= nscreate_time;
  7841. int statusCode = status;
  7842. if (statusCode == 2) {
  7843. statusCode = erpStatus;
  7844. } else if (statusCode == 3) {
  7845. statusCode = 15;
  7846. }
  7847. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7848. item[@"order_status"]= nsstatus;
  7849. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7850. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7851. // item[@"model_count"]
  7852. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7853. count++;
  7854. }
  7855. ret[@"count"]= [NSNumber numberWithInt:count];
  7856. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7857. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7858. ret[@"result"]= [NSNumber numberWithInt:2];
  7859. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7860. ret[@"time_zone"] = @"PST";
  7861. sqlite3_finalize(statement);
  7862. }
  7863. 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];
  7864. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7865. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7866. [iSalesDB close_db:db];
  7867. return [RAConvertor dict2data:ret];
  7868. }
  7869. #pragma mark update gnotes
  7870. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7871. {
  7872. DebugLog(@"params: %@",params);
  7873. // comments = Meyoyoyoyoyoyoy;
  7874. // orderCode = MOB1608110001;
  7875. // password = 123456;
  7876. // user = EvanK;
  7877. 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]];
  7878. int ret = [iSalesDB execSql:sql];
  7879. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7880. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7881. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7882. // [dic setValue:@"160409" forKey:@"min_ver"];
  7883. return [RAConvertor dict2data:dic];
  7884. }
  7885. #pragma mark move to wishlist
  7886. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7887. {
  7888. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7889. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7890. _id = [NSString stringWithFormat:@"(%@)",_id];
  7891. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7892. sqlite3 *db = [iSalesDB get_db];
  7893. __block NSString *product_id = @"";
  7894. __block NSString *item_count_str = @"";
  7895. // __block NSString *item_id = nil;
  7896. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7897. // product_id = [self textAtColumn:0 statement:stmt];
  7898. int item_count = sqlite3_column_int(stmt, 1);
  7899. // item_id = [self textAtColumn:2 statement:stmt];
  7900. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7901. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7902. if (p_id.length) {
  7903. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7904. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7905. }
  7906. }];
  7907. [iSalesDB close_db:db];
  7908. // 去除第一个,
  7909. if (product_id.length > 1) {
  7910. product_id = [product_id substringFromIndex:1];
  7911. }
  7912. if (item_count_str.length > 1) {
  7913. item_count_str = [item_count_str substringFromIndex:1];
  7914. }
  7915. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7916. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7917. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7918. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7919. sqlite3 *db1 = [iSalesDB get_db];
  7920. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7921. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7922. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7923. // 删除
  7924. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7925. int ret = [iSalesDB execSql:deleteSql db:db1];
  7926. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7927. [iSalesDB close_db:db1];
  7928. return [RAConvertor dict2data:dic];
  7929. }
  7930. #pragma mark cart delete
  7931. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7932. {
  7933. // cartItemId = 548;
  7934. // orderCode = MOB1608110001;
  7935. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7936. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7937. _id = [NSString stringWithFormat:@"(%@)",_id];
  7938. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7939. int ret = [iSalesDB execSql:sql];
  7940. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7941. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7942. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7943. // [dic setValue:@"160409" forKey:@"min_ver"];
  7944. return [RAConvertor dict2data:dic];
  7945. }
  7946. #pragma mark set price
  7947. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7948. {
  7949. DebugLog(@"cart set price params: %@",params);
  7950. // "cartitem_id" = 1;
  7951. // discount = "0.000000";
  7952. // "item_note" = "";
  7953. // price = "269.000000";
  7954. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7955. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7956. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7957. // NSString *price = [self valueInParams:params key:@"price"];
  7958. // bool badd_price_changed=false;
  7959. // sqlite3* db=[iSalesDB get_db];
  7960. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7961. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7962. // NSRange range;
  7963. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7964. //
  7965. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7966. // badd_price_changed=true;
  7967. // [iSalesDB close_db:db];
  7968. //
  7969. // if(badd_price_changed)
  7970. // {
  7971. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7972. // }
  7973. //
  7974. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7975. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7976. int ret = [iSalesDB execSql:sql];
  7977. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7978. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7979. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7980. // [dic setValue:@"160409" forKey:@"min_ver"];
  7981. // return [RAConvertor dict2data:dic];
  7982. return dic;
  7983. }
  7984. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7985. {
  7986. DebugLog(@"cart set price params: %@",params);
  7987. // "cartitem_id" = 1;
  7988. // discount = "0.000000";
  7989. // "item_note" = "";
  7990. // price = "269.000000";
  7991. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7992. NSString *notes = [self valueInParams:params key:@"item_note"];
  7993. NSString *discount = [self valueInParams:params key:@"discount"];
  7994. NSString *price = [self valueInParams:params key:@"price"];
  7995. // bool badd_price_changed=false;
  7996. // sqlite3* db=[iSalesDB get_db];
  7997. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7998. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7999. // NSRange range;
  8000. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  8001. //
  8002. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  8003. // badd_price_changed=true;
  8004. // [iSalesDB close_db:db];
  8005. //
  8006. // if(badd_price_changed)
  8007. // {
  8008. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  8009. // }
  8010. //
  8011. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  8012. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  8013. int ret = [iSalesDB execSql:sql];
  8014. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8015. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8016. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8017. // [dic setValue:@"160409" forKey:@"min_ver"];
  8018. return [RAConvertor dict2data:dic];
  8019. }
  8020. #pragma mark set line notes
  8021. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  8022. {
  8023. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8024. NSString *notes = [self valueInParams:params key:@"notes"];
  8025. notes = [self translateSingleQuote:notes];
  8026. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  8027. int ret = [iSalesDB execSql:sql];
  8028. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8029. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8030. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8031. // [dic setValue:@"160409" forKey:@"min_ver"];
  8032. return [RAConvertor dict2data:dic];
  8033. }
  8034. #pragma mark set qty
  8035. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  8036. {
  8037. assert(params[@"can_create_backorder"]!=nil);
  8038. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8039. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8040. int item_count = [params[@"inputInt"] intValue];
  8041. // 购买检查数量大于库存
  8042. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8043. if (!params[@"can_create_backorder"]) {
  8044. 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];
  8045. __block BOOL out_of_stock = NO;
  8046. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8047. int availability = sqlite3_column_int(stmt, 0);
  8048. if (availability < item_count) {
  8049. out_of_stock = YES;
  8050. }
  8051. }];
  8052. if (out_of_stock) { // 缺货
  8053. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  8054. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  8055. return [RAConvertor dict2data:dic];
  8056. }
  8057. }
  8058. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  8059. int ret = [iSalesDB execSql:sql];
  8060. __block int item_id = 0;
  8061. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8062. item_id = sqlite3_column_int(stmt, 0);
  8063. }];
  8064. sqlite3 *db = [iSalesDB get_db];
  8065. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8066. [iSalesDB close_db:db];
  8067. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8068. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8069. // [dic setValue:@"160409" forKey:@"min_ver"];
  8070. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  8071. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  8072. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  8073. return [RAConvertor dict2data:dic];
  8074. }
  8075. #pragma mark place order
  8076. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  8077. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8078. if (aname) {
  8079. [dic setValue:aname forKey:@"aname"];
  8080. }
  8081. if (control) {
  8082. [dic setValue:control forKey:@"control"];
  8083. }
  8084. if (keyboard) {
  8085. [dic setValue:keyboard forKey:@"keyboard"];
  8086. }
  8087. if (name) {
  8088. [dic setValue:name forKey:@"name"];
  8089. }
  8090. if (value) {
  8091. [dic setValue:value forKey:@"value"];
  8092. }
  8093. return dic;
  8094. }
  8095. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  8096. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8097. orderCode = [self translateSingleQuote:orderCode];
  8098. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  8099. 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];
  8100. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8101. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  8102. NSString *img0 = [self textAtColumn:1 statement:stmt];
  8103. NSString *img1 = [self textAtColumn:2 statement:stmt];
  8104. NSString *img2 = [self textAtColumn:3 statement:stmt];
  8105. NSString *company_name = [self textAtColumn:4 statement:stmt];
  8106. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  8107. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  8108. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  8109. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  8110. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  8111. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  8112. NSString *email = [self textAtColumn:11 statement:stmt];
  8113. NSString *phone = [self textAtColumn:12 statement:stmt];
  8114. NSString *fax = [self textAtColumn:13 statement:stmt];
  8115. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  8116. NSString *city = [self textAtColumn:15 statement:stmt];
  8117. NSString *state = [self textAtColumn:16 statement:stmt];
  8118. NSString *country = [self textAtColumn:17 statement:stmt];
  8119. NSString *name = [self textAtColumn:18 statement:stmt];
  8120. // contact id
  8121. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  8122. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  8123. [contact_id_dic setValue:@"text" forKey:@"control"];
  8124. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  8125. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  8126. [contact_id_dic setValue:@"true" forKey:@"required"];
  8127. [contact_id_dic setValue:contact_id forKey:@"value"];
  8128. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  8129. // business card
  8130. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  8131. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  8132. [business_card_dic setValue:@"img" forKey:@"control"];
  8133. [business_card_dic setValue:@"1" forKey:@"disable"];
  8134. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  8135. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  8136. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  8137. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  8138. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  8139. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  8140. [business_card_dic setValue:@"business_card" forKey:@"name"];
  8141. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  8142. // fax
  8143. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8144. [customer_dic setValue:fax_dic forKey:@"item_10"];
  8145. // zipcode
  8146. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  8147. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  8148. // city
  8149. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  8150. [customer_dic setValue:city_dic forKey:@"item_12"];
  8151. // state
  8152. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  8153. [customer_dic setValue:state_dic forKey:@"item_13"];
  8154. // country
  8155. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  8156. [customer_dic setValue:country_dic forKey:@"item_14"];
  8157. // company name
  8158. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  8159. [customer_dic setValue:company_dic forKey:@"item_2"];
  8160. // addr_1
  8161. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  8162. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  8163. // addr_2
  8164. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  8165. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  8166. // addr_3
  8167. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  8168. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  8169. // addr_4
  8170. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  8171. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  8172. // Contact
  8173. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  8174. [customer_dic setValue:contact_dic forKey:@"item_7"];
  8175. // email
  8176. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8177. [customer_dic setValue:email_dic forKey:@"item_8"];
  8178. // phone
  8179. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8180. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8181. // title
  8182. [customer_dic setValue:@"Customer" forKey:@"title"];
  8183. // count
  8184. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8185. }];
  8186. // setting
  8187. NSDictionary *setting = params[@"setting"];
  8188. NSNumber *hide = setting[@"CustomerHide"];
  8189. [customer_dic setValue:hide forKey:@"hide"];
  8190. return customer_dic;
  8191. }
  8192. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8193. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8194. // setting
  8195. NSDictionary *setting = params[@"setting"];
  8196. NSNumber *hide = setting[@"ShipToHide"];
  8197. [dic setValue:hide forKey:@"hide"];
  8198. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8199. orderCode = [self translateSingleQuote:orderCode];
  8200. 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];
  8201. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8202. NSString *cid = [self textAtColumn:0 statement:stmt];
  8203. NSString *name = [self textAtColumn:1 statement:stmt];
  8204. NSString *ext = [self textAtColumn:2 statement:stmt];
  8205. NSString *contact = [self textAtColumn:3 statement:stmt];
  8206. NSString *email = [self textAtColumn:4 statement:stmt];
  8207. NSString *fax = [self textAtColumn:5 statement:stmt];
  8208. NSString *phone = [self textAtColumn:6 statement:stmt];
  8209. // count
  8210. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8211. // title
  8212. [dic setValue:@"Ship To" forKey:@"title"];
  8213. // choose
  8214. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8215. [choose_dic setValue:@"choose" forKey:@"aname"];
  8216. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8217. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8218. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8219. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8220. @"receive_contact" : @"customer_contact",
  8221. @"receive_email" : @"customer_email",
  8222. @"receive_ext" : @"customer_contact_ext",
  8223. @"receive_fax" : @"customer_fax",
  8224. @"receive_name" : @"customer_name",
  8225. @"receive_phone" : @"customer_phone"},
  8226. @"refresh" : [NSNumber numberWithInteger:1],
  8227. @"type" : @"pull"};
  8228. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8229. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8230. @"name" : @"Add new address",
  8231. @"refresh" : [NSNumber numberWithInteger:1],
  8232. @"value" : @"new_addr"
  8233. };
  8234. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8235. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8236. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8237. @"receive_contact" : @"customer_contact",
  8238. @"receive_email" : @"customer_email",
  8239. @"receive_ext" : @"customer_contact_ext",
  8240. @"receive_fax" : @"customer_fax",
  8241. @"receive_name" : @"customer_name",
  8242. @"receive_phone" : @"customer_phone"},
  8243. @"name" : @"select_ship_to",
  8244. @"refresh" : [NSNumber numberWithInteger:1],
  8245. @"value" : @"Sales_Order_Ship_To"};
  8246. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8247. [dic setValue:choose_dic forKey:@"item_0"];
  8248. // contact id
  8249. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8250. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8251. [contact_id_dic setValue:@"true" forKey:@"required"];
  8252. [dic setValue:contact_id_dic forKey:@"item_1"];
  8253. // company name
  8254. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8255. [dic setValue:company_name_dic forKey:@"item_2"];
  8256. // address
  8257. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8258. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8259. if ([required integerValue]) {
  8260. [ext_dic setValue:@"true" forKey:@"required"];
  8261. }
  8262. [dic setValue:ext_dic forKey:@"item_3"];
  8263. // contact
  8264. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8265. [dic setValue:contact_dic forKey:@"item_4"];
  8266. // phone
  8267. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8268. [dic setValue:phone_dic forKey:@"item_5"];
  8269. // fax
  8270. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8271. [dic setValue:fax_dic forKey:@"item_6"];
  8272. // email
  8273. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8274. [dic setValue:email_dic forKey:@"item_7"];
  8275. }];
  8276. return dic;
  8277. }
  8278. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8279. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8280. // setting
  8281. NSDictionary *setting = params[@"setting"];
  8282. NSNumber *hide = setting[@"ShipFromHide"];
  8283. [dic setValue:hide forKey:@"hide"];
  8284. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8285. orderCode = [self translateSingleQuote:orderCode];
  8286. 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];
  8287. 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';";
  8288. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8289. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8290. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8291. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8292. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8293. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8294. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8295. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8296. if (!cid.length) {
  8297. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8298. cid = [self textAtColumn:0 statement:statment];
  8299. name = [self textAtColumn:1 statement:statment];
  8300. ext = [self textAtColumn:2 statement:statment];
  8301. contact = [self textAtColumn:3 statement:statment];
  8302. email = [self textAtColumn:4 statement:statment];
  8303. fax = [self textAtColumn:5 statement:statment];
  8304. phone = [self textAtColumn:6 statement:statment];
  8305. }];
  8306. }
  8307. // count
  8308. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8309. // title
  8310. [dic setValue:@"Ship From" forKey:@"title"];
  8311. // hide
  8312. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8313. // choose
  8314. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8315. [choose_dic setValue:@"choose" forKey:@"aname"];
  8316. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8317. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8318. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8319. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8320. @"sender_contact" : @"customer_contact",
  8321. @"sender_email" : @"customer_email",
  8322. @"sender_ext" : @"customer_contact_ext",
  8323. @"sender_fax" : @"customer_fax",
  8324. @"sender_name" : @"customer_name",
  8325. @"sender_phone" : @"customer_phone"},
  8326. @"name" : @"select_cid",
  8327. @"refresh" : [NSNumber numberWithInteger:0],
  8328. @"value" : @"Sales_Order_Ship_From"};
  8329. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8330. [dic setValue:choose_dic forKey:@"item_0"];
  8331. // contact id
  8332. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8333. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8334. [contact_id_dic setValue:@"true" forKey:@"required"];
  8335. [dic setValue:contact_id_dic forKey:@"item_1"];
  8336. // company name
  8337. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8338. [dic setValue:company_name_dic forKey:@"item_2"];
  8339. // address
  8340. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8341. [dic setValue:ext_dic forKey:@"item_3"];
  8342. // contact
  8343. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8344. [dic setValue:contact_dic forKey:@"item_4"];
  8345. // phone
  8346. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8347. [dic setValue:phone_dic forKey:@"item_5"];
  8348. // fax
  8349. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8350. [dic setValue:fax_dic forKey:@"item_6"];
  8351. // email
  8352. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8353. [dic setValue:email_dic forKey:@"item_7"];
  8354. }];
  8355. return dic;
  8356. }
  8357. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8358. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8359. // setting
  8360. NSDictionary *setting = params[@"setting"];
  8361. NSNumber *hide = setting[@"FreightBillToHide"];
  8362. [dic setValue:hide forKey:@"hide"];
  8363. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8364. orderCode = [self translateSingleQuote:orderCode];
  8365. 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];
  8366. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8367. NSString *cid = [self textAtColumn:0 statement:stmt];
  8368. NSString *name = [self textAtColumn:1 statement:stmt];
  8369. NSString *ext = [self textAtColumn:2 statement:stmt];
  8370. NSString *contact = [self textAtColumn:3 statement:stmt];
  8371. NSString *email = [self textAtColumn:4 statement:stmt];
  8372. NSString *fax = [self textAtColumn:5 statement:stmt];
  8373. NSString *phone = [self textAtColumn:6 statement:stmt];
  8374. // count
  8375. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8376. // title
  8377. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8378. // hide
  8379. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8380. // choose
  8381. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8382. [choose_dic setValue:@"choose" forKey:@"aname"];
  8383. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8384. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8385. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8386. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8387. @"shipping_billto_contact" : @"receive_contact",
  8388. @"shipping_billto_email" : @"receive_email",
  8389. @"shipping_billto_ext" : @"receive_ext",
  8390. @"shipping_billto_fax" : @"receive_fax",
  8391. @"shipping_billto_name" : @"receive_name",
  8392. @"shipping_billto_phone" : @"receive_phone"},
  8393. @"type" : @"pull"};
  8394. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8395. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8396. @"type" : @"pull",
  8397. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8398. @"shipping_billto_contact" : @"customer_contact",
  8399. @"shipping_billto_email" : @"customer_email",
  8400. @"shipping_billto_ext" : @"customer_contact_ext",
  8401. @"shipping_billto_fax" : @"customer_fax",
  8402. @"shipping_billto_name" : @"customer_name",
  8403. @"shipping_billto_phone" : @"customer_phone"}
  8404. };
  8405. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8406. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8407. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8408. @"shipping_billto_contact" : @"sender_contact",
  8409. @"shipping_billto_email" : @"sender_email",
  8410. @"shipping_billto_ext" : @"sender_ext",
  8411. @"shipping_billto_fax" : @"sender_fax",
  8412. @"shipping_billto_name" : @"sender_name",
  8413. @"shipping_billto_phone" : @"sender_phone"},
  8414. @"type" : @"pull"
  8415. };
  8416. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8417. NSDictionary *select_freight_bill_to_dic = @{
  8418. @"aname" : @"Select freight bill to",
  8419. @"name" : @"select_cid",
  8420. @"refresh" : [NSNumber numberWithInteger:0],
  8421. @"value" : @"Sales_Order_Freight_Bill_To",
  8422. @"key_map" : @{
  8423. @"shipping_billto_cid" : @"customer_cid",
  8424. @"shipping_billto_contact" : @"customer_contact",
  8425. @"shipping_billto_email" : @"customer_email",
  8426. @"shipping_billto_ext" : @"customer_contact_ext",
  8427. @"shipping_billto_fax" : @"customer_fax",
  8428. @"shipping_billto_name" : @"customer_name",
  8429. @"shipping_billto_phone" : @"customer_phone"
  8430. }
  8431. };
  8432. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8433. [dic setValue:choose_dic forKey:@"item_0"];
  8434. // contact id
  8435. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8436. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8437. [contact_id_dic setValue:@"true" forKey:@"required"];
  8438. [dic setValue:contact_id_dic forKey:@"item_1"];
  8439. // company name
  8440. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8441. [dic setValue:company_name_dic forKey:@"item_2"];
  8442. // address
  8443. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8444. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8445. if ([ext_required integerValue]) {
  8446. [ext_dic setValue:@"true" forKey:@"required"];
  8447. }
  8448. [dic setValue:ext_dic forKey:@"item_3"];
  8449. // contact
  8450. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8451. [dic setValue:contact_dic forKey:@"item_4"];
  8452. // phone
  8453. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8454. [dic setValue:phone_dic forKey:@"item_5"];
  8455. // fax
  8456. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8457. [dic setValue:fax_dic forKey:@"item_6"];
  8458. // email
  8459. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8460. [dic setValue:email_dic forKey:@"item_7"];
  8461. }];
  8462. return dic;
  8463. }
  8464. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8465. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8466. // setting
  8467. NSDictionary *setting = params[@"setting"];
  8468. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8469. [dic setValue:hide forKey:@"hide"];
  8470. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8471. orderCode = [self translateSingleQuote:orderCode];
  8472. 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];
  8473. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8474. NSString *cid = [self textAtColumn:0 statement:stmt];
  8475. NSString *name = [self textAtColumn:1 statement:stmt];
  8476. NSString *ext = [self textAtColumn:2 statement:stmt];
  8477. NSString *contact = [self textAtColumn:3 statement:stmt];
  8478. NSString *email = [self textAtColumn:4 statement:stmt];
  8479. NSString *fax = [self textAtColumn:5 statement:stmt];
  8480. NSString *phone = [self textAtColumn:6 statement:stmt];
  8481. // count
  8482. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8483. // title
  8484. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8485. // hide
  8486. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8487. // choose
  8488. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8489. [choose_dic setValue:@"choose" forKey:@"aname"];
  8490. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8491. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8492. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8493. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8494. @"billing_contact" : @"receive_contact",
  8495. @"billing_email" : @"receive_email",
  8496. @"billing_ext" : @"receive_ext",
  8497. @"billing_fax" : @"receive_fax",
  8498. @"billing_name" : @"receive_name",
  8499. @"billing_phone" : @"receive_phone"},
  8500. @"type" : @"pull"};
  8501. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8502. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8503. @"type" : @"pull",
  8504. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8505. @"billing_contact" : @"customer_contact",
  8506. @"billing_email" : @"customer_email",
  8507. @"billing_ext" : @"customer_contact_ext",
  8508. @"billing_fax" : @"customer_fax",
  8509. @"billing_name" : @"customer_name",
  8510. @"billing_phone" : @"customer_phone"}
  8511. };
  8512. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8513. NSDictionary *select_bill_to_dic = @{
  8514. @"aname" : @"Select bill to",
  8515. @"name" : @"select_cid",
  8516. @"refresh" : [NSNumber numberWithInteger:0],
  8517. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8518. @"key_map" : @{
  8519. @"billing_cid" : @"customer_cid",
  8520. @"billing_contact" : @"customer_contact",
  8521. @"billing_email" : @"customer_email",
  8522. @"billing_ext" : @"customer_contact_ext",
  8523. @"billing_fax" : @"customer_fax",
  8524. @"billing_name" : @"customer_name",
  8525. @"billing_phone" : @"customer_phone"
  8526. }
  8527. };
  8528. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8529. [dic setValue:choose_dic forKey:@"item_0"];
  8530. // contact id
  8531. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8532. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8533. [contact_id_dic setValue:@"true" forKey:@"required"];
  8534. [dic setValue:contact_id_dic forKey:@"item_1"];
  8535. // company name
  8536. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8537. [dic setValue:company_name_dic forKey:@"item_2"];
  8538. // address
  8539. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8540. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8541. if ([ext_required integerValue]) {
  8542. [ext_dic setValue:@"true" forKey:@"required"];
  8543. }
  8544. [dic setValue:ext_dic forKey:@"item_3"];
  8545. // contact
  8546. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8547. [dic setValue:contact_dic forKey:@"item_4"];
  8548. // phone
  8549. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8550. [dic setValue:phone_dic forKey:@"item_5"];
  8551. // fax
  8552. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8553. [dic setValue:fax_dic forKey:@"item_6"];
  8554. // email
  8555. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8556. [dic setValue:email_dic forKey:@"item_7"];
  8557. }];
  8558. return dic;
  8559. }
  8560. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8561. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8562. // setting
  8563. NSDictionary *setting = params[@"setting"];
  8564. NSNumber *hide = setting[@"ReturnToHide"];
  8565. [dic setValue:hide forKey:@"hide"];
  8566. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8567. orderCode = [self translateSingleQuote:orderCode];
  8568. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  8569. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8570. NSString *cid = [self textAtColumn:0 statement:stmt];
  8571. NSString *name = [self textAtColumn:1 statement:stmt];
  8572. NSString *ext = [self textAtColumn:2 statement:stmt];
  8573. NSString *contact = [self textAtColumn:3 statement:stmt];
  8574. NSString *email = [self textAtColumn:4 statement:stmt];
  8575. NSString *fax = [self textAtColumn:5 statement:stmt];
  8576. NSString *phone = [self textAtColumn:6 statement:stmt];
  8577. // count
  8578. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8579. // title
  8580. [dic setValue:@"Return To" forKey:@"title"];
  8581. // hide
  8582. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8583. // choose
  8584. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8585. [choose_dic setValue:@"choose" forKey:@"aname"];
  8586. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8587. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8588. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8589. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8590. @"returnto_contact" : @"sender_contact",
  8591. @"returnto_email" : @"sender_email",
  8592. @"returnto_ext" : @"sender_ext",
  8593. @"returnto_fax" : @"sender_fax",
  8594. @"returnto_name" : @"sender_name",
  8595. @"returnto_phone" : @"sender_phone"},
  8596. @"type" : @"pull"};
  8597. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8598. NSDictionary *select_return_to_dic = @{
  8599. @"aname" : @"Select return to",
  8600. @"name" : @"select_cid",
  8601. @"refresh" : [NSNumber numberWithInteger:0],
  8602. @"value" : @"Contact_Return_To",
  8603. @"key_map" : @{
  8604. @"returnto_cid" : @"customer_cid",
  8605. @"returnto_contact" : @"customer_contact",
  8606. @"returnto_email" : @"customer_email",
  8607. @"returnto_ext" : @"customer_contact_ext",
  8608. @"returnto_fax" : @"customer_fax",
  8609. @"returnto_name" : @"customer_name",
  8610. @"returnto_phone" : @"customer_phone"
  8611. }
  8612. };
  8613. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8614. [dic setValue:choose_dic forKey:@"item_0"];
  8615. // contact id
  8616. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8617. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8618. [contact_id_dic setValue:@"true" forKey:@"required"];
  8619. [dic setValue:contact_id_dic forKey:@"item_1"];
  8620. // company name
  8621. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8622. [dic setValue:company_name_dic forKey:@"item_2"];
  8623. // address
  8624. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8625. [dic setValue:ext_dic forKey:@"item_3"];
  8626. // contact
  8627. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8628. [dic setValue:contact_dic forKey:@"item_4"];
  8629. // phone
  8630. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8631. [dic setValue:phone_dic forKey:@"item_5"];
  8632. // fax
  8633. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8634. [dic setValue:fax_dic forKey:@"item_6"];
  8635. // email
  8636. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8637. [dic setValue:email_dic forKey:@"item_7"];
  8638. }];
  8639. return dic;
  8640. }
  8641. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8642. {
  8643. assert(params[@"user"]!=nil);
  8644. assert(params[@"contact_id"]!=nil);
  8645. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8646. orderCode = [self translateSingleQuote:orderCode];
  8647. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8648. __block double TotalCuft = 0;
  8649. __block double TotalWeight = 0;
  8650. __block int TotalCarton = 0;
  8651. __block double payments = 0;
  8652. // setting
  8653. NSDictionary *setting = params[@"setting"];
  8654. NSNumber *hide = setting[@"ModelInformationHide"];
  8655. [dic setValue:hide forKey:@"hide"];
  8656. 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];
  8657. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8658. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8659. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8660. // item id
  8661. int item_id = sqlite3_column_int(stmt, 0);
  8662. // count
  8663. int item_count = sqlite3_column_int(stmt, 1);
  8664. // stockUom
  8665. int stockUom = sqlite3_column_int(stmt, 2);
  8666. // unit price
  8667. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8668. NSString* Price=nil;
  8669. if([str_price isEqualToString:@""])
  8670. {
  8671. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8672. if(price==nil)
  8673. Price=@"No Price.";
  8674. else
  8675. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8676. }
  8677. else
  8678. {
  8679. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8680. }
  8681. // discount
  8682. double discount = sqlite3_column_double(stmt, 4);
  8683. // name
  8684. NSString *name = [self textAtColumn:5 statement:stmt];
  8685. // description
  8686. NSString *description = [self textAtColumn:6 statement:stmt];
  8687. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8688. // line note
  8689. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8690. int avaulability = sqlite3_column_int(stmt, 8);
  8691. // img
  8692. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8693. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8694. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8695. if(combine != nil)
  8696. {
  8697. // int citem=0;
  8698. int bcount=[[combine valueForKey:@"count"] intValue];
  8699. for(int bc=0;bc<bcount;bc++)
  8700. {
  8701. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8702. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8703. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8704. subTotal += uprice * modulus * item_count;
  8705. }
  8706. }
  8707. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8708. [model_dic setValue:@"model" forKey:@"control"];
  8709. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8710. [model_dic setValue:description forKey:@"description"];
  8711. [model_dic setValue:line_note forKey:@"note"];
  8712. [model_dic setValue:img forKey:@"img_url"];
  8713. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8714. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8715. [model_dic setValue:Price forKey:@"unit_price"];
  8716. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8717. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8718. if (combine) {
  8719. [model_dic setValue:combine forKey:@"combine"];
  8720. }
  8721. // well,what under the row is the info for total
  8722. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8723. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8724. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8725. int carton=[bsubtotaljson[@"carton"] intValue];
  8726. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8727. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8728. TotalCuft += cuft;
  8729. TotalWeight += weight;
  8730. TotalCarton += carton;
  8731. payments += subTotal;
  8732. //---------------------------
  8733. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8734. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8735. }];
  8736. [dic setValue:@"Model Information" forKey:@"title"];
  8737. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8738. return dic;
  8739. }
  8740. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8741. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8742. [dic setValue:@"Remarks Content" forKey:@"title"];
  8743. // setting
  8744. NSDictionary *setting = params[@"setting"];
  8745. NSNumber *hide = setting[@"RemarksContentHide"];
  8746. [dic setValue:hide forKey:@"hide"];
  8747. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8748. orderCode = [self translateSingleQuote:orderCode];
  8749. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8750. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8751. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8752. int mustCall = sqlite3_column_int(stmt, 1);
  8753. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8754. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8755. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8756. NSDictionary *po_dic = @{
  8757. @"aname" : @"PO#",
  8758. @"control" : @"edit",
  8759. @"keyboard" : @"text",
  8760. @"name" : @"poNumber",
  8761. @"value" : poNumber
  8762. };
  8763. #ifdef BUILD_CONTRAST
  8764. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8765. NSDictionary *schedule_dict = @{
  8766. @"aname" : @"Schdule Date",
  8767. @"control" : @"datepicker",
  8768. @"type": @"date",
  8769. @"required": @"true",
  8770. @"name" : @"schedule_date_str",
  8771. @"value" : nsdate
  8772. };
  8773. #endif
  8774. NSDictionary *must_call_dic = @{
  8775. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8776. @"control" : @"switch",
  8777. @"name" : @"must_call",
  8778. @"value" : mustCall ? @"true" : @"false"
  8779. };
  8780. NSDictionary *general_notes_dic = @{
  8781. @"aname" : @"General notes",
  8782. @"control" : @"text_view",
  8783. @"keyboard" : @"text",
  8784. @"name" : @"comments",
  8785. @"value" : generalNotes
  8786. };
  8787. // NSDictionary *internal_notes_dic = @{
  8788. // @"aname" : @"Internal notes",
  8789. // @"control" : @"text_view",
  8790. // @"keyboard" : @"text",
  8791. // @"name" : @"internal_notes",
  8792. // @"value" : internalNotes
  8793. // };
  8794. #ifdef BUILD_CONTRAST
  8795. [dic setValue:po_dic forKey:@"item_0"];
  8796. [dic setValue:schedule_dict forKey:@"item_1"];
  8797. [dic setValue:must_call_dic forKey:@"item_2"];
  8798. [dic setValue:general_notes_dic forKey:@"item_3"];
  8799. #else
  8800. [dic setValue:po_dic forKey:@"item_0"];
  8801. [dic setValue:must_call_dic forKey:@"item_1"];
  8802. [dic setValue:general_notes_dic forKey:@"item_2"];
  8803. #endif
  8804. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8805. }];
  8806. #ifdef BUILD_CONTRAST
  8807. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8808. #else
  8809. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8810. #endif
  8811. return dic;
  8812. }
  8813. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8814. // params
  8815. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8816. orderCode = [self translateSingleQuote:orderCode];
  8817. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8818. // setting
  8819. NSDictionary *setting = params[@"setting"];
  8820. NSNumber *hide = setting[@"OrderTotalHide"];
  8821. [dic setValue:hide forKey:@"hide"];
  8822. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8823. __block double lift_gate_value = 0;
  8824. __block double handling_fee = 0;
  8825. __block double shipping = 0;
  8826. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8827. int lift_gate = sqlite3_column_int(stmt, 0);
  8828. lift_gate_value = sqlite3_column_double(stmt, 1);
  8829. shipping = sqlite3_column_double(stmt, 2);
  8830. handling_fee = sqlite3_column_double(stmt, 3);
  8831. if (!lift_gate) {
  8832. lift_gate_value = 0;
  8833. }
  8834. }];
  8835. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8836. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8837. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8838. double payments = [[total valueForKey:@"payments"] doubleValue];
  8839. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8840. double totalPrice = payments;
  8841. [dic setValue:@"Order Total" forKey:@"title"];
  8842. NSDictionary *payments_dic = @{
  8843. @"align" : @"right",
  8844. @"aname" : @"Payments/Credits",
  8845. @"control" : @"text",
  8846. @"name" : @"paymentsAndCredits",
  8847. @"type" : @"price",
  8848. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8849. };
  8850. [dic setValue:payments_dic forKey:@"item_0"];
  8851. // version 1.71 remove
  8852. // NSDictionary *handling_fee_dic = @{
  8853. // @"align" : @"right",
  8854. // @"aname" : @"Handling Fee",
  8855. // @"control" : @"text",
  8856. // @"name" : @"handling_fee_value",
  8857. // @"required" : @"true",
  8858. // @"type" : @"price",
  8859. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8860. // };
  8861. NSDictionary *shipping_dic = @{
  8862. @"align" : @"right",
  8863. @"aname" : @"Shipping*",
  8864. @"control" : @"text",
  8865. @"name" : @"shipping",
  8866. @"required" : @"true",
  8867. @"type" : @"price",
  8868. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8869. };
  8870. NSDictionary *lift_gate_dic = @{
  8871. @"align" : @"right",
  8872. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8873. @"control" : @"text",
  8874. @"name" : @"lift_gate_value",
  8875. @"required" : @"true",
  8876. @"type" : @"price",
  8877. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8878. };
  8879. int item_count = 1;
  8880. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8881. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8882. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8883. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8884. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8885. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8886. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8887. } else {
  8888. }
  8889. }
  8890. // version 1.71 remove
  8891. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8892. NSDictionary *total_price_dic = @{
  8893. @"align" : @"right",
  8894. @"aname" : @"Total",
  8895. @"control" : @"text",
  8896. @"name" : @"totalPrice",
  8897. @"type" : @"price",
  8898. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8899. };
  8900. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8901. NSDictionary *total_cuft_dic = @{
  8902. @"align" : @"right",
  8903. @"aname" : @"Total Cuft",
  8904. @"control" : @"text",
  8905. @"name" : @"",
  8906. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8907. };
  8908. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8909. NSDictionary *total_weight_dic = @{
  8910. @"align" : @"right",
  8911. @"aname" : @"Total Weight",
  8912. @"control" : @"text",
  8913. @"name" : @"",
  8914. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8915. };
  8916. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8917. NSDictionary *total_carton_dic = @{
  8918. @"align" : @"right",
  8919. @"aname" : @"Total Carton",
  8920. @"control" : @"text",
  8921. @"name" : @"",
  8922. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8923. };
  8924. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8925. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8926. return dic;
  8927. }
  8928. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8929. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8930. orderCode = [self translateSingleQuote:orderCode];
  8931. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8932. // setting
  8933. NSDictionary *setting = params[@"setting"];
  8934. NSNumber *hide = setting[@"SignatureHide"];
  8935. [dic setValue:hide forKey:@"hide"];
  8936. [dic setValue:@"Signature" forKey:@"title"];
  8937. __block NSString *pic_path = @"";
  8938. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8939. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8940. pic_path = [self textAtColumn:0 statement:stmt];
  8941. }];
  8942. NSDictionary *pic_dic = @{
  8943. @"aname" : @"Signature",
  8944. @"avalue" :pic_path,
  8945. @"control" : @"signature",
  8946. @"name" : @"sign_picpath",
  8947. @"value" : pic_path
  8948. };
  8949. [dic setValue:pic_dic forKey:@"item_0"];
  8950. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8951. return dic;
  8952. }
  8953. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8954. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8955. // setting
  8956. NSDictionary *setting = params[@"setting"];
  8957. NSNumber *hide = setting[@"ShippingMethodHide"];
  8958. [dic setValue:hide forKey:@"hide"];
  8959. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8960. orderCode = [self translateSingleQuote:orderCode];
  8961. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8962. __block NSString *logist = @"";
  8963. __block NSString *carrier = @"";
  8964. __block NSString *lift_gate = @"";
  8965. __block int lift_gate_integer = 0;
  8966. __block NSString *logistic_note = @"";
  8967. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8968. logist = [self textAtColumn:0 statement:stmt];
  8969. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8970. logistic_note = [self textAtColumn:2 statement:stmt];
  8971. carrier = [self textAtColumn:3 statement:stmt];
  8972. }];
  8973. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8974. [dic setValue:@"Shipping Method" forKey:@"title"];
  8975. // val_0
  8976. int PERSONAL_PICK_UP_check = 0;
  8977. int USE_MY_CARRIER_check = 0;
  8978. NSString *logistic_note_text = @"";
  8979. int will_call_check = 0;
  8980. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8981. will_call_check = 1;
  8982. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8983. PERSONAL_PICK_UP_check = 1;
  8984. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8985. USE_MY_CARRIER_check = 1;
  8986. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8987. logistic_note = [logistic_note_array firstObject];
  8988. if (logistic_note_array.count > 1) {
  8989. logistic_note_text = [logistic_note_array lastObject];
  8990. }
  8991. }
  8992. }
  8993. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8994. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8995. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8996. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8997. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8998. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8999. NSDictionary *val0_subItem_item0 = @{
  9000. @"aname" : @"Option",
  9001. @"cadedate" : @{
  9002. @"count" : [NSNumber numberWithInteger:2],
  9003. @"val_0" : @{
  9004. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  9005. @"refresh" : [NSNumber numberWithInteger:0],
  9006. @"value" : @"PERSONAL PICK UP",
  9007. @"value_id" : @"PERSONAL PICK UP"
  9008. },
  9009. @"val_1" : @{
  9010. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  9011. @"refresh" : [NSNumber numberWithInteger:0],
  9012. @"sub_item" : @{
  9013. @"count" : [NSNumber numberWithInteger:1],
  9014. @"item_0" : @{
  9015. @"aname" : @"BOL",
  9016. @"control" : @"edit",
  9017. @"keyboard" : @"text",
  9018. @"name" : @"logist_note_text",
  9019. @"refresh" : [NSNumber numberWithInteger:0],
  9020. @"required" : @"false",
  9021. @"value" : logistic_note_text
  9022. }
  9023. },
  9024. @"value" : @"USE MY CARRIER",
  9025. @"value_id" : @"USE MY CARRIER"
  9026. }
  9027. },
  9028. @"control" : @"enum",
  9029. @"name" : @"logistic_note",
  9030. @"required" : @"true",
  9031. @"single_select" : @"true"
  9032. };
  9033. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  9034. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  9035. // val_1
  9036. lift_gate = [self valueInParams:params key:@"lift_gate"];
  9037. if([lift_gate isEqualToString:@""]) {
  9038. if (lift_gate_integer == 1) {
  9039. lift_gate = @"true";
  9040. } else {
  9041. lift_gate = @"false";
  9042. }
  9043. }
  9044. // carrier = [self valueInParams:params key:@"carrier"];
  9045. // if([carrier isEqualToString:@""]) {
  9046. //
  9047. // if (lift_gate_integer == 1) {
  9048. // lift_gate = @"true";
  9049. // } else {
  9050. // lift_gate = @"false";
  9051. // }
  9052. //
  9053. // }
  9054. int common_carrier_check = 0;
  9055. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  9056. common_carrier_check = 1;
  9057. [params setValue:lift_gate forKey:@"lift_gate"];
  9058. #ifdef BUILD_CONTRAST
  9059. [params setValue:carrier forKey:@"carrier"];
  9060. #endif
  9061. }
  9062. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  9063. // NSString * code = [iSalesDB jk_queryText:Sql];
  9064. // [self offline_getCarrierCode:offline_getCarrierCode];
  9065. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  9066. NSDictionary *val_1 = @{
  9067. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  9068. @"sub_item" : @{
  9069. @"item_0" : @{
  9070. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  9071. @"control" : @"switch",
  9072. @"name" : @"lift_gate",
  9073. @"refresh" : [NSNumber numberWithInteger:1],
  9074. @"required" : @"true",
  9075. @"value" : lift_gate
  9076. },
  9077. #ifdef BUILD_CONTRAST
  9078. @"count" : [NSNumber numberWithInteger:2],
  9079. @"item_1" : @{
  9080. @"aname" : @"Carrier",
  9081. @"cadedate" :carrierDic,
  9082. @"control" : @"enum",
  9083. @"name" : @"carrier",
  9084. @"required" : @"false",
  9085. @"value" : @"",
  9086. @"single_select" : @"true"
  9087. }
  9088. #else
  9089. @"count" : [NSNumber numberWithInteger:1]
  9090. #endif
  9091. },
  9092. @"value" : @"COMMON CARRIER",
  9093. @"value_id" : @"COMMON CARRIER"
  9094. };
  9095. // cadedate
  9096. NSDictionary *cadedate = @{
  9097. @"count" : [NSNumber numberWithInteger:2],
  9098. @"val_0" : val_0,
  9099. @"val_1" : val_1
  9100. };
  9101. // item_0
  9102. NSMutableDictionary *item_0 = @{
  9103. @"aname" : @"Shipping",
  9104. @"cadedate" : cadedate,
  9105. @"control" : @"enum",
  9106. @"name" : @"logist",
  9107. @"refresh" : [NSNumber numberWithInteger:1],
  9108. @"single_select" : @"true",
  9109. }.mutableCopy;
  9110. NSNumber *required = setting[@"ShippingMethodRequire"];
  9111. if ([required integerValue]) {
  9112. [item_0 setValue:@"true" forKey:@"required"];
  9113. }
  9114. [dic setValue:item_0 forKey:@"item_0"];
  9115. if ([logist isEqualToString:@"WILL CALL"]) {
  9116. [dic removeObjectForKey:@"lift_gate"];
  9117. }
  9118. return dic;
  9119. }
  9120. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  9121. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9122. orderCode = [self translateSingleQuote:orderCode];
  9123. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  9124. __block int submit_as_integer = 0;
  9125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9126. submit_as_integer = sqlite3_column_int(stmt, 0);
  9127. }];
  9128. int check11 = 0;
  9129. int check10 = 0;
  9130. if (submit_as_integer == 11) {
  9131. check11 = 1;
  9132. }
  9133. if (submit_as_integer == 10) {
  9134. check10 = 1;
  9135. }
  9136. // section_0
  9137. NSMutableDictionary *dic = @{
  9138. @"count" : @(1),
  9139. @"item_0" : @{
  9140. @"aname" : @"Submit Order As",
  9141. @"cadedate" : @{
  9142. @"count" : @(2),
  9143. @"val_0" : @{
  9144. @"check" : [NSNumber numberWithInteger:check11],
  9145. @"value" : @"Sales Order",
  9146. @"value_id" : @(11)
  9147. },
  9148. @"val_1" : @{
  9149. @"check" : [NSNumber numberWithInteger:check10],
  9150. @"value" : @"Quote",
  9151. @"value_id" : @(10)
  9152. }
  9153. },
  9154. @"control" : @"enum",
  9155. @"name" : @"erpOrderStatus",
  9156. @"required" : @"true",
  9157. @"single_select" : @"true"
  9158. },
  9159. @"title" : @"Order Type"
  9160. }.mutableCopy;
  9161. // setting
  9162. NSDictionary *setting = params[@"setting"];
  9163. NSNumber *hide = setting[@"OrderTypeHide"];
  9164. [dic setValue:hide forKey:@"hide"];
  9165. return dic;
  9166. }
  9167. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  9168. countryCode = [self translateSingleQuote:countryCode];
  9169. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  9170. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9171. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  9172. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  9173. if (name == NULL) {
  9174. name = "";
  9175. }
  9176. if (code == NULL) {
  9177. code = "";
  9178. }
  9179. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9180. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  9181. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  9182. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9183. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  9184. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9185. }
  9186. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  9187. [container setValue:stateDic forKey:key];
  9188. }] mutableCopy];
  9189. [ret removeObjectForKey:@"result"];
  9190. // failure 可以不用了,一样的
  9191. if (ret.allKeys.count == 0) {
  9192. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9193. [stateDic setValue:@"Other" forKey:@"value"];
  9194. [stateDic setValue:@"" forKey:@"value_id"];
  9195. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9196. if (state_code && [@"" isEqualToString:state_code]) {
  9197. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9198. }
  9199. NSString *key = [NSString stringWithFormat:@"val_0"];
  9200. [ret setValue:stateDic forKey:key];
  9201. }
  9202. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  9203. return ret;
  9204. }
  9205. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  9206. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9207. orderCode = [self translateSingleQuote:orderCode];
  9208. 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];
  9209. __block NSString *payType = @"";
  9210. __block NSString *firstName = @"";
  9211. __block NSString *lastName = @"";
  9212. __block NSString *addr1 = @"";
  9213. __block NSString *addr2 = @"";
  9214. __block NSString *zipcode = @"";
  9215. __block NSString *cardType = @"";
  9216. __block NSString *cardNumber = @"";
  9217. __block NSString *securityCode = @"";
  9218. __block NSString *month = @"";
  9219. __block NSString *year = @"";
  9220. __block NSString *city = @"";
  9221. __block NSString *state = @"";
  9222. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9223. payType = [self textAtColumn:0 statement:stmt];
  9224. firstName = [self textAtColumn:1 statement:stmt];
  9225. lastName = [self textAtColumn:2 statement:stmt];
  9226. addr1 = [self textAtColumn:3 statement:stmt];
  9227. addr2 = [self textAtColumn:4 statement:stmt];
  9228. zipcode = [self textAtColumn:5 statement:stmt];
  9229. cardType = [self textAtColumn:6 statement:stmt];
  9230. cardNumber = [self textAtColumn:7 statement:stmt];
  9231. securityCode = [self textAtColumn:8 statement:stmt];
  9232. month = [self textAtColumn:9 statement:stmt];
  9233. year = [self textAtColumn:10 statement:stmt];
  9234. city = [self textAtColumn:11 statement:stmt];
  9235. state = [self textAtColumn:12 statement:stmt];
  9236. }];
  9237. NSString *required = @"true";
  9238. // setting
  9239. NSDictionary *setting = params[@"setting"];
  9240. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9241. if ([requiredNumber integerValue]) {
  9242. required = @"true";
  9243. } else {
  9244. required = @"false";
  9245. }
  9246. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9247. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9248. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9249. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9250. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9251. NSString *type = [self textAtColumn:1 statement:stmt];
  9252. NSMutableDictionary *val = @{
  9253. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9254. @"value" : type,
  9255. @"value_id" : type_id
  9256. }.mutableCopy;
  9257. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9258. NSDictionary *sub_item =nil;
  9259. #ifdef BUILD_CONTRAST
  9260. sub_item = @{
  9261. @"count" : @(3),
  9262. @"item_0" : @{
  9263. @"aname" : @"Number",
  9264. @"control" : @"edit",
  9265. @"keyboard" : @"int",
  9266. @"length" : @"16",
  9267. @"name" : @"credit_card_number",
  9268. @"required" : @"false",
  9269. @"value" : cardNumber
  9270. },
  9271. @"item_1" : @{
  9272. @"aname" : @"Expiration Date",
  9273. @"control" : @"monthpicker",
  9274. @"name" : @"credit_card_expiration",
  9275. @"required" : @"false",
  9276. @"type" : @"date",
  9277. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9278. },
  9279. @"item_2" : @{
  9280. @"aname" : @"Security Code",
  9281. @"control" : @"edit",
  9282. @"keyboard" : @"int",
  9283. @"length" : @"4",
  9284. @"name" : @"credit_card_security_code",
  9285. @"required" : @"false",
  9286. @"value" : securityCode
  9287. },
  9288. };
  9289. #else
  9290. sub_item= @{
  9291. @"count" : @(3),
  9292. @"item_0" : @{
  9293. @"aname" : @"choose",
  9294. @"control" : @"multi_action",
  9295. @"count" : @(1),
  9296. @"item_0" : @{
  9297. @"aname" : @"Same as customer",
  9298. @"key_map" : @{
  9299. @"credit_card_address1" : @"customer_address1",
  9300. @"credit_card_address2" : @"customer_address2",
  9301. @"credit_card_city" : @"customer_city",
  9302. @"credit_card_first_name" : @"customer_first_name",
  9303. @"credit_card_last_name" : @"customer_last_name",
  9304. @"credit_card_state" : @"customer_state",
  9305. @"credit_card_zipcode" : @"customer_zipcode"
  9306. },
  9307. @"type" : @"pull"
  9308. }
  9309. },
  9310. @"item_1" : @{
  9311. @"aname" : @"",
  9312. @"color" : @"red",
  9313. @"control" : @"text",
  9314. @"name" : @"",
  9315. @"value" : @"USA Credit cards only"
  9316. },
  9317. @"item_2" : @{
  9318. @"aname" : @"Fill",
  9319. @"cadedate" : @{
  9320. @"count" : @(2),
  9321. @"val_0" : @{
  9322. @"check" : @(1),
  9323. @"sub_item" : @{
  9324. @"count" : @(11),
  9325. @"item_0" : @{
  9326. @"aname" : @"Type",
  9327. @"cadedate" : @{
  9328. @"count" : @(2),
  9329. @"val_0" : @{
  9330. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9331. @"value" : @"VISA",
  9332. @"value_id" : @"VISA"/*@(0)*/
  9333. },
  9334. @"val_1" : @{
  9335. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9336. @"value" : @"MASTER CARD",
  9337. @"value_id" : @"MASTER CARD"/*@(1)*/
  9338. }
  9339. },
  9340. @"control" : @"enum",
  9341. @"name" : @"credit_card_type",
  9342. @"required" : @"true",
  9343. @"single_select" : @"true"
  9344. },
  9345. @"item_1" : @{
  9346. @"aname" : @"Number",
  9347. @"control" : @"edit",
  9348. @"keyboard" : @"int",
  9349. @"length" : @"16",
  9350. @"name" : @"credit_card_number",
  9351. @"required" : @"true",
  9352. @"value" : cardNumber
  9353. },
  9354. @"item_10" : @{
  9355. @"aname" : @"State",
  9356. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9357. @"control" : @"enum",
  9358. @"enum" : @"true",
  9359. @"name" : @"credit_card_state",
  9360. @"required" : @"true",
  9361. @"single_select" : @"true"
  9362. },
  9363. @"item_2" : @{
  9364. @"aname" : @"Expiration Date",
  9365. @"control" : @"monthpicker",
  9366. @"name" : @"credit_card_expiration",
  9367. @"required" : @"true",
  9368. @"type" : @"date",
  9369. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9370. },
  9371. @"item_3" : @{
  9372. @"aname" : @"Security Code",
  9373. @"control" : @"edit",
  9374. @"keyboard" : @"int",
  9375. @"length" : @"3",
  9376. @"name" : @"credit_card_security_code",
  9377. @"required" : @"true",
  9378. @"value" : securityCode
  9379. },
  9380. @"item_4" : @{
  9381. @"aname" : @"First Name",
  9382. @"control" : @"edit",
  9383. @"keyboard" : @"text",
  9384. @"name" : @"credit_card_first_name",
  9385. @"required" : @"true",
  9386. @"value" : firstName
  9387. },
  9388. @"item_5" : @{
  9389. @"aname" : @"Last Name",
  9390. @"control" : @"edit",
  9391. @"keyboard" : @"text",
  9392. @"name" : @"credit_card_last_name",
  9393. @"required" : @"true",
  9394. @"value" : lastName
  9395. },
  9396. @"item_6" : @{
  9397. @"aname" : @"Address 1",
  9398. @"control" : @"edit",
  9399. @"keyboard" : @"text",
  9400. @"name" : @"credit_card_address1",
  9401. @"required" : @"true",
  9402. @"value" : addr1
  9403. },
  9404. @"item_7" : @{
  9405. @"aname" : @"Address 2",
  9406. @"control" : @"edit",
  9407. @"keyboard" : @"text",
  9408. @"name" : @"credit_card_address2",
  9409. @"value" : addr2
  9410. },
  9411. @"item_8" : @{
  9412. @"aname" : @"zip code",
  9413. @"control" : @"edit",
  9414. @"keyboard" : @"text",
  9415. @"name" : @"credit_card_zipcode",
  9416. @"required" : @"true",
  9417. @"value" : zipcode
  9418. },
  9419. @"item_9" : @{
  9420. @"aname" : @"City",
  9421. @"control" : @"edit",
  9422. @"keyboard" : @"text",
  9423. @"name" : @"credit_card_city",
  9424. @"required" : @"true",
  9425. @"value" : city
  9426. }
  9427. },
  9428. @"value" : @"Fill Now",
  9429. @"value_id" : @""
  9430. },
  9431. @"val_1" : @{
  9432. @"check" : @(0),
  9433. @"value" : @"Fill Later",
  9434. @"value_id" : @""
  9435. }
  9436. },
  9437. @"control" : @"enum",
  9438. @"name" : @"",
  9439. @"single_select" : @"true"
  9440. }
  9441. };
  9442. #endif;
  9443. [val setObject:sub_item forKey:@"sub_item"];
  9444. }
  9445. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9446. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9447. }];
  9448. // "section_2"
  9449. NSMutableDictionary *dic = @{
  9450. @"count" : @(1),
  9451. @"item_0" : @{
  9452. @"aname" : @"Payment",
  9453. @"required" : required,
  9454. @"cadedate" : cadedate,
  9455. // @{
  9456. // @"count" : @(6),
  9457. // @"val_3" : @{
  9458. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9459. // @"value" : @"Check",
  9460. // @"value_id" : @"Check"
  9461. // },
  9462. // @"val_2" : @{
  9463. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9464. // @"value" : @"Cash",
  9465. // @"value_id" : @"Cash"
  9466. // },
  9467. // @"val_1" : @{
  9468. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9469. // @"value" : @"NET 60",
  9470. // @"value_id" : @"NET 30"
  9471. // },
  9472. // @"val_4" : @{
  9473. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9474. // @"value" : @"Wire Transfer",
  9475. // @"value_id" : @"Wire Transfer"
  9476. // },
  9477. // @"val_0" : @{
  9478. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9479. // @"sub_item" : @{
  9480. // @"count" : @(3),
  9481. // @"item_0" : @{
  9482. // @"aname" : @"choose",
  9483. // @"control" : @"multi_action",
  9484. // @"count" : @(1),
  9485. // @"item_0" : @{
  9486. // @"aname" : @"Same as customer",
  9487. // @"key_map" : @{
  9488. // @"credit_card_address1" : @"customer_address1",
  9489. // @"credit_card_address2" : @"customer_address2",
  9490. // @"credit_card_city" : @"customer_city",
  9491. // @"credit_card_first_name" : @"customer_first_name",
  9492. // @"credit_card_last_name" : @"customer_last_name",
  9493. // @"credit_card_state" : @"customer_state",
  9494. // @"credit_card_zipcode" : @"customer_zipcode"
  9495. // },
  9496. // @"type" : @"pull"
  9497. // }
  9498. // },
  9499. // @"item_1" : @{
  9500. // @"aname" : @"",
  9501. // @"color" : @"red",
  9502. // @"control" : @"text",
  9503. // @"name" : @"",
  9504. // @"value" : @"USA Credit cards only"
  9505. // },
  9506. // @"item_2" : @{
  9507. // @"aname" : @"Fill",
  9508. // @"cadedate" : @{
  9509. // @"count" : @(2),
  9510. // @"val_0" : @{
  9511. // @"check" : @(1),
  9512. // @"sub_item" : @{
  9513. // @"count" : @(11),
  9514. // @"item_0" : @{
  9515. // @"aname" : @"Type",
  9516. // @"cadedate" : @{
  9517. // @"count" : @(2),
  9518. // @"val_0" : @{
  9519. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9520. // @"value" : @"VISA",
  9521. // @"value_id" : @(0)
  9522. // },
  9523. // @"val_1" : @{
  9524. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9525. // @"value" : @"MASTER CARD",
  9526. // @"value_id" : @(1)
  9527. // }
  9528. // },
  9529. // @"control" : @"enum",
  9530. // @"name" : @"credit_card_type",
  9531. // @"required" : @"true",
  9532. // @"single_select" : @"true"
  9533. // },
  9534. // @"item_1" : @{
  9535. // @"aname" : @"Number",
  9536. // @"control" : @"edit",
  9537. // @"keyboard" : @"int",
  9538. // @"length" : @"16",
  9539. // @"name" : @"credit_card_number",
  9540. // @"required" : @"true",
  9541. // @"value" : cardNumber
  9542. // },
  9543. // @"item_10" : @{
  9544. // @"aname" : @"State",
  9545. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9546. // @"control" : @"enum",
  9547. // @"enum" : @"true",
  9548. // @"name" : @"credit_card_state",
  9549. // @"required" : @"true",
  9550. // @"single_select" : @"true"
  9551. // },
  9552. // @"item_2" : @{
  9553. // @"aname" : @"Expiration Date",
  9554. // @"control" : @"monthpicker",
  9555. // @"name" : @"credit_card_expiration",
  9556. // @"required" : @"true",
  9557. // @"type" : @"date",
  9558. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9559. // },
  9560. // @"item_3" : @{
  9561. // @"aname" : @"Security Code",
  9562. // @"control" : @"edit",
  9563. // @"keyboard" : @"int",
  9564. // @"length" : @"3",
  9565. // @"name" : @"credit_card_security_code",
  9566. // @"required" : @"true",
  9567. // @"value" : securityCode
  9568. // },
  9569. // @"item_4" : @{
  9570. // @"aname" : @"First Name",
  9571. // @"control" : @"edit",
  9572. // @"keyboard" : @"text",
  9573. // @"name" : @"credit_card_first_name",
  9574. // @"required" : @"true",
  9575. // @"value" : firstName
  9576. // },
  9577. // @"item_5" : @{
  9578. // @"aname" : @"Last Name",
  9579. // @"control" : @"edit",
  9580. // @"keyboard" : @"text",
  9581. // @"name" : @"credit_card_last_name",
  9582. // @"required" : @"true",
  9583. // @"value" : lastName
  9584. // },
  9585. // @"item_6" : @{
  9586. // @"aname" : @"Address 1",
  9587. // @"control" : @"edit",
  9588. // @"keyboard" : @"text",
  9589. // @"name" : @"credit_card_address1",
  9590. // @"required" : @"true",
  9591. // @"value" : addr1
  9592. // },
  9593. // @"item_7" : @{
  9594. // @"aname" : @"Address 2",
  9595. // @"control" : @"edit",
  9596. // @"keyboard" : @"text",
  9597. // @"name" : @"credit_card_address2",
  9598. // @"value" : addr2
  9599. // },
  9600. // @"item_8" : @{
  9601. // @"aname" : @"zip code",
  9602. // @"control" : @"edit",
  9603. // @"keyboard" : @"text",
  9604. // @"name" : @"credit_card_zipcode",
  9605. // @"required" : @"true",
  9606. // @"value" : zipcode
  9607. // },
  9608. // @"item_9" : @{
  9609. // @"aname" : @"City",
  9610. // @"control" : @"edit",
  9611. // @"keyboard" : @"text",
  9612. // @"name" : @"credit_card_city",
  9613. // @"required" : @"true",
  9614. // @"value" : city
  9615. // }
  9616. // },
  9617. // @"value" : @"Fill Now",
  9618. // @"value_id" : @""
  9619. // },
  9620. // @"val_1" : @{
  9621. // @"check" : @(0),
  9622. // @"value" : @"Fill Later",
  9623. // @"value_id" : @""
  9624. // }
  9625. // },
  9626. // @"control" : @"enum",
  9627. // @"name" : @"",
  9628. // @"single_select" : @"true"
  9629. // }
  9630. // },
  9631. // @"value" : @"Visa/Master",
  9632. // @"value_id" : @"Credit Card"
  9633. // },
  9634. // @"val_5" : @{
  9635. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9636. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9637. // @"value_id" : @"FOLLOW EXISTING"
  9638. // }
  9639. // },
  9640. @"control" : @"enum",
  9641. @"name" : @"paymentType",
  9642. @"single_select" : @"true"
  9643. },
  9644. @"title" : @"Payment Information"
  9645. }.mutableCopy;
  9646. NSNumber *hide = setting[@"PaymentInformationHide"];
  9647. [dic setValue:hide forKey:@"hide"];
  9648. return dic;
  9649. }
  9650. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9651. // params
  9652. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9653. orderCode = [self translateSingleQuote:orderCode];
  9654. // 缺货检查
  9655. 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];
  9656. __block BOOL outOfStock = NO;
  9657. sqlite3 *database = db;
  9658. if (!db) {
  9659. database = [iSalesDB get_db];
  9660. }
  9661. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9662. int availability = sqlite3_column_int(stmt, 0);
  9663. int item_qty = sqlite3_column_int(stmt, 1);
  9664. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9665. }];
  9666. if (!db) {
  9667. [iSalesDB close_db:database];
  9668. }
  9669. return outOfStock;
  9670. }
  9671. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9672. {
  9673. assert(params[@"user"]!=nil);
  9674. assert(params[@"contact_id"]!=nil);
  9675. assert(params[@"can_create_backorder"]!=nil);
  9676. sqlite3 *db = [iSalesDB get_db];
  9677. // params
  9678. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9679. orderCode = [self translateSingleQuote:orderCode];
  9680. // 缺货检查
  9681. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9682. if (![params[@"can_create_backorder"] boolValue]) {
  9683. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9684. if (out_of_stock) {
  9685. [iSalesDB close_db:db];
  9686. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9687. [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"];
  9688. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9689. return [RAConvertor dict2data:resultDic];
  9690. }
  9691. }
  9692. // UISetting
  9693. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9694. NSString *cachefolder = [paths objectAtIndex:0];
  9695. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9696. NSData* json =nil;
  9697. json=[NSData dataWithContentsOfFile:img_cache];
  9698. NSError *error=nil;
  9699. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9700. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9701. [params setObject:setting forKey:@"setting"];
  9702. int section_count = 0;
  9703. // 0 Order Type 1 Shipping Method 2 Payment Information
  9704. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9705. // 0 Order Type
  9706. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9707. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9708. [ret setValue:order_type_dic forKey:key0];
  9709. // 1 Shipping Method
  9710. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9711. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9712. [ret setValue:shipping_method_dic forKey:key1];
  9713. // 2 Payment Information
  9714. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9715. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9716. [ret setValue:payment_info_dic forKey:key2];
  9717. // 3 Customer
  9718. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9719. NSDictionary *customer_dic = [self customerDic:params db:db];
  9720. [ret setValue:customer_dic forKey:key3];
  9721. // 4 Ship To
  9722. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9723. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9724. [ret setValue:ship_to_dic forKey:key4];
  9725. // 5 Ship From
  9726. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9727. if (![shipFromDisable integerValue]) {
  9728. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9729. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9730. [ret setValue:ship_from_dic forKey:key5];
  9731. }
  9732. // 6 Freight Bill To
  9733. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9734. if (![freightBillToDisable integerValue]) {
  9735. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9736. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9737. [ret setValue:freight_bill_to forKey:key6];
  9738. }
  9739. // 7 Merchandise Bill To
  9740. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9741. if (![merchandiseBillToDisable integerValue]) {
  9742. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9743. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9744. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9745. }
  9746. // 8 Return To
  9747. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9748. if (![returnToDisable integerValue]) {
  9749. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9750. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9751. [ret setValue:return_to_dic forKey:key8];
  9752. }
  9753. // 9 Model Information
  9754. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9755. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9756. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9757. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9758. [ret setValue:model_info_dic forKey:key9];
  9759. // 10 Remarks Content
  9760. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9761. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9762. [ret setValue:remarks_content_dic forKey:key10];
  9763. // 11 Order Total
  9764. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9765. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9766. [ret setValue:order_total_dic forKey:key11];
  9767. // 12 Signature
  9768. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9769. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9770. [ret setValue:sign_dic forKey:key12];
  9771. [ret setValue:@(section_count) forKey:@"section_count"];
  9772. [iSalesDB close_db:db];
  9773. return [RAConvertor dict2data:ret];
  9774. // return nil;
  9775. }
  9776. #pragma mark addr editor
  9777. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9778. NSMutableDictionary *new_item = [item mutableCopy];
  9779. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9780. return new_item;
  9781. }
  9782. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9783. {
  9784. // "is_subaction" = true;
  9785. // orderCode = MOB1608240002;
  9786. // password = 123456;
  9787. // "subaction_tag" = 1;
  9788. // user = EvanK;
  9789. // {
  9790. // "is_subaction" = true;
  9791. // orderCode = MOB1608240002;
  9792. // password = 123456;
  9793. // "refresh_trigger" = zipcode;
  9794. // "subaction_tag" = 1;
  9795. // user = EvanK;
  9796. // }
  9797. NSString *country_code = nil;
  9798. NSString *zipCode = nil;
  9799. NSString *stateCode = nil;
  9800. NSString *city = nil;
  9801. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9802. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9803. NSString *code_id = params[@"country"];
  9804. country_code = [self countryCodeByid:code_id];
  9805. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9806. NSString *zip_code = params[@"zipcode"];
  9807. // 剔除全部为空格
  9808. int spaceCount = 0;
  9809. for (int i = 0; i < zip_code.length; i++) {
  9810. if ([zip_code characterAtIndex:i] == ' ') {
  9811. spaceCount++;
  9812. }
  9813. }
  9814. if (spaceCount == zip_code.length) {
  9815. zip_code = @"";
  9816. }
  9817. zipCode = zip_code;
  9818. if (zipCode.length > 0) {
  9819. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9820. country_code = [dic valueForKey:@"country_code"];
  9821. if (!country_code) {
  9822. // country_code = @"US";
  9823. NSString *code_id = params[@"country"];
  9824. country_code = [self countryCodeByid:code_id];
  9825. }
  9826. stateCode = [dic valueForKey:@"state_code"];
  9827. if(!stateCode.length) {
  9828. stateCode = params[@"state"];
  9829. }
  9830. city = [dic valueForKey:@"city"];
  9831. if (!city.length) {
  9832. city = params[@"city"];
  9833. }
  9834. // zip code
  9835. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9836. // [zipDic setValue:zipCode forKey:@"value"];
  9837. // [section_0 setValue:zipDic forKey:@"item_11"];
  9838. } else {
  9839. NSString *code_id = params[@"country"];
  9840. country_code = [self countryCodeByid:code_id];
  9841. stateCode = params[@"state"];
  9842. city = params[@"city"];
  9843. }
  9844. }
  9845. }
  9846. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9847. // [ret removeObjectForKey:@"up_params"];
  9848. [ret setObject:@"New Address" forKey:@"title"];
  9849. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9850. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9851. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9852. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9853. // [country_dic removeObjectForKey:@"refresh"];
  9854. // [country_dic removeObjectForKey:@"restore"];
  9855. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9856. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9857. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9858. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9859. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9860. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9861. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9862. // [zip_code_dic removeObjectForKey:@"refresh"];
  9863. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9864. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9865. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9866. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9867. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9868. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9869. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9870. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9871. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9872. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9873. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9874. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9875. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9876. // country
  9877. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9878. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9879. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9880. // state
  9881. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9882. // NSDictionary *tmpDic = @{
  9883. // @"value" : @"Other",
  9884. // @"value_id" : @"",
  9885. // @"check" : [NSNumber numberWithInteger:0]
  9886. // };
  9887. //
  9888. // for (int i = 0; i < allState.allKeys.count; i++) {
  9889. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9890. //
  9891. // NSDictionary *tmp = allState[key];
  9892. // [allState setValue:tmpDic forKey:key];
  9893. // tmpDic = tmp;
  9894. // }
  9895. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9896. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9897. [ret setValue:new_section_0 forKey:@"section_0"];
  9898. return [RAConvertor dict2data:ret];
  9899. }
  9900. #pragma mark save addr
  9901. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9902. {
  9903. // NSString *companyName = [self valueInParams:params key:@"company"];
  9904. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9905. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9906. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9907. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9908. // NSString *countryId = [self valueInParams:params key:@"country"];
  9909. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9910. // NSString *city = [self valueInParams:params key:@"city"];
  9911. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9912. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9913. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9914. // NSString *phone = [self valueInParams:params key:@"phone"];
  9915. // NSString *fax = [self valueInParams:params key:@"fax"];
  9916. // NSString *email = [self valueInParams:params key:@"email"];
  9917. return [self offline_saveContact:params update:NO isCustomer:NO];
  9918. }
  9919. #pragma mark cancel order
  9920. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9921. {
  9922. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9923. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9924. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9925. if (order_id.length) {
  9926. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9927. }
  9928. int ret = [iSalesDB execSql:sql];
  9929. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9930. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9931. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9932. // [dic setValue:@"160409" forKey:@"min_ver"];
  9933. return [RAConvertor dict2data:dic];
  9934. }
  9935. #pragma mark sign order
  9936. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9937. {
  9938. //参考 offline_saveBusinesscard
  9939. DebugLog(@"sign order params: %@",params);
  9940. // orderCode = MOB1608240002;
  9941. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9942. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9943. orderCode = [self translateSingleQuote:orderCode];
  9944. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9945. picPath = [self translateSingleQuote:picPath];
  9946. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9947. int ret = [iSalesDB execSql:sql];
  9948. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9949. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9950. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9951. // [dic setValue:@"160409" forKey:@"min_ver"];
  9952. return [RAConvertor dict2data:dic];
  9953. }
  9954. #pragma mark save order
  9955. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9956. NSString *ret = [self valueInParams:params key:key];
  9957. if (translate) {
  9958. ret = [self translateSingleQuote:ret];
  9959. }
  9960. return ret;
  9961. }
  9962. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9963. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9964. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9965. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9966. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9967. if (schedule_date.length==0 ) {
  9968. schedule_date = @"";
  9969. } else {
  9970. schedule_date = [self rchangeDateFormate:schedule_date];
  9971. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9972. }
  9973. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9974. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9975. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9976. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9977. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9978. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9979. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9980. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9981. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9982. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9983. if (![total_price isEqualToString:@""]) {
  9984. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9985. } else {
  9986. total_price = @"";
  9987. }
  9988. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9989. if ([paymentsAndCredits isEqualToString:@""]) {
  9990. paymentsAndCredits = @"";
  9991. } else {
  9992. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9993. }
  9994. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9995. if ([handling_fee_value isEqualToString:@""]) {
  9996. handling_fee_value = @"";
  9997. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9998. handling_fee_value = @"";
  9999. } else {
  10000. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  10001. }
  10002. NSString *handling_fee_placeholder = handling_fee_value;
  10003. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  10004. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  10005. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  10006. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  10007. NSString *lift_gate_placeholder = @"";
  10008. if ([lift_gate_value isEqualToString:@""]) {
  10009. lift_gate_placeholder = @"";
  10010. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  10011. lift_gate_placeholder = @"";
  10012. } else {
  10013. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  10014. }
  10015. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  10016. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  10017. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  10018. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  10019. 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];
  10020. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  10021. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  10022. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  10023. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  10024. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  10025. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  10026. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  10027. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  10028. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  10029. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  10030. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  10031. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  10032. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  10033. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  10034. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  10035. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  10036. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  10037. BOOL number_nil = [credit_card_number isEqualToString:@""];
  10038. if (!number_nil) {
  10039. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  10040. }
  10041. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  10042. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  10043. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  10044. if (!security_code_nil) {
  10045. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  10046. }
  10047. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  10048. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  10049. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  10050. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  10051. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  10052. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  10053. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  10054. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  10055. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  10056. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  10057. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  10058. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  10059. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  10060. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  10061. NSString *contact_id = customer_cid;
  10062. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  10063. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  10064. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  10065. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  10066. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  10067. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  10068. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  10069. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  10070. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  10071. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  10072. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  10073. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  10074. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  10075. //
  10076. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  10077. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  10078. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  10079. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  10080. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  10081. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  10082. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  10083. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  10084. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  10085. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  10086. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  10087. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  10088. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  10089. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  10090. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  10091. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  10092. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  10093. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  10094. 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];
  10095. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  10096. if (receive_cid.length) {
  10097. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  10098. }
  10099. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  10100. if (receive_name.length) {
  10101. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  10102. }
  10103. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  10104. if (receive_ext.length) {
  10105. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  10106. }
  10107. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  10108. if (receive_contact.length) {
  10109. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  10110. }
  10111. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  10112. if (receive_phone.length) {
  10113. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  10114. }
  10115. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  10116. if (receive_email.length) {
  10117. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  10118. }
  10119. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  10120. if (receive_fax.length) {
  10121. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  10122. }
  10123. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  10124. if (sender_cid.length) {
  10125. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  10126. }
  10127. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  10128. if (sender_name.length) {
  10129. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  10130. }
  10131. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  10132. if (sender_ext.length) {
  10133. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  10134. }
  10135. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  10136. if(sender_contact.length) {
  10137. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  10138. }
  10139. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  10140. if (sender_phone.length) {
  10141. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  10142. }
  10143. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  10144. if (sender_fax.length) {
  10145. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  10146. }
  10147. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  10148. if (sender_email.length) {
  10149. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  10150. }
  10151. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  10152. if (shipping_billto_cid.length) {
  10153. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  10154. }
  10155. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  10156. if (shipping_billto_name.length) {
  10157. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  10158. }
  10159. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  10160. if (shipping_billto_ext.length) {
  10161. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  10162. }
  10163. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  10164. if (shipping_billto_contact.length) {
  10165. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  10166. }
  10167. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  10168. if (shipping_billto_phone.length) {
  10169. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  10170. }
  10171. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  10172. if (shipping_billto_fax.length) {
  10173. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  10174. }
  10175. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  10176. if (shipping_billto_email.length) {
  10177. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  10178. }
  10179. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  10180. if (billing_cid.length) {
  10181. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  10182. }
  10183. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  10184. if (billing_name.length) {
  10185. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  10186. }
  10187. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  10188. if (billing_ext.length) {
  10189. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  10190. }
  10191. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  10192. if (billing_contact.length) {
  10193. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  10194. }
  10195. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  10196. if (billing_phone.length) {
  10197. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  10198. }
  10199. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  10200. if (billing_fax.length) {
  10201. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  10202. }
  10203. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  10204. if (billing_email.length) {
  10205. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  10206. }
  10207. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  10208. if (returnto_cid.length) {
  10209. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  10210. }
  10211. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10212. if (returnto_name.length) {
  10213. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10214. }
  10215. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10216. if (returnto_ext.length) {
  10217. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10218. }
  10219. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10220. if (returnto_contact.length) {
  10221. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10222. }
  10223. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10224. if (returnto_phone.length) {
  10225. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10226. }
  10227. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10228. if (returnto_fax.length) {
  10229. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10230. }
  10231. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10232. if (returnto_email.length) {
  10233. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10234. }
  10235. NSString *order_status = @"status = 1,";
  10236. if (submit) {
  10237. order_status = @"status = -11,";
  10238. }
  10239. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10240. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10241. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10242. NSString *sync_sql = @"";
  10243. if (submit) {
  10244. param[@"truck_carrier"]=carrier;
  10245. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10246. [param setValue:sales_rep forKey:@"sales_rep"];
  10247. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10248. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10249. }
  10250. 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];
  10251. DebugLog(@"save order contactSql: %@",contactSql);
  10252. DebugLog(@"save order orderSql: %@",orderSql);
  10253. // int contact_ret = [iSalesDB execSql:contactSql];
  10254. int order_ret = [iSalesDB execSql:orderSql];
  10255. int ret = order_ret;
  10256. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10257. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10258. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10259. // [dic setValue:@"160409" forKey:@"min_ver"];
  10260. [dic setObject:so_id forKey:@"so#"];
  10261. return [RAConvertor dict2data:dic];
  10262. }
  10263. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10264. {
  10265. // id foo = nil;
  10266. // NSMutableArray *a = @[].mutableCopy;
  10267. // [a addObject:foo];
  10268. return [self saveorder:param submit:NO];
  10269. }
  10270. #pragma mark add to cart by name
  10271. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10272. {
  10273. NSString *orderCode = [params objectForKey:@"orderCode"];
  10274. NSDictionary *newParams = @{
  10275. @"product_id" : params[@"product_id_string"],
  10276. @"orderCode" : orderCode,
  10277. @"can_create_backorder":params[@"can_create_backorder"]
  10278. };
  10279. return [self offline_add2cart:[newParams mutableCopy]];
  10280. }
  10281. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10282. {
  10283. NSString *orderCode = [params objectForKey:@"orderCode"];
  10284. NSString *upccode = [params objectForKey:@"upc_code"];
  10285. // product_name = [self translateSingleQuote:product_name];
  10286. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10287. // bool search_upc = [params[@"search_upc"] boolValue];
  10288. sqlite3 *db = [iSalesDB get_db];
  10289. __block NSMutableString *product_id_string = [NSMutableString string];
  10290. // NSString *name = [product_name_array objectAtIndex:i];
  10291. NSString *sql =nil;
  10292. 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];
  10293. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10294. int product_id = sqlite3_column_int(stmt, 0);
  10295. [product_id_string appendFormat:@"%d",product_id];
  10296. }];
  10297. if (!orderCode) {
  10298. orderCode = @"";
  10299. }
  10300. NSDictionary *newParams = @{
  10301. @"product_id" : product_id_string,
  10302. @"orderCode" : orderCode,
  10303. @"can_create_backorder":params[@"can_create_backorder"]
  10304. };
  10305. [iSalesDB close_db:db];
  10306. return [self offline_add2cart:[newParams mutableCopy]];
  10307. }
  10308. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10309. {
  10310. NSString *orderCode = [params objectForKey:@"orderCode"];
  10311. NSString *product_name = [params objectForKey:@"product_name"];
  10312. product_name = [self translateSingleQuote:product_name];
  10313. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10314. bool search_upc = [params[@"search_upc"] boolValue];
  10315. sqlite3 *db = [iSalesDB get_db];
  10316. __block NSMutableString *product_id_string = [NSMutableString string];
  10317. for (int i = 0; i < product_name_array.count; i++) {
  10318. NSString *name = [product_name_array objectAtIndex:i];
  10319. NSString *sql =nil;
  10320. if(search_upc)
  10321. 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];
  10322. else
  10323. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10324. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10325. int product_id = sqlite3_column_int(stmt, 0);
  10326. if (i == product_name_array.count - 1) {
  10327. [product_id_string appendFormat:@"%d",product_id];
  10328. } else {
  10329. [product_id_string appendFormat:@"%d,",product_id];
  10330. }
  10331. }];
  10332. }
  10333. if (!orderCode) {
  10334. orderCode = @"";
  10335. }
  10336. NSDictionary *newParams = @{
  10337. @"product_id" : product_id_string,
  10338. @"orderCode" : orderCode,
  10339. @"can_create_backorder":params[@"can_create_backorder"]
  10340. };
  10341. [iSalesDB close_db:db];
  10342. return [self offline_add2cart:[newParams mutableCopy]];
  10343. }
  10344. #pragma mark reset order
  10345. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10346. {
  10347. // UIApplication * app = [UIApplication sharedApplication];
  10348. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10349. [iSalesDB disable_trigger];
  10350. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10351. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10352. [iSalesDB enable_trigger];
  10353. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10354. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10355. return [RAConvertor dict2data:dic];
  10356. }
  10357. #pragma mark submit order
  10358. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10359. {
  10360. return [self saveorder:params submit:YES];
  10361. }
  10362. #pragma mark copy order
  10363. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10364. {
  10365. NSMutableDictionary *ret = @{}.mutableCopy;
  10366. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10367. sqlite3 *db = [iSalesDB get_db];
  10368. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10369. // 首先查看联系人是否active
  10370. 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];
  10371. __block int customer_is_active = 1;
  10372. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10373. customer_is_active = sqlite3_column_int(stmt, 0);
  10374. }];
  10375. if (!customer_is_active) {
  10376. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10377. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10378. [iSalesDB close_db:db];
  10379. return [RAConvertor dict2data:ret];
  10380. }
  10381. // new order
  10382. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10383. NSString *new_order_code = [self get_offline_soid:db];
  10384. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10385. user = [self translateSingleQuote:user];
  10386. 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
  10387. __block int result = 1;
  10388. result = [iSalesDB execSql:insert_order_sql db:db];
  10389. if (!result) {
  10390. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10391. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10392. [iSalesDB close_db:db];
  10393. return [RAConvertor dict2data:ret];
  10394. }
  10395. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10396. // __block NSString *product_id = @"";
  10397. __weak typeof(self) weakSelf = self;
  10398. 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];
  10399. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10400. int is_active = sqlite3_column_int(stmt, 1);
  10401. if (is_active) {
  10402. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10403. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10404. 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];
  10405. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10406. }
  10407. }];
  10408. // product_id = [product_id substringFromIndex:1];
  10409. //
  10410. // [self offline_add2cart:@{}.mutableCopy];
  10411. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10412. [iSalesDB close_db:db];
  10413. if (result) {
  10414. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10415. } else {
  10416. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10417. }
  10418. [ret setObject:new_order_code forKey:@"so_id"];
  10419. return [RAConvertor dict2data:ret];
  10420. }
  10421. #pragma mark move wish list to cart
  10422. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10423. assert(params[@"can_create_backorder"]!=nil);
  10424. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10425. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10426. NSString *collectId = params[@"collectId"];
  10427. 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];
  10428. __block NSString *product_id = @"";
  10429. __block NSString *qty = @"";
  10430. __block int number_of_outOfStock = 0;
  10431. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10432. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10433. int productId = sqlite3_column_int(stmt, 0);
  10434. int item_qty = sqlite3_column_int(stmt, 1);
  10435. int availability = sqlite3_column_int(stmt, 2);
  10436. int _id = sqlite3_column_int(stmt, 3);
  10437. if (![params[@"can_create_backorder"] boolValue]) {
  10438. // 库存小于购买量为缺货
  10439. if (availability >= item_qty) {
  10440. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10441. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10442. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10443. } else {
  10444. number_of_outOfStock++;
  10445. }
  10446. } else {
  10447. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10448. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10449. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10450. }
  10451. }];
  10452. NSMutableDictionary *retDic = nil;
  10453. if (![params[@"can_create_backorder"] boolValue]) {
  10454. if (delete_collectId.count == 0) {
  10455. retDic = [NSMutableDictionary dictionary];
  10456. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10457. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10458. return [RAConvertor dict2data:retDic];
  10459. }
  10460. }
  10461. if (product_id.length > 1) {
  10462. product_id = [product_id substringFromIndex:1];
  10463. }
  10464. if (qty.length > 1) {
  10465. qty = [qty substringFromIndex:1];
  10466. }
  10467. NSString *orderCode = params[@"orderCode"];
  10468. if (!orderCode) {
  10469. orderCode = @"";
  10470. }
  10471. NSDictionary *newParams = @{
  10472. @"product_id" : product_id,
  10473. @"orderCode" : orderCode,
  10474. @"qty" : qty,
  10475. @"can_create_backorder":params[@"can_create_backorder"]
  10476. };
  10477. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10478. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10479. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10480. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10481. NSInteger ret = [wish_return[@"result"] intValue];
  10482. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10483. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10484. }
  10485. if (![params[@"can_create_backorder"] boolValue]) {
  10486. if (number_of_outOfStock > 0) {
  10487. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10488. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10489. }
  10490. }
  10491. return [RAConvertor dict2data:retDic];
  10492. }
  10493. #pragma mark - portfolio
  10494. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10495. // assert(params[@"contact_id"]!=nil);
  10496. assert(params[@"user"]!=nil);
  10497. assert(params[@"can_see_price"]!=nil);
  10498. int sort = [[params valueForKey:@"sort"] intValue];
  10499. int offset = [[params valueForKey:@"offset"] intValue];
  10500. int limit = [[params valueForKey:@"limit"] intValue];
  10501. NSString *orderBy = @"";
  10502. switch (sort) {
  10503. case 0:{
  10504. orderBy = @"p.modify_time desc";
  10505. }
  10506. break;
  10507. case 1:{
  10508. orderBy = @"modify_time asc";
  10509. }
  10510. break;
  10511. case 2:{
  10512. orderBy = @"p.name asc";
  10513. }
  10514. break;
  10515. case 3:{
  10516. orderBy = @"p.name desc";
  10517. }
  10518. break;
  10519. case 4:{
  10520. orderBy = @"p.description asc";
  10521. }
  10522. break;
  10523. case 5: {
  10524. orderBy = @"m.default_category asc";
  10525. }
  10526. default:
  10527. break;
  10528. }
  10529. // 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];
  10530. 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];
  10531. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10532. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10533. sqlite3 *db = [iSalesDB get_db];
  10534. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10535. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10536. int product_id = sqlite3_column_int(stmt, 0);
  10537. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10538. NSString *name = [self textAtColumn:1 statement:stmt];
  10539. NSString *description = [self textAtColumn:2 statement:stmt];
  10540. double price = sqlite3_column_double(stmt, 3);
  10541. double discount = sqlite3_column_double(stmt,4);
  10542. int qty = sqlite3_column_int(stmt, 5);
  10543. int percentage = sqlite3_column_int(stmt, 6);
  10544. int item_id = sqlite3_column_int(stmt, 7);
  10545. // int fashion_id = sqlite3_column_int(stmt, 8);
  10546. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10547. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10548. double percent = sqlite3_column_double(stmt, 11);
  10549. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10550. if ([price_null isEqualToString:@"null"]) {
  10551. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10552. }
  10553. NSMutableDictionary *item = @{
  10554. @"linenotes": line_note,
  10555. @"check": @(1),
  10556. @"product_id": product_id_string,
  10557. @"available_qty": @(qty),
  10558. @"available_percent" : @(percent),
  10559. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10560. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10561. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10562. @"img": img_path,
  10563. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10564. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10565. }.mutableCopy;
  10566. if (percentage) {
  10567. [item removeObjectForKey:@"available_qty"];
  10568. } else {
  10569. [item removeObjectForKey:@"available_percent"];
  10570. }
  10571. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10572. if ([qty_null isEqualToString:@"null"]) {
  10573. [item removeObjectForKey:@"available_qty"];
  10574. }
  10575. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10576. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10577. }];
  10578. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10579. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10580. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10581. }
  10582. [iSalesDB close_db:db];
  10583. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10584. [dic setValue:@"" forKey:@"email_content"];
  10585. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10586. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10587. return [RAConvertor dict2data:dic];
  10588. }
  10589. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10590. __block int qty = 0;
  10591. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10592. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10593. qty = sqlite3_column_int(stmt, 0);
  10594. }];
  10595. return qty;
  10596. }
  10597. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10598. {
  10599. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10600. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10601. // NSMutableDictionary * values = params[@"replaceValue"];
  10602. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10603. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10604. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10605. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10606. NSString *pdf_path = @"";
  10607. if (direct) {
  10608. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10609. } else {
  10610. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10611. }
  10612. NSString *create_user = [self valueInParams:params key:@"user"];
  10613. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10614. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10615. // model info
  10616. // 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];
  10617. // V1.90 more color
  10618. 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];
  10619. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10620. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10621. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10622. sqlite3 *db = [iSalesDB get_db];
  10623. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10624. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10625. int product_id = sqlite3_column_int(stmt, 0);
  10626. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10627. double price = sqlite3_column_double(stmt, 1);
  10628. double discount = sqlite3_column_double(stmt,2);
  10629. int qty = sqlite3_column_int(stmt, 3);
  10630. int percentage = sqlite3_column_int(stmt, 4);
  10631. int item_id = sqlite3_column_int(stmt, 5);
  10632. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10633. double percent = sqlite3_column_double(stmt, 7);
  10634. int more_color = sqlite3_column_int(stmt, 8);
  10635. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10636. /* if ([price_null isEqualToString:@"null"]) {
  10637. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10638. }
  10639. */
  10640. [product_ids_string appendFormat:@"%@,",product_id_string];
  10641. // Regular Price
  10642. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10643. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10644. // QTY
  10645. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10646. if ([qty_null isEqualToString:@"null"]) {
  10647. // 查available
  10648. qty = [self model_QTY:product_id_string db:db];
  10649. }
  10650. if (percentage) {
  10651. qty = qty * percent / 100;
  10652. }
  10653. // Special Price
  10654. if ([price_null isEqualToString:@"null"]) {
  10655. // price = regular price
  10656. price = [regular_price_str doubleValue];
  10657. }
  10658. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10659. if (![discount_null isEqualToString:@"null"]) {
  10660. price = price * (1 - discount / 100.0);
  10661. }
  10662. NSMutableDictionary *item = @{
  10663. @"line_note": line_note,
  10664. @"product_id": product_id_string,
  10665. @"available_qty": @(qty),
  10666. @"more_color":@(more_color),
  10667. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10668. @"regular_price" : regular_price_str,
  10669. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10670. }.mutableCopy;
  10671. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10672. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10673. }];
  10674. [iSalesDB close_db:db];
  10675. if (product_ids_string.length > 0) {
  10676. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10677. }
  10678. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10679. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10680. return [RAConvertor dict2data:resultDictionary];
  10681. }
  10682. NSString *model_info = [RAConvertor dict2string:dic];
  10683. // 创建PDF
  10684. // 在preview情况下保存,则不需要新建了
  10685. if (direct) {
  10686. NSMutableDictionary *tear_sheet_params = params;
  10687. // if (tear_sheet_id) {
  10688. // tear_sheet_params = values;
  10689. // }
  10690. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10691. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10692. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10693. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10694. resultDictionary = pdfInfo.mutableCopy;
  10695. } else { // 创建PDF失败
  10696. return pdfData;
  10697. }
  10698. } else {
  10699. // pdf_path 就是本地路径
  10700. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10701. }
  10702. // 将文件移动到PDF Cache文件夹
  10703. NSString *newPath = [pdf_path lastPathComponent];
  10704. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10705. NSString *cachefolder = [paths objectAtIndex:0];
  10706. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10707. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10708. NSFileManager *fileManager = [NSFileManager defaultManager];
  10709. NSError *error = nil;
  10710. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10711. if (error) { // 移动文件失败
  10712. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10713. return [RAConvertor dict2data:resultDictionary];
  10714. }
  10715. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10716. pdf_path = [newPath lastPathComponent];
  10717. // 保存信息
  10718. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10719. NSString *off_params = [RAConvertor dict2string:params];
  10720. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10721. pdf_path = [self translateSingleQuote:pdf_path];
  10722. create_user = [self translateSingleQuote:create_user];
  10723. tear_note = [self translateSingleQuote:tear_note];
  10724. tear_name = [self translateSingleQuote:tear_name];
  10725. model_info = [self translateSingleQuote:model_info];
  10726. configureParams = [self translateSingleQuote:configureParams];
  10727. off_params = [self translateSingleQuote:off_params];
  10728. 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];
  10729. if (tear_sheet_id) {
  10730. int _id = [tear_sheet_id intValue];
  10731. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10732. }
  10733. int result = [iSalesDB execSql:save_pdf_sql];
  10734. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10735. //
  10736. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10737. if (remove_Item) {
  10738. // portfolioId
  10739. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10740. }
  10741. return [RAConvertor dict2data:resultDictionary];
  10742. }
  10743. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10744. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10745. }
  10746. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10747. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10748. }
  10749. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10750. int offset = [[params valueForKey:@"offset"] intValue];
  10751. int limit = [[params valueForKey:@"limit"] intValue];
  10752. NSString *keyword = [params valueForKey:@"keyWord"];
  10753. NSString *where = @"where is_delete is null or is_delete = 0";
  10754. if (keyword.length) {
  10755. keyword = [self translateSingleQuote:keyword];
  10756. 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];
  10757. }
  10758. 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
  10759. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10760. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10761. NSString *cachefolder = [paths objectAtIndex:0];
  10762. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10763. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10764. NSString *name = [self textAtColumn:0 statement:stmt];
  10765. NSString *note = [self textAtColumn:1 statement:stmt];
  10766. NSString *time = [self textAtColumn:2 statement:stmt];
  10767. NSString *user = [self textAtColumn:3 statement:stmt];
  10768. NSString *path = [self textAtColumn:4 statement:stmt];
  10769. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10770. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10771. path = [pdfFolder stringByAppendingPathComponent:path];
  10772. BOOL bdir=NO;
  10773. NSFileManager* fileManager = [NSFileManager defaultManager];
  10774. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10775. {
  10776. //pdf文件不存在
  10777. path=nil;
  10778. }
  10779. time = [self changeDateTimeFormate:time];
  10780. time = [time stringByAppendingString:@" PST"];
  10781. int sheet_id = sqlite3_column_int(stmt, 5);
  10782. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10783. item[@"tearsheetsId"]=@(sheet_id);
  10784. item[@"pdf_path"]=path;
  10785. item[@"create_time"]=time;
  10786. item[@"create_user"]=user;
  10787. item[@"tear_note"]=note;
  10788. item[@"tear_name"]=name;
  10789. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10790. item[@"model_info"]=model_info;
  10791. item[@"off_params"]=off_params;
  10792. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10793. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10794. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10795. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10796. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10797. }];
  10798. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10799. return [RAConvertor dict2data:dic];
  10800. }
  10801. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10802. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10803. // NSString *user = [params objectForKey:@"user"];
  10804. // 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];
  10805. 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];
  10806. int result = [iSalesDB execSql:sql];
  10807. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10808. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10809. if (result == RESULT_TRUE) {
  10810. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10811. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10812. sqlite3 *db = [iSalesDB get_db];
  10813. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10814. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10815. [iSalesDB close_db:db];
  10816. }
  10817. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10818. return [RAConvertor dict2data:dic];
  10819. }
  10820. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10821. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10822. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10823. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10824. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10825. NSDictionary *company_item = @{
  10826. @"control": @"text",
  10827. @"keyboard": @"text",
  10828. @"name": @"company_name",
  10829. @"value": COMPANY_FULL_NAME,
  10830. @"aname": @"Company Name"
  10831. };
  10832. [section_0 setObject:company_item forKey:@"item_0"];
  10833. [dic setObject:section_0 forKey:@"section_0"];
  10834. // Regurlar Price
  10835. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10836. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10837. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10838. __block long val_count = 0;
  10839. NSString *sql = @"select name,type,order_by from price order by order_by";
  10840. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10841. NSString *name = [self textAtColumn:0 statement:stmt];
  10842. int type = sqlite3_column_int(stmt, 1);
  10843. int order_by = sqlite3_column_int(stmt, 2);
  10844. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10845. NSDictionary *price_dic = @{
  10846. @"value" : name,
  10847. @"value_id" : [NSNumber numberWithInteger:type],
  10848. @"check" : order_by == 0 ? @(1) : @(0)
  10849. };
  10850. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10851. val_count = ++(*count);
  10852. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10853. val_count = 0;
  10854. }];
  10855. [cadedate setObject:@{@"value" : @"None",
  10856. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10857. val_count++;
  10858. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10859. [price setObject:cadedate forKey:@"cadedate"];
  10860. [section1 setObject:price forKey:@"item_2"];
  10861. [dic setObject:section1 forKey:@"section_1"];
  10862. return [RAConvertor dict2data:dic];
  10863. }
  10864. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10865. NSString *product_id = [params objectForKey:@"fashionId"];
  10866. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10867. __block int result = RESULT_TRUE;
  10868. __block int qty = 0;
  10869. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10870. qty = sqlite3_column_int(stmt, 0);
  10871. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10872. result = RESULT_FALSE;
  10873. }];
  10874. NSMutableDictionary *dic = @{
  10875. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10876. @"mode" : @"Regular Mode",
  10877. @"quantity_available" : @(qty),
  10878. @"result" : @(result),
  10879. }.mutableCopy;
  10880. return [RAConvertor dict2data:dic];
  10881. }
  10882. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10883. NSString *item_ids = [params objectForKey:@"item_id"];
  10884. NSString *line_notes = [params objectForKey:@"notes"];
  10885. NSString *price_str = [params objectForKey:@"price"];
  10886. NSString *discount_str = [params objectForKey:@"discount"];
  10887. NSString *percent = [params objectForKey:@"available_percent"];
  10888. NSString *qty = [params objectForKey:@"available_qty"];
  10889. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10890. int dot = 0;
  10891. if (line_notes) {
  10892. line_notes = [self translateSingleQuote:line_notes];
  10893. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10894. sql = [sql stringByAppendingString:line_notes];
  10895. dot = 1;
  10896. } else {
  10897. line_notes = @"";
  10898. }
  10899. if (price_str) {
  10900. if (dot) {
  10901. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10902. } else {
  10903. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10904. dot = 1;
  10905. }
  10906. sql = [sql stringByAppendingString:price_str];
  10907. } else {
  10908. price_str = @"";
  10909. }
  10910. if (discount_str) {
  10911. if (dot) {
  10912. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10913. } else {
  10914. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10915. dot = 1;
  10916. }
  10917. sql = [sql stringByAppendingString:discount_str];
  10918. } else {
  10919. discount_str = @"";
  10920. }
  10921. if (percent) {
  10922. if (dot) {
  10923. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10924. } else {
  10925. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10926. dot = 1;
  10927. }
  10928. sql = [sql stringByAppendingString:percent];
  10929. } else {
  10930. percent = @"";
  10931. }
  10932. if (qty) {
  10933. if (dot) {
  10934. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10935. } else {
  10936. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10937. dot = 1;
  10938. }
  10939. sql = [sql stringByAppendingString:qty];
  10940. } else {
  10941. qty = @"";
  10942. }
  10943. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10944. sql = [sql stringByAppendingString:where];
  10945. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10946. int result = [iSalesDB execSql:sql];
  10947. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10948. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10949. return [RAConvertor dict2data:dic];
  10950. }
  10951. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10952. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10953. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10954. sqlite3 *db = [iSalesDB get_db];
  10955. 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];
  10956. int result = [iSalesDB execSql:sql db:db];
  10957. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10958. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10959. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10960. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10961. [iSalesDB close_db:db];
  10962. return [RAConvertor dict2data:dic];
  10963. }
  10964. + (void)offline_removePDFWithName:(NSString *)name {
  10965. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10966. NSString *cachefolder = [paths objectAtIndex:0];
  10967. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10968. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10969. NSFileManager *fileManager = [NSFileManager defaultManager];
  10970. [fileManager removeItemAtPath:path error:nil];
  10971. }
  10972. + (void)offline_clear_PDFCache {
  10973. NSFileManager *fileManager = [NSFileManager defaultManager];
  10974. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10975. NSString *cachefolder = [paths objectAtIndex:0];
  10976. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10977. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10978. for (NSString *path in pdf_files) {
  10979. [self offline_removePDFWithName:[path lastPathComponent]];
  10980. }
  10981. }
  10982. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10983. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10984. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10985. NSString *user = [params objectForKey:@"user"];
  10986. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10987. if (![create_user isEqualToString:user]) {
  10988. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10989. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10990. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10991. return [RAConvertor dict2data:dic];
  10992. }
  10993. 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]];
  10994. __block int is_local = 0;
  10995. __block NSString *path = @"";
  10996. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10997. is_local = sqlite3_column_int(stmt, 0);
  10998. path = [self textAtColumn:1 statement:stmt];
  10999. }];
  11000. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  11001. if (is_local == 1) {
  11002. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  11003. // 删除文件
  11004. [self offline_removePDFWithName:path];
  11005. }
  11006. int result = [iSalesDB execSql:sql];
  11007. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  11008. [dic setObject:@"Regular Mode" forKey:@"mode"];
  11009. return [RAConvertor dict2data:dic];
  11010. }
  11011. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  11012. {
  11013. // assert(add_params[@"contact_id"]!=nil);
  11014. assert(add_params[@"user"]!=nil);
  11015. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11016. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11017. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11018. NSString* where=@"1=1";
  11019. if (ver!=nil) {
  11020. where=@"is_dirty=1";
  11021. }
  11022. 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];
  11023. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11024. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11025. sqlite3 *db = [iSalesDB get_db];
  11026. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11027. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11028. NSInteger _id = sqlite3_column_int(stmt, 0);
  11029. NSInteger product_id = sqlite3_column_int(stmt, 1);
  11030. NSString *name = [self textAtColumn:2 statement:stmt];
  11031. NSString *desc = [self textAtColumn:3 statement:stmt];
  11032. NSInteger item_id = sqlite3_column_int(stmt, 4);
  11033. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  11034. NSInteger qty = sqlite3_column_int(stmt, 6);
  11035. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  11036. double percent = sqlite3_column_double(stmt, 8);
  11037. double price = sqlite3_column_double(stmt, 9);
  11038. double discount = sqlite3_column_double(stmt, 10);
  11039. NSString *img = [self textAtColumn:11 statement:stmt];
  11040. NSString *line_note = [self textAtColumn:12 statement:stmt];
  11041. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  11042. NSString *create_time = [self textAtColumn:14 statement:stmt];
  11043. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  11044. NSString *price_null = [self textAtColumn:9 statement:stmt];
  11045. if ([price_null isEqualToString:@"null"]) {
  11046. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  11047. }
  11048. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  11049. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  11050. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  11051. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11052. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  11053. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  11054. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  11055. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  11056. if ([qty_null isEqualToString:@"null"]) {
  11057. // [item setValue:@"null" forKey:@"available_qty"];
  11058. } else {
  11059. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  11060. }
  11061. if ([is_percent_null isEqualToString:@"null"]) {
  11062. // [item setValue:@"null" forKey:@"percentage"];
  11063. } else {
  11064. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  11065. }
  11066. if ([percent_null isEqualToString:@"null"]) {
  11067. // [item setValue:@"null" forKey:@"percent"];
  11068. } else {
  11069. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  11070. }
  11071. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  11072. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  11073. [item setValue:name forKey:@"name"];
  11074. [item setValue:desc forKey:@"description"];
  11075. [item setValue:img forKey:@"img"];
  11076. [item setValue:line_note forKey:@"line_note"];
  11077. [item setValue:create_time forKey:@"createtime"];
  11078. [item setValue:modify_time forKey:@"modifytime"];
  11079. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11080. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11081. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11082. }];
  11083. [iSalesDB close_db:db];
  11084. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  11085. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11086. }
  11087. return ret;
  11088. }
  11089. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  11090. {
  11091. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11092. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11093. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11094. NSString* where=@"1=1";
  11095. if (ver!=nil) {
  11096. where=@"is_dirty=1";
  11097. }
  11098. 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];
  11099. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11100. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11101. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11102. NSInteger _id = sqlite3_column_int(stmt, 0);
  11103. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  11104. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  11105. NSString *create_user = [self textAtColumn:3 statement:stmt];
  11106. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  11107. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  11108. NSString *model_info = [self textAtColumn:6 statement:stmt];
  11109. NSString *createtime = [self textAtColumn:7 statement:stmt];
  11110. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  11111. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  11112. NSString *off_params = [self textAtColumn:10 statement:stmt];
  11113. NSString *uuid = [NSUUID UUID].UUIDString;
  11114. int is_delete = sqlite3_column_int(stmt, 11);
  11115. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  11116. int is_local = sqlite3_column_int(stmt, 12);
  11117. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11118. if (![tearsheet_id_null isEqualToString:@"null"]) {
  11119. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  11120. }
  11121. [item setObject:pdf_path forKey:@"pdf_path"];
  11122. [item setObject:create_user forKey:@"create_user"];
  11123. [item setObject:tear_note forKey:@"tear_note"];
  11124. [item setObject:tear_name forKey:@"tear_name"];
  11125. [item setObject:model_info forKey:@"model_info"];
  11126. [item setObject:createtime forKey:@"createtime"];
  11127. [item setObject:modifytime forKey:@"modifytime"];
  11128. [item setObject:urlParams forKey:@"urlParams"];
  11129. [item setObject:off_params forKey:@"off_params"];
  11130. [item setObject:uuid forKey:@"pdf_token"];
  11131. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  11132. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  11133. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  11134. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  11135. } else {
  11136. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11137. }
  11138. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11139. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11140. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  11141. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11142. }];
  11143. return ret;
  11144. }
  11145. #pragma mark 2020
  11146. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11147. {
  11148. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11149. sqlite3 *db = [iSalesDB get_db];
  11150. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  11151. if (contactType) {
  11152. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  11153. } else {
  11154. contactType = @"1 = 1";
  11155. }
  11156. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  11157. DebugLog(@"offline contact list keyword: %@",keyword);
  11158. // advanced search
  11159. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  11160. if (contact_name) {
  11161. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11162. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  11163. } else {
  11164. contact_name = @"";
  11165. }
  11166. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  11167. if (customer_phone) {
  11168. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11169. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  11170. } else {
  11171. customer_phone = @"";
  11172. }
  11173. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  11174. if (customer_fax) {
  11175. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11176. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  11177. } else {
  11178. customer_fax = @"";
  11179. }
  11180. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  11181. if (customer_zipcode) {
  11182. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11183. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  11184. } else {
  11185. customer_zipcode = @"";
  11186. }
  11187. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  11188. if (customer_sales_rep) {
  11189. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11190. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  11191. } else {
  11192. customer_sales_rep = @"";
  11193. }
  11194. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  11195. if (customer_state) {
  11196. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11197. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  11198. } else {
  11199. customer_state = @"";
  11200. }
  11201. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  11202. if (customer_name) {
  11203. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11204. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  11205. } else {
  11206. customer_name = @"";
  11207. }
  11208. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  11209. if (customer_country) {
  11210. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11211. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  11212. } else {
  11213. customer_country = @"";
  11214. }
  11215. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11216. if (customer_cid) {
  11217. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11218. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11219. } else {
  11220. customer_cid = @"";
  11221. }
  11222. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11223. if (customer_city) {
  11224. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11225. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11226. } else {
  11227. customer_city = @"";
  11228. }
  11229. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11230. if (customer_address) {
  11231. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11232. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11233. } else {
  11234. customer_address = @"";
  11235. }
  11236. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11237. if (customer_email) {
  11238. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11239. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11240. } else {
  11241. customer_email = @"";
  11242. }
  11243. NSString *price_name = [params valueForKey:@"price_name"];
  11244. if (price_name) {
  11245. if ([price_name containsString:@","]) {
  11246. // 首先从 price表中查处name
  11247. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11248. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11249. for (int i = 1;i < pArray.count;i++) {
  11250. NSString *p = pArray[i];
  11251. [mutablePStr appendFormat:@" or type = %@ ",p];
  11252. }
  11253. [mutablePStr appendString:@";"];
  11254. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11255. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11256. char *name = (char *)sqlite3_column_text(stmt, 0);
  11257. if (!name)
  11258. name = "";
  11259. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11260. }];
  11261. // 再根据name 拼sql
  11262. NSMutableString *mutable_price_name = [NSMutableString string];
  11263. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11264. for (int i = 1; i < price_name_array.count; i++) {
  11265. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11266. }
  11267. [mutable_price_name appendString:@")"];
  11268. price_name = mutable_price_name;
  11269. } else {
  11270. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11271. if ([price_name isEqualToString:@""]) {
  11272. price_name = @"";
  11273. } else {
  11274. __block NSString *price;
  11275. price_name = [self translateSingleQuote:price_name];
  11276. [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) {
  11277. char *p = (char *)sqlite3_column_text(stmt, 0);
  11278. if (p == NULL) {
  11279. p = "";
  11280. }
  11281. price = [NSString stringWithUTF8String:p];
  11282. }];
  11283. if ([price isEqualToString:@""]) {
  11284. price_name = @"";
  11285. } else {
  11286. price = [self translateSingleQuote:price];
  11287. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11288. }
  11289. }
  11290. }
  11291. } else {
  11292. price_name = @"";
  11293. }
  11294. int limit = [[params valueForKey:@"limit"] intValue];
  11295. int offset = [[params valueForKey:@"offset"] intValue];
  11296. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11297. 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];
  11298. 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];
  11299. // int result= [iSalesDB AddExFunction:db];
  11300. int count =0;
  11301. NSString *sqlQuery = nil;
  11302. if(keyword.length==0)
  11303. {
  11304. // 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];
  11305. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11306. sqlQuery = sql;
  11307. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11308. }
  11309. else
  11310. {
  11311. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11312. keyword = keyword.lowercaseString;
  11313. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11314. 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];
  11315. 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]];
  11316. }
  11317. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11318. sqlite3_stmt * statement;
  11319. [ret setValue:@"2" forKey:@"result"];
  11320. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11321. // 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";
  11322. int i = 0;
  11323. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11324. {
  11325. while (sqlite3_step(statement) == SQLITE_ROW)
  11326. {
  11327. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11328. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11329. int editable = sqlite3_column_int(statement, 0);
  11330. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11331. NSString *nscompany_name =nil;
  11332. if(company_name==nil)
  11333. nscompany_name=@"";
  11334. else
  11335. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11336. char *country = (char*)sqlite3_column_text(statement, 2);
  11337. if(country==nil)
  11338. country="";
  11339. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11340. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11341. // if(addr==nil)
  11342. // addr="";
  11343. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11344. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11345. if(zipcode==nil)
  11346. zipcode="";
  11347. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11348. char *state = (char*)sqlite3_column_text(statement, 5);
  11349. if(state==nil)
  11350. state="";
  11351. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11352. char *city = (char*)sqlite3_column_text(statement, 6);
  11353. if(city==nil)
  11354. city="";
  11355. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11356. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11357. // NSString *nscontact_name = nil;
  11358. // if(contact_name==nil)
  11359. // nscontact_name=@"";
  11360. // else
  11361. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11362. char *phone = (char*)sqlite3_column_text(statement, 8);
  11363. NSString *nsphone = nil;
  11364. if(phone==nil)
  11365. nsphone=@"";
  11366. else
  11367. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11368. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11369. if(contact_id==nil)
  11370. contact_id="";
  11371. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11372. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11373. if(addr_1==nil)
  11374. addr_1="";
  11375. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11376. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11377. if(addr_2==nil)
  11378. addr_2="";
  11379. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11380. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11381. if(addr_3==nil)
  11382. addr_3="";
  11383. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11384. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11385. if(addr_4==nil)
  11386. addr_4="";
  11387. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11388. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11389. if(first_name==nil)
  11390. first_name="";
  11391. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11392. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11393. if(last_name==nil)
  11394. last_name="";
  11395. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11396. char *fax = (char*)sqlite3_column_text(statement, 16);
  11397. NSString *nsfax = nil;
  11398. if(fax==nil)
  11399. nsfax=@"";
  11400. else
  11401. {
  11402. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11403. if(nsfax.length>0)
  11404. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11405. }
  11406. char *email = (char*)sqlite3_column_text(statement, 17);
  11407. NSString *nsemail = nil;
  11408. if(email==nil)
  11409. nsemail=@"";
  11410. else
  11411. {
  11412. nsemail= [[NSString alloc]initWithUTF8String:email];
  11413. if(nsemail.length>0)
  11414. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11415. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11416. }
  11417. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11418. [arr_name addObject:nsfirst_name];
  11419. [arr_name addObject:nslast_name];
  11420. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11421. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11422. {
  11423. // decrypt
  11424. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11425. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11426. nsphone=[AESCrypt fastdecrypt:nsphone];
  11427. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11428. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11429. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11430. }
  11431. [arr_addr addObject:nscompany_name];
  11432. [arr_addr addObject:nscontact_name];
  11433. [arr_addr addObject:@"<br>"];
  11434. [arr_addr addObject:nsaddr_1];
  11435. [arr_addr addObject:nsaddr_2];
  11436. [arr_addr addObject:nsaddr_3];
  11437. [arr_addr addObject:nsaddr_4];
  11438. //[arr_addr addObject:nsaddr];
  11439. [arr_addr addObject:nszipcode];
  11440. [arr_addr addObject:nscity];
  11441. [arr_addr addObject:nsstate];
  11442. [arr_addr addObject:nscountry];
  11443. [arr_addr addObject:@"<br>"];
  11444. [arr_addr addObject:nsphone];
  11445. [arr_addr addObject:nsfax];
  11446. [arr_addr addObject:nsemail];
  11447. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11448. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11449. [item setValue:name forKey:@"name"];
  11450. [item setValue:nscontact_id forKey:@"contact_id"];
  11451. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11452. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11453. i++;
  11454. }
  11455. sqlite3_finalize(statement);
  11456. }
  11457. [iSalesDB close_db:db];
  11458. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11459. dispatch_async(dispatch_get_main_queue(), ^{
  11460. UIApplication * app = [UIApplication sharedApplication];
  11461. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11462. [ret setValue:appDelegate.mode forKey:@"mode"];
  11463. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11464. result(ret);
  11465. });
  11466. return ;
  11467. });
  11468. }
  11469. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11470. //{
  11471. //
  11472. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11473. //
  11474. // NSString* contactId = [params valueForKey:@"contactId"];
  11475. //
  11476. //
  11477. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11478. //
  11479. //
  11480. //
  11481. //
  11482. //
  11483. //
  11484. // sqlite3 *db = [iSalesDB get_db];
  11485. //
  11486. //
  11487. //
  11488. //
  11489. //
  11490. // NSString *sqlQuery = nil;
  11491. //
  11492. //
  11493. // {
  11494. // 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];
  11495. //
  11496. // }
  11497. //
  11498. //
  11499. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11500. // sqlite3_stmt * statement;
  11501. //
  11502. //
  11503. // [ret setValue:@"2" forKey:@"result"];
  11504. //
  11505. //
  11506. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11507. // {
  11508. //
  11509. // //int i = 0;
  11510. // if (sqlite3_step(statement) == SQLITE_ROW)
  11511. // {
  11512. //
  11513. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11514. //
  11515. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11516. //
  11517. //
  11518. // // int editable = sqlite3_column_int(statement, 0);
  11519. //
  11520. //
  11521. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11522. // NSString *nscompany_name =nil;
  11523. // if(company_name==nil)
  11524. // nscompany_name=@"";
  11525. // else
  11526. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11527. //
  11528. //
  11529. // char *country = (char*)sqlite3_column_text(statement, 2);
  11530. // if(country==nil)
  11531. // country="";
  11532. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11533. //
  11534. //
  11535. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11536. // // if(addr==nil)
  11537. // // addr="";
  11538. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11539. //
  11540. //
  11541. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11542. // if(zipcode==nil)
  11543. // zipcode="";
  11544. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11545. //
  11546. //
  11547. // char *state = (char*)sqlite3_column_text(statement, 5);
  11548. // if(state==nil)
  11549. // state="";
  11550. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11551. //
  11552. // char *city = (char*)sqlite3_column_text(statement, 6);
  11553. // if(city==nil)
  11554. // city="";
  11555. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11556. //
  11557. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11558. // // NSString *nscontact_name = nil;
  11559. // // if(contact_name==nil)
  11560. // // nscontact_name=@"";
  11561. // // else
  11562. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11563. //
  11564. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11565. // NSString *nsphone = nil;
  11566. // if(phone==nil)
  11567. // nsphone=@"";
  11568. // else
  11569. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11570. //
  11571. //
  11572. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11573. // // if(contact_id==nil)
  11574. // // contact_id="";
  11575. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11576. //
  11577. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11578. // if(addr_1==nil)
  11579. // addr_1="";
  11580. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11581. //
  11582. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11583. // if(addr_2==nil)
  11584. // addr_2="";
  11585. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11586. //
  11587. //
  11588. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11589. // if(addr_3==nil)
  11590. // addr_3="";
  11591. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11592. //
  11593. //
  11594. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11595. // if(addr_4==nil)
  11596. // addr_4="";
  11597. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11598. //
  11599. //
  11600. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11601. // if(first_name==nil)
  11602. // first_name="";
  11603. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11604. //
  11605. //
  11606. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11607. // if(last_name==nil)
  11608. // last_name="";
  11609. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11610. //
  11611. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11612. // NSString *nsfax = nil;
  11613. // if(fax==nil)
  11614. // nsfax=@"";
  11615. // else
  11616. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11617. //
  11618. // char *email = (char*)sqlite3_column_text(statement, 17);
  11619. // NSString *nsemail = nil;
  11620. // if(email==nil)
  11621. // nsemail=@"";
  11622. // else
  11623. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11624. //
  11625. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11626. // NSString *nsimg_0 = nil;
  11627. // if(img_0==nil)
  11628. // nsimg_0=@"";
  11629. // else
  11630. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11631. //
  11632. // [self copy_bcardImg:nsimg_0];
  11633. //
  11634. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11635. // NSString *nsimg_1 = nil;
  11636. // if(img_1==nil)
  11637. // nsimg_1=@"";
  11638. // else
  11639. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11640. // [self copy_bcardImg:nsimg_1];
  11641. //
  11642. //
  11643. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11644. // NSString *nsimg_2 = nil;
  11645. // if(img_2==nil)
  11646. // nsimg_2=@"";
  11647. // else
  11648. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11649. // [self copy_bcardImg:nsimg_2];
  11650. //
  11651. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11652. // NSString *nsprice_type = nil;
  11653. // if(price_type==nil)
  11654. // nsprice_type=@"";
  11655. // else
  11656. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11657. //
  11658. //
  11659. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11660. // NSString *nsnotes = nil;
  11661. // if(notes==nil)
  11662. // nsnotes=@"";
  11663. // else
  11664. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11665. //
  11666. //
  11667. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11668. // NSString *nssalesrep = nil;
  11669. // if(salesrep==nil)
  11670. // nssalesrep=@"";
  11671. // else
  11672. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11673. //
  11674. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11675. //
  11676. //
  11677. // {
  11678. // // decrypt
  11679. //
  11680. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11681. //
  11682. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11683. //
  11684. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11685. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11686. //
  11687. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11688. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11689. //
  11690. // }
  11691. //
  11692. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11693. //
  11694. // [arr_name addObject:nsfirst_name];
  11695. // [arr_name addObject:nslast_name];
  11696. //
  11697. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11698. //
  11699. //
  11700. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11701. //
  11702. // [arr_ext addObject:nsaddr_1];
  11703. // [arr_ext addObject:nsaddr_2];
  11704. // [arr_ext addObject:nsaddr_3];
  11705. // [arr_ext addObject:nsaddr_4];
  11706. // [arr_ext addObject:@"\r\n"];
  11707. //
  11708. // [arr_ext addObject:nscity];
  11709. // [arr_ext addObject:nsstate];
  11710. // [arr_ext addObject:nszipcode];
  11711. // [arr_ext addObject:nscountry];
  11712. //
  11713. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11714. //
  11715. //
  11716. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11717. //
  11718. //
  11719. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11720. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11721. // [item setValue:nscountry forKey:@"customer_country"];
  11722. // [item setValue:nsphone forKey:@"customer_phone"];
  11723. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11724. // [item setValue:nscompany_name forKey:@"customer_name"];
  11725. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11726. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11727. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11728. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11729. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11730. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11731. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11732. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11733. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11734. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11735. // [item setValue:nscity forKey:@"customer_city"];
  11736. // [item setValue:nsstate forKey:@"customer_state"];
  11737. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11738. // [item setValue:contactId forKey:@"customer_cid"];
  11739. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11740. // [item setValue:nsfax forKey:@"customer_fax"];
  11741. // [item setValue:nsemail forKey:@"customer_email"];
  11742. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11743. //
  11744. //
  11745. // [ret setObject:item forKey:@"customerInfo"];
  11746. // // i++;
  11747. //
  11748. //
  11749. //
  11750. // }
  11751. //
  11752. //
  11753. //
  11754. //
  11755. // sqlite3_finalize(statement);
  11756. // }
  11757. //
  11758. //
  11759. //
  11760. // [iSalesDB close_db:db];
  11761. //
  11762. //
  11763. //
  11764. //
  11765. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11766. // dispatch_async(dispatch_get_main_queue(), ^{
  11767. // UIApplication * app = [UIApplication sharedApplication];
  11768. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11769. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11770. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11771. // result(ret);
  11772. // });
  11773. //
  11774. // return ;
  11775. // });
  11776. //}
  11777. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11778. {
  11779. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11780. NSString* order_code= appDelegate.order_code;
  11781. NSString* user = appDelegate.user;
  11782. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11783. NSString* category = [params valueForKey:@"category"];
  11784. if (!category || [category isEqualToString:@""]) {
  11785. category = @"%";
  11786. }
  11787. category = [self translateSingleQuote:category];
  11788. int limit = [[params valueForKey:@"limit"] intValue];
  11789. int offset = [[params valueForKey:@"offset"] intValue];
  11790. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11791. NSString *limit_str = @"";
  11792. if (limited) {
  11793. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11794. }
  11795. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11796. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11797. sqlite3 *db = [iSalesDB get_db];
  11798. // [iSalesDB AddExFunction:db];
  11799. int count;
  11800. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11801. 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];
  11802. double price_min = 0;
  11803. double price_max = 0;
  11804. if ([params.allKeys containsObject:@"alert"]) {
  11805. // alert
  11806. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11807. NSString *alert = params[@"alert"];
  11808. if ([alert isEqualToString:@"Display All"]) {
  11809. alert = [NSString stringWithFormat:@""];
  11810. } else {
  11811. alert = [self translateSingleQuote:alert];
  11812. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11813. }
  11814. // available
  11815. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11816. NSString *available = params[@"available"];
  11817. NSString *available_condition;
  11818. if ([available isEqualToString:@"Display All"]) {
  11819. available_condition = @"";
  11820. } else if ([available isEqualToString:@"Available Now"]) {
  11821. available_condition = @"and availability > 0";
  11822. } else {
  11823. available_condition = @"and availability == 0";
  11824. }
  11825. // best seller
  11826. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11827. NSString *best_seller = @"";
  11828. NSString *order_best_seller = @"m.name asc";
  11829. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11830. best_seller = @"and best_seller > 0";
  11831. order_best_seller = @"m.best_seller desc,m.name asc";
  11832. }
  11833. // price
  11834. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11835. NSString *price = params[@"price"];
  11836. price_min = 0;
  11837. price_max = MAXFLOAT;
  11838. if (user && price != nil) {
  11839. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11840. NSMutableString *priceName = [NSMutableString string];
  11841. for (int i = 0; i < priceTypeArray.count; i++) {
  11842. NSString *pricetype = priceTypeArray[i];
  11843. pricetype = [self translateSingleQuote:pricetype];
  11844. if (i == 0) {
  11845. [priceName appendFormat:@"'%@'",pricetype];
  11846. } else {
  11847. [priceName appendFormat:@",'%@'",pricetype];
  11848. }
  11849. }
  11850. if ([price isEqualToString:@"Display All"]) {
  11851. price = [NSString stringWithFormat:@""];
  11852. } else if([price containsString:@"+"]){
  11853. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11854. price_min = [price doubleValue];
  11855. 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];
  11856. } else {
  11857. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11858. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11859. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11860. 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];
  11861. }
  11862. } else {
  11863. price = @"";
  11864. }
  11865. // sold_by_qty : Sold in quantities of %@
  11866. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11867. NSString *qty = params[@"sold_by_qty"];
  11868. if ([qty isEqualToString:@"Display All"]) {
  11869. qty = @"";
  11870. } else {
  11871. qty = [self translateSingleQuote:qty];
  11872. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11873. }
  11874. // cate
  11875. // category = [self translateSingleQuote:category];
  11876. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11877. // cate mutiple selection
  11878. NSString *category_id = params[@"category"];
  11879. NSMutableArray *cate_id_array = nil;
  11880. NSMutableString *cateWhere = [NSMutableString string];
  11881. if ([category_id isEqualToString:@""] || !category_id) {
  11882. [cateWhere appendString:@"1 = 1"];
  11883. } else {
  11884. if ([category_id containsString:@","]) {
  11885. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11886. } else {
  11887. cate_id_array = [@[category_id] mutableCopy];
  11888. }
  11889. [cateWhere appendString:@"("];
  11890. for (int i = 0; i < cate_id_array.count; i++) {
  11891. if (i == 0) {
  11892. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11893. } else {
  11894. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11895. }
  11896. }
  11897. [cateWhere appendString:@")"];
  11898. }
  11899. // where bestseller > 0 order by bestseller desc
  11900. // sql query: alert availability(int) best_seller(int) price qty
  11901. 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];
  11902. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11903. }
  11904. DebugLog(@"offline category where: %@",where);
  11905. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11906. if (!user) {
  11907. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11908. }
  11909. [ret setValue:filter forKey:@"filter"];
  11910. DebugLog(@"offline_category sql:%@",sqlQuery);
  11911. sqlite3_stmt * statement;
  11912. [ret setValue:@"2" forKey:@"result"];
  11913. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11914. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11915. // int count=0;
  11916. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11917. {
  11918. int i=0;
  11919. while (sqlite3_step(statement) == SQLITE_ROW)
  11920. {
  11921. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11922. char *name = (char*)sqlite3_column_text(statement, 0);
  11923. if(name==nil)
  11924. name="";
  11925. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11926. char *description = (char*)sqlite3_column_text(statement, 1);
  11927. if(description==nil)
  11928. description="";
  11929. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11930. int product_id = sqlite3_column_int(statement, 2);
  11931. int wid = sqlite3_column_int(statement, 3);
  11932. int closeout = sqlite3_column_int(statement, 4);
  11933. int cid = sqlite3_column_int(statement, 5);
  11934. int wisdelete = sqlite3_column_int(statement, 6);
  11935. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11936. int more_color = sqlite3_column_int(statement, 8);
  11937. // Defaul Category ID
  11938. __block NSString *categoryID = nil;
  11939. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11940. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11941. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11942. if(default_category==nil)
  11943. default_category="";
  11944. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11945. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11946. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11947. categoryID = nsdefault_category;
  11948. }];
  11949. if (!categoryID.length) {
  11950. NSString *cateIDs = params[@"category"];
  11951. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11952. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11953. for (NSString *cateID in requestCategoryArr) {
  11954. BOOL needBreak = NO;
  11955. for (NSString *itemCateIDBox in itemCategoryArr) {
  11956. if (itemCateIDBox.length > 4) {
  11957. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11958. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11959. if ([itemCategoryID isEqualToString:cateID]) {
  11960. needBreak = YES;
  11961. categoryID = itemCategoryID;
  11962. break;
  11963. }
  11964. }
  11965. }
  11966. if (needBreak) {
  11967. break;
  11968. }
  11969. }
  11970. }
  11971. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11972. if(wid !=0 && wisdelete != 1)
  11973. [item setValue:@"true" forKey:@"wish_exists"];
  11974. else
  11975. [item setValue:@"false" forKey:@"wish_exists"];
  11976. if(closeout==0)
  11977. [item setValue:@"false" forKey:@"is_closeout"];
  11978. else
  11979. [item setValue:@"true" forKey:@"is_closeout"];
  11980. if(cid==0)
  11981. [item setValue:@"false" forKey:@"cart_exists"];
  11982. else
  11983. [item setValue:@"true" forKey:@"cart_exists"];
  11984. if (more_color == 0) {
  11985. [item setObject:@(false) forKey:@"more_color"];
  11986. } else if (more_color == 1) {
  11987. [item setObject:@(true) forKey:@"more_color"];
  11988. }
  11989. [item addEntriesFromDictionary:imgjson];
  11990. // [item setValue:nsurl forKey:@"img"];
  11991. [item setValue:nsname forKey:@"name"];
  11992. [item setValue:nsdescription forKey:@"description"];
  11993. if (categoryID) {
  11994. [item setValue:categoryID forKey:@"item_category_id"];
  11995. }
  11996. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11997. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11998. i++;
  11999. }
  12000. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12001. [ret setObject:items forKey:@"items"];
  12002. sqlite3_finalize(statement);
  12003. } else {
  12004. DebugLog(@"nothing...");
  12005. }
  12006. DebugLog(@"count:%d",count);
  12007. [iSalesDB close_db:db];
  12008. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12009. dispatch_async(dispatch_get_main_queue(), ^{
  12010. // UIApplication * app = [UIApplication sharedApplication];
  12011. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12012. // [ret setValue:appDelegate.mode forKey:@"mode"];
  12013. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  12014. result(ret);
  12015. });
  12016. });
  12017. // NSString* orderCode = [params valueForKey:@"orderCode"];
  12018. }
  12019. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12020. {
  12021. [self categoryList:params limited:YES completionHandler:result];
  12022. }
  12023. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12024. {
  12025. UIApplication * app = [UIApplication sharedApplication];
  12026. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12027. NSString* order_code = appDelegate.order_code;
  12028. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12029. NSString* keyword = [params valueForKey:@"keyword"];
  12030. keyword=keyword.lowercaseString;
  12031. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  12032. int limit = [[params valueForKey:@"limit"] intValue];
  12033. int offset = [[params valueForKey:@"offset"] intValue];
  12034. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12035. NSString *limit_str = @"";
  12036. if (limited) {
  12037. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  12038. }
  12039. sqlite3 *db = [iSalesDB get_db];
  12040. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  12041. NSString *sqlQuery = nil;
  12042. if(exactMatch )
  12043. 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 ;
  12044. else
  12045. 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
  12046. DebugLog(@"offline_search sql:%@",sqlQuery);
  12047. sqlite3_stmt * statement;
  12048. [ret setValue:@"2" forKey:@"result"];
  12049. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12050. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12051. // int count=0;
  12052. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12053. {
  12054. int i=0;
  12055. while (sqlite3_step(statement) == SQLITE_ROW)
  12056. {
  12057. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12058. // char *name = (char*)sqlite3_column_text(statement, 1);
  12059. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12060. char *name = (char*)sqlite3_column_text(statement, 0);
  12061. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12062. char *description = (char*)sqlite3_column_text(statement, 1);
  12063. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12064. int product_id = sqlite3_column_int(statement, 2);
  12065. // char *url = (char*)sqlite3_column_text(statement, 3);
  12066. // if(url==nil)
  12067. // url="";
  12068. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12069. int wid = sqlite3_column_int(statement, 3);
  12070. int closeout = sqlite3_column_int(statement, 4);
  12071. int cid = sqlite3_column_int(statement, 5);
  12072. int wisdelete = sqlite3_column_int(statement, 6);
  12073. int more_color = sqlite3_column_int(statement, 7);
  12074. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12075. if(wid !=0 && wisdelete != 1)
  12076. [item setValue:@"true" forKey:@"wish_exists"];
  12077. else
  12078. [item setValue:@"false" forKey:@"wish_exists"];
  12079. if(closeout==0)
  12080. [item setValue:@"false" forKey:@"is_closeout"];
  12081. else
  12082. [item setValue:@"true" forKey:@"is_closeout"];
  12083. if(cid==0)
  12084. [item setValue:@"false" forKey:@"cart_exists"];
  12085. else
  12086. [item setValue:@"true" forKey:@"cart_exists"];
  12087. if (more_color == 0) {
  12088. [item setObject:@(false) forKey:@"more_color"];
  12089. } else if (more_color == 1) {
  12090. [item setObject:@(true) forKey:@"more_color"];
  12091. }
  12092. [item addEntriesFromDictionary:imgjson];
  12093. // [item setValue:nsurl forKey:@"img"];
  12094. [item setValue:nsname forKey:@"fash_name"];
  12095. [item setValue:nsdescription forKey:@"description"];
  12096. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12097. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12098. i++;
  12099. }
  12100. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12101. [ret setObject:items forKey:@"items"];
  12102. sqlite3_finalize(statement);
  12103. }
  12104. DebugLog(@"count:%d",count);
  12105. [iSalesDB close_db:db];
  12106. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12107. dispatch_async(dispatch_get_main_queue(), ^{
  12108. result(ret);
  12109. });
  12110. });
  12111. }
  12112. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12113. {
  12114. [self search:params limited:YES completionHandler:result];
  12115. }
  12116. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12117. //{
  12118. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  12119. // NSString* order_code = appDelegate.order_code;
  12120. // NSString* user = appDelegate.user;
  12121. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12122. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  12123. //
  12124. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  12125. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  12126. //
  12127. // // category
  12128. // NSDictionary *category_menu = [self offline_category_menu];
  12129. // [filter setValue:category_menu forKey:@"category"];
  12130. //
  12131. // NSString* where= nil;
  12132. // NSString* orderby= @"m.name";
  12133. // if (!filterSearch) {
  12134. // int covertype = [[params valueForKey:@"covertype"] intValue];
  12135. //
  12136. // switch (covertype) {
  12137. // case 0:
  12138. // {
  12139. // where=@"m.category like'%%#005#%%'";
  12140. // break;
  12141. // }
  12142. // case 1:
  12143. // {
  12144. // where=@"m.alert like '%QS%'";
  12145. // break;
  12146. // }
  12147. // case 2:
  12148. // {
  12149. // where=@"m.availability>0";
  12150. // break;
  12151. // }
  12152. // case 3:
  12153. // {
  12154. // where=@"m.best_seller>0";
  12155. // orderby=@"m.best_seller desc,m.name asc";
  12156. // break;
  12157. // }
  12158. // default:
  12159. // where=@"1=1";
  12160. // break;
  12161. // }
  12162. //
  12163. // }
  12164. //
  12165. //
  12166. // int limit = [[params valueForKey:@"limit"] intValue];
  12167. // int offset = [[params valueForKey:@"offset"] intValue];
  12168. //
  12169. // NSString *limit_str = @"";
  12170. // if (limited) {
  12171. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  12172. // }
  12173. //
  12174. //
  12175. // sqlite3 *db = [iSalesDB get_db];
  12176. // // [iSalesDB AddExFunction:db];
  12177. //
  12178. // int count;
  12179. //
  12180. // NSString *sqlQuery = nil;
  12181. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  12182. // 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];
  12183. //
  12184. //
  12185. // double price_min = 0;
  12186. // double price_max = 0;
  12187. // if (filterSearch) {
  12188. // // alert
  12189. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  12190. // NSString *alert = params[@"alert"];
  12191. // if ([alert isEqualToString:@"Display All"]) {
  12192. // alert = [NSString stringWithFormat:@""];
  12193. // } else {
  12194. // alert = [self translateSingleQuote:alert];
  12195. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  12196. // }
  12197. //
  12198. // // available
  12199. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  12200. // NSString *available = params[@"available"];
  12201. // NSString *available_condition;
  12202. // if ([available isEqualToString:@"Display All"]) {
  12203. // available_condition = @"";
  12204. // } else if ([available isEqualToString:@"Available Now"]) {
  12205. // available_condition = @"and availability > 0";
  12206. // } else {
  12207. // available_condition = @"and availability == 0";
  12208. // }
  12209. //
  12210. // // best seller
  12211. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  12212. // NSString *best_seller = @"";
  12213. // NSString *order_best_seller = @"m.name asc";
  12214. //
  12215. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12216. // best_seller = @"and best_seller > 0";
  12217. // order_best_seller = @"m.best_seller desc,m.name asc";
  12218. // }
  12219. //
  12220. // // price
  12221. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12222. // NSString *price = params[@"price"];
  12223. // price_min = 0;
  12224. // price_max = MAXFLOAT;
  12225. // if (user) {
  12226. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12227. // NSMutableString *priceName = [NSMutableString string];
  12228. // for (int i = 0; i < priceTypeArray.count; i++) {
  12229. // NSString *pricetype = priceTypeArray[i];
  12230. // pricetype = [self translateSingleQuote:pricetype];
  12231. // if (i == 0) {
  12232. // [priceName appendFormat:@"'%@'",pricetype];
  12233. // } else {
  12234. // [priceName appendFormat:@",'%@'",pricetype];
  12235. // }
  12236. // }
  12237. //
  12238. // if ([price isEqualToString:@"Display All"]) {
  12239. // price = [NSString stringWithFormat:@""];
  12240. // } else if([price containsString:@"+"]){
  12241. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12242. // price_min = [price doubleValue];
  12243. // 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];
  12244. // } else {
  12245. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12246. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12247. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12248. // 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];
  12249. // }
  12250. // } else {
  12251. // price = @"";
  12252. // }
  12253. //
  12254. // // sold_by_qty : Sold in quantities of %@
  12255. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12256. // NSString *qty = params[@"sold_by_qty"];
  12257. // if ([qty isEqualToString:@"Display All"]) {
  12258. // qty = @"";
  12259. // } else {
  12260. // qty = [self translateSingleQuote:qty];
  12261. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12262. // }
  12263. //
  12264. // // category
  12265. // NSString *category_id = params[@"ctgId"];
  12266. // NSMutableArray *cate_id_array = nil;
  12267. // NSMutableString *cateWhere = [NSMutableString string];
  12268. //
  12269. // if ([category_id isEqualToString:@""] || !category_id) {
  12270. // [cateWhere appendString:@"1 = 1"];
  12271. // } else {
  12272. //
  12273. // if ([category_id containsString:@","]) {
  12274. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12275. // } else {
  12276. // cate_id_array = [@[category_id] mutableCopy];
  12277. // }
  12278. // /*-----------*/
  12279. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12280. //
  12281. // [cateWhere appendString:@"("];
  12282. // for (int i = 0; i < cate_id_array.count; i++) {
  12283. //
  12284. // for (NSString *key0 in cateDic.allKeys) {
  12285. //
  12286. // if ([key0 containsString:@"category_"]) {
  12287. //
  12288. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12289. //
  12290. // for (NSString *key1 in category0.allKeys) {
  12291. //
  12292. // if ([key1 containsString:@"category_"]) {
  12293. //
  12294. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12295. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12296. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12297. //
  12298. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12299. // if (i == 0) {
  12300. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12301. // } else {
  12302. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12303. // }
  12304. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12305. // [category0 setValue:category1 forKey:key1];
  12306. // [cateDic setValue:category0 forKey:key0];
  12307. //
  12308. // }
  12309. //
  12310. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12311. //
  12312. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12313. //
  12314. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12315. // if (i == 0) {
  12316. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12317. // } else {
  12318. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12319. // }
  12320. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12321. // [cateDic setValue:category0 forKey:key0];
  12322. //
  12323. // }
  12324. //
  12325. // }
  12326. //
  12327. // }
  12328. //
  12329. // }
  12330. //
  12331. // }
  12332. //
  12333. // }
  12334. // [cateWhere appendString:@")"];
  12335. //
  12336. // [filter setValue:cateDic forKey:@"category"];
  12337. // }
  12338. //
  12339. // // where bestseller > 0 order by bestseller desc
  12340. // // sql query: alert availability(int) best_seller(int) price qty
  12341. //
  12342. // 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];
  12343. //
  12344. //
  12345. // // count
  12346. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12347. //
  12348. // }
  12349. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12350. //
  12351. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12352. //
  12353. //
  12354. //
  12355. //
  12356. // if (!user) {
  12357. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12358. // }
  12359. //
  12360. // [ret setValue:filter forKey:@"filter"];
  12361. //
  12362. //
  12363. //
  12364. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12365. //
  12366. // sqlite3_stmt * statement;
  12367. // [ret setValue:@"2" forKey:@"result"];
  12368. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12369. //
  12370. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12371. // // int count=0;
  12372. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12373. // {
  12374. //
  12375. // int i=0;
  12376. // while (sqlite3_step(statement) == SQLITE_ROW)
  12377. // {
  12378. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12379. //
  12380. //
  12381. // char *name = (char*)sqlite3_column_text(statement, 0);
  12382. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12383. //
  12384. // char *description = (char*)sqlite3_column_text(statement, 1);
  12385. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12386. //
  12387. //
  12388. //
  12389. // int product_id = sqlite3_column_int(statement, 2);
  12390. //
  12391. //
  12392. // int wid = sqlite3_column_int(statement, 3);
  12393. // int closeout = sqlite3_column_int(statement, 4);
  12394. // int cid = sqlite3_column_int(statement, 5);
  12395. // int wisdelete = sqlite3_column_int(statement, 6);
  12396. // int more_color = sqlite3_column_int(statement, 7);
  12397. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12398. //
  12399. // if(wid !=0 && wisdelete != 1)
  12400. // [item setValue:@"true" forKey:@"wish_exists"];
  12401. // else
  12402. // [item setValue:@"false" forKey:@"wish_exists"];
  12403. //
  12404. // if(closeout==0)
  12405. // [item setValue:@"false" forKey:@"is_closeout"];
  12406. // else
  12407. // [item setValue:@"true" forKey:@"is_closeout"];
  12408. //
  12409. // if(cid==0)
  12410. // [item setValue:@"false" forKey:@"cart_exists"];
  12411. // else
  12412. // [item setValue:@"true" forKey:@"cart_exists"];
  12413. //
  12414. // if (more_color == 0) {
  12415. // [item setObject:@(false) forKey:@"more_color"];
  12416. // } else if (more_color == 1) {
  12417. // [item setObject:@(true) forKey:@"more_color"];
  12418. // }
  12419. //
  12420. // [item addEntriesFromDictionary:imgjson];
  12421. //
  12422. //
  12423. // // [item setValue:nsurl forKey:@"img"];
  12424. // [item setValue:nsname forKey:@"fash_name"];
  12425. // [item setValue:nsdescription forKey:@"description"];
  12426. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12427. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12428. // i++;
  12429. // }
  12430. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12431. // [ret setObject:items forKey:@"items"];
  12432. // sqlite3_finalize(statement);
  12433. // }
  12434. //
  12435. // [iSalesDB close_db:db];
  12436. //
  12437. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12438. //
  12439. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12440. // dispatch_async(dispatch_get_main_queue(), ^{
  12441. //
  12442. // result(ret);
  12443. // });
  12444. //
  12445. // });
  12446. //
  12447. //
  12448. //
  12449. //}
  12450. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12451. //{
  12452. // [self itemsearch:params limited:YES completionHandler:result];
  12453. //}
  12454. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12455. {
  12456. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12457. NSString* offline_command=params[@"offline_Command"];
  12458. NSMutableDictionary* ret=nil;
  12459. if([offline_command isEqualToString:@"model_NIYMAL"])
  12460. {
  12461. NSString* category = params[@"category"];
  12462. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12463. }
  12464. dispatch_async(dispatch_get_main_queue(), ^{
  12465. result(ret);
  12466. });
  12467. });
  12468. }
  12469. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12470. {
  12471. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12472. int sort = [[params objectForKey:@"sort"] intValue];
  12473. NSString *sort_str = @"";
  12474. switch (sort) {
  12475. case 0:{
  12476. sort_str = @"order by w.modify_time desc";
  12477. }
  12478. break;
  12479. case 1:{
  12480. sort_str = @"order by w.modify_time asc";
  12481. }
  12482. break;
  12483. case 2:{
  12484. sort_str = @"order by m.name asc";
  12485. }
  12486. break;
  12487. case 3:{
  12488. sort_str = @"order by m.name desc";
  12489. }
  12490. break;
  12491. case 4:{
  12492. sort_str = @"order by m.description asc";
  12493. }
  12494. break;
  12495. default:
  12496. break;
  12497. }
  12498. // NSString* user = appDelegate.user;
  12499. sqlite3 *db = [iSalesDB get_db];
  12500. // order by w.create_time
  12501. 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];
  12502. // 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];
  12503. sqlite3_stmt * statement;
  12504. NSDate *date1 = [NSDate date];
  12505. // NSDate *date2 = nil;
  12506. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12507. int count=0;
  12508. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12509. {
  12510. while (sqlite3_step(statement) == SQLITE_ROW)
  12511. {
  12512. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12513. int product_id = sqlite3_column_double(statement, 0);
  12514. char *description = (char*)sqlite3_column_text(statement, 1);
  12515. if(description==nil)
  12516. description= "";
  12517. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12518. int item_id = sqlite3_column_double(statement, 2);
  12519. NSDate *date_image = [NSDate date];
  12520. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12521. printf("image : ");
  12522. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12523. // char *url = (char*)sqlite3_column_text(statement, 3);
  12524. // if(url==nil)
  12525. // url="";
  12526. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12527. int qty = sqlite3_column_int(statement, 3);
  12528. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12529. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12530. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12531. item[@"description"]= nsdescription;
  12532. item[@"img"]= nsurl;
  12533. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12534. count++;
  12535. }
  12536. printf("total time:");
  12537. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12538. ret[@"count"]= [NSNumber numberWithInt:count];
  12539. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12540. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12541. ret[@"result"]= [NSNumber numberWithInt:2];
  12542. sqlite3_finalize(statement);
  12543. }
  12544. [iSalesDB close_db:db];
  12545. dispatch_async(dispatch_get_main_queue(), ^{
  12546. UIApplication * app = [UIApplication sharedApplication];
  12547. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12548. appDelegate.wish_count =count;
  12549. [appDelegate update_count_mark];
  12550. result(ret);
  12551. });
  12552. });
  12553. }
  12554. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12555. {
  12556. // UIApplication * app = [UIApplication sharedApplication];
  12557. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12558. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12559. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12560. sqlite3 *db = [iSalesDB get_db];
  12561. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12562. NSString* product_id=params[@"product_id"];
  12563. NSString *item_count_str = params[@"item_count"];
  12564. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12565. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12566. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12567. // NSString *sql = @"";
  12568. for(int i=0;i<arr.count;i++)
  12569. {
  12570. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12571. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12572. __block int cart_count = 0;
  12573. if (!item_count_str) {
  12574. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12575. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12576. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12577. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12578. cart_count = [[model_set_components lastObject] intValue];
  12579. }];
  12580. }
  12581. if(count==0)
  12582. {
  12583. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12584. sqlite3_stmt *stmt;
  12585. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12586. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12587. if (item_count_arr) {
  12588. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12589. } else {
  12590. sqlite3_bind_int(stmt,2,cart_count);
  12591. }
  12592. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12593. [iSalesDB execSql:@"ROLLBACK" db:db];
  12594. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12595. [iSalesDB close_db:db];
  12596. DebugLog(@"add to wishlist error");
  12597. dispatch_async(dispatch_get_main_queue(), ^{
  12598. result(ret);
  12599. });
  12600. }
  12601. } else {
  12602. int qty = 0;
  12603. if (item_count_arr) {
  12604. qty = [item_count_arr[i] intValue];
  12605. } else {
  12606. qty = cart_count;
  12607. }
  12608. 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]];
  12609. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12610. [iSalesDB execSql:@"ROLLBACK" db:db];
  12611. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12612. [iSalesDB close_db:db];
  12613. DebugLog(@"add to wishlist error");
  12614. dispatch_async(dispatch_get_main_queue(), ^{
  12615. result(ret);
  12616. });
  12617. }
  12618. }
  12619. }
  12620. // [iSalesDB execSql:sql db:db];
  12621. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12622. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12623. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12624. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12625. [iSalesDB close_db:db];
  12626. ret[@"result"]= [NSNumber numberWithInt:2];
  12627. dispatch_async(dispatch_get_main_queue(), ^{
  12628. UIApplication * app = [UIApplication sharedApplication];
  12629. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12630. appDelegate.wish_count =count;
  12631. [appDelegate update_count_mark];
  12632. result(ret);
  12633. });
  12634. });
  12635. }
  12636. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12637. {
  12638. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12639. bool bLogin = appDelegate.bLogin;
  12640. __block NSString* contact_id = appDelegate.contact_id;
  12641. __block NSString* user = appDelegate.user;
  12642. __block NSString* order_code = appDelegate.order_code;
  12643. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12644. NSString* model_name = [params valueForKey:@"product_name"];
  12645. NSString* product_id = [params valueForKey:@"product_id"];
  12646. NSString* upc_code = [params valueForKey:@"upc_code"];
  12647. NSString* category = [params valueForKey:@"category"];
  12648. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12649. if(category==nil) {
  12650. category = default_category_id;
  12651. } else {
  12652. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12653. // 参数重有多个category id
  12654. if (arr_0.count > 1) {
  12655. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12656. // 取交集
  12657. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12658. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12659. [set_0 intersectSet:set_1];
  12660. if (set_0.count == 1) {
  12661. category = (NSString *)[set_0 anyObject];
  12662. } else {
  12663. if ([set_0 containsObject:default_category_id]) {
  12664. category = default_category_id;
  12665. } else {
  12666. category = (NSString *)[set_0 anyObject];
  12667. }
  12668. }
  12669. }
  12670. }
  12671. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12672. sqlite3 *db = [iSalesDB get_db];
  12673. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12674. NSString *sqlQuery = nil;
  12675. if(product_id==nil)
  12676. if(model_name==nil)
  12677. {
  12678. 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='%@';
  12679. }
  12680. else
  12681. {
  12682. 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='%@';
  12683. }
  12684. else
  12685. 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=%@;
  12686. sqlite3_stmt * statement;
  12687. [ret setValue:@"2" forKey:@"result"];
  12688. [ret setValue:@"3" forKey:@"detail_section_count"];
  12689. // int count=0;
  12690. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12691. {
  12692. if (sqlite3_step(statement) == SQLITE_ROW)
  12693. {
  12694. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12695. char *name = (char*)sqlite3_column_text(statement, 0);
  12696. if(name==nil)
  12697. name="";
  12698. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12699. char *description = (char*)sqlite3_column_text(statement, 1);
  12700. if(description==nil)
  12701. description="";
  12702. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12703. int product_id = sqlite3_column_int(statement, 2);
  12704. char *color = (char*)sqlite3_column_text(statement, 3);
  12705. if(color==nil)
  12706. color="";
  12707. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12708. //
  12709. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12710. // if(legcolor==nil)
  12711. // legcolor="";
  12712. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12713. //
  12714. //
  12715. int availability = sqlite3_column_int(statement, 5);
  12716. //
  12717. int incoming_stock = sqlite3_column_int(statement, 6);
  12718. char *demension = (char*)sqlite3_column_text(statement, 7);
  12719. if(demension==nil)
  12720. demension="";
  12721. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12722. // ,,,,,,,,,
  12723. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12724. if(seat_height==nil)
  12725. seat_height="";
  12726. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12727. char *material = (char*)sqlite3_column_text(statement, 9);
  12728. if(material==nil)
  12729. material="";
  12730. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12731. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12732. if(box_dim==nil)
  12733. box_dim="";
  12734. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12735. char *volume = (char*)sqlite3_column_text(statement, 11);
  12736. if(volume==nil)
  12737. volume="";
  12738. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12739. double weight = sqlite3_column_double(statement, 12);
  12740. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12741. if(model_set==nil)
  12742. model_set="";
  12743. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12744. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12745. if(load_ability==nil)
  12746. load_ability="";
  12747. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12748. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12749. // if(default_category==nil)
  12750. // default_category="";
  12751. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12752. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12753. if(fabric_content==nil)
  12754. fabric_content="";
  12755. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12756. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12757. if(assembling==nil)
  12758. assembling="";
  12759. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12760. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12761. if(made_in==nil)
  12762. made_in="";
  12763. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12764. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12765. if(special_remarks==nil)
  12766. special_remarks="";
  12767. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12768. int stockUcom = sqlite3_column_double(statement, 20);
  12769. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12770. if(product_group==nil)
  12771. product_group="";
  12772. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12773. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12774. // if(fashion_selector==nil)
  12775. // fashion_selector="";
  12776. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12777. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12778. if(selector_field==nil)
  12779. selector_field="";
  12780. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12781. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12782. if(property_field==nil)
  12783. property_field="";
  12784. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12785. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12786. if(packaging==nil)
  12787. packaging="";
  12788. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12789. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12790. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12791. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12792. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12793. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12794. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12795. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12796. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12797. NSString* Availability=nil;
  12798. if(availability>0)
  12799. Availability=[NSString stringWithFormat:@"%d",availability];
  12800. else
  12801. Availability = @"Out of Stock";
  12802. [img_section setValue:Availability forKey:@"Availability"];
  12803. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12804. char *eta = (char*)sqlite3_column_text(statement, 25);
  12805. if(eta==nil)
  12806. eta="";
  12807. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12808. if ([nseta isEqualToString:@"null"]) {
  12809. nseta = @"";
  12810. }
  12811. if (availability <= 0) {
  12812. [img_section setValue:nseta forKey:@"ETA"];
  12813. }
  12814. int item_id = sqlite3_column_int(statement, 26);
  12815. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12816. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12817. NSString* Price=nil;
  12818. if(bLogin==false)
  12819. Price=@"Must Sign in.";
  12820. else
  12821. {
  12822. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12823. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12824. if(price==nil)
  12825. Price=@"No Price.";
  12826. else
  12827. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12828. }
  12829. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12830. [img_section setValue:Price forKey:@"price"];
  12831. [img_section setValue:nsname forKey:@"model_name"];
  12832. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12833. if (order_code) { // 离线order code即so#
  12834. 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];
  12835. __block int cartQTY = 0;
  12836. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12837. cartQTY = sqlite3_column_int(stmt, 0);
  12838. }];
  12839. if (cartQTY > 0) {
  12840. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12841. }
  12842. }
  12843. [ret setObject:img_section forKey:@"img_section"];
  12844. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12845. int detail0_item_count=0;
  12846. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12847. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12848. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12849. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12850. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12851. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12852. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12853. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12854. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12855. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12856. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12857. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12858. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12859. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12860. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12861. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12862. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12863. {
  12864. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12865. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12866. }
  12867. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12868. [detail0_section setValue:@"kv" forKey:@"type"];
  12869. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12870. [ret setObject:detail0_section forKey:@"detail_0"];
  12871. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12872. // [detail1_section setValue:@"detail" forKey:@"target"];
  12873. // [detail1_section setValue:@"popup" forKey:@"action"];
  12874. // [detail1_section setValue:@"content" forKey:@"type"];
  12875. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12876. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12877. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12878. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12879. [ret setObject:detail1_section forKey:@"detail_1"];
  12880. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12881. [detail2_section setValue:@"detail" forKey:@"target"];
  12882. [detail2_section setValue:@"popup" forKey:@"action"];
  12883. [detail2_section setValue:@"content" forKey:@"type"];
  12884. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12885. [detail2_section setValue:@"true" forKey:@"single_row"];
  12886. [detail2_section setValue:@"local" forKey:@"data"];
  12887. [ret setObject:detail2_section forKey:@"detail_2"];
  12888. }
  12889. sqlite3_finalize(statement);
  12890. }
  12891. else
  12892. {
  12893. [ret setValue:@"8" forKey:@"result"];
  12894. }
  12895. // DebugLog(@"count:%d",count);
  12896. [iSalesDB close_db:db];
  12897. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12898. dispatch_async(dispatch_get_main_queue(), ^{
  12899. result(ret);
  12900. });
  12901. });
  12902. }
  12903. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12904. //{
  12905. //
  12906. // UIApplication * app = [UIApplication sharedApplication];
  12907. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12908. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12909. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12910. // sqlite3 *db = [iSalesDB get_db];
  12911. // NSString* collectId=params[@"collectId"];
  12912. //
  12913. //
  12914. //
  12915. //
  12916. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12917. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12918. // [iSalesDB execSql:sqlQuery db:db];
  12919. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12920. // [iSalesDB close_db:db];
  12921. //
  12922. //
  12923. // ret[@"result"]= [NSNumber numberWithInt:2];
  12924. // dispatch_async(dispatch_get_main_queue(), ^{
  12925. //
  12926. // appDelegate.wish_count =count;
  12927. //
  12928. // [appDelegate update_count_mark];
  12929. // result(ret);
  12930. // });
  12931. //
  12932. // });
  12933. //}
  12934. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12935. {
  12936. UIApplication * app = [UIApplication sharedApplication];
  12937. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12938. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12939. [iSalesDB disable_trigger];
  12940. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12941. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12942. [iSalesDB enable_trigger];
  12943. dispatch_async(dispatch_get_main_queue(), ^{
  12944. //
  12945. // NSString* user = [params valueForKey:@"user"];
  12946. //
  12947. // NSString* password = [params valueForKey:@"password"];
  12948. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12949. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12950. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12951. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12952. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12953. [appDelegate update_count_mark];
  12954. appDelegate.can_show_price =false;
  12955. appDelegate.can_see_price =false;
  12956. appDelegate.can_create_portfolio =false;
  12957. appDelegate.can_create_order =false;
  12958. appDelegate.can_cancel_order =false;
  12959. appDelegate.can_set_cart_price =false;
  12960. appDelegate.can_delete_order =false;
  12961. appDelegate.can_submit_order =false;
  12962. appDelegate.can_set_tearsheet_price =false;
  12963. appDelegate.can_update_contact_info = false;
  12964. appDelegate.save_order_logout = false;
  12965. appDelegate.submit_order_logout = false;
  12966. appDelegate.alert_sold_in_quantities = false;
  12967. appDelegate.ipad_perm =nil ;
  12968. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12969. appDelegate.OrderFilter= nil;
  12970. [appDelegate SetSo:nil];
  12971. [appDelegate set_main_button_panel];
  12972. result(ret);
  12973. });
  12974. });
  12975. }
  12976. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12977. {
  12978. // iSalesDB.
  12979. // UIApplication * app = [UIApplication sharedApplication];
  12980. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12981. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12982. [iSalesDB disable_trigger];
  12983. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12984. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12985. [iSalesDB enable_trigger];
  12986. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12987. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12988. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12989. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12990. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12991. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12992. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12993. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12994. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12995. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12996. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12997. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12998. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12999. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  13000. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  13001. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  13002. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  13003. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  13004. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  13005. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  13006. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  13007. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  13008. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  13009. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  13010. [arr_name addObject:customer_first_name];
  13011. [arr_name addObject:customer_last_name];
  13012. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  13013. // default ship from
  13014. 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';";
  13015. __block NSString *cid = @"";
  13016. __block NSString *name = @"";
  13017. __block NSString *ext = @"";
  13018. __block NSString *contact = @"";
  13019. __block NSString *email = @"";
  13020. __block NSString *fax = @"";
  13021. __block NSString *phone = @"";
  13022. sqlite3 *db = [iSalesDB get_db];
  13023. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13024. cid = [self textAtColumn:0 statement:statment];
  13025. name = [self textAtColumn:1 statement:statment];
  13026. ext = [self textAtColumn:2 statement:statment];
  13027. contact = [self textAtColumn:3 statement:statment];
  13028. email = [self textAtColumn:4 statement:statment];
  13029. fax = [self textAtColumn:5 statement:statment];
  13030. phone = [self textAtColumn:6 statement:statment];
  13031. }];
  13032. // default carrier
  13033. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  13034. __block NSString *carrier = @"";
  13035. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13036. carrier = [self textAtColumn:0 statement:statment];
  13037. }];
  13038. NSString* so_id = [self get_offline_soid:db];
  13039. if(so_id==nil)
  13040. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  13041. 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];
  13042. int result_code =[iSalesDB execSql:sql_neworder db:db];
  13043. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  13044. //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'
  13045. //soId
  13046. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  13047. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  13048. sqlite3_stmt * statement;
  13049. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  13050. {
  13051. if (sqlite3_step(statement) == SQLITE_ROW)
  13052. {
  13053. // char *name = (char*)sqlite3_column_text(statement, 1);
  13054. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  13055. //ret = sqlite3_column_int(statement, 0);
  13056. char *soId = (char*)sqlite3_column_text(statement, 0);
  13057. if(soId==nil)
  13058. soId="";
  13059. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  13060. [ret setValue:nssoId forKey:@"soId"];
  13061. [ret setValue:nssoId forKey:@"orderCode"];
  13062. }
  13063. sqlite3_finalize(statement);
  13064. }
  13065. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  13066. [ret setValue:@"Regular Mode" forKey:@"mode"];
  13067. [iSalesDB close_db:db];
  13068. dispatch_async(dispatch_get_main_queue(), ^{
  13069. result(ret);
  13070. });
  13071. });
  13072. }
  13073. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  13074. {
  13075. UIApplication * app = [UIApplication sharedApplication];
  13076. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  13077. bool can_create_backorder= appDelegate.can_create_backorder;
  13078. NSString* user = appDelegate.user;
  13079. NSString* contact_id = appDelegate.contact_id;
  13080. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  13081. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  13082. sqlite3 *db = [iSalesDB get_db];
  13083. NSString* orderCode=params[@"orderCode"];
  13084. int sort = [[params objectForKey:@"sort"] intValue];
  13085. NSString *sort_str = @"";
  13086. switch (sort) {
  13087. case 0:{
  13088. sort_str = @"order by c.modify_time desc";
  13089. }
  13090. break;
  13091. case 1:{
  13092. sort_str = @"order by c.modify_time asc";
  13093. }
  13094. break;
  13095. case 2:{
  13096. sort_str = @"order by m.name asc";
  13097. }
  13098. break;
  13099. case 3:{
  13100. sort_str = @"order by m.name desc";
  13101. }
  13102. break;
  13103. case 4:{
  13104. sort_str = @"order by m.description asc";
  13105. }
  13106. break;
  13107. default:
  13108. break;
  13109. }
  13110. 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 ];
  13111. // 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 ];
  13112. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  13113. DebugLog(@"offline_login sql:%@",sqlQuery);
  13114. sqlite3_stmt * statement;
  13115. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  13116. NSDate *date1 = [NSDate date];
  13117. int count=0;
  13118. int cart_count=0;
  13119. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  13120. if ( dbresult== SQLITE_OK)
  13121. {
  13122. while (sqlite3_step(statement) == SQLITE_ROW)
  13123. {
  13124. // NSDate *row_date = [NSDate date];
  13125. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13126. int product_id = sqlite3_column_int(statement, 0);
  13127. char *str_price = (char*)sqlite3_column_text(statement, 1);
  13128. int item_id = sqlite3_column_int(statement, 7);
  13129. NSString* Price=nil;
  13130. if(str_price==nil)
  13131. {
  13132. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  13133. // NSDate *price_date = [NSDate date];
  13134. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  13135. // DebugLog(@"price time interval");
  13136. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  13137. if(price==nil)
  13138. Price=@"No Price.";
  13139. else
  13140. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  13141. }
  13142. else
  13143. {
  13144. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  13145. }
  13146. double discount = sqlite3_column_double(statement, 2);
  13147. int item_count = sqlite3_column_int(statement, 3);
  13148. char *line_note = (char*)sqlite3_column_text(statement, 4);
  13149. NSString *nsline_note=nil;
  13150. if(line_note!=nil)
  13151. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  13152. char *name = (char*)sqlite3_column_text(statement, 5);
  13153. NSString *nsname=nil;
  13154. if(name!=nil)
  13155. nsname= [[NSString alloc]initWithUTF8String:name];
  13156. char *description = (char*)sqlite3_column_text(statement, 6);
  13157. NSString *nsdescription=nil;
  13158. if(description!=nil)
  13159. nsdescription= [[NSString alloc]initWithUTF8String:description];
  13160. int stockUom = sqlite3_column_int(statement, 8);
  13161. int _id = sqlite3_column_int(statement, 9);
  13162. int availability = sqlite3_column_int(statement, 10);
  13163. // NSDate *subtotal_date = [NSDate date];
  13164. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  13165. // DebugLog(@"subtotal_date time interval");
  13166. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  13167. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  13168. double weight=[bsubtotaljson[@"weight"] doubleValue];
  13169. int carton=[bsubtotaljson[@"carton"] intValue];
  13170. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  13171. int mpack =[bsubtotaljson[@"mpack"] intValue];
  13172. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  13173. // NSDate *img_date = [NSDate date];
  13174. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  13175. // DebugLog(@"img_date time interval");
  13176. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  13177. itemjson[@"model"]=nsname;
  13178. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  13179. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  13180. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  13181. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  13182. itemjson[@"check"]=@"true";
  13183. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  13184. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  13185. itemjson[@"unit_price"]=Price;
  13186. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  13187. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  13188. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  13189. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  13190. itemjson[@"note"]=nsline_note;
  13191. if (!can_create_backorder) {
  13192. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  13193. }
  13194. // NSDate *date2 = [NSDate date];
  13195. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  13196. // DebugLog(@"model_bundle time interval");
  13197. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  13198. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  13199. count++;
  13200. // DebugLog(@"row time interval");
  13201. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  13202. }
  13203. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  13204. sqlite3_finalize(statement);
  13205. }
  13206. DebugLog(@"request cart total time interval");
  13207. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  13208. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  13209. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  13210. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  13211. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  13212. ret[@"count"]=[NSNumber numberWithInt:count ];
  13213. ret[@"mode"]=@"Regular Mode";
  13214. [iSalesDB close_db:db];
  13215. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  13216. DebugLog(@"general notes :%@",general_note);
  13217. ret[@"general_note"]= general_note;
  13218. dispatch_async(dispatch_get_main_queue(), ^{
  13219. result(ret);
  13220. });
  13221. });
  13222. }
  13223. @end