OLDataProvider.m 720 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "AppDelegate.h"
  15. #import "pdfCreator.h"
  16. #import "RASingleton.h"
  17. #import "QRCodeGenerator.h"
  18. #import "config.h"
  19. #import "ImageUtils.h"
  20. #import "RAConvertor.h"
  21. #import "ActiveViewController.h"
  22. #import "RANetwork.h"
  23. #import "RADataProvider.h"
  24. @interface OLDataProvider ()
  25. @end
  26. @implementation OLDataProvider
  27. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  28. {
  29. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  30. NSString* ret=@"No Price.";
  31. sqlite3_stmt * statement;
  32. // int count=0;
  33. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  34. {
  35. if (sqlite3_step(statement) == SQLITE_ROW)
  36. {
  37. char *price = (char*)sqlite3_column_text(statement, 0);
  38. if(price==nil)
  39. price="";
  40. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  41. double dp= [nsprice doubleValue];
  42. ret=[NSString stringWithFormat:@"%.2f",dp];
  43. }
  44. sqlite3_finalize(statement);
  45. }
  46. return ret;
  47. // // [iSalesDB close_db:db];
  48. //
  49. // return nil;
  50. //// if(dprice==DBL_MAX)
  51. //// ret= nil;
  52. //// else
  53. //// ret= [NSNumber numberWithDouble:dprice];
  54. //// return ret;
  55. }
  56. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  57. {
  58. NSString* ret= nil;
  59. NSString *sqlQuery = nil;
  60. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  61. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  62. sqlite3_stmt * statement;
  63. // int count=0;
  64. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  65. {
  66. if (sqlite3_step(statement) == SQLITE_ROW)
  67. {
  68. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  69. if(imgurl==nil)
  70. imgurl="";
  71. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  72. ret=nsimgurl;
  73. }
  74. sqlite3_finalize(statement);
  75. }
  76. else
  77. {
  78. ret=@"";
  79. }
  80. // [iSalesDB close_db:db];
  81. // DebugLog(@"data string: %@",ret );
  82. return ret;
  83. }
  84. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  85. {
  86. // assert(params[@"contact_id"]!=nil);
  87. assert(params[@"user"]!=nil);
  88. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  89. //生成portfolio pdf需要的数据
  90. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  91. data[@"npd_url"]=COMPANY_WEB;
  92. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  93. [formatter setDateFormat:@"MMMM yyyy"];
  94. NSString* date = [formatter stringFromDate:[NSDate date]];
  95. data[@"create_month"]=date;
  96. data[@"company_name"]=COMPANY_FULL_NAME;
  97. data[@"catalog_name"]=params[@"catalog_name"];
  98. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  99. data[@"TOTAL_PAGE"]=0;
  100. data[@"CURRENT_PAGE"]=0;
  101. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  102. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  103. bool show_stockout=[params[@"show_stockout"] boolValue];
  104. NSString *sql = nil;
  105. // v1.90 more color
  106. NSString *group_by = @"";
  107. if(values==nil)
  108. {
  109. if (show_group_by) {
  110. group_by = @"group by b.product_group ";
  111. }
  112. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  113. }
  114. else
  115. {
  116. if (show_group_by) {
  117. group_by = @"group by product_group ";
  118. }
  119. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  120. }
  121. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  122. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  123. sqlite3 *db = [iSalesDB get_db];
  124. // NSDictionary *resultDic =
  125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  126. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  127. int product_id = sqlite3_column_int(stmt, 0);
  128. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  129. NSString *name = [self textAtColumn:1 statement:stmt];
  130. NSString *description = [self textAtColumn:2 statement:stmt];
  131. double price = sqlite3_column_double(stmt, 3);
  132. NSString *s_price = [self textAtColumn:3 statement:stmt];
  133. double discount = sqlite3_column_double(stmt,4);
  134. int qty = sqlite3_column_int(stmt, 5);
  135. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  136. int is_percentage = sqlite3_column_int(stmt, 6);
  137. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  138. int item_id = sqlite3_column_int(stmt, 7);
  139. // int fashion_id = sqlite3_column_int(stmt, 8);
  140. NSString *line_note = [self textAtColumn:9 statement:stmt];
  141. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  142. double percent = sqlite3_column_double(stmt, 10);
  143. // int portfolio_id = sqlite3_column_int(stmt, 11);
  144. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  145. int availability = sqlite3_column_int(stmt, 12);
  146. NSString *color = [self textAtColumn:13 statement:stmt];
  147. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  148. NSString *demension = [self textAtColumn:15 statement:stmt];
  149. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  150. NSString *material = [self textAtColumn:17 statement:stmt];
  151. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  152. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  153. double volume = sqlite3_column_double(stmt, 19);
  154. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  155. double weight = sqlite3_column_double(stmt, 20);
  156. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  157. int model_set = sqlite3_column_int(stmt, 21);
  158. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  159. double load_ability = sqlite3_column_double(stmt, 22);
  160. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  161. NSString *assembling = [self textAtColumn:24 statement:stmt];
  162. NSString *made_in = [self textAtColumn:25 statement:stmt];
  163. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  164. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  165. float bprice=0;
  166. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  167. for(int b=0;b< [bundle[@"count"] intValue];b++)
  168. {
  169. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  170. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  171. }
  172. if(gprice!=nil)
  173. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  174. if(s_percent.length==0||is_percentage==0)
  175. {
  176. percent=100.0;
  177. if([s_qty isEqualToString:@"null"])
  178. qty=availability;
  179. }
  180. else
  181. {
  182. qty=availability;
  183. }
  184. qty=qty*percent/100+0.5;
  185. for(int i=0;i<[values[@"count"] intValue];i++)
  186. {
  187. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  188. if([valueitem[@"product_id"] intValue]==product_id)
  189. {
  190. line_note=valueitem[@"line_note"];
  191. qty=[valueitem[@"available_qty"] intValue];
  192. if([[valueitem allKeys] containsObject:@"available_qty"])
  193. s_qty=[NSString stringWithFormat:@"%d",qty];
  194. else
  195. s_qty=@"null";
  196. if(valueitem[@"regular_price"]!=nil)
  197. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  198. s_price=valueitem[@"special_price"];
  199. price=[valueitem[@"special_price"] floatValue];
  200. discount=0;
  201. // v1.90
  202. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  203. }
  204. }
  205. if(is_percentage==0&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  210. {
  211. return;
  212. }
  213. NSString* set_price=@"";
  214. if([params[@"entered_price"] boolValue])
  215. {
  216. if (s_price==nil ) {
  217. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  218. }
  219. else
  220. {
  221. if(price*(1-discount/100.0)!=gprice.floatValue)
  222. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  223. }
  224. }
  225. NSString* get_price=@"";
  226. {
  227. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  228. // DebugLog(@"price time interval");
  229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  230. if(gprice==nil)
  231. get_price=@"Price:No Price.";
  232. else
  233. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  234. }
  235. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  236. // if ([qty_null isEqualToString:@"null"]) {
  237. // qty=availability;
  238. // }
  239. //
  240. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  241. // if ([qty_percent_null isEqualToString:@"null"]) {
  242. // percentage=1;
  243. // }
  244. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  245. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  246. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  247. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  248. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  249. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  250. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  251. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  252. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  253. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  254. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  255. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  256. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  257. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  258. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  259. if([params[@"availability"] boolValue]==false)
  260. str_availability=@"";
  261. else
  262. {
  263. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  264. str_availability=@"<b>Availability:</b> In Production";
  265. }
  266. if([params[@"color"] boolValue]==false || color.length==0 )
  267. str_color=@"";
  268. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  269. str_model_set=@"";
  270. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  271. str_legcolor=@"";
  272. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  273. str_demension=@"";
  274. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  275. str_seat_height=@"";
  276. if([params[@"material"] boolValue]==false || material.length==0 )
  277. str_meterial=@"";
  278. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  279. str_box_dim=@"";
  280. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  281. str_volume=@"";
  282. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  283. str_weight=@"";
  284. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  285. str_load_ability=@"";
  286. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  287. str_fabric_content=@"";
  288. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  289. str_assembling=@"";
  290. if([params[@"made"] boolValue]==false || made_in.length==0 )
  291. str_made_in=@"";
  292. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  293. str_line_note=@"";
  294. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  295. [arr_detail addObject:str_availability];
  296. [arr_detail addObject:str_color];
  297. [arr_detail addObject:str_model_set];
  298. [arr_detail addObject:str_legcolor];
  299. [arr_detail addObject:str_demension];
  300. [arr_detail addObject:str_seat_height];
  301. [arr_detail addObject:str_meterial];
  302. [arr_detail addObject:str_box_dim];
  303. [arr_detail addObject:str_volume];
  304. [arr_detail addObject:str_weight];
  305. [arr_detail addObject:str_load_ability];
  306. [arr_detail addObject:str_fabric_content];
  307. [arr_detail addObject:str_assembling];
  308. [arr_detail addObject:str_made_in];
  309. [arr_detail addObject:str_line_note];
  310. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  311. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  312. //model image;
  313. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  314. // qr image
  315. NSString* qrpath=nil;
  316. if([params[@"show_barcode"] boolValue])
  317. {
  318. NSString* temp = NSTemporaryDirectory();
  319. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  320. qrpath=[temp stringByAppendingPathComponent:filename];
  321. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  322. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  323. }
  324. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  326. item[@"img"]=img_path;
  327. item[@"img_qr"]=qrpath;
  328. item[@"special_price"]=set_price;
  329. item[@"price"]=get_price;
  330. item[@"name"]=name;
  331. item[@"description"]=description;
  332. item[@"detail"]=detail;
  333. item[@"img"]=img_path;
  334. if (show_group_by && more_color_int != 0) {
  335. item[@"more_color"] = more_color_path;
  336. }
  337. // @{
  338. // //@"linenotes": line_note,
  339. //// @"product_id": product_id_string,
  340. //// @"available_qty": @(qty),
  341. //// @"available_percent" : @(percent),
  342. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  343. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  344. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"":
  352. // }.mutableCopy;
  353. // if (percentage) {
  354. // [item removeObjectForKey:@"available_qty"];
  355. // } else {
  356. // [item removeObjectForKey:@"available_percent"];
  357. // }
  358. //
  359. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  360. // if ([qty_null isEqualToString:@"null"]) {
  361. // [item removeObjectForKey:@"available_qty"];
  362. // }
  363. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  364. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  365. }];
  366. [iSalesDB close_db:db];
  367. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  368. // [dic setValue:@"" forKey:@"email_content"];
  369. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  370. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  371. //
  372. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  373. // cell[@"count"]=[NSNumber numberWithInt:10];
  374. grid[@"cell0"]=dic;
  375. data[@"grid0"]=grid;
  376. return data;
  377. }
  378. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  379. {
  380. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  381. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  382. NSString *cache_folder=[paths objectAtIndex:0];
  383. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  384. BOOL bdir=NO;
  385. NSFileManager* fileManager = [NSFileManager defaultManager];
  386. if(PDF_DEBUG)
  387. {
  388. NSData *data = [NSData dataWithContentsOfFile:default_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  393. {
  394. NSError * error=nil;
  395. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  396. {
  397. return nil;
  398. }
  399. }
  400. NSData *data = [NSData dataWithContentsOfFile:template_path];
  401. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  402. return ret;
  403. }
  404. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  405. {
  406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  407. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  408. DebugLog(@"offline_login sql:%@",sqlQuery);
  409. sqlite3_stmt * statement;
  410. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  411. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(statement) == SQLITE_ROW)
  414. {
  415. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  416. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  417. // int can_show_price = sqlite3_column_int(statement, 0);
  418. // int can_see_price = sqlite3_column_int(statement, 1);
  419. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  420. // if(contact_id==nil)
  421. // contact_id="";
  422. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  423. // int user_type = sqlite3_column_int(statement, 3);
  424. // int can_cancel_order = sqlite3_column_int(statement, 4);
  425. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  426. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  427. // int can_delete_order = sqlite3_column_int(statement, 7);
  428. // int can_submit_order = sqlite3_column_int(statement, 8);
  429. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  430. // int can_create_order = sqlite3_column_int(statement, 10);
  431. // char *mode = (char*)sqlite3_column_text(statement, 11);
  432. // if(mode==nil)
  433. // mode="";
  434. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  435. // char *username = (char*)sqlite3_column_text(statement, 12);
  436. // if(username==nil)
  437. // username="";
  438. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  439. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  440. char *first_name = (char*)sqlite3_column_text(statement, 14);
  441. if(first_name==nil)
  442. first_name="";
  443. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  444. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  445. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  446. // [header setValue:nscontact_id forKey:@"contact_id"];
  447. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  448. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  450. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  451. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  452. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  454. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  455. //
  456. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  457. //
  458. // [header setValue:nsusername forKey:@"username"];
  459. //
  460. // NSError* error=nil;
  461. //
  462. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  463. // [header setValue:statusFilter forKey:@"statusFilter"];
  464. //
  465. //
  466. //
  467. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  468. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  469. //
  470. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  471. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  472. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  473. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  474. //
  475. // [ret setObject:header forKey:@"header"];
  476. [ret setValue:nsfirst_name forKey:@"first_name"];
  477. }
  478. sqlite3_finalize(statement);
  479. }
  480. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  481. return ret;
  482. }
  483. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  484. {
  485. assert(user!=nil);
  486. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  487. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  488. data[@"npd_url"]=COMPANY_WEB;
  489. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  490. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  491. NSString* date = [formatter stringFromDate:[NSDate date]];
  492. data[@"print_date"]=date;
  493. data[@"company_name"]=COMPANY_FULL_NAME;
  494. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  495. data[@"TOTAL_PAGE"]=0;
  496. data[@"CURRENT_PAGE"]=0;
  497. // [formatter setDateFormat:@"MM/dd/yyyy"];
  498. // date = [formatter stringFromDate:[NSDate date]];
  499. // data[@"creat_date"]=date;
  500. NSString* temp = NSTemporaryDirectory();
  501. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  502. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  503. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  504. // bool bwrite=
  505. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  506. data[@"img_barcode"]=qrpath;
  507. data[@"so_id"]=order_code;
  508. data[@"company"]=COMPANY_FULL_NAME;
  509. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  510. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  511. sqlite3 *db = [iSalesDB get_db];
  512. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  513. sqlite3_stmt * statement;
  514. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  515. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  516. row0[@"count"]=[NSNumber numberWithInt:1];
  517. datasource[@"row0"]=row0;
  518. NSString* print_status=@"";
  519. NSString* order_type=@"Offline Quote";
  520. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  521. {
  522. if (sqlite3_step(statement) == SQLITE_ROW)
  523. {
  524. int erpOrderStatus = sqlite3_column_int(statement, 0);
  525. int status = sqlite3_column_int(statement, 26);
  526. if(status==2)
  527. {
  528. switch (erpOrderStatus) {
  529. case 10:
  530. // order_type=@"Quoted Order";
  531. print_status=@"Quote";
  532. break;
  533. case 11:
  534. print_status=@"Booked";
  535. break;
  536. case 12:
  537. print_status=@"Picked";
  538. break;
  539. case 13:
  540. print_status=@"Shipped";
  541. break;
  542. case 14:
  543. print_status=@"Closed";
  544. break;
  545. case 15:
  546. print_status=@"Canceled";
  547. break;
  548. case 16:
  549. print_status=@"All";
  550. break;
  551. case 18:
  552. print_status=@"Processing";
  553. break;
  554. default:
  555. break;
  556. }
  557. }
  558. else{
  559. switch (status) {
  560. case 0:
  561. {
  562. print_status=@"Quote";
  563. break;
  564. }
  565. case 1:
  566. {
  567. print_status=@"Saved";
  568. break;
  569. }
  570. case 3:
  571. {
  572. //canceled
  573. print_status=@"Canceled";
  574. break;
  575. }
  576. default:
  577. break;
  578. }
  579. }
  580. NSString* sender_name = [self textAtColumn:1 statement:statement];
  581. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  582. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  583. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  584. NSString* sender_email = [self textAtColumn:5 statement:statement];
  585. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  586. NSString* billing_name = [self textAtColumn:7 statement:statement];
  587. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  588. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  589. if(billing_phone.length>0)
  590. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  591. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  592. if(billing_fax.length>0)
  593. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  594. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  595. [billto_arr0 addObject:billing_phone];
  596. [billto_arr0 addObject:billing_fax];
  597. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  598. NSString* billing_email = [self textAtColumn:11 statement:statement];
  599. if(billing_email.length>0)
  600. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  601. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  602. if(billing_contact.length>0)
  603. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  604. NSString* receive_name = [self textAtColumn:12 statement:statement];
  605. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  606. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  607. if(receive_phone.length>0)
  608. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  609. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  610. if(receive_fax.length>0)
  611. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  612. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  613. [receive_arr0 addObject:receive_phone];
  614. [receive_arr0 addObject:receive_fax];
  615. NSString* receive_email = [self textAtColumn:16 statement:statement];
  616. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  617. if(receive_email.length>0)
  618. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  619. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  620. if(receive_contact.length>0)
  621. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  622. NSString* poNumber = [self textAtColumn:17 statement:statement];
  623. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  624. NSString* carrier = [self textAtColumn:19 statement:statement];
  625. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  626. NSString* terms = [self textAtColumn:21 statement:statement];
  627. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  628. NSString* port_destination = [self textAtColumn:23 statement:statement];
  629. NSString* etd = [self textAtColumn:24 statement:statement];
  630. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  631. if(sign_picpath.length==0)
  632. sign_picpath=nil;
  633. NSString* create_date = [self textAtColumn:27 statement:statement];
  634. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  635. NSString* general_notes=[self textAtColumn:33 statement:statement];
  636. NSString *customerID = [self textAtColumn:34 statement:statement];
  637. if (customerID.length == 0) {
  638. customerID = nil;
  639. }
  640. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  641. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  642. if(general_notes.length>0)
  643. {
  644. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  645. row2[@"item_0"]=@{@"g_notes":general_notes };
  646. row2[@"count"]=[NSNumber numberWithInt:1];
  647. datasource[@"row2"]=row2;
  648. }
  649. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  650. [billto_arr addObject:billing_ext];
  651. [billto_arr addObject:billing_pf];
  652. [billto_arr addObject:billing_contact];
  653. [billto_arr addObject:billing_email];
  654. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  655. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  656. [receive_arr addObject:receive_ext];
  657. [receive_arr addObject:receive_pf];
  658. [receive_arr addObject:receive_contact];
  659. [receive_arr addObject:receive_email];
  660. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  661. float shipping = sqlite3_column_double(statement, 28);
  662. float lift_gate_value = sqlite3_column_double(statement, 29);
  663. float handling_fee_value = sqlite3_column_double(statement, 30);
  664. data[@"company"]=sender_name;
  665. data[@"shipfrom_addr"]=sender_ext;
  666. data[@"shipfrom_phone"]=sender_phone;
  667. data[@"shipfrom_fax"]=sender_fax;
  668. data[@"shipfrom_email"]=sender_email;
  669. data[@"warehouse_name"]=warehouse_name;
  670. data[@"bill_to_name"]=billing_name;
  671. data[@"bill_to_ext"]=billto;
  672. data[@"ship_to_name"]=receive_name;
  673. data[@"ship_to_ext"]=receive;
  674. data[@"po_no"]=poNumber;
  675. data[@"rep"]=sales_rep;
  676. data[@"e_schdule"]=etd;
  677. data[@"sales_terms"]=sales_terms;
  678. data[@"port_destination"]=port_destination;
  679. data[@"terms"]=terms;
  680. data[@"truck_carrier"]=carrier;
  681. data[@"vendor_no"]=vendor_no;
  682. data[@"customer_sign"]=sign_picpath;
  683. data[@"print_status"]=print_status;
  684. data[@"create_date"]=create_date;
  685. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  686. sqlite3_stmt * statement_cart;
  687. // NSDate *date1 = [NSDate date];
  688. int count=0;
  689. // int cart_count=0;
  690. float credits=0;
  691. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  692. if ( dbresult== SQLITE_OK)
  693. {
  694. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  695. int row1count=0;
  696. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  697. {
  698. // NSDate *row_date = [NSDate date];
  699. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  700. // int product_id = sqlite3_column_int(statement_cart, 0);
  701. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  702. int item_id = sqlite3_column_int(statement_cart, 7);
  703. double discount = sqlite3_column_double(statement_cart, 2);
  704. NSString* Price=nil;
  705. if(str_price==nil)
  706. {
  707. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  708. // NSDate *price_date = [NSDate date];
  709. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  710. // DebugLog(@"price time interval");
  711. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  712. if(price==nil)
  713. Price=@"No Price.";
  714. else
  715. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  716. }
  717. else
  718. {
  719. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  720. }
  721. float dunitprice=0;
  722. int item_count = sqlite3_column_int(statement_cart, 3);
  723. NSString* Amount=@"";
  724. if(![Price isEqualToString:@"No Price."])
  725. {
  726. dunitprice=[Price floatValue];
  727. dunitprice=dunitprice* (1-discount/100.0);
  728. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  729. credits+=dunitprice*item_count;
  730. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  731. }
  732. else
  733. {
  734. Price=@"";
  735. }
  736. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  737. // NSString *nsline_note=nil;
  738. // if(line_note!=nil)
  739. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  740. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  741. NSString *nsname=nil;
  742. if(name!=nil)
  743. nsname= [[NSString alloc]initWithUTF8String:name];
  744. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  745. NSString *nsdescription=nil;
  746. if(description!=nil)
  747. nsdescription= [[NSString alloc]initWithUTF8String:description];
  748. // int stockUom = sqlite3_column_int(statement_cart, 8);
  749. // int _id = sqlite3_column_int(statement_cart, 9);
  750. // NSDate *subtotal_date = [NSDate date];
  751. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  752. // DebugLog(@"subtotal_date time interval");
  753. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  754. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  755. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  756. // int carton=[bsubtotaljson[@"carton"] intValue];
  757. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  758. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  759. // NSDate *img_date = [NSDate date];
  760. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  761. // DebugLog(@"img_date time interval");
  762. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  763. itemjson[@"model"]=nsname;
  764. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  765. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  766. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  767. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  768. // itemjson[@"check"]=@"true";
  769. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  770. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  771. itemjson[@"unit_price"]=Price;
  772. itemjson[@"amount"]=Amount;
  773. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  774. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  775. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  776. // itemjson[@"note"]=nsline_note;
  777. // NSDate *date2 = [NSDate date];
  778. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  779. // DebugLog(@"model_bundle time interval");
  780. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  781. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  782. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  783. row1count++;
  784. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  785. for(int c=0;c<[combine[@"count"] intValue];c++)
  786. {
  787. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  788. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  789. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  790. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  791. credits+=combine_amount;
  792. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  793. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  794. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  795. row1count++;
  796. }
  797. count++;
  798. // DebugLog(@"row time interval");
  799. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  800. }
  801. row1[@"count"]=[NSNumber numberWithInt:row1count];
  802. datasource[@"row1"]=row1;
  803. data[@"table_model"]=datasource;
  804. sqlite3_finalize(statement_cart);
  805. }
  806. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  807. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  808. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  809. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  810. // if(credits>0)
  811. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  812. // else
  813. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  814. if(shipping>0)
  815. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  816. else
  817. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  818. if(lift_gate_value>0)
  819. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  820. else
  821. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  822. if(handling_fee_value>0)
  823. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  824. else
  825. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  826. float total = credits+shipping+lift_gate_value+handling_fee_value;
  827. if(total>0)
  828. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  829. else
  830. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  831. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  832. fee[@"row0"]=fee_row0;
  833. data[@"table_fee"]=fee;
  834. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  835. // ret[@"sign_url"] = sign_url;
  836. //
  837. // customer_contact = [self textAtColumn:52 statement:statement];
  838. // customer_email = [self textAtColumn:53 statement:statement];
  839. // customer_phone = [self textAtColumn:54 statement:statement];
  840. // customer_fax = [self textAtColumn:55 statement:statement];
  841. //
  842. //
  843. // int offline_edit=sqlite3_column_int(statement, 56);
  844. //
  845. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  846. //
  847. // char *soid = (char*)sqlite3_column_text(statement, 1);
  848. // if(soid==nil)
  849. // soid= "";
  850. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  851. // // so#
  852. // ret[@"so#"] = nssoid;
  853. //
  854. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  855. // if(poNumber==nil)
  856. // poNumber= "";
  857. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  858. //
  859. //
  860. //
  861. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  862. // if(create_time==nil)
  863. // create_time= "";
  864. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  865. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  866. //
  867. // int status = sqlite3_column_int(statement, 4);
  868. // int erpStatus = sqlite3_column_int(statement, 49);
  869. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  870. //
  871. // // status
  872. // if (status > 1 && status != 3) {
  873. // status = erpStatus;
  874. // } else if (status == 3) {
  875. // status = 15;
  876. // }
  877. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  878. // ret[@"order_status"] = nsstatus;
  879. //
  880. //
  881. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  882. // if(company_name==nil)
  883. // company_name= "";
  884. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  885. // // company name
  886. // ret[@"company_name"] = nscompany_name;
  887. //
  888. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  889. // if(customer_contact==nil)
  890. // customer_contact= "";
  891. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  892. //
  893. //
  894. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  895. // if(addr_1==nil)
  896. // addr_1="";
  897. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  898. //
  899. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  900. // if(addr_2==nil)
  901. // addr_2="";
  902. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  903. //
  904. //
  905. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  906. // if(addr_3==nil)
  907. // addr_3="";
  908. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  909. //
  910. //
  911. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  912. // if(addr_4==nil)
  913. // addr_4="";
  914. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  915. //
  916. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  917. // [arr_addr addObject:nsaddr_1];
  918. // [arr_addr addObject:nsaddr_2];
  919. // [arr_addr addObject:nsaddr_3];
  920. // [arr_addr addObject:nsaddr_4];
  921. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  922. //
  923. //
  924. // char *logist = (char*)sqlite3_column_text(statement, 11);
  925. // if(logist==nil)
  926. // logist= "";
  927. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  928. // if (status == -11 || status == 10 || status == 11) {
  929. // nslogist = [self textAtColumn:59 statement:statement];
  930. // };
  931. //
  932. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  933. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  934. // shipping = @"Shipping To Be Quoted";
  935. // } else {
  936. // shippingFee = sqlite3_column_double(statement, 12);
  937. // }
  938. //
  939. // // Shipping
  940. // ret[@"Shipping"] = shipping;
  941. //
  942. // int have_lift_gate = sqlite3_column_int(statement, 17);
  943. // lift_gate = sqlite3_column_double(statement, 13);
  944. // // Liftgate Fee(No loading dock)
  945. // if (!have_lift_gate) {
  946. // lift_gate = 0;
  947. // }
  948. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  949. // if (sqlite3_column_int(statement, 57)) {
  950. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  951. // }
  952. //
  953. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  954. // if(general_notes==nil)
  955. // general_notes= "";
  956. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  957. //
  958. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  959. // if(internal_notes==nil)
  960. // internal_notes= "";
  961. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  962. //
  963. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  964. // if(payment_type==nil)
  965. // payment_type= "";
  966. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  967. //
  968. //
  969. //
  970. // // order info
  971. // orderinfo = [self textFileName:@"order_info.html"];
  972. //
  973. //
  974. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  975. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  978. //
  979. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  987. //
  988. // NSString *payment = nil;
  989. // if([nspayment_type isEqualToString:@"Credit Card"])
  990. // {
  991. // payment = [self textFileName:@"creditcardpayment.html"];
  992. //
  993. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  994. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  995. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  996. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  997. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  998. // NSString *card_type = [self textAtColumn:42 statement:statement];
  999. // if (card_type.length > 0) { // 显示星号
  1000. // card_type = @"****";
  1001. // }
  1002. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1003. // if (card_number.length > 0 && card_number.length > 4) {
  1004. // for (int i = 0; i < card_number.length - 4; i++) {
  1005. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1006. // }
  1007. // } else {
  1008. // card_number = @"";
  1009. // }
  1010. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1011. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1012. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1013. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1014. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1015. // card_expiration = @"****";
  1016. // }
  1017. //
  1018. //
  1019. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1020. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1021. //
  1022. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1023. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1024. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1025. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1026. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1029. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1030. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1031. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1032. //
  1033. //
  1034. // }
  1035. // else
  1036. // {
  1037. // payment=[self textFileName:@"normalpayment.html"];
  1038. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1039. // }
  1040. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1041. //
  1042. // ret[@"result"]= [NSNumber numberWithInt:2];
  1043. //
  1044. // // more info
  1045. // moreInfo = [self textFileName:@"more_info.html"];
  1046. //
  1047. //
  1048. // // ShipToCompany_or_&nbsp
  1049. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1051. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1053. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1055. //
  1056. //
  1057. // // ShipFromCompany_or_&nbsp
  1058. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1060. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1062. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1064. //
  1065. //
  1066. // // FreightBillToCompany_or_&nbsp
  1067. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1069. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1071. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1073. //
  1074. //
  1075. // // MerchandiseBillToCompany_or_&nbsp
  1076. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1078. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1079. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1080. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1081. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1082. //
  1083. //
  1084. // // ReturnToCompany_or_&nbsp
  1085. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1086. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1087. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1088. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1089. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1090. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1091. // //
  1092. //
  1093. // // DebugLog(@"more info : %@",moreInfo);
  1094. //
  1095. // // handling fee
  1096. // handlingFee = sqlite3_column_double(statement, 33);
  1097. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1098. // if (sqlite3_column_int(statement, 58)) {
  1099. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1100. // }
  1101. // //
  1102. //
  1103. // // customer info
  1104. // customerID = [self textAtColumn:36 statement:statement];
  1105. //
  1106. // // mode
  1107. // ret[@"mode"] = appDelegate.mode;
  1108. //
  1109. // // model_count
  1110. // ret[@"model_count"] = @(0);
  1111. }
  1112. sqlite3_finalize(statement);
  1113. }
  1114. [iSalesDB close_db:db];
  1115. data[@"order_type"]=order_type;
  1116. /*
  1117. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1118. // "customer_email" = "Shui Hu";
  1119. // "customer_fax" = "";
  1120. // "customer_first_name" = F;
  1121. // "customer_last_name" = L;
  1122. // "customer_name" = ",da He Xiang Dong Liu A";
  1123. // "customer_phone" = "Hey Xuan Feng";
  1124. contactInfo[@"customer_phone"] = customer_phone;
  1125. contactInfo[@"customer_fax"] = customer_fax;
  1126. contactInfo[@"customer_email"] = customer_email;
  1127. NSString *first_name = @"";
  1128. NSString *last_name = @"";
  1129. if ([customer_contact isEqualToString:@""]) {
  1130. } else if ([customer_contact containsString:@" "]) {
  1131. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1132. first_name = [customer_contact substringToIndex:first_space_index];
  1133. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1134. }
  1135. contactInfo[@"customer_first_name"] = first_name;
  1136. contactInfo[@"customer_last_name"] = last_name;
  1137. ret[@"customerInfo"] = contactInfo;
  1138. // models
  1139. if (nssoid) {
  1140. __block double TotalCuft = 0;
  1141. __block double TotalWeight = 0;
  1142. __block int TotalCarton = 0;
  1143. __block double allItemPrice = 0;
  1144. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1145. sqlite3 *db1 = [iSalesDB get_db];
  1146. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1147. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1148. int product_id = sqlite3_column_int(stmt, 0);
  1149. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1150. int item_id = sqlite3_column_int(stmt, 7);
  1151. NSString* Price=nil;
  1152. if(str_price==nil)
  1153. {
  1154. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1155. if(price==nil)
  1156. Price=@"No Price.";
  1157. else
  1158. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1159. }
  1160. else
  1161. {
  1162. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1163. }
  1164. double discount = sqlite3_column_double(stmt, 2);
  1165. int item_count = sqlite3_column_int(stmt, 3);
  1166. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1167. NSString *nsline_note=nil;
  1168. if(line_note!=nil)
  1169. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1170. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1171. // NSString *nsname = nil;
  1172. // if(name!=nil)
  1173. // nsname= [[NSString alloc]initWithUTF8String:name];
  1174. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1175. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1176. // NSString *nsdescription=nil;
  1177. // if(description!=nil)
  1178. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1179. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1180. // int stockUom = sqlite3_column_int(stmt, 8);
  1181. // int _id = sqlite3_column_int(stmt, 9);
  1182. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1183. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1184. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1185. int carton=[bsubtotaljson[@"carton"] intValue];
  1186. TotalCuft += cuft;
  1187. TotalWeight += weight;
  1188. TotalCarton += carton;
  1189. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1190. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1191. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1192. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1193. itemjson[@"note"]=nsline_note;
  1194. itemjson[@"origin_price"] = Price;
  1195. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1196. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1198. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1199. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1200. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1201. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1202. if(itemjson[@"combine"] != nil)
  1203. {
  1204. // int citem=0;
  1205. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1206. for(int bc=0;bc<bcount;bc++)
  1207. {
  1208. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1209. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1210. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1211. subTotal += uprice * modulus * item_count;
  1212. }
  1213. }
  1214. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1215. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1216. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1217. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1218. allItemPrice += subTotal;
  1219. }];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1223. // payments/Credits
  1224. // payments_and_credist = sqlite3_column_double(statement, 34);
  1225. payments_and_credist = allItemPrice;
  1226. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1227. // // total
  1228. // totalPrice = sqlite3_column_double(statement, 35);
  1229. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1230. } else {
  1231. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1234. // payments/Credits
  1235. payments_and_credist = 0;
  1236. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1237. }
  1238. // total
  1239. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1240. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1241. ret[@"order_info"]= orderinfo;
  1242. ret[@"more_order_info"] = moreInfo;
  1243. return [RAConvertor dict2data:ret];
  1244. */
  1245. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1246. return data;
  1247. }
  1248. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1249. {
  1250. assert(params[@"user"]!=nil);
  1251. NSString* template_file=nil;
  1252. switch([params[@"thumb"] intValue])
  1253. {
  1254. case 0:
  1255. template_file= @"so.json";
  1256. break;
  1257. case 1:
  1258. template_file= @"so_thumb.json";
  1259. break;
  1260. default:
  1261. template_file= @"so.json";
  1262. }
  1263. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1264. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1265. if(data[@"table_model"][@"row2"]==nil)
  1266. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1267. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1268. DebugLog(@"%@",file);
  1269. // return nil;
  1270. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1271. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1272. if (file) {
  1273. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1274. [dic setObject:file forKey:@"pdf_path"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAConvertor dict2data:dic];
  1277. }
  1278. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1279. dic[@"isLocalFile"]=@"true";
  1280. return [RAConvertor dict2data:dic];
  1281. }
  1282. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1283. {
  1284. // assert(params[@"contact_id"]!=nil);
  1285. // assert(params[@"user"]!=nil);
  1286. if(params[@"user"]==nil)
  1287. {
  1288. AppDelegate *appDelegate = nil;
  1289. //some UI methods ej
  1290. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1291. if(appDelegate.user!=nil)
  1292. [params setValue:appDelegate.user forKey:@"user"];
  1293. }
  1294. NSString* template_file=nil;
  1295. switch([params[@"pdf_style"] intValue])
  1296. {
  1297. case 0:
  1298. template_file= @"portfolio_2x3.json";
  1299. break;
  1300. case 1:
  1301. template_file= @"portfolio_3x2.json";
  1302. break;
  1303. default:
  1304. template_file= @"portfolio_3x2.json";
  1305. }
  1306. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1307. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1308. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1309. DebugLog(@"%@",file);
  1310. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1311. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1312. if (file) {
  1313. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1314. [dic setObject:file forKey:@"pdf_path"];
  1315. dic[@"isLocalFile"]=@"true";
  1316. return [RAConvertor dict2data:dic];
  1317. }
  1318. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1319. dic[@"isLocalFile"]=@"true";
  1320. return [RAConvertor dict2data:dic];
  1321. }
  1322. +(NSString*) get_offline_soid:(sqlite3*)db
  1323. {
  1324. NSString* soid=nil;
  1325. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1326. [formatter setDateFormat:@"yyMMdd"];
  1327. NSString* date = [formatter stringFromDate:[NSDate date]];
  1328. for(int i=1;i<999;i++)
  1329. {
  1330. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1331. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1332. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1333. if(count==0)
  1334. return soid;
  1335. }
  1336. return nil;
  1337. }
  1338. +(NSArray*) enumOfflineOrder
  1339. {
  1340. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1341. sqlite3 *db = [iSalesDB get_db];
  1342. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1343. sqlite3_stmt * statement;
  1344. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1345. if ( dbresult== SQLITE_OK)
  1346. {
  1347. while (sqlite3_step(statement) == SQLITE_ROW)
  1348. {
  1349. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1350. NSString* nsso_id=nil;
  1351. if(so_id!=nil)
  1352. {
  1353. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1354. }
  1355. [ret addObject:nsso_id];
  1356. }
  1357. sqlite3_finalize(statement);
  1358. }
  1359. [iSalesDB close_db:db];
  1360. return ret;
  1361. }
  1362. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1363. {
  1364. if(submitlist==nil)
  1365. return;
  1366. NSString *userdir = [OLDataProvider getUserPath];
  1367. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1368. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1369. }
  1370. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1371. {
  1372. if(orderlist==nil)
  1373. return;
  1374. NSString *userdir = [OLDataProvider getUserPath];
  1375. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1376. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1377. }
  1378. +(NSMutableDictionary*) getScanSubmitList
  1379. {
  1380. NSString *userdir = [OLDataProvider getUserPath];
  1381. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1382. NSMutableDictionary* submitlist = nil;
  1383. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1384. submitlist = [NSMutableDictionary new];
  1385. }
  1386. else
  1387. {
  1388. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1389. }
  1390. return submitlist;
  1391. }
  1392. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1393. //// NSString *userdir = [self getUserPath:user];
  1394. // NSMutableDictionary* orderlist = nil;
  1395. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1396. //
  1397. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1398. //
  1399. //
  1400. // orderlist = [NSMutableDictionary new];
  1401. //
  1402. // // [orderlist addObject:appDelegate.order_code];
  1403. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1404. //
  1405. // }
  1406. // else
  1407. // {
  1408. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1409. //
  1410. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1411. //
  1412. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1413. // }
  1414. // return orderlist;
  1415. //}
  1416. +(NSMutableDictionary*) getScanOrderList{
  1417. NSString *userdir = [OLDataProvider getUserPath];
  1418. NSMutableDictionary* orderlist = nil;
  1419. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1420. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1421. orderlist = [NSMutableDictionary new];
  1422. // [orderlist addObject:appDelegate.order_code];
  1423. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1424. }
  1425. else
  1426. {
  1427. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1428. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1429. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1430. }
  1431. return orderlist;
  1432. }
  1433. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1434. {
  1435. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1436. // NSString *documents = [paths objectAtIndex:0];
  1437. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1438. // NSString *userdir = [OLDataProvider getUserPath];
  1439. NSMutableDictionary* orderlist = [self getScanOrderList];
  1440. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1441. int count = [orderlist[@"count"] intValue];
  1442. if(count==0)
  1443. {
  1444. // order list 为空,不用维护
  1445. #ifdef RA_NOTIFICATION
  1446. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1447. #else
  1448. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1449. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1450. #endif
  1451. return;
  1452. }
  1453. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1454. //
  1455. // // order list 为空,不用维护
  1456. //
  1457. //
  1458. // // [orderlist addObject:appDelegate.order_code];
  1459. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1460. //
  1461. // }
  1462. // else
  1463. // {
  1464. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1465. //
  1466. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1467. //
  1468. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1469. // }
  1470. //==================== order list 删除
  1471. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1472. NSMutableDictionary* submititem =nil;
  1473. int newcount = 0;
  1474. for(int i=0;i<count;i++)
  1475. {
  1476. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1477. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1478. {
  1479. submititem = item;
  1480. continue;
  1481. }
  1482. else
  1483. {
  1484. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1485. newcount++;
  1486. }
  1487. }
  1488. if(submititem==nil)
  1489. {
  1490. //order list 里没有和当前打开的order code一致的order
  1491. //这种情况往往是因为重复点提交按钮,order已关闭。
  1492. return;
  1493. }
  1494. newlist[@"count"] = @(newcount);
  1495. [self saveScanOrderList:newlist];
  1496. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1497. //////////////////---=============================== submitlist 添加
  1498. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1499. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1500. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1501. //
  1502. // submitlist = [NSMutableDictionary new];
  1503. //
  1504. //
  1505. // }
  1506. // else
  1507. // {
  1508. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1509. //
  1510. //
  1511. // }
  1512. int submitcount =[submitlist[@"count"] intValue];
  1513. submititem[@"order_status"] = soid;//@"Submitted Order";
  1514. submititem[@"customer_name"] = company;//@"Submitted Order";
  1515. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1516. submitlist[@"count"] = @(submitcount+1);
  1517. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1518. [self saveScanSubmitList:submitlist];
  1519. #ifdef RA_NOTIFICATION
  1520. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1521. #else
  1522. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1523. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1524. #endif
  1525. }
  1526. +(void) save2submitScanOrder1
  1527. {
  1528. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1529. // NSString *documents = [paths objectAtIndex:0];
  1530. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1531. // NSString *userdir = [OLDataProvider getUserPath];
  1532. NSMutableDictionary* orderlist = [self getScanOrderList];
  1533. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1534. int count = [orderlist[@"count"] intValue];
  1535. if(count==0)
  1536. {
  1537. // order list 为空,不用维护
  1538. #ifdef RA_NOTIFICATION
  1539. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1540. #else
  1541. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1542. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1543. #endif
  1544. return;
  1545. return;
  1546. }
  1547. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1548. int newcount = 0;
  1549. for(int i=0;i<count;i++)
  1550. {
  1551. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1552. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1553. continue;
  1554. else
  1555. {
  1556. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1557. newcount++;
  1558. }
  1559. }
  1560. newlist[@"count"] = @(newcount);
  1561. [self saveScanOrderList:newlist];
  1562. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1563. #ifdef RA_NOTIFICATION
  1564. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1565. #else
  1566. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1567. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1568. #endif
  1569. }
  1570. //+(void) createScanTempFolder
  1571. //{
  1572. // return;
  1573. //// NSString* scanFilePath = [self getScanTempPath];
  1574. //// BOOL bdir=YES;
  1575. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1576. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1577. //// {
  1578. ////
  1579. //// NSError *error = nil;
  1580. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1581. ////
  1582. //// if(!bsuccess)
  1583. //// DebugLog(@"Create cache folder failed");
  1584. ////
  1585. //// // if(bsuccess)
  1586. //// // {
  1587. //// // sqlite3 *db = [self get_db];
  1588. //// //
  1589. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1590. //// // [iSalesDB close_db:db];
  1591. //// // }
  1592. //// }
  1593. //}
  1594. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1595. {
  1596. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1597. NSString *documents = [paths objectAtIndex:0];
  1598. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1599. NSString* servername = addressDic[@"name"];
  1600. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1601. NSString* user = appDelegate.user;
  1602. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1603. orderpath = [orderpath stringByAppendingPathComponent:user];
  1604. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1605. BOOL bdir=YES;
  1606. NSFileManager* fileManager = [NSFileManager defaultManager];
  1607. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1608. {
  1609. NSError *error = nil;
  1610. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1611. if(!bsuccess)
  1612. DebugLog(@"Create cache folder failed");
  1613. // if(bsuccess)
  1614. // {
  1615. // sqlite3 *db = [self get_db];
  1616. //
  1617. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1618. // [iSalesDB close_db:db];
  1619. // }
  1620. }
  1621. return orderpath;
  1622. }
  1623. +(NSString*) getScanOrderPath
  1624. {
  1625. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1626. NSString *documents = [paths objectAtIndex:0];
  1627. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1628. NSString* servername = addressDic[@"name"];
  1629. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1630. NSString* user = appDelegate.user;
  1631. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1632. orderpath = [orderpath stringByAppendingPathComponent:user];
  1633. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1634. BOOL bdir=YES;
  1635. NSFileManager* fileManager = [NSFileManager defaultManager];
  1636. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1637. {
  1638. NSError *error = nil;
  1639. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1640. if(!bsuccess)
  1641. DebugLog(@"Create cache folder failed");
  1642. // if(bsuccess)
  1643. // {
  1644. // sqlite3 *db = [self get_db];
  1645. //
  1646. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1647. // [iSalesDB close_db:db];
  1648. // }
  1649. }
  1650. return orderpath;
  1651. }
  1652. //+(NSString*) getScanTempPath
  1653. //{
  1654. // return nil;
  1655. ////
  1656. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1657. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1658. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1659. //}
  1660. //+(void) moveScanTemp2Order
  1661. //{
  1662. // return;
  1663. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1664. ////
  1665. //// NSError *error = nil;
  1666. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1667. ////
  1668. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1669. //// // for(int i=0;i<arr_files.count;i++)
  1670. //// // {
  1671. //// // NSString* file=arr_files[i];
  1672. //// //
  1673. //// //
  1674. //// // }
  1675. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1676. ////
  1677. //
  1678. //}
  1679. //+(NSString*) getUserPath: (NSString*) user
  1680. //{
  1681. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1682. // NSString *documents = [paths objectAtIndex:0];
  1683. //
  1684. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1685. //
  1686. // NSString* servername = addressDic[@"name"];
  1687. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1688. //// NSString* user = appDelegate.user;
  1689. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1690. // userpath = [userpath stringByAppendingPathComponent:user];
  1691. // return userpath;
  1692. //}
  1693. +(NSString*) getServerPath
  1694. {
  1695. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1696. NSString *documents = [paths objectAtIndex:0];
  1697. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1698. NSString* servername = addressDic[@"name"];
  1699. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1700. // NSString* user = appDelegate.user;
  1701. // NSString* user = RASingleton.sharedInstance.user;
  1702. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1703. // userpath = [userpath stringByAppendingPathComponent:user];
  1704. return serverpath;
  1705. }
  1706. +(NSString*) getUserPath
  1707. {
  1708. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1709. NSString *documents = [paths objectAtIndex:0];
  1710. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1711. NSString* servername = addressDic[@"name"];
  1712. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1713. // NSString* user = appDelegate.user;
  1714. NSString* user = RASingleton.sharedInstance.user;
  1715. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1716. userpath = [userpath stringByAppendingPathComponent:user];
  1717. return userpath;
  1718. }
  1719. +(bool) isSavedScanOrder:(NSString*) order_code
  1720. {
  1721. if(order_code == nil)
  1722. return false;
  1723. NSMutableDictionary * orderlist=[self getScanOrderList];
  1724. int count = [orderlist[@"count"] intValue];
  1725. for(int i=0;i<count;i++)
  1726. {
  1727. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1728. if([item[@"order_code"] isEqualToString: order_code ])
  1729. {
  1730. return true;
  1731. }
  1732. }
  1733. return false;
  1734. }
  1735. +(NSString*) getScanPath
  1736. {
  1737. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1738. if(appDelegate.order_code==nil)
  1739. {
  1740. appDelegate.order_code=[[NSUUID new] UUIDString];
  1741. }
  1742. return [self getScanOrderPath];
  1743. // else
  1744. // {
  1745. // [self createScanTempFolder];
  1746. // return [self getScanTempPath];
  1747. // }
  1748. }
  1749. +(void) scanCloseOrder
  1750. {
  1751. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1752. {
  1753. // 当前订单先关闭
  1754. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1755. appDelegate.order_code = nil;
  1756. // RASingleton.sharedInstance.scan_temp_code = nil;
  1757. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1758. { appDelegate.customerInfo = nil;
  1759. appDelegate.contact_id = nil;
  1760. }
  1761. RASingleton.sharedInstance.scan_cart = nil;
  1762. RASingleton.sharedInstance.price_type = 1;
  1763. // [appDelegate updateScanButton:false];
  1764. [appDelegate update_count_mark];
  1765. [appDelegate closeOrder];
  1766. }
  1767. }
  1768. +(NSString*) createScanTempCode
  1769. {
  1770. return [[NSUUID new] UUIDString];
  1771. }
  1772. +(NSString*) scanTemplatePath:(NSString*)file
  1773. {
  1774. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1775. NSString *documents = [paths objectAtIndex:0];
  1776. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1777. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1778. return templatefile;
  1779. }
  1780. +(void) realguestLogin
  1781. {
  1782. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1783. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1784. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1785. }
  1786. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1787. {
  1788. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1789. NSString *documents = [paths objectAtIndex:0];
  1790. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1791. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1792. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1793. return [dict mutableCopy];
  1794. }
  1795. +(void) deleteSubmittedOrder:(NSString*) order_code
  1796. {
  1797. NSString *userdir = [OLDataProvider getUserPath];
  1798. if(order_code.length==0)
  1799. return;
  1800. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1801. // NSString *documents = [paths objectAtIndex:0];
  1802. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1803. // if([appDelegate.order_code isEqualToString:order_code])
  1804. // {
  1805. // //要删除的订单是当前打开的订单;
  1806. // appDelegate.order_code = nil;
  1807. // appDelegate.customerInfo = nil;
  1808. // RASingleton.sharedInstance.scan_cart = nil;
  1809. // appDelegate.contact_id = nil;
  1810. // // [appDelegate updateScanButton:false];
  1811. //
  1812. // [appDelegate update_count_mark];
  1813. // }
  1814. //删除订单目录
  1815. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1816. //维护订单列表
  1817. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1818. int count = [submitlist[@"count"] intValue];
  1819. if(count==0)
  1820. {
  1821. // order list 为空,不用维护
  1822. #ifdef RA_NOTIFICATION
  1823. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1824. #else
  1825. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1826. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1827. #endif
  1828. return;
  1829. }
  1830. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1831. //
  1832. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1833. //
  1834. //
  1835. //
  1836. // // [orderlist addObject:appDelegate.order_code];
  1837. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1838. //
  1839. // }
  1840. // else
  1841. // {
  1842. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1843. //
  1844. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1845. //
  1846. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1847. // }
  1848. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1849. int newcount = 0;
  1850. for(int i=0;i<count;i++)
  1851. {
  1852. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1853. if([item[@"order_code"] isEqualToString: order_code ])
  1854. continue;
  1855. else
  1856. {
  1857. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1858. newcount++;
  1859. }
  1860. }
  1861. newlist[@"count"] = @(newcount);
  1862. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1863. [self saveScanSubmitList:newlist];
  1864. #ifdef RA_NOTIFICATION
  1865. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1866. #else
  1867. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1868. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1869. #endif
  1870. }
  1871. +(void) updateScanOrder:(NSString*) order_code
  1872. {
  1873. #ifdef SCANNER_ORDER
  1874. NSString *userdir = [OLDataProvider getUserPath];
  1875. if(order_code.length==0)
  1876. return;
  1877. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1878. // NSString *documents = [paths objectAtIndex:0];
  1879. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1880. NSMutableDictionary* orderlist = [self getScanOrderList];
  1881. int count = [orderlist[@"count"] intValue];
  1882. if(count==0)
  1883. {
  1884. // order list 为空,不用维护
  1885. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1886. return;
  1887. }
  1888. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1889. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1890. //
  1891. //
  1892. //
  1893. // // [orderlist addObject:appDelegate.order_code];
  1894. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1895. //
  1896. // }
  1897. // else
  1898. // {
  1899. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1900. //
  1901. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1902. //
  1903. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1904. // }
  1905. //
  1906. //
  1907. // int count = [orderlist[@"count"] intValue];
  1908. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1909. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1910. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1911. float p =0;
  1912. float s =0;
  1913. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1914. {
  1915. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1916. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1917. int mcount = [cartitem[@"count"] intValue];
  1918. double msurcharge =0;
  1919. // remove net price
  1920. // if(RASingleton.sharedInstance.price_type==1)
  1921. // {
  1922. // if([cartitem[@"special_price"] boolValue])
  1923. // {
  1924. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1925. // }
  1926. // else
  1927. // {
  1928. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1929. // }
  1930. // }
  1931. s+=msurcharge;
  1932. }
  1933. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1934. int order_qty=0;
  1935. for(int c=0;c<[section[@"count"] intValue];c++)
  1936. {
  1937. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1938. order_qty+=[item[@"count"] intValue];
  1939. }
  1940. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1941. // int newcount = 0;
  1942. for(int i=0;i<count;i++)
  1943. {
  1944. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1945. if([item[@"order_code"] isEqualToString: order_code ])
  1946. {
  1947. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1948. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1949. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1950. }
  1951. }
  1952. // newlist[@"count"] = @(newcount);
  1953. [self saveScanOrderList:orderlist];
  1954. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1955. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1956. #endif
  1957. }
  1958. +(void) deleteScanOrder:(NSString*) order_code
  1959. {
  1960. NSString *userdir = [OLDataProvider getUserPath];
  1961. if(order_code.length==0)
  1962. return;
  1963. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1964. // NSString *documents = [paths objectAtIndex:0];
  1965. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1966. if([appDelegate.order_code isEqualToString:order_code])
  1967. {
  1968. //要删除的订单是当前打开的订单;
  1969. // appDelegate.order_code = nil;
  1970. // appDelegate.customerInfo = nil;
  1971. // RASingleton.sharedInstance.scan_cart = nil;
  1972. // appDelegate.contact_id = nil;
  1973. [self scanCloseOrder];
  1974. // [appDelegate updateScanButton:false];
  1975. [appDelegate update_count_mark];
  1976. }
  1977. //删除订单目录
  1978. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1979. //维护订单列表
  1980. NSMutableDictionary* orderlist = [self getScanOrderList];
  1981. int count = [orderlist[@"count"] intValue];
  1982. if(count==0)
  1983. {
  1984. // order list 为空,不用维护
  1985. #ifdef RA_NOTIFICATION
  1986. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1987. #else
  1988. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1989. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1990. #endif
  1991. return;
  1992. }
  1993. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1994. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1995. //
  1996. //
  1997. //
  1998. // // [orderlist addObject:appDelegate.order_code];
  1999. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2000. //
  2001. // }
  2002. // else
  2003. // {
  2004. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2005. //
  2006. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2007. //
  2008. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2009. // }
  2010. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2011. int newcount = 0;
  2012. for(int i=0;i<count;i++)
  2013. {
  2014. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2015. if([item[@"order_code"] isEqualToString: order_code ])
  2016. continue;
  2017. else
  2018. {
  2019. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2020. newcount++;
  2021. }
  2022. }
  2023. newlist[@"count"] = @(newcount);
  2024. [self saveScanOrderList:newlist];
  2025. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2026. #ifdef RA_NOTIFICATION
  2027. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2028. #else
  2029. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2030. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2031. #endif
  2032. }
  2033. #ifdef SCANNER_ORDER
  2034. +(void) scanRefreshCart
  2035. {
  2036. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2037. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2038. NSMutableDictionary * section = cart[@"section_0"];
  2039. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2040. int count = [section[@"count"] intValue];
  2041. int newcount = 0;
  2042. for(int i=0;i<count;i++)
  2043. {
  2044. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2045. NSString* model = item[@"model"];
  2046. NSDictionary* modeljson=appDelegate.scan_model[model];
  2047. if(modeljson==nil)
  2048. continue;
  2049. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2050. cartitem[@"note"]=item[@"note"];
  2051. cartitem[@"discount"]=item[@"discount"];
  2052. // [item removeObjectForKey:@"discount"];
  2053. // [item removeObjectForKey:@"note"];
  2054. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2055. newcount++;
  2056. }
  2057. newsection[@"count"]=@(newcount);
  2058. newsection[@"available"] = @(1);
  2059. newsection[@"title"] = @"Available List";
  2060. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2061. }
  2062. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2063. {
  2064. // _modelJson = modelJson;
  2065. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2066. // self.labelPrice0.text = appDelegate.price0_name;
  2067. // self.labelPrice1.text = appDelegate.price1_name;
  2068. // self.labelPrice2.text = appDelegate.price2_name;
  2069. // self.labelPrice3.text = appDelegate.price3_name;
  2070. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2071. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2072. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2073. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2074. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2075. NSLog( [RAConvertor dict2string:_modelJson]);
  2076. NSString * port=_modelJson[@"port"];
  2077. if(port.length==0)
  2078. port = @"N/A";
  2079. NSString * origin=_modelJson[@"origin"];
  2080. if(origin.length==0)
  2081. origin = @"N/A";
  2082. NSString * dimension=_modelJson[@"dimension"];
  2083. if(dimension.length==0)
  2084. dimension = @"N/A";
  2085. // _labelModel.text=_modelJson[@"model"];
  2086. // _labelDescription.text=_modelJson[@"description"];
  2087. // _labelDimension.text=dimension;
  2088. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2089. // _labelOrigin.text=origin;
  2090. // _labelPort.text=port;
  2091. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2092. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2093. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2094. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2095. if(![price0 isEqualToString: @"N/A"])
  2096. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2097. if(![price1 isEqualToString: @"N/A"])
  2098. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2099. if(![price2 isEqualToString: @"N/A"])
  2100. {
  2101. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2102. // if(RASingleton.sharedInstance.price_type==1)
  2103. _modelJson [@"special_price"] = @true;
  2104. }
  2105. if(![price3 isEqualToString: @"N/A"])
  2106. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2107. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2108. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2109. {
  2110. // if(RASingleton.sharedInstance.price_type==1)
  2111. _modelJson [@"net_price"] = @true;
  2112. // remove net price
  2113. _modelJson [@"net_price"] = @false;
  2114. }
  2115. //
  2116. // if(![price1 isEqualToString:@"N/A"])
  2117. // {
  2118. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2119. // _modelJson[@"price2"]= price2;
  2120. // }
  2121. // else
  2122. // {
  2123. // price2=@"N/A";
  2124. // }
  2125. //
  2126. // if([price0 isEqualToString:@"N/A"])
  2127. // _labelPriceCTNR.text= @"";//price0;
  2128. // else
  2129. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2130. //
  2131. // if([price3 isEqualToString:@"N/A"])
  2132. // _labelPriceNCA.text= price3;
  2133. // else
  2134. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2135. // _labelPrice25p.text=price2;
  2136. //
  2137. // if([_modelJson [@"special_price"] boolValue])
  2138. // {
  2139. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2140. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2141. // else
  2142. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2143. // }
  2144. // else
  2145. // {
  2146. // _labelPriceSpecial.text=@"";//@"N/A";
  2147. // }
  2148. //
  2149. // if([_modelJson [@"net_price"] boolValue])
  2150. // {
  2151. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2152. //// {
  2153. //// _labelPriceNet.text=_modelJson[@"price1"];
  2154. ////// _labelPriceNCA.text=@"N/A";
  2155. //// }
  2156. //// else
  2157. //// {
  2158. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2159. ////// _labelPriceNCA.text=@"N/A";
  2160. //// }
  2161. //
  2162. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2163. // {
  2164. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2165. //// _labelPriceNCA.text=@"N/A";
  2166. // }
  2167. // else
  2168. // {
  2169. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2170. //// _labelPriceNCA.text=@"N/A";
  2171. // }
  2172. //
  2173. // }
  2174. // else
  2175. // {
  2176. // _labelPriceNet.text=@"";//@"N/A";
  2177. // }
  2178. //
  2179. // {
  2180. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2181. // {
  2182. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2183. //// _labelPriceNCA.text=@"N/A";
  2184. // }
  2185. // else
  2186. // {
  2187. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2188. //// _labelPriceNet.text=@"N/A";
  2189. // }
  2190. // }
  2191. NSString * available=_modelJson[@"available"];
  2192. if(available.length==0)
  2193. available = @"N/A";
  2194. // _labelAvailable.text=available;
  2195. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2196. if(unit_cuft.length==0)
  2197. unit_cuft = @"N/A";
  2198. else
  2199. {
  2200. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2201. }
  2202. NSString * cell_price;
  2203. if(RASingleton.sharedInstance.price_type==1)
  2204. {
  2205. // if(_modelJson [@"net_price"])
  2206. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2207. // else if(_modelJson [@"special_price"])
  2208. // {
  2209. // unit_price= _modelJson[@"price2"];
  2210. // }
  2211. // else
  2212. // {
  2213. // unit_price= _modelJson[@"price1"];
  2214. // }
  2215. }
  2216. else
  2217. {
  2218. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2219. }
  2220. NSString * mpack=_modelJson[@"stockUom"];
  2221. if([mpack isEqualToString:@"N/A"])
  2222. mpack=@"1";
  2223. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2224. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2225. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2226. NSMutableDictionary* jitem = nil;
  2227. jitem = [_modelJson mutableCopy];
  2228. int stockUom =[jitem[@"stockUom"] intValue];
  2229. if(stockUom==0)
  2230. stockUom=1;
  2231. bool newitem = true;
  2232. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2233. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2234. // for(int i=0;i<count;i++)
  2235. // {
  2236. //
  2237. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2238. //
  2239. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2240. // {
  2241. //
  2242. //// int oldcount = [litem[@"stockUom"] intValue];
  2243. // newitem = false;
  2244. //
  2245. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2246. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2247. //
  2248. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2249. // break;
  2250. //
  2251. //
  2252. // }
  2253. //
  2254. //
  2255. //
  2256. // }
  2257. // if(true)
  2258. // {
  2259. jitem[@"count"]=@(stockUom);
  2260. jitem[@"check"]=@(true);
  2261. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2262. NSString * unit_price;
  2263. if(RASingleton.sharedInstance.price_type==0)
  2264. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2265. else if(_modelJson [@"special_price"])
  2266. {
  2267. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2268. }
  2269. else if(_modelJson [@"net_price"])
  2270. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2271. else
  2272. {
  2273. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2274. }
  2275. if([unit_price isEqualToString:@"N/A"])
  2276. unit_price = @"0";
  2277. else
  2278. {
  2279. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2280. }
  2281. jitem[@"unit_price"] = unit_price;
  2282. jitem[@"erp_unit_price"] = unit_price;
  2283. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2284. // section[@"count"]= @(count+1);
  2285. // count++;
  2286. // }
  2287. return jitem;
  2288. }
  2289. +(void) saveScanCart:(NSMutableDictionary*) cart
  2290. {
  2291. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2292. if(appDelegate.order_code.length<=0)
  2293. return;
  2294. if(cart==nil)
  2295. return;
  2296. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2297. // NSString *documents = [paths objectAtIndex:0];
  2298. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2299. if(RASingleton.sharedInstance.price_type==0)
  2300. {
  2301. NSMutableDictionary * section = cart[@"section_0"];
  2302. int count = [section[@"count"] intValue];
  2303. for(int i=0;i<count;i++)
  2304. {
  2305. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2306. [item removeObjectForKey:@"discount"];
  2307. [item removeObjectForKey:@"note"];
  2308. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2309. }
  2310. cart[@"section_0"]=section;
  2311. }
  2312. NSString *orderdir = [self getScanPath];
  2313. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2314. cart[@"price_group"] = appDelegate.price_group;
  2315. [RAUtils dicttofile:cartpath dict:cart];
  2316. }
  2317. #endif
  2318. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2319. {
  2320. assert(add_params[@"user"]!=nil);
  2321. // assert(add_params[@"contact_id"]!=nil);
  2322. // assert(add_params[@"password"]!=nil);
  2323. NSString* serial= [[NSUUID UUID] UUIDString];
  2324. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2325. NSString *cachefolder = [paths objectAtIndex:0];
  2326. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2327. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2328. NSFileManager* fileManager = [NSFileManager defaultManager];
  2329. BOOL bdir=YES;
  2330. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2331. {
  2332. NSError *error = nil;
  2333. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2334. if(!bsuccess)
  2335. DebugLog(@"Create UPLOAD folder failed");
  2336. }
  2337. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2338. ret[@"contact"]=[self prepareContact:serial];
  2339. ret[@"wishlist"]=[self prepareWishlist:serial];
  2340. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2341. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2342. ret[@"view_portfolio"] = [self preparePDF:serial];
  2343. NSString* str= [RAConvertor dict2string:ret];
  2344. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2345. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2346. NSError *error=nil;
  2347. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2348. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2349. ZipArchive* zip = [[ZipArchive alloc] init];
  2350. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2351. for(int i=0;i<arr_files.count;i++)
  2352. {
  2353. NSString* file=arr_files[i];
  2354. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2355. }
  2356. if( ![zip CloseZipFile2] )
  2357. {
  2358. zippath = @"";
  2359. }
  2360. ret[@"file"]=zippath;
  2361. return ret;
  2362. }
  2363. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2364. {
  2365. if(filename.length==0)
  2366. return false;
  2367. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2368. bool ret=false;
  2369. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2370. NSString *cachefolder = [paths objectAtIndex:0];
  2371. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2372. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2373. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2374. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2375. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2376. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2377. NSFileManager* fileManager = [NSFileManager defaultManager];
  2378. BOOL bdir=NO;
  2379. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2380. {
  2381. NSError *error = nil;
  2382. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2383. {
  2384. ret=false;
  2385. }
  2386. else
  2387. {
  2388. ret=true;
  2389. }
  2390. }
  2391. return ret;
  2392. }
  2393. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2394. {
  2395. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2396. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2397. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2398. NSString* where=@" 1=1";
  2399. if (ver!=nil) {
  2400. where=@"is_dirty=1";
  2401. }
  2402. sqlite3 *db = [iSalesDB get_db];
  2403. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2404. sqlite3_stmt * statement;
  2405. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2406. int count=0;
  2407. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2408. if ( dbresult== SQLITE_OK)
  2409. {
  2410. while (sqlite3_step(statement) == SQLITE_ROW)
  2411. {
  2412. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2413. int _id = sqlite3_column_int(statement, 0);
  2414. int product_id = sqlite3_column_int(statement, 1);
  2415. int item_id = sqlite3_column_int(statement, 2);
  2416. int qty = sqlite3_column_int(statement, 3);
  2417. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2418. NSString* nscreate_time=nil;
  2419. if(create_time!=nil)
  2420. {
  2421. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2422. }
  2423. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2424. NSString* nsmodify_time=nil;
  2425. if(modify_time!=nil)
  2426. {
  2427. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2428. }
  2429. int is_delete = sqlite3_column_int(statement, 6);
  2430. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2431. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2432. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2433. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2434. itemjson[@"create_time"]=nscreate_time;
  2435. itemjson[@"modify_time"]=nsmodify_time;
  2436. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2437. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2438. count++;
  2439. }
  2440. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2441. sqlite3_finalize(statement);
  2442. }
  2443. ret[@"count"]=[NSNumber numberWithInt:count ];
  2444. [iSalesDB close_db:db];
  2445. return ret;
  2446. }
  2447. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2448. {
  2449. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2450. // UIApplication * app = [UIApplication sharedApplication];
  2451. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2452. assert(add_params[@"user"]!=nil);
  2453. // assert(add_params[@"password"]!=nil);
  2454. 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 ];
  2455. // 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 ];
  2456. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2457. DebugLog(@"offline_login sql:%@",sqlQuery);
  2458. sqlite3_stmt * statement;
  2459. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2460. int count=0;
  2461. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2462. if ( dbresult== SQLITE_OK)
  2463. {
  2464. while (sqlite3_step(statement) == SQLITE_ROW)
  2465. {
  2466. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2467. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2468. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2469. int product_id = sqlite3_column_int(statement, 0);
  2470. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2471. int item_id = sqlite3_column_int(statement, 7);
  2472. NSString* Price=nil;
  2473. if(str_price==nil)
  2474. {
  2475. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2476. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2477. if(price==nil)
  2478. Price=@"No Price.";
  2479. else
  2480. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2481. }
  2482. else
  2483. {
  2484. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2485. }
  2486. double discount = sqlite3_column_double(statement, 2);
  2487. int item_count = sqlite3_column_int(statement, 3);
  2488. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2489. NSString *nsline_note=nil;
  2490. if(line_note!=nil)
  2491. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2492. char *name = (char*)sqlite3_column_text(statement, 5);
  2493. NSString *nsname=nil;
  2494. if(name!=nil)
  2495. nsname= [[NSString alloc]initWithUTF8String:name];
  2496. char *description = (char*)sqlite3_column_text(statement, 6);
  2497. NSString *nsdescription=nil;
  2498. if(description!=nil)
  2499. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2500. // int stockUom = sqlite3_column_int(statement, 8);
  2501. // int _id = sqlite3_column_int(statement, 9);
  2502. //
  2503. //
  2504. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2505. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2506. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2507. // int carton=[bsubtotaljson[@"carton"] intValue];
  2508. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2509. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2510. itemjson[@"model"]=nsname;
  2511. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2512. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2513. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2514. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2515. // itemjson[@"check"]=@"true";
  2516. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2517. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2518. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2519. itemjson[@"unit_price"]=Price;
  2520. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2521. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2522. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2523. itemjson[@"note"]=nsline_note;
  2524. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2525. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2526. count++;
  2527. }
  2528. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2529. sqlite3_finalize(statement);
  2530. }
  2531. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2532. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2533. return ret;
  2534. }
  2535. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2536. {
  2537. assert(add_params[@"user"]!=nil);
  2538. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2539. sqlite3 *db = [iSalesDB get_db];
  2540. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id,discount from offline_order where order_id is null";
  2541. // for(int i=0;i<arr_soid.count;i++)
  2542. sqlite3_stmt * statement;
  2543. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2544. //int count=0;
  2545. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2546. if ( dbresult== SQLITE_OK)
  2547. {
  2548. int count=0;
  2549. while (sqlite3_step(statement) == SQLITE_ROW)
  2550. {
  2551. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2552. int _id = sqlite3_column_int(statement, 0);
  2553. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2554. NSString* nssync_data=nil;
  2555. if(sync_data!=nil)
  2556. {
  2557. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2558. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2559. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2560. }
  2561. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2562. NSString* nsimg_1=nil;
  2563. if(img_1!=nil)
  2564. {
  2565. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2566. [self copy_upImg:serial file:nsimg_1];
  2567. }
  2568. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2569. NSString* nsso_no=nil;
  2570. if(so_no!=nil)
  2571. {
  2572. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2573. }
  2574. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2575. itemjson[@"order_type"]=@"submit order";
  2576. else
  2577. itemjson[@"order_type"]=@"archive order";
  2578. double discount =sqlite3_column_double(statement, 4);
  2579. itemjson[@"discount"]=@(discount);
  2580. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2581. itemjson[@"json_data"]= nssync_data;
  2582. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2583. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2584. count++;
  2585. }
  2586. ret[@"count"]=[NSNumber numberWithInt:count ];
  2587. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2588. }
  2589. sqlite3_finalize(statement);
  2590. [iSalesDB close_db:db];
  2591. return ret;
  2592. }
  2593. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2594. {
  2595. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2596. sqlite3 *db = [iSalesDB get_db];
  2597. // UIApplication * app = [UIApplication sharedApplication];
  2598. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2599. 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";
  2600. sqlite3_stmt * statement;
  2601. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2602. int count=0;
  2603. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2604. if ( dbresult== SQLITE_OK)
  2605. {
  2606. while (sqlite3_step(statement) == SQLITE_ROW)
  2607. {
  2608. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2609. int _id = sqlite3_column_int(statement, 0);
  2610. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2611. NSString* nsimg_0=nil;
  2612. if(img_0!=nil)
  2613. {
  2614. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2615. [self copy_upImg:serial file:nsimg_0];
  2616. }
  2617. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2618. NSString* nsimg_1=nil;
  2619. if(img_1!=nil)
  2620. {
  2621. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2622. [self copy_upImg:serial file:nsimg_1];
  2623. }
  2624. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2625. NSString* nsimg_2=nil;
  2626. if(img_2!=nil)
  2627. {
  2628. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2629. [self copy_upImg:serial file:nsimg_2];
  2630. }
  2631. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2632. NSString* nssync_data=nil;
  2633. if(sync_data!=nil)
  2634. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2635. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2636. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2637. itemjson[@"json_data"]= nssync_data;
  2638. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2639. count++;
  2640. }
  2641. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2642. sqlite3_finalize(statement);
  2643. }
  2644. ret[@"count"]=[NSNumber numberWithInt:count ];
  2645. [iSalesDB close_db:db];
  2646. return ret;
  2647. }
  2648. +(bool) check_offlinedata
  2649. {
  2650. // UIApplication * app = [UIApplication sharedApplication];
  2651. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2652. sqlite3 *db = [iSalesDB get_db];
  2653. NSString * where=@"1=1";
  2654. // if(appDelegate.user!=nil)
  2655. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2656. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2657. [iSalesDB close_db:db];
  2658. if(count==0)
  2659. {
  2660. return false;
  2661. }
  2662. return true;
  2663. //
  2664. //[iSalesDB close_db:db];
  2665. }
  2666. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2667. {
  2668. // UIApplication * app = [UIApplication sharedApplication];
  2669. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2670. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2671. sqlite3 *db = [iSalesDB get_db];
  2672. NSString* collectId=params[@"collectId"];
  2673. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2674. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2675. [iSalesDB execSql:sqlQuery db:db];
  2676. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2677. [iSalesDB close_db:db];
  2678. // appDelegate.wish_count =count;
  2679. //
  2680. // [appDelegate update_count_mark];
  2681. ret[@"result"]= [NSNumber numberWithInt:2];
  2682. ret[@"wish_count"]=@(count);
  2683. return ret;
  2684. }
  2685. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2686. //{
  2687. //
  2688. // UIApplication * app = [UIApplication sharedApplication];
  2689. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2690. //
  2691. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2692. // sqlite3 *db = [iSalesDB get_db];
  2693. // NSString* product_id=params[@"product_id"];
  2694. //
  2695. //
  2696. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2697. //
  2698. //
  2699. // for(int i=0;i<arr.count;i++)
  2700. // {
  2701. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2702. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2703. // if(count==0)
  2704. // {
  2705. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2706. // [iSalesDB execSql:sqlQuery db:db];
  2707. // }
  2708. // }
  2709. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2710. // [iSalesDB close_db:db];
  2711. //
  2712. // appDelegate.wish_count =count;
  2713. //
  2714. // [appDelegate update_count_mark];
  2715. // ret[@"result"]= [NSNumber numberWithInt:2];
  2716. // return ret;
  2717. // //
  2718. // //return ret;
  2719. //}
  2720. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2721. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2722. DebugLog(@"time interval: %lf",interval);
  2723. }
  2724. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2725. //{
  2726. // UIApplication * app = [UIApplication sharedApplication];
  2727. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2728. //
  2729. // int sort = [[params objectForKey:@"sort"] intValue];
  2730. // NSString *sort_str = @"";
  2731. // switch (sort) {
  2732. // case 0:{
  2733. // sort_str = @"order by w.modify_time desc";
  2734. // }
  2735. // break;
  2736. // case 1:{
  2737. // sort_str = @"order by w.modify_time asc";
  2738. // }
  2739. // break;
  2740. // case 2:{
  2741. // sort_str = @"order by m.name asc";
  2742. // }
  2743. // break;
  2744. // case 3:{
  2745. // sort_str = @"order by m.name desc";
  2746. // }
  2747. // break;
  2748. // case 4:{
  2749. // sort_str = @"order by m.description asc";
  2750. // }
  2751. // break;
  2752. //
  2753. // default:
  2754. // break;
  2755. //
  2756. // }
  2757. //
  2758. //
  2759. //// NSString* user = appDelegate.user;
  2760. //
  2761. // sqlite3 *db = [iSalesDB get_db];
  2762. //
  2763. // // order by w.create_time
  2764. // 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];
  2765. //
  2766. //// 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];
  2767. //
  2768. //
  2769. // sqlite3_stmt * statement;
  2770. //
  2771. // NSDate *date1 = [NSDate date];
  2772. //// NSDate *date2 = nil;
  2773. //
  2774. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2775. // int count=0;
  2776. //
  2777. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2778. // {
  2779. //
  2780. //
  2781. // while (sqlite3_step(statement) == SQLITE_ROW)
  2782. // {
  2783. //
  2784. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2785. //
  2786. // int product_id = sqlite3_column_double(statement, 0);
  2787. //
  2788. //
  2789. //
  2790. //
  2791. //
  2792. // char *description = (char*)sqlite3_column_text(statement, 1);
  2793. // if(description==nil)
  2794. // description= "";
  2795. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2796. //
  2797. // int item_id = sqlite3_column_double(statement, 2);
  2798. //
  2799. // NSDate *date_image = [NSDate date];
  2800. //
  2801. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2802. //
  2803. // printf("image : ");
  2804. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2805. //
  2806. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2807. // // if(url==nil)
  2808. // // url="";
  2809. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2810. //
  2811. // int qty = sqlite3_column_int(statement, 3);
  2812. //
  2813. //
  2814. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2815. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2816. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2817. // item[@"description"]= nsdescription;
  2818. // item[@"img"]= nsurl;
  2819. //
  2820. //
  2821. //
  2822. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2823. // count++;
  2824. //
  2825. // }
  2826. // printf("total time:");
  2827. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2828. //
  2829. // ret[@"count"]= [NSNumber numberWithInt:count];
  2830. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2831. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2832. // ret[@"result"]= [NSNumber numberWithInt:2];
  2833. //
  2834. //
  2835. // appDelegate.wish_count =count;
  2836. //
  2837. // [appDelegate update_count_mark];
  2838. // sqlite3_finalize(statement);
  2839. //
  2840. //
  2841. //
  2842. //
  2843. // }
  2844. //
  2845. // [iSalesDB close_db:db];
  2846. //
  2847. // return ret;
  2848. //}
  2849. +(NSDictionary*) offline_notimpl
  2850. {
  2851. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2852. ret[@"result"]=@"8";
  2853. ret[@"err_msg"]=@"offline mode does not support this function.";
  2854. return ret;
  2855. }
  2856. +(NSDictionary*) offline_home
  2857. {
  2858. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2859. NSString *cachefolder = [paths objectAtIndex:0];
  2860. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2861. NSData* json =nil;
  2862. json=[NSData dataWithContentsOfFile:img_cache];
  2863. if(json==nil)
  2864. return nil;
  2865. NSError *error=nil;
  2866. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2867. return menu;
  2868. }
  2869. +(NSDictionary*) offline_category_menu
  2870. {
  2871. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2872. NSString *cachefolder = [paths objectAtIndex:0];
  2873. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2874. NSData* json =nil;
  2875. json=[NSData dataWithContentsOfFile:img_cache];
  2876. if(json==nil)
  2877. return nil;
  2878. NSError *error=nil;
  2879. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2880. return menu;
  2881. }
  2882. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2883. {
  2884. NSString* offline_command=params[@"offline_Command"];
  2885. NSDictionary* ret=nil;
  2886. if([offline_command isEqualToString:@"model_NIYMAL"])
  2887. {
  2888. NSString* category = params[@"category"];
  2889. ret = [self refresh_model_NIYMAL:category];
  2890. }
  2891. return ret;
  2892. }
  2893. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2894. {
  2895. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2896. [ret setValue:@"2" forKey:@"result"];
  2897. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2898. sqlite3* db= [iSalesDB get_db];
  2899. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2900. [iSalesDB close_db:db];
  2901. [ret setObject:detail1_section forKey:@"detail_1"];
  2902. return ret;
  2903. }
  2904. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2905. {
  2906. //assert(user!=nil);
  2907. // UIApplication * app = [UIApplication sharedApplication];
  2908. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2909. // NSArray* arr1 = [self get_user_all_price_type];
  2910. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2911. // NSSet *set1 = [NSSet setWithArray:arr1];
  2912. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2913. // if(appDelegate.contact_id==nil)
  2914. // set2=[set1 mutableCopy];
  2915. // else
  2916. // [set2 intersectsSet:set1];
  2917. // NSArray *retarr = [set2 allObjects];
  2918. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2919. // sqlite3 *db = [iSalesDB get_db];
  2920. NSString* sqlQuery = nil;
  2921. if(contact_id==nil)
  2922. {
  2923. if(!blogin)
  2924. return nil;
  2925. 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];
  2926. }
  2927. else
  2928. 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];
  2929. sqlite3_stmt * statement;
  2930. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2931. int count=0;
  2932. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2933. {
  2934. while (sqlite3_step(statement) == SQLITE_ROW)
  2935. {
  2936. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2937. char *name = (char*)sqlite3_column_text(statement, 0);
  2938. if(name==nil)
  2939. name="";
  2940. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2941. // double price = sqlite3_column_double(statement, 1);
  2942. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2943. // if(isnull==nil)
  2944. // item[nsname]= @"No Price";
  2945. // else
  2946. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2947. char *price = (char*)sqlite3_column_text(statement, 1);
  2948. if(price!=nil)
  2949. {
  2950. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2951. nsprice=[AESCrypt fastdecrypt:nsprice];
  2952. if(nsprice.length>0)
  2953. {
  2954. double dp= [nsprice doubleValue];
  2955. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2956. }
  2957. }
  2958. else
  2959. {
  2960. item[nsname]= @"No Price";
  2961. }
  2962. // int type= sqlite3_column_int(statement, 2);
  2963. //item[@"type"]=@"price";
  2964. // item[nsname]= nsprice;
  2965. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2966. count++;
  2967. }
  2968. ret[@"count"]= [NSNumber numberWithInt:count];
  2969. sqlite3_finalize(statement);
  2970. }
  2971. // [iSalesDB close_db:db];
  2972. return ret;
  2973. }
  2974. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2975. //{
  2976. // NSArray* arr1 = [self get_user_all_price_type:db];
  2977. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2978. //
  2979. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2980. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2981. // // [set2 intersectsSet:set1];
  2982. // //
  2983. // //
  2984. // // NSArray *retarr = [set2 allObjects];
  2985. //
  2986. // NSString* whereprice=nil;
  2987. // if(contact_id==nil)
  2988. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2989. // else
  2990. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2991. //
  2992. //
  2993. // // sqlite3 *db = [iSalesDB get_db];
  2994. //
  2995. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2996. // sqlite3_stmt * statement;
  2997. //
  2998. //
  2999. // NSNumber* ret = nil;
  3000. // double dprice=DBL_MAX;
  3001. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3002. // {
  3003. //
  3004. //
  3005. // while (sqlite3_step(statement) == SQLITE_ROW)
  3006. // {
  3007. //
  3008. // // double val = sqlite3_column_double(statement, 0);
  3009. // char *price = (char*)sqlite3_column_text(statement, 0);
  3010. // if(price!=nil)
  3011. // {
  3012. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3013. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3014. // if(nsprice.length>0)
  3015. // {
  3016. // double dp= [nsprice doubleValue];
  3017. // if(dp<dprice)
  3018. // dprice=dp;
  3019. // }
  3020. // }
  3021. // }
  3022. //
  3023. //
  3024. //
  3025. //
  3026. // sqlite3_finalize(statement);
  3027. //
  3028. //
  3029. //
  3030. //
  3031. // }
  3032. //
  3033. // // [iSalesDB close_db:db];
  3034. //
  3035. // if(dprice==DBL_MAX)
  3036. // ret= nil;
  3037. // else
  3038. // ret= [NSNumber numberWithDouble:dprice];
  3039. // return ret;
  3040. //}
  3041. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3042. {
  3043. assert(user!=nil);
  3044. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3045. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3046. NSString* whereprice=nil;
  3047. if(contact_id==nil)
  3048. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3049. else
  3050. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3051. // sqlite3 *db = [iSalesDB get_db];
  3052. NSString *productIdCondition = @"1 = 1";
  3053. if (product_id) {
  3054. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3055. }
  3056. NSString *itemIdCondition = @"";
  3057. if (item_id) {
  3058. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3059. }
  3060. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3061. sqlite3_stmt * statement;
  3062. NSNumber* ret = nil;
  3063. double dprice=DBL_MAX;
  3064. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3065. {
  3066. while (sqlite3_step(statement) == SQLITE_ROW)
  3067. {
  3068. // double val = sqlite3_column_double(statement, 0);
  3069. char *price = (char*)sqlite3_column_text(statement, 0);
  3070. if(price!=nil)
  3071. {
  3072. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3073. nsprice=[AESCrypt fastdecrypt:nsprice];
  3074. if(nsprice.length>0)
  3075. {
  3076. double dp= [nsprice doubleValue];
  3077. if(dp<dprice)
  3078. dprice=dp;
  3079. }
  3080. }
  3081. }
  3082. sqlite3_finalize(statement);
  3083. }
  3084. // [iSalesDB close_db:db];
  3085. if(dprice==DBL_MAX)
  3086. ret= nil;
  3087. else
  3088. ret= [NSNumber numberWithDouble:dprice];
  3089. return ret;
  3090. }
  3091. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3092. {
  3093. assert(user!=nil);
  3094. NSArray* ret=nil;
  3095. // sqlite3 *db = [iSalesDB get_db];
  3096. // no customer assigned , use login user contact_id
  3097. // UIApplication * app = [UIApplication sharedApplication];
  3098. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3099. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3100. sqlite3_stmt * statement;
  3101. // int count=0;
  3102. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3103. {
  3104. if (sqlite3_step(statement) == SQLITE_ROW)
  3105. {
  3106. char *val = (char*)sqlite3_column_text(statement, 0);
  3107. if(val==nil)
  3108. val="";
  3109. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3110. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3111. }
  3112. sqlite3_finalize(statement);
  3113. }
  3114. // [iSalesDB close_db:db];
  3115. return ret;
  3116. }
  3117. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3118. {
  3119. // sqlite3 *db = [iSalesDB get_db];
  3120. if(contact_id==nil)
  3121. {
  3122. // no customer assigned , use login user contact_id
  3123. // UIApplication * app = [UIApplication sharedApplication];
  3124. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3125. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3126. sqlite3_stmt * statement;
  3127. // int count=0;
  3128. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3129. {
  3130. if (sqlite3_step(statement) == SQLITE_ROW)
  3131. {
  3132. char *val = (char*)sqlite3_column_text(statement, 0);
  3133. if(val==nil)
  3134. val="";
  3135. contact_id = [[NSString alloc]initWithUTF8String:val];
  3136. }
  3137. sqlite3_finalize(statement);
  3138. }
  3139. if(contact_id.length<=0)
  3140. {
  3141. // [iSalesDB close_db:db];
  3142. return nil;
  3143. }
  3144. }
  3145. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3146. sqlite3_stmt * statement;
  3147. NSArray* ret=nil;
  3148. // int count=0;
  3149. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3150. {
  3151. if (sqlite3_step(statement) == SQLITE_ROW)
  3152. {
  3153. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3154. if(price_type==nil)
  3155. price_type="";
  3156. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3157. if(nsprice_type.length>0)
  3158. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3159. else
  3160. ret=nil;
  3161. }
  3162. sqlite3_finalize(statement);
  3163. }
  3164. // [iSalesDB close_db:db];
  3165. return ret;
  3166. }
  3167. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3168. {
  3169. NSString* ret= nil;
  3170. // sqlite3 *db = [iSalesDB get_db];
  3171. NSString *sqlQuery = nil;
  3172. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3173. // 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;
  3174. // 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
  3175. if(product_id==nil && model_name)
  3176. 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;
  3177. else if (product_id)
  3178. 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
  3179. sqlite3_stmt * statement;
  3180. // int count=0;
  3181. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3182. {
  3183. if (sqlite3_step(statement) == SQLITE_ROW)
  3184. {
  3185. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3186. if(imgurl==nil)
  3187. imgurl="";
  3188. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3189. ret=nsimgurl;
  3190. }
  3191. sqlite3_finalize(statement);
  3192. }
  3193. else
  3194. {
  3195. [ret setValue:@"8" forKey:@"result"];
  3196. }
  3197. // [iSalesDB close_db:db];
  3198. DebugLog(@"data string: %@",ret );
  3199. return ret;
  3200. }
  3201. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3202. {
  3203. NSString* ret= nil;
  3204. sqlite3 *db = [iSalesDB get_db];
  3205. NSString *sqlQuery = nil;
  3206. if(product_id==nil)
  3207. if(upc_code==nil)
  3208. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3209. else
  3210. 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='%@';
  3211. else
  3212. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3213. sqlite3_stmt * statement;
  3214. // int count=0;
  3215. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3216. {
  3217. if (sqlite3_step(statement) == SQLITE_ROW)
  3218. {
  3219. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3220. if(default_category==nil)
  3221. default_category="";
  3222. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3223. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3224. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3225. ret=nsdefault_category;
  3226. }
  3227. sqlite3_finalize(statement);
  3228. }
  3229. else
  3230. {
  3231. [ret setValue:@"8" forKey:@"result"];
  3232. }
  3233. [iSalesDB close_db:db];
  3234. DebugLog(@"data string: %@",ret );
  3235. return ret;
  3236. }
  3237. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3238. NSString *sql = nil;
  3239. if (product_id != nil) {
  3240. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3241. } else {
  3242. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3243. }
  3244. NSString *result_set = [iSalesDB jk_queryText:sql];
  3245. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3246. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3247. return result_set;
  3248. }
  3249. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3250. //{
  3251. //
  3252. //
  3253. // assert(params[@"user"]!=nil);
  3254. //
  3255. //
  3256. //
  3257. // NSString* model_name = [params valueForKey:@"product_name"];
  3258. //
  3259. // NSString* product_id = [params valueForKey:@"product_id"];
  3260. //
  3261. // NSString* category = [params valueForKey:@"category"];
  3262. //
  3263. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3264. // if(category==nil) {
  3265. // category = default_category_id;
  3266. // } else {
  3267. //
  3268. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3269. // // 参数重有多个category id
  3270. // if (arr_0.count > 1) {
  3271. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3272. //
  3273. // // 取交集
  3274. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3275. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3276. // [set_0 intersectSet:set_1];
  3277. //
  3278. // if (set_0.count == 1) {
  3279. // category = (NSString *)[set_0 anyObject];
  3280. // } else {
  3281. // if ([set_0 containsObject:default_category_id]) {
  3282. // category = default_category_id;
  3283. // } else {
  3284. // category = (NSString *)[set_0 anyObject];
  3285. // }
  3286. // }
  3287. // }
  3288. // }
  3289. //
  3290. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3291. //
  3292. //
  3293. //
  3294. //
  3295. //
  3296. //
  3297. // sqlite3 *db = [iSalesDB get_db];
  3298. //
  3299. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3300. //
  3301. //
  3302. // NSString *sqlQuery = nil;
  3303. //
  3304. // if(product_id==nil)
  3305. // 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='%@';
  3306. // else
  3307. //
  3308. // 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=%@;
  3309. //
  3310. //
  3311. // sqlite3_stmt * statement;
  3312. // [ret setValue:@"2" forKey:@"result"];
  3313. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3314. //
  3315. // // int count=0;
  3316. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3317. // {
  3318. //
  3319. //
  3320. // if (sqlite3_step(statement) == SQLITE_ROW)
  3321. // {
  3322. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3323. //
  3324. //
  3325. // char *name = (char*)sqlite3_column_text(statement, 0);
  3326. // if(name==nil)
  3327. // name="";
  3328. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3329. //
  3330. // char *description = (char*)sqlite3_column_text(statement, 1);
  3331. // if(description==nil)
  3332. // description="";
  3333. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3334. //
  3335. //
  3336. // int product_id = sqlite3_column_int(statement, 2);
  3337. //
  3338. //
  3339. // char *color = (char*)sqlite3_column_text(statement, 3);
  3340. // if(color==nil)
  3341. // color="";
  3342. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3343. // //
  3344. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3345. // // if(legcolor==nil)
  3346. // // legcolor="";
  3347. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3348. // //
  3349. // //
  3350. // int availability = sqlite3_column_int(statement, 5);
  3351. // //
  3352. // int incoming_stock = sqlite3_column_int(statement, 6);
  3353. //
  3354. //
  3355. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3356. // if(demension==nil)
  3357. // demension="";
  3358. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3359. //
  3360. //
  3361. //
  3362. // // ,,,,,,,,,
  3363. //
  3364. //
  3365. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3366. // if(seat_height==nil)
  3367. // seat_height="";
  3368. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3369. //
  3370. //
  3371. // char *material = (char*)sqlite3_column_text(statement, 9);
  3372. // if(material==nil)
  3373. // material="";
  3374. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3375. //
  3376. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3377. // if(box_dim==nil)
  3378. // box_dim="";
  3379. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3380. //
  3381. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3382. // if(volume==nil)
  3383. // volume="";
  3384. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3385. //
  3386. // double weight = sqlite3_column_double(statement, 12);
  3387. //
  3388. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3389. // if(model_set==nil)
  3390. // model_set="";
  3391. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3392. //
  3393. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3394. // if(load_ability==nil)
  3395. // load_ability="";
  3396. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3397. //
  3398. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3399. //// if(default_category==nil)
  3400. //// default_category="";
  3401. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3402. //
  3403. //
  3404. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3405. // if(fabric_content==nil)
  3406. // fabric_content="";
  3407. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3408. //
  3409. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3410. // if(assembling==nil)
  3411. // assembling="";
  3412. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3413. //
  3414. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3415. // if(made_in==nil)
  3416. // made_in="";
  3417. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3418. //
  3419. //
  3420. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3421. // if(special_remarks==nil)
  3422. // special_remarks="";
  3423. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3424. //
  3425. //
  3426. // int stockUcom = sqlite3_column_double(statement, 20);
  3427. //
  3428. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3429. // if(product_group==nil)
  3430. // product_group="";
  3431. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3432. //
  3433. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3434. // // if(fashion_selector==nil)
  3435. // // fashion_selector="";
  3436. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3437. //
  3438. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3439. // if(selector_field==nil)
  3440. // selector_field="";
  3441. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3442. //
  3443. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3444. // if(property_field==nil)
  3445. // property_field="";
  3446. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3447. //
  3448. //
  3449. //
  3450. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3451. // if(packaging==nil)
  3452. // packaging="";
  3453. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3454. //
  3455. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3456. //
  3457. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3458. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3459. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3460. //
  3461. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3462. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3463. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3464. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3465. //
  3466. //
  3467. //
  3468. //
  3469. //
  3470. // NSString* Availability=nil;
  3471. // if(availability>0)
  3472. // Availability=[NSString stringWithFormat:@"%d",availability];
  3473. // else
  3474. // Availability = @"Out of Stock";
  3475. //
  3476. // [img_section setValue:Availability forKey:@"Availability"];
  3477. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3478. //
  3479. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3480. //
  3481. //
  3482. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3483. // if(eta==nil)
  3484. // eta="";
  3485. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3486. // if ([nseta isEqualToString:@"null"]) {
  3487. // nseta = @"";
  3488. // }
  3489. // if (availability <= 0) {
  3490. // [img_section setValue:nseta forKey:@"ETA"];
  3491. // }
  3492. //
  3493. //
  3494. // int item_id = sqlite3_column_int(statement, 26);
  3495. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3496. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3497. //
  3498. // NSString* Price=nil;
  3499. // if(appDelegate.bLogin==false)
  3500. // Price=@"Must Sign in.";
  3501. // else
  3502. // {
  3503. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3504. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3505. // if(price==nil)
  3506. // Price=@"No Price.";
  3507. // else
  3508. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3509. // }
  3510. //
  3511. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3512. // [img_section setValue:Price forKey:@"price"];
  3513. // [img_section setValue:nsname forKey:@"model_name"];
  3514. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3515. //
  3516. // if (appDelegate.order_code) { // 离线order code即so#
  3517. //
  3518. // 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];
  3519. // __block int cartQTY = 0;
  3520. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3521. //
  3522. // cartQTY = sqlite3_column_int(stmt, 0);
  3523. //
  3524. // }];
  3525. //
  3526. // if (cartQTY > 0) {
  3527. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3528. // }
  3529. // }
  3530. //
  3531. // [ret setObject:img_section forKey:@"img_section"];
  3532. //
  3533. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3534. // int detail0_item_count=0;
  3535. //
  3536. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3537. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3538. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3539. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3540. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3541. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3542. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3543. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3544. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3545. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3546. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3547. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3548. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3549. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3550. //
  3551. //
  3552. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3553. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3554. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3555. // {
  3556. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3557. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3558. // }
  3559. //
  3560. //
  3561. //
  3562. //
  3563. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3564. // [detail0_section setValue:@"kv" forKey:@"type"];
  3565. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3566. //
  3567. // [ret setObject:detail0_section forKey:@"detail_0"];
  3568. //
  3569. //
  3570. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3571. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3572. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3573. // // [detail1_section setValue:@"content" forKey:@"type"];
  3574. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3575. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3576. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3577. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3578. // [ret setObject:detail1_section forKey:@"detail_1"];
  3579. //
  3580. //
  3581. //
  3582. //
  3583. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3584. // [detail2_section setValue:@"detail" forKey:@"target"];
  3585. // [detail2_section setValue:@"popup" forKey:@"action"];
  3586. // [detail2_section setValue:@"content" forKey:@"type"];
  3587. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3588. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3589. // [detail2_section setValue:@"local" forKey:@"data"];
  3590. // [ret setObject:detail2_section forKey:@"detail_2"];
  3591. // }
  3592. //
  3593. //
  3594. //
  3595. //
  3596. // sqlite3_finalize(statement);
  3597. // }
  3598. // else
  3599. // {
  3600. // [ret setValue:@"8" forKey:@"result"];
  3601. // }
  3602. //// DebugLog(@"count:%d",count);
  3603. //
  3604. //
  3605. // [iSalesDB close_db:db];
  3606. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3607. //
  3608. // return ret;
  3609. //}
  3610. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3611. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3612. NSString* keyword = [params valueForKey:@"keyword"];
  3613. keyword=keyword.lowercaseString;
  3614. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3615. int limit = [[params valueForKey:@"limit"] intValue];
  3616. int offset = [[params valueForKey:@"offset"] intValue];
  3617. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3618. NSString *limit_str = @"";
  3619. if (limited) {
  3620. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3621. }
  3622. sqlite3 *db = [iSalesDB get_db];
  3623. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3624. // UIApplication * app = [UIApplication sharedApplication];
  3625. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3626. NSString *sqlQuery = nil;
  3627. if(exactMatch )
  3628. 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 ;
  3629. else
  3630. 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
  3631. DebugLog(@"offline_search sql:%@",sqlQuery);
  3632. sqlite3_stmt * statement;
  3633. [ret setValue:@"2" forKey:@"result"];
  3634. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3635. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3636. // int count=0;
  3637. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3638. {
  3639. int i=0;
  3640. while (sqlite3_step(statement) == SQLITE_ROW)
  3641. {
  3642. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3643. // char *name = (char*)sqlite3_column_text(statement, 1);
  3644. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3645. char *name = (char*)sqlite3_column_text(statement, 0);
  3646. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3647. char *description = (char*)sqlite3_column_text(statement, 1);
  3648. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3649. int product_id = sqlite3_column_int(statement, 2);
  3650. // char *url = (char*)sqlite3_column_text(statement, 3);
  3651. // if(url==nil)
  3652. // url="";
  3653. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3654. int wid = sqlite3_column_int(statement, 3);
  3655. int closeout = sqlite3_column_int(statement, 4);
  3656. int cid = sqlite3_column_int(statement, 5);
  3657. int wisdelete = sqlite3_column_int(statement, 6);
  3658. int more_color = sqlite3_column_int(statement, 7);
  3659. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3660. if(wid !=0 && wisdelete != 1)
  3661. [item setValue:@"true" forKey:@"wish_exists"];
  3662. else
  3663. [item setValue:@"false" forKey:@"wish_exists"];
  3664. if(closeout==0)
  3665. [item setValue:@"false" forKey:@"is_closeout"];
  3666. else
  3667. [item setValue:@"true" forKey:@"is_closeout"];
  3668. if(cid==0)
  3669. [item setValue:@"false" forKey:@"cart_exists"];
  3670. else
  3671. [item setValue:@"true" forKey:@"cart_exists"];
  3672. if (more_color == 0) {
  3673. [item setObject:@(false) forKey:@"more_color"];
  3674. } else if (more_color == 1) {
  3675. [item setObject:@(true) forKey:@"more_color"];
  3676. }
  3677. [item addEntriesFromDictionary:imgjson];
  3678. // [item setValue:nsurl forKey:@"img"];
  3679. [item setValue:nsname forKey:@"fash_name"];
  3680. [item setValue:nsdescription forKey:@"description"];
  3681. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3682. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3683. i++;
  3684. }
  3685. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3686. [ret setObject:items forKey:@"items"];
  3687. sqlite3_finalize(statement);
  3688. }
  3689. DebugLog(@"count:%d",count);
  3690. [iSalesDB close_db:db];
  3691. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3692. return ret;
  3693. }
  3694. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3695. {
  3696. return [self search:params limited:YES];
  3697. }
  3698. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3699. //{
  3700. // UIApplication * app = [UIApplication sharedApplication];
  3701. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3702. //
  3703. //// [iSalesDB disable_trigger]
  3704. // [iSalesDB disable_trigger];
  3705. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3706. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3707. //
  3708. // [iSalesDB enable_trigger];
  3709. //// [iSalesDB enable_trigger]
  3710. // //
  3711. // // NSString* user = [params valueForKey:@"user"];
  3712. // //
  3713. // // NSString* password = [params valueForKey:@"password"];
  3714. //
  3715. //
  3716. //
  3717. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3718. //
  3719. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3720. //
  3721. //
  3722. //
  3723. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3724. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3725. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3726. //
  3727. // [appDelegate update_count_mark];
  3728. //
  3729. //
  3730. // appDelegate.can_show_price =false;
  3731. // appDelegate.can_see_price =false;
  3732. // appDelegate.can_create_portfolio =false;
  3733. // appDelegate.can_create_order =false;
  3734. //
  3735. //
  3736. // appDelegate.can_cancel_order =false;
  3737. // appDelegate.can_set_cart_price =false;
  3738. // appDelegate.can_delete_order =false;
  3739. // appDelegate.can_submit_order =false;
  3740. // appDelegate.can_set_tearsheet_price =false;
  3741. // appDelegate.can_update_contact_info = false;
  3742. //
  3743. // appDelegate.save_order_logout = false;
  3744. // appDelegate.submit_order_logout = false;
  3745. // appDelegate.alert_sold_in_quantities = false;
  3746. //
  3747. // appDelegate.ipad_perm =nil ;
  3748. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3749. // appDelegate.OrderFilter= nil;
  3750. // [appDelegate SetSo:nil];
  3751. // [appDelegate set_main_button_panel];
  3752. //
  3753. //
  3754. // // sqlite3 *db = [iSalesDB get_db];
  3755. // //
  3756. // //
  3757. // //
  3758. // //
  3759. // //
  3760. // // 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"]];
  3761. // //
  3762. // //
  3763. // //
  3764. // //
  3765. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3766. // // sqlite3_stmt * statement;
  3767. // //
  3768. // //
  3769. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3770. // //
  3771. // //
  3772. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3773. // // {
  3774. // //
  3775. // //
  3776. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3777. // // {
  3778. // //
  3779. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3780. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3781. // //
  3782. // //
  3783. // //
  3784. // // int can_show_price = sqlite3_column_int(statement, 0);
  3785. // // int can_see_price = sqlite3_column_int(statement, 1);
  3786. // //
  3787. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3788. // // if(contact_id==nil)
  3789. // // contact_id="";
  3790. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3791. // //
  3792. // // int user_type = sqlite3_column_int(statement, 3);
  3793. // //
  3794. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3795. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3796. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3797. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3798. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3799. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3800. // // int can_create_order = sqlite3_column_int(statement, 10);
  3801. // //
  3802. // //
  3803. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3804. // // if(mode==nil)
  3805. // // mode="";
  3806. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3807. // //
  3808. // //
  3809. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3810. // // if(username==nil)
  3811. // // username="";
  3812. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3813. // //
  3814. // //
  3815. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3816. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3817. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3818. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3819. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3820. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3821. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3822. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3823. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3824. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3825. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3826. // //
  3827. // // [header setValue:nsusername forKey:@"username"];
  3828. // //
  3829. // //
  3830. // // [ret setObject:header forKey:@"header"];
  3831. // // [ret setValue:nsmode forKey:@"mode"];
  3832. // //
  3833. // //
  3834. // // }
  3835. // //
  3836. // //
  3837. // //
  3838. // // sqlite3_finalize(statement);
  3839. // // }
  3840. // //
  3841. // //
  3842. // //
  3843. // // [iSalesDB close_db:db];
  3844. // //
  3845. // //
  3846. // //
  3847. // //
  3848. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3849. //
  3850. // return ret;
  3851. //}
  3852. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3853. //{
  3854. //
  3855. // UIApplication * app = [UIApplication sharedApplication];
  3856. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3857. //
  3858. // [iSalesDB disable_trigger];
  3859. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3860. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3861. // [iSalesDB enable_trigger];
  3862. //
  3863. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3864. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3865. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3866. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3867. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3868. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3869. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3870. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3871. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3872. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3873. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3874. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3875. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3876. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3877. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3878. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3879. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3880. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3881. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3882. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3883. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3884. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3885. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3886. //
  3887. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3888. //
  3889. // [arr_name addObject:customer_first_name];
  3890. // [arr_name addObject:customer_last_name];
  3891. //
  3892. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3893. //
  3894. // // default ship from
  3895. // 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';";
  3896. //
  3897. // __block NSString *cid = @"";
  3898. // __block NSString *name = @"";
  3899. // __block NSString *ext = @"";
  3900. // __block NSString *contact = @"";
  3901. // __block NSString *email = @"";
  3902. // __block NSString *fax = @"";
  3903. // __block NSString *phone = @"";
  3904. //
  3905. // sqlite3 *db = [iSalesDB get_db];
  3906. //
  3907. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3908. //
  3909. // cid = [self textAtColumn:0 statement:statment];
  3910. // name = [self textAtColumn:1 statement:statment];
  3911. // ext = [self textAtColumn:2 statement:statment];
  3912. // contact = [self textAtColumn:3 statement:statment];
  3913. // email = [self textAtColumn:4 statement:statment];
  3914. // fax = [self textAtColumn:5 statement:statment];
  3915. // phone = [self textAtColumn:6 statement:statment];
  3916. //
  3917. // }];
  3918. //
  3919. // NSString* so_id = [self get_offline_soid:db];
  3920. // if(so_id==nil)
  3921. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3922. //
  3923. // 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];
  3924. //
  3925. //
  3926. //
  3927. // int result =[iSalesDB execSql:sql_neworder db:db];
  3928. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3929. //
  3930. //
  3931. //
  3932. // //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'
  3933. // //soId
  3934. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3935. //
  3936. //
  3937. //
  3938. //
  3939. //
  3940. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3941. // sqlite3_stmt * statement;
  3942. //
  3943. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3944. // {
  3945. // if (sqlite3_step(statement) == SQLITE_ROW)
  3946. // {
  3947. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3948. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3949. //
  3950. // //ret = sqlite3_column_int(statement, 0);
  3951. //
  3952. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3953. // if(soId==nil)
  3954. // soId="";
  3955. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3956. // [ret setValue:nssoId forKey:@"soId"];
  3957. // [ret setValue:nssoId forKey:@"orderCode"];
  3958. //
  3959. // }
  3960. // sqlite3_finalize(statement);
  3961. // }
  3962. //
  3963. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3964. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3965. //
  3966. //
  3967. // [iSalesDB close_db:db];
  3968. //
  3969. // return [RAConvertor dict2data:ret];
  3970. //
  3971. //}
  3972. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3973. {
  3974. //assert(params[@"order_code"]);
  3975. // assert(params[@"order_code"]);
  3976. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3977. NSString* orderCode = [params valueForKey:@"orderCode"];
  3978. NSString* app_order_code= params[@"appDelegate.order_code"];
  3979. // UIApplication * app = [UIApplication sharedApplication];
  3980. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3981. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3982. {
  3983. [iSalesDB disable_trigger];
  3984. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3985. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3986. [iSalesDB enable_trigger];
  3987. }
  3988. sqlite3 *db = [iSalesDB get_db];
  3989. int cart_count=[self query_ordercartcount:orderCode db:db];
  3990. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3991. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3992. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3993. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3994. [iSalesDB close_db:db];
  3995. return [RAConvertor dict2data:ret];
  3996. }
  3997. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3998. {
  3999. assert(params[@"can_create_backorder"]!=nil);
  4000. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4001. sqlite3 *db = [iSalesDB get_db];
  4002. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4003. int count =0;
  4004. if(params[@"count"]!=nil)
  4005. {
  4006. count = [params[@"count"] intValue];
  4007. }
  4008. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4009. NSString* product_id=params[@"product_id"];
  4010. NSString* orderCode=params[@"orderCode"];
  4011. NSString *qty = params[@"qty"];
  4012. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4013. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4014. __block int number_of_outOfStock = 0;
  4015. for(int i=0;i<arr_id.count;i++)
  4016. {
  4017. NSInteger item_qty= count;
  4018. if (qty) {
  4019. item_qty = [qty_arr[i] integerValue];
  4020. }
  4021. if(item_qty==0)
  4022. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4023. // 检查新加Model数量是否大于库存
  4024. if (![params[@"can_create_backorder"] boolValue]) {
  4025. __block BOOL needContinue = NO;
  4026. [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) {
  4027. int availability = sqlite3_column_int(stmt, 0);
  4028. // 库存小于购买量为缺货
  4029. if (availability < item_qty || availability <= 0) {
  4030. number_of_outOfStock++;
  4031. needContinue = YES;
  4032. }
  4033. }];
  4034. if (needContinue) {
  4035. continue;
  4036. }
  4037. }
  4038. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4039. NSString* sql=nil;
  4040. sqlite3_stmt *stmt = nil;
  4041. BOOL shouldStep = NO;
  4042. if(_id<0)
  4043. {
  4044. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4045. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4046. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4047. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4048. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4049. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4050. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4051. shouldStep = YES;
  4052. }
  4053. else
  4054. {
  4055. if (qty) { // wish list move to cart
  4056. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4057. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4058. sqlite3_bind_int(stmt, 1, _id);
  4059. shouldStep = YES;
  4060. } else {
  4061. 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];
  4062. __block BOOL update = YES;
  4063. if (![params[@"can_create_backorder"] boolValue]) {
  4064. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4065. int newQTY = sqlite3_column_int(stmt, 0);
  4066. int availability = sqlite3_column_int(stmt, 1);
  4067. if (newQTY > availability) { // 库存不够
  4068. update = NO;
  4069. number_of_outOfStock++;
  4070. }
  4071. }];
  4072. }
  4073. if (update) {
  4074. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4075. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4076. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4077. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4078. shouldStep = YES;
  4079. }
  4080. }
  4081. }
  4082. if (shouldStep) {
  4083. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4084. [iSalesDB execSql:@"ROLLBACK" db:db];
  4085. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4086. [iSalesDB close_db:db];
  4087. DebugLog(@"add to cart error");
  4088. return [RAConvertor dict2data:ret];
  4089. }
  4090. }
  4091. }
  4092. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4093. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4094. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4095. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4096. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4097. if (![params[@"can_create_backorder"] boolValue]) {
  4098. if (number_of_outOfStock > 0) {
  4099. ret[@"result"]=[NSNumber numberWithInt:8];
  4100. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4101. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4102. }
  4103. }
  4104. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4105. [iSalesDB close_db:db];
  4106. return [RAConvertor dict2data:ret];
  4107. }
  4108. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4109. {
  4110. // UIApplication * app = [UIApplication sharedApplication];
  4111. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4112. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4113. 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];
  4114. sqlite3_stmt * statement;
  4115. int count=0;
  4116. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4117. {
  4118. while (sqlite3_step(statement) == SQLITE_ROW)
  4119. {
  4120. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4121. int bitem_id = sqlite3_column_int(statement, 0);
  4122. int bitem_qty = sqlite3_column_int(statement, 1);
  4123. char *name = (char*)sqlite3_column_text(statement, 2);
  4124. if(name==nil)
  4125. name="";
  4126. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4127. char *description = (char*)sqlite3_column_text(statement, 3);
  4128. if(description==nil)
  4129. description="";
  4130. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4131. double unit_price = sqlite3_column_double(statement, 4);
  4132. int use_unitprice = sqlite3_column_int(statement, 5);
  4133. if(use_unitprice!=1)
  4134. {
  4135. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4136. }
  4137. itemjson[@"model"]=nsname;
  4138. itemjson[@"description"]=nsdescription;
  4139. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4140. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4141. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4142. if(compute)
  4143. {
  4144. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4145. }
  4146. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4147. count++;
  4148. }
  4149. sqlite3_finalize(statement);
  4150. }
  4151. ret[@"count"]=@(count);
  4152. if(count==0)
  4153. return nil;
  4154. else
  4155. return ret;
  4156. }
  4157. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4158. {
  4159. //compute: add part to subtotal;
  4160. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4161. dict_item[@(item_id)]=@"1";
  4162. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4163. double cuft=0;
  4164. double weight=0;
  4165. int carton=0;
  4166. int impack=0;
  4167. 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];
  4168. sqlite3_stmt * statement;
  4169. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4170. {
  4171. if (sqlite3_step(statement) == SQLITE_ROW)
  4172. {
  4173. double ulength = sqlite3_column_double(statement, 0);
  4174. double uwidth = sqlite3_column_double(statement, 1);
  4175. double uheight = sqlite3_column_double(statement, 2);
  4176. double uweight = sqlite3_column_double(statement, 3);
  4177. double mlength = sqlite3_column_double(statement, 4);
  4178. double mwidth = sqlite3_column_double(statement, 5);
  4179. double mheight = sqlite3_column_double(statement, 6);
  4180. double mweight = sqlite3_column_double(statement, 7);
  4181. double ilength = sqlite3_column_double(statement, 8);
  4182. double iwidth = sqlite3_column_double(statement, 9);
  4183. double iheight = sqlite3_column_double(statement, 10);
  4184. double iweight = sqlite3_column_double(statement, 11);
  4185. // int pcs = sqlite3_column_int(statement,12);
  4186. int mpack = sqlite3_column_int(statement, 13);
  4187. impack = mpack;
  4188. int ipack = sqlite3_column_int(statement, 14);
  4189. double ucbf = sqlite3_column_double(statement, 15);
  4190. // double icbf = sqlite3_column_double(statement, 16);
  4191. // double mcbf = sqlite3_column_double(statement, 17);
  4192. if(ipack==0)
  4193. {
  4194. carton= count/mpack ;
  4195. weight = mweight*carton;
  4196. cuft= carton*(mlength*mwidth*mheight);
  4197. int remain=count%mpack;
  4198. if(remain==0)
  4199. {
  4200. //do nothing;
  4201. }
  4202. else
  4203. {
  4204. carton++;
  4205. weight += uweight*remain;
  4206. cuft += (ulength*uwidth*uheight)*remain;
  4207. }
  4208. }
  4209. else
  4210. {
  4211. carton = count/(mpack*ipack);
  4212. weight = mweight*carton;
  4213. cuft= carton*(mlength*mwidth*mheight);
  4214. int remain=count%(mpack*ipack);
  4215. if(remain==0)
  4216. {
  4217. // do nothing;
  4218. }
  4219. else
  4220. {
  4221. carton++;
  4222. int icarton =remain/ipack;
  4223. int iremain=remain%ipack;
  4224. weight += iweight*icarton;
  4225. cuft += (ilength*iwidth*iheight)*icarton;
  4226. if(iremain==0)
  4227. {
  4228. //do nothing;
  4229. }
  4230. else
  4231. {
  4232. weight += uweight*iremain;
  4233. cuft += (ulength*uwidth*uheight)*iremain;
  4234. }
  4235. }
  4236. }
  4237. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4238. cuft=ucbf*count;
  4239. weight= uweight*count;
  4240. #endif
  4241. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4242. cuft=ucbf*count;
  4243. weight= uweight*count;
  4244. #endif
  4245. }
  4246. sqlite3_finalize(statement);
  4247. }
  4248. if(compute)
  4249. {
  4250. NSArray * arr_count=nil;
  4251. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4252. for(int i=0;i<arr_bundle.count;i++)
  4253. {
  4254. dict_item[arr_bundle[i]]=@"1";
  4255. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4256. cuft+=[bundlejson[@"cuft"] doubleValue];
  4257. weight+=[bundlejson[@"weight"] doubleValue];
  4258. carton+=[bundlejson[@"carton"] intValue];
  4259. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4260. }
  4261. }
  4262. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4263. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4264. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4265. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4266. ret[@"items"]=dict_item;
  4267. return ret;
  4268. }
  4269. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4270. {
  4271. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4272. // 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 ];
  4273. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4274. DebugLog(@"offline_login sql:%@",sqlQuery);
  4275. sqlite3_stmt * statement;
  4276. int cart_count=0;
  4277. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4278. if ( dbresult== SQLITE_OK)
  4279. {
  4280. while (sqlite3_step(statement) == SQLITE_ROW)
  4281. {
  4282. int item_id = sqlite3_column_int(statement, 0);
  4283. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4284. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4285. }
  4286. sqlite3_finalize(statement);
  4287. }
  4288. return cart_count;
  4289. }
  4290. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4291. //{
  4292. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4293. // sqlite3 *db = [iSalesDB get_db];
  4294. // UIApplication * app = [UIApplication sharedApplication];
  4295. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4296. //
  4297. // NSString* orderCode=params[@"orderCode"];
  4298. //
  4299. // int sort = [[params objectForKey:@"sort"] intValue];
  4300. // NSString *sort_str = @"";
  4301. // switch (sort) {
  4302. // case 0:{
  4303. // sort_str = @"order by c.modify_time desc";
  4304. // }
  4305. // break;
  4306. // case 1:{
  4307. // sort_str = @"order by c.modify_time asc";
  4308. // }
  4309. // break;
  4310. // case 2:{
  4311. // sort_str = @"order by m.name asc";
  4312. // }
  4313. // break;
  4314. // case 3:{
  4315. // sort_str = @"order by m.name desc";
  4316. // }
  4317. // break;
  4318. // case 4:{
  4319. // sort_str = @"order by m.description asc";
  4320. // }
  4321. // break;
  4322. //
  4323. // default:
  4324. // break;
  4325. //
  4326. // }
  4327. //
  4328. //
  4329. //
  4330. // 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 ];
  4331. //
  4332. //
  4333. //// 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 ];
  4334. //
  4335. //
  4336. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4337. //
  4338. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4339. // sqlite3_stmt * statement;
  4340. //
  4341. //
  4342. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4343. //
  4344. // NSDate *date1 = [NSDate date];
  4345. //
  4346. // int count=0;
  4347. // int cart_count=0;
  4348. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4349. // if ( dbresult== SQLITE_OK)
  4350. // {
  4351. //
  4352. //
  4353. // while (sqlite3_step(statement) == SQLITE_ROW)
  4354. // {
  4355. //// NSDate *row_date = [NSDate date];
  4356. //
  4357. //
  4358. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4359. //
  4360. //
  4361. //
  4362. //
  4363. // int product_id = sqlite3_column_int(statement, 0);
  4364. //
  4365. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4366. //
  4367. // int item_id = sqlite3_column_int(statement, 7);
  4368. //
  4369. // NSString* Price=nil;
  4370. // if(str_price==nil)
  4371. // {
  4372. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4373. //// NSDate *price_date = [NSDate date];
  4374. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4375. //// DebugLog(@"price time interval");
  4376. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4377. //
  4378. // if(price==nil)
  4379. // Price=@"No Price.";
  4380. // else
  4381. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4382. // }
  4383. // else
  4384. // {
  4385. //
  4386. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4387. // }
  4388. //
  4389. //
  4390. // double discount = sqlite3_column_double(statement, 2);
  4391. // int item_count = sqlite3_column_int(statement, 3);
  4392. //
  4393. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4394. // NSString *nsline_note=nil;
  4395. // if(line_note!=nil)
  4396. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4397. //
  4398. //
  4399. // char *name = (char*)sqlite3_column_text(statement, 5);
  4400. // NSString *nsname=nil;
  4401. // if(name!=nil)
  4402. // nsname= [[NSString alloc]initWithUTF8String:name];
  4403. //
  4404. // char *description = (char*)sqlite3_column_text(statement, 6);
  4405. // NSString *nsdescription=nil;
  4406. // if(description!=nil)
  4407. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4408. //
  4409. //
  4410. //
  4411. // int stockUom = sqlite3_column_int(statement, 8);
  4412. // int _id = sqlite3_column_int(statement, 9);
  4413. // int availability = sqlite3_column_int(statement, 10);
  4414. //
  4415. //// NSDate *subtotal_date = [NSDate date];
  4416. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4417. //// DebugLog(@"subtotal_date time interval");
  4418. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4419. //
  4420. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4421. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4422. // int carton=[bsubtotaljson[@"carton"] intValue];
  4423. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4424. //
  4425. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4426. //// NSDate *img_date = [NSDate date];
  4427. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4428. //// DebugLog(@"img_date time interval");
  4429. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4430. //
  4431. // itemjson[@"model"]=nsname;
  4432. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4433. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4434. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4435. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4436. // itemjson[@"check"]=@"true";
  4437. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4438. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4439. // itemjson[@"unit_price"]=Price;
  4440. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4441. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4442. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4443. // itemjson[@"note"]=nsline_note;
  4444. // if (!appDelegate.can_create_backorder) {
  4445. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4446. // }
  4447. //// NSDate *date2 = [NSDate date];
  4448. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4449. //// DebugLog(@"model_bundle time interval");
  4450. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4451. //
  4452. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4453. // count++;
  4454. //
  4455. //// DebugLog(@"row time interval");
  4456. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4457. // }
  4458. //
  4459. //
  4460. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4461. // sqlite3_finalize(statement);
  4462. // }
  4463. //
  4464. //
  4465. // DebugLog(@"request cart total time interval");
  4466. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4467. //
  4468. //
  4469. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4470. //
  4471. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4472. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4473. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4474. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4475. //
  4476. // ret[@"mode"]=@"Regular Mode";
  4477. //
  4478. // [iSalesDB close_db:db];
  4479. //
  4480. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4481. //
  4482. // DebugLog(@"general notes :%@",general_note);
  4483. //
  4484. // ret[@"general_note"]= general_note;
  4485. //
  4486. // return [RAConvertor dict2data:ret];
  4487. //}
  4488. +(NSData*) offline_login :(NSMutableDictionary *) params
  4489. {
  4490. NSString* user = [params valueForKey:@"user"];
  4491. NSString* password = [params valueForKey:@"password"];
  4492. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4493. sqlite3 *db = [iSalesDB get_db];
  4494. 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"]];
  4495. DebugLog(@"offline_login sql:%@",sqlQuery);
  4496. sqlite3_stmt * statement;
  4497. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4498. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4499. {
  4500. if (sqlite3_step(statement) == SQLITE_ROW)
  4501. {
  4502. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4503. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4504. int can_show_price = sqlite3_column_int(statement, 0);
  4505. int can_see_price = sqlite3_column_int(statement, 1);
  4506. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4507. if(contact_id==nil)
  4508. contact_id="";
  4509. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4510. int user_type = sqlite3_column_int(statement, 3);
  4511. int can_cancel_order = sqlite3_column_int(statement, 4);
  4512. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4513. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4514. int can_delete_order = sqlite3_column_int(statement, 7);
  4515. int can_submit_order = sqlite3_column_int(statement, 8);
  4516. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4517. int can_create_order = sqlite3_column_int(statement, 10);
  4518. char *mode = (char*)sqlite3_column_text(statement, 11);
  4519. if(mode==nil)
  4520. mode="";
  4521. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4522. char *username = (char*)sqlite3_column_text(statement, 12);
  4523. if(username==nil)
  4524. username="";
  4525. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4526. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4527. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4528. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4529. [header setValue:nscontact_id forKey:@"contact_id"];
  4530. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4531. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4532. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4533. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4534. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4535. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4536. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4537. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4538. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4539. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4540. [header setValue:nsusername forKey:@"username"];
  4541. NSError* error=nil;
  4542. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4543. [header setValue:statusFilter forKey:@"statusFilter"];
  4544. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4545. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4546. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4547. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4548. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4549. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4550. [ret setObject:header forKey:@"header"];
  4551. [ret setValue:nsmode forKey:@"mode"];
  4552. }
  4553. sqlite3_finalize(statement);
  4554. }
  4555. [iSalesDB close_db:db];
  4556. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4557. return [RAConvertor dict2data:ret];
  4558. }
  4559. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4560. {
  4561. assert(params[@"mode"]!=nil);
  4562. NSString* contactId = [params valueForKey:@"contactId"];
  4563. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4564. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4565. sqlite3 *db = [iSalesDB get_db];
  4566. NSString *sqlQuery = nil;
  4567. {
  4568. 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];
  4569. }
  4570. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4571. sqlite3_stmt * statement;
  4572. [ret setValue:@"2" forKey:@"result"];
  4573. int carrier = -1;
  4574. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4575. {
  4576. //int i = 0;
  4577. if (sqlite3_step(statement) == SQLITE_ROW)
  4578. {
  4579. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4580. // int editable = sqlite3_column_int(statement, 0);
  4581. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4582. NSString *nscompany_name =nil;
  4583. if(company_name==nil)
  4584. nscompany_name=@"";
  4585. else
  4586. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4587. char *country = (char*)sqlite3_column_text(statement, 2);
  4588. if(country==nil)
  4589. country="";
  4590. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4591. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4592. // if(addr==nil)
  4593. // addr="";
  4594. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4595. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4596. if(zipcode==nil)
  4597. zipcode="";
  4598. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4599. char *state = (char*)sqlite3_column_text(statement, 5);
  4600. if(state==nil)
  4601. state="";
  4602. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4603. char *city = (char*)sqlite3_column_text(statement, 6);
  4604. if(city==nil)
  4605. city="";
  4606. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4607. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4608. // NSString *nscontact_name = nil;
  4609. // if(contact_name==nil)
  4610. // nscontact_name=@"";
  4611. // else
  4612. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4613. char *phone = (char*)sqlite3_column_text(statement, 8);
  4614. NSString *nsphone = nil;
  4615. if(phone==nil)
  4616. nsphone=@"";
  4617. else
  4618. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4619. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4620. // if(contact_id==nil)
  4621. // contact_id="";
  4622. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4623. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4624. if(addr_1==nil)
  4625. addr_1="";
  4626. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4627. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4628. if(addr_2==nil)
  4629. addr_2="";
  4630. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4631. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4632. if(addr_3==nil)
  4633. addr_3="";
  4634. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4635. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4636. if(addr_4==nil)
  4637. addr_4="";
  4638. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4639. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4640. if(first_name==nil)
  4641. first_name="";
  4642. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4643. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4644. if(last_name==nil)
  4645. last_name="";
  4646. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4647. char *fax = (char*)sqlite3_column_text(statement, 16);
  4648. NSString *nsfax = nil;
  4649. if(fax==nil)
  4650. nsfax=@"";
  4651. else
  4652. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4653. char *email = (char*)sqlite3_column_text(statement, 17);
  4654. NSString *nsemail = nil;
  4655. if(email==nil)
  4656. nsemail=@"";
  4657. else
  4658. nsemail= [[NSString alloc]initWithUTF8String:email];
  4659. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4660. NSString *nsimg_0 = nil;
  4661. if(img_0==nil)
  4662. nsimg_0=@"";
  4663. else
  4664. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4665. [self copy_bcardImg:nsimg_0];
  4666. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4667. NSString *nsimg_1 = nil;
  4668. if(img_1==nil)
  4669. nsimg_1=@"";
  4670. else
  4671. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4672. [self copy_bcardImg:nsimg_1];
  4673. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4674. NSString *nsimg_2 = nil;
  4675. if(img_2==nil)
  4676. nsimg_2=@"";
  4677. else
  4678. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4679. [self copy_bcardImg:nsimg_2];
  4680. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4681. NSString *nsprice_type = nil;
  4682. if(price_type==nil)
  4683. nsprice_type=@"";
  4684. else
  4685. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4686. char *notes = (char*)sqlite3_column_text(statement, 22);
  4687. NSString *nsnotes = nil;
  4688. if(notes==nil)
  4689. nsnotes=@"";
  4690. else
  4691. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4692. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4693. NSString *nssalesrep = nil;
  4694. if(salesrep==nil)
  4695. nssalesrep=@"";
  4696. else
  4697. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4698. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4699. carrier = sqlite3_column_int(statement, 25);
  4700. {
  4701. // decrypt
  4702. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4703. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4704. nsphone=[AESCrypt fastdecrypt:nsphone];
  4705. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4706. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4707. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4708. }
  4709. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4710. [arr_name addObject:nsfirst_name];
  4711. [arr_name addObject:nslast_name];
  4712. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4713. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4714. [arr_ext addObject:nsaddr_1];
  4715. [arr_ext addObject:nsaddr_2];
  4716. [arr_ext addObject:nsaddr_3];
  4717. [arr_ext addObject:nsaddr_4];
  4718. [arr_ext addObject:@"\r\n"];
  4719. [arr_ext addObject:nscity];
  4720. [arr_ext addObject:nsstate];
  4721. [arr_ext addObject:nszipcode];
  4722. [arr_ext addObject:nscountry];
  4723. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4724. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4725. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4726. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4727. [item setValue:nscountry forKey:@"customer_country"];
  4728. [item setValue:nsphone forKey:@"customer_phone"];
  4729. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4730. [item setValue:nscompany_name forKey:@"customer_name"];
  4731. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4732. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4733. [item setValue:nsext forKey:@"customer_contact_ext"];
  4734. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4735. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4736. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4737. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4738. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4739. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4740. [item setValue:nslast_name forKey:@"customer_last_name"];
  4741. [item setValue:nscity forKey:@"customer_city"];
  4742. [item setValue:nsstate forKey:@"customer_state"];
  4743. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4744. [item setValue:contactId forKey:@"customer_cid"];
  4745. [item setValue:nscontact_name forKey:@"customer_contact"];
  4746. [item setValue:nsfax forKey:@"customer_fax"];
  4747. [item setValue:nsemail forKey:@"customer_email"];
  4748. [item setValue:contact_type forKey:@"customer_contact_type"];
  4749. // i++;
  4750. }
  4751. // UIApplication * app = [UIApplication sharedApplication];
  4752. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4753. [ret setValue:params[@"mode"] forKey:@"mode"];
  4754. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4755. sqlite3_finalize(statement);
  4756. }
  4757. [iSalesDB close_db:db];
  4758. NSString* scarrier = [self offline_getCarrier:carrier];
  4759. item[@"customer_truck_carrier"] = scarrier;
  4760. [ret setObject:item forKey:@"customerInfo"];
  4761. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4762. return ret;
  4763. }
  4764. + (bool) copy_bcardImg:(NSString*) filename
  4765. {
  4766. if(filename.length==0)
  4767. return false;
  4768. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4769. bool ret=false;
  4770. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4771. NSString *cachefolder = [paths objectAtIndex:0];
  4772. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4773. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4774. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4775. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4776. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4777. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4778. //
  4779. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4780. NSFileManager* fileManager = [NSFileManager defaultManager];
  4781. BOOL bdir=NO;
  4782. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4783. {
  4784. NSError *error = nil;
  4785. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4786. {
  4787. ret=false;
  4788. }
  4789. else
  4790. {
  4791. ret=true;
  4792. }
  4793. // NSError *error = nil;
  4794. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4795. //
  4796. // if(!bsuccess)
  4797. // {
  4798. // DebugLog(@"Create offline_createimg folder failed");
  4799. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4800. // return [RAConvertor dict2data:ret];
  4801. // }
  4802. // if(bsuccess)
  4803. // {
  4804. // sqlite3 *db = [self get_db];
  4805. //
  4806. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4807. // [iSalesDB close_db:db];
  4808. // }
  4809. }
  4810. return ret;
  4811. //
  4812. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4813. // if(bsuccess)
  4814. // {
  4815. // NSError *error = nil;
  4816. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4817. // {
  4818. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4819. // }
  4820. // else
  4821. // {
  4822. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4823. //
  4824. // ret[@"img_url_aname"]=filename;
  4825. // ret[@"img_url"]=savedImagePath;
  4826. // }
  4827. // }
  4828. }
  4829. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4830. {
  4831. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4832. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4833. NSString *cachefolder = [paths objectAtIndex:0];
  4834. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4835. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4836. NSFileManager* fileManager = [NSFileManager defaultManager];
  4837. BOOL bdir=YES;
  4838. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4839. {
  4840. NSError *error = nil;
  4841. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4842. if(!bsuccess)
  4843. {
  4844. DebugLog(@"Create offline_createimg folder failed");
  4845. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4846. return [RAConvertor dict2data:ret];
  4847. }
  4848. // if(bsuccess)
  4849. // {
  4850. // sqlite3 *db = [self get_db];
  4851. //
  4852. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4853. // [iSalesDB close_db:db];
  4854. // }
  4855. }
  4856. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4857. {
  4858. NSError *error = nil;
  4859. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4860. if(!bsuccess)
  4861. {
  4862. DebugLog(@"Create img_cache folder failed");
  4863. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4864. return [RAConvertor dict2data:ret];
  4865. }
  4866. // if(bsuccess)
  4867. // {
  4868. // sqlite3 *db = [self get_db];
  4869. //
  4870. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4871. // [iSalesDB close_db:db];
  4872. // }
  4873. }
  4874. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4875. //JEPG格式
  4876. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4877. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4878. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4879. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4880. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4881. if(bsuccess)
  4882. {
  4883. NSError *error = nil;
  4884. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4885. {
  4886. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4887. }
  4888. else
  4889. {
  4890. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4891. ret[@"img_url_aname"]=filename;
  4892. ret[@"img_url"]=filename;
  4893. }
  4894. }
  4895. else
  4896. {
  4897. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4898. }
  4899. return [RAConvertor dict2data:ret];
  4900. }
  4901. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4902. {
  4903. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4904. category = [category substringToIndex:3];
  4905. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4906. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4907. params[@"category"]= category;
  4908. ret[@"params"]= params;
  4909. [ret setValue:@"detail" forKey:@"target"];
  4910. [ret setValue:@"popup" forKey:@"action"];
  4911. [ret setValue:@"content" forKey:@"type"];
  4912. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4913. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4914. [ret setValue:@"true" forKey:@"single_row"];
  4915. [ret setValue:@"true" forKey:@"partial_refresh"];
  4916. // sqlite3 *db = [iSalesDB get_db];
  4917. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4918. NSString *sqlQuery =nil;
  4919. #ifdef BUILD_NPD
  4920. 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 ;
  4921. #else
  4922. 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];
  4923. #endif
  4924. sqlite3_stmt * statement;
  4925. int count = 0;
  4926. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4927. // int count=0;
  4928. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4929. {
  4930. int i=0;
  4931. while (sqlite3_step(statement) == SQLITE_ROW)
  4932. {
  4933. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4934. // char *name = (char*)sqlite3_column_text(statement, 1);
  4935. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4936. char *name = (char*)sqlite3_column_text(statement, 0);
  4937. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4938. int product_id = sqlite3_column_int(statement, 1);
  4939. char *url = (char*)sqlite3_column_text(statement, 2);
  4940. if(url==nil)
  4941. url="";
  4942. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4943. [item setValue:nsurl forKey:@"picture_path"];
  4944. [item setValue:nsname forKey:@"fash_name"];
  4945. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4946. [item setValue:category forKey:@"category"];
  4947. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4948. i++;
  4949. }
  4950. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4951. sqlite3_finalize(statement);
  4952. }
  4953. DebugLog(@"count:%d",count);
  4954. // [iSalesDB close_db:db];
  4955. return ret;
  4956. }
  4957. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4958. {
  4959. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4960. [ret setValue:key forKey:@"key"];
  4961. [ret setValue:value forKey:@"val"];
  4962. [ret setValue:@"price" forKey:@"type"];
  4963. return ret;
  4964. }
  4965. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4966. {
  4967. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4968. [ret setValue:key forKey:@"key"];
  4969. [ret setValue:value forKey:@"val"];
  4970. return ret;
  4971. }
  4972. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4973. {
  4974. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4975. [ret setValue:@"0" forKey:@"img_count"];
  4976. // sqlite3 *db = [iSalesDB get_db];
  4977. 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 ;
  4978. sqlite3_stmt * statement;
  4979. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4980. {
  4981. int i=0;
  4982. if (sqlite3_step(statement) == SQLITE_ROW)
  4983. {
  4984. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4985. // char *name = (char*)sqlite3_column_text(statement, 1);
  4986. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4987. char *value = (char*)sqlite3_column_text(statement, 0);
  4988. if(value==nil)
  4989. value="";
  4990. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4991. char *key = (char*)sqlite3_column_text(statement, 1);
  4992. if(key==nil)
  4993. key="";
  4994. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4995. [item setValue:nsvalue forKey:@"val"];
  4996. [item setValue:nskey forKey:@"key"];
  4997. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4998. [ret setValue:@"1" forKey:@"count"];
  4999. i++;
  5000. }
  5001. sqlite3_finalize(statement);
  5002. }
  5003. // [iSalesDB close_db:db];
  5004. return ret;
  5005. }
  5006. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5007. {
  5008. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5009. [ret setValue:@"0" forKey:@"count"];
  5010. // sqlite3 *db = [iSalesDB get_db];
  5011. 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;
  5012. sqlite3_stmt * statement;
  5013. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5014. {
  5015. int i=0;
  5016. while (sqlite3_step(statement) == SQLITE_ROW)
  5017. {
  5018. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5019. // char *name = (char*)sqlite3_column_text(statement, 1);
  5020. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5021. char *value = (char*)sqlite3_column_text(statement, 0);
  5022. if(value==nil)
  5023. value="";
  5024. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5025. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5026. if(selector_display==nil)
  5027. selector_display="";
  5028. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5029. int product_id = sqlite3_column_int(statement, 2);
  5030. char *category = (char*)sqlite3_column_text(statement, 3);
  5031. if(category==nil)
  5032. category="";
  5033. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5034. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5035. [item setValue:nsvalue forKey:@"title"];
  5036. [item setValue:url forKey:@"pic_url"];
  5037. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5038. [params setValue:@"2" forKey:@"count"];
  5039. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5040. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5041. [param0 setValue:@"product_id" forKey:@"name"];
  5042. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5043. [param1 setValue:nscategory forKey:@"val"];
  5044. [param1 setValue:@"category" forKey:@"name"];
  5045. [params setObject:param0 forKey:@"param_0"];
  5046. [params setObject:param1 forKey:@"param_1"];
  5047. [item setObject:params forKey:@"params"];
  5048. [ret setValue:nsselector_display forKey:@"name"];
  5049. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5050. i++;
  5051. }
  5052. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5053. [ret setValue:@"switch" forKey:@"action"];
  5054. sqlite3_finalize(statement);
  5055. }
  5056. // [iSalesDB close_db:db];
  5057. return ret;
  5058. }
  5059. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5060. {
  5061. // model 在 category search 显示的图片。
  5062. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5063. // sqlite3 *db = [iSalesDB get_db];
  5064. 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];
  5065. sqlite3_stmt * statement;
  5066. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5067. {
  5068. while (sqlite3_step(statement) == SQLITE_ROW)
  5069. {
  5070. char *url = (char*)sqlite3_column_text(statement, 0);
  5071. if(url==nil)
  5072. url="";
  5073. int type = sqlite3_column_int(statement, 1);
  5074. if(type==0)
  5075. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5076. else
  5077. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5078. }
  5079. sqlite3_finalize(statement);
  5080. }
  5081. // [iSalesDB close_db:db];
  5082. return ret;
  5083. }
  5084. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5085. {
  5086. int item_id=-1;
  5087. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5088. sqlite3_stmt * statement;
  5089. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5090. {
  5091. if (sqlite3_step(statement) == SQLITE_ROW)
  5092. {
  5093. item_id = sqlite3_column_int(statement, 0);
  5094. }
  5095. sqlite3_finalize(statement);
  5096. }
  5097. return item_id;
  5098. }
  5099. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5100. {
  5101. // NSString* ret = @"";
  5102. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5103. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5104. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5105. sqlite3_stmt * statement;
  5106. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5107. {
  5108. while (sqlite3_step(statement) == SQLITE_ROW)
  5109. {
  5110. int bitem_id = sqlite3_column_int(statement, 0);
  5111. int bitem_qty = sqlite3_column_int(statement, 1);
  5112. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5113. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5114. }
  5115. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5116. sqlite3_finalize(statement);
  5117. }
  5118. // if(ret==nil)
  5119. // ret=@"";
  5120. *count=arr_count;
  5121. return arr_bundle;
  5122. }
  5123. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5124. {
  5125. // get default sold qty, return -1 if model not found;
  5126. int ret = -1;
  5127. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5128. sqlite3_stmt * statement;
  5129. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5130. {
  5131. if (sqlite3_step(statement) == SQLITE_ROW)
  5132. {
  5133. ret = sqlite3_column_int(statement, 0);
  5134. }
  5135. sqlite3_finalize(statement);
  5136. }
  5137. return ret;
  5138. }
  5139. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5140. {
  5141. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5142. [ret setValue:@"0" forKey:@"img_count"];
  5143. // sqlite3 *db = [iSalesDB get_db];
  5144. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5145. sqlite3_stmt * statement;
  5146. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5147. {
  5148. int i=0;
  5149. while (sqlite3_step(statement) == SQLITE_ROW)
  5150. {
  5151. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5152. // char *name = (char*)sqlite3_column_text(statement, 1);
  5153. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5154. char *url = (char*)sqlite3_column_text(statement, 0);
  5155. if(url==nil)
  5156. url="";
  5157. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5158. [item setValue:nsurl forKey:@"s"];
  5159. [item setValue:nsurl forKey:@"l"];
  5160. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5161. i++;
  5162. }
  5163. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5164. sqlite3_finalize(statement);
  5165. }
  5166. // [iSalesDB close_db:db];
  5167. return ret;
  5168. }
  5169. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5170. // UIApplication * app = [UIApplication sharedApplication];
  5171. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5172. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5173. sqlite3 *db = [iSalesDB get_db];
  5174. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5175. NSString* product_id=params[@"product_id"];
  5176. NSString *item_count_str = params[@"item_count"];
  5177. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5178. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5179. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5180. // NSString *sql = @"";
  5181. for(int i=0;i<arr.count;i++)
  5182. {
  5183. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5184. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5185. __block int cart_count = 0;
  5186. if (!item_count_str) {
  5187. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5188. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5189. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5190. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5191. cart_count = [[model_set_components lastObject] intValue];
  5192. }];
  5193. }
  5194. if(count==0)
  5195. {
  5196. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5197. sqlite3_stmt *stmt;
  5198. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5199. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5200. if (item_count_arr) {
  5201. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5202. } else {
  5203. sqlite3_bind_int(stmt,2,cart_count);
  5204. }
  5205. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5206. [iSalesDB execSql:@"ROLLBACK" db:db];
  5207. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5208. [iSalesDB close_db:db];
  5209. DebugLog(@"add to wishlist error");
  5210. return ret;
  5211. }
  5212. } else {
  5213. int qty = 0;
  5214. if (item_count_arr) {
  5215. qty = [item_count_arr[i] intValue];
  5216. } else {
  5217. qty = cart_count;
  5218. }
  5219. 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]];
  5220. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5221. [iSalesDB execSql:@"ROLLBACK" db:db];
  5222. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5223. [iSalesDB close_db:db];
  5224. DebugLog(@"add to wishlist error");
  5225. return ret;
  5226. }
  5227. }
  5228. }
  5229. // [iSalesDB execSql:sql db:db];
  5230. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5231. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5232. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5233. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5234. [iSalesDB close_db:db];
  5235. ret[@"wish_count"]=@(count);
  5236. ret[@"result"]= [NSNumber numberWithInt:2];
  5237. return ret;
  5238. }
  5239. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5240. // 0 category
  5241. // 1 search
  5242. // 2 itemsearch
  5243. NSData *ret = nil;
  5244. NSDictionary *items = nil;
  5245. switch (from) {
  5246. case 0:{
  5247. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5248. }
  5249. break;
  5250. case 1:{
  5251. items = [[self search:params limited:NO] objectForKey:@"items"];
  5252. }
  5253. break;
  5254. case 2:{
  5255. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5256. }
  5257. break;
  5258. default:
  5259. break;
  5260. }
  5261. if (!items) {
  5262. return ret;
  5263. }
  5264. int count = [[items objectForKey:@"count"] intValue];
  5265. NSMutableString *product_id_str = [@"" mutableCopy];
  5266. for (int i = 0; i < count; i++) {
  5267. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5268. NSDictionary *item = [items objectForKey:key];
  5269. NSString *product_id = [item objectForKey:@"product_id"];
  5270. if (i == 0) {
  5271. [product_id_str appendString:product_id];
  5272. } else {
  5273. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5274. }
  5275. }
  5276. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5277. if ([add_to isEqualToString:@"cart"]) {
  5278. NSString *order_code = [params objectForKey:@"orderCode"];
  5279. if (order_code.length) {
  5280. NSDictionary *newParams = @{
  5281. @"product_id" : product_id_str,
  5282. @"orderCode" : order_code,
  5283. @"can_create_backorder":params[@"can_create_backorder"]
  5284. };
  5285. ret = [self offline_add2cart:newParams.mutableCopy];
  5286. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5287. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5288. ret = [RAConvertor dict2data:retDic];
  5289. }
  5290. } else if([add_to isEqualToString:@"wishlist"]) {
  5291. NSDictionary *newParams = @{
  5292. @"product_id" : product_id_str
  5293. };
  5294. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5295. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5296. ret = [RAConvertor dict2data:retDic];
  5297. } else if([add_to isEqualToString:@"portfolio"]) {
  5298. NSDictionary *newParams = @{
  5299. @"product_id" : product_id_str
  5300. };
  5301. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5302. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5303. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5304. ret = [RAConvertor dict2data:retDic];
  5305. }
  5306. return ret;
  5307. }
  5308. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5309. {
  5310. return [self addAll:params from:0];
  5311. }
  5312. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5313. {
  5314. return [self addAll:params from:1];
  5315. }
  5316. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5317. {
  5318. return [self addAll:params from:2];
  5319. }
  5320. #pragma mark - Jack
  5321. #warning 做SQL操作时转义!!
  5322. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5323. // "val_227" : {
  5324. // "check" : 1,
  5325. // "value" : "US United States",
  5326. // "value_id" : "228"
  5327. // },
  5328. if (!countryCode) {
  5329. countryCode = @"US";
  5330. }
  5331. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5332. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5333. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5334. int code_id = sqlite3_column_int(stmt, 3); // id
  5335. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5336. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5337. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5338. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5339. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5340. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5341. }
  5342. long n = *count;
  5343. *count = n + 1;
  5344. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5345. [container setValue:countryDic forKey:key];
  5346. }] mutableCopy];
  5347. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5348. return ret;
  5349. }
  5350. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5351. // "val_227" : {
  5352. // "check" : 1,
  5353. // "value" : "US United States",
  5354. // "value_id" : "228"
  5355. // },
  5356. __block NSString* OrderCarrier =@"";
  5357. // sqlite3 *db = [iSalesDB get_db];
  5358. // sqlite3_stmt * statement;
  5359. [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) {
  5360. OrderCarrier = [self textAtColumn:0 statement:statment];
  5361. // sqlite3_finalize(statement);
  5362. }];
  5363. // [iSalesDB close_db:db];
  5364. __block int c=0;
  5365. 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) {
  5366. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5367. int code_id = sqlite3_column_int(stmt, 2); // id
  5368. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5369. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5370. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5371. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5372. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5373. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5374. }
  5375. c++;
  5376. long n = *count;
  5377. *count = n + 1;
  5378. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5379. [container setValue:carrierDic forKey:key];
  5380. }] mutableCopy];
  5381. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5382. return ret;
  5383. }
  5384. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5385. // "val_227" : {
  5386. // "check" : 1,
  5387. // "value" : "US United States",
  5388. // "value_id" : "228"
  5389. // },
  5390. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5391. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5392. int code_id = sqlite3_column_int(stmt, 2); // id
  5393. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5394. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5395. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5396. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5397. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5398. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5399. }
  5400. long n = *count;
  5401. *count = n + 1;
  5402. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5403. [container setValue:carrierDic forKey:key];
  5404. }] mutableCopy];
  5405. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5406. return ret;
  5407. }
  5408. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5409. // // "val_227" : {
  5410. // // "check" : 1,
  5411. // // "value" : "US United States",
  5412. // // "value_id" : "228"
  5413. // // },
  5414. //
  5415. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5416. // NSString * code = [iSalesDB jk_queryText:Sql];
  5417. //
  5418. // return [self offline_getCarrier:code];
  5419. //
  5420. //}
  5421. + (NSString *)offline_getCarrier:(int)code {
  5422. // "val_227" : {
  5423. // "check" : 1,
  5424. // "value" : "US United States",
  5425. // "value_id" : "228"
  5426. // },
  5427. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5428. NSString * ret = [iSalesDB jk_queryText:Sql];
  5429. return ret;
  5430. }
  5431. + (int )offline_getCarrierCode:(NSString*)name {
  5432. // "val_227" : {
  5433. // "check" : 1,
  5434. // "value" : "US United States",
  5435. // "value_id" : "228"
  5436. // },
  5437. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5438. NSString * ret = [iSalesDB jk_queryText:Sql];
  5439. return [ret intValue];
  5440. }
  5441. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5442. countryCode = [self translateSingleQuote:countryCode];
  5443. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5444. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5445. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5446. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5447. if (name == NULL) {
  5448. name = "";
  5449. }
  5450. if (code == NULL) {
  5451. code = "";
  5452. }
  5453. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5454. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5455. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5456. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5457. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5458. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5459. }
  5460. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5461. [container setValue:stateDic forKey:key];
  5462. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5463. DebugLog(@"query all state error: %@",err_msg);
  5464. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5465. // [stateDic setValue:@"Other" forKey:@"value"];
  5466. // [stateDic setValue:@"" forKey:@"value_id"];
  5467. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5468. //
  5469. // if (state_code && [@"" isEqualToString:state_code]) {
  5470. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5471. // }
  5472. //
  5473. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5474. // [container setValue:stateDic forKey:key];
  5475. }] mutableCopy];
  5476. [ret removeObjectForKey:@"result"];
  5477. // failure 可以不用了,一样的
  5478. if (ret.allKeys.count == 0) {
  5479. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5480. [stateDic setValue:@"Other" forKey:@"value"];
  5481. [stateDic setValue:@"" forKey:@"value_id"];
  5482. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5483. if (state_code && [@"" isEqualToString:state_code]) {
  5484. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5485. }
  5486. NSString *key = [NSString stringWithFormat:@"val_0"];
  5487. [ret setValue:stateDic forKey:key];
  5488. }
  5489. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5490. return ret;
  5491. }
  5492. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5493. codeId = [self translateSingleQuote:codeId];
  5494. 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];
  5495. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5496. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5497. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5498. if (name == NULL) {
  5499. name = "";
  5500. }
  5501. if (code == NULL) {
  5502. code = "";
  5503. }
  5504. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5505. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5506. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5507. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5508. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5509. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5510. }
  5511. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5512. [container setValue:stateDic forKey:key];
  5513. }] mutableCopy];
  5514. [ret removeObjectForKey:@"result"];
  5515. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5516. return ret;
  5517. }
  5518. + (NSDictionary *)offline_getPrice {
  5519. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5520. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5521. char *name = (char *) sqlite3_column_text(stmt, 1);
  5522. int type = sqlite3_column_int(stmt, 2);
  5523. int orderBy = sqlite3_column_int(stmt, 3);
  5524. if (name == NULL) {
  5525. name = "";
  5526. }
  5527. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5528. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5529. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5530. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5531. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5532. if (orderBy == 0) {
  5533. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5534. }
  5535. [container setValue:priceDic forKey:key];
  5536. }] mutableCopy];
  5537. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5538. return ret;
  5539. }
  5540. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5541. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5542. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5543. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5544. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5545. // int _id = sqlite3_column_int(stmt, 0);
  5546. NSString *name = [self textAtColumn:1 statement:stmt];
  5547. NSDictionary *typeDic = @{
  5548. @"value_id" : name,
  5549. @"value" : name,
  5550. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5551. };
  5552. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5553. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5554. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5555. }];
  5556. return ret;
  5557. }
  5558. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5559. // 首先从offline_login表中取出sales_code
  5560. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5561. // NSString *user = app.user;
  5562. user = [self translateSingleQuote:user];
  5563. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5564. __block NSString *user_code = @"";
  5565. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5566. char *code = (char *)sqlite3_column_text(stmt, 0);
  5567. if (code == NULL) {
  5568. code = "";
  5569. }
  5570. user_code = [NSString stringWithUTF8String:code];
  5571. }];
  5572. // 再取所有salesRep
  5573. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5574. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5575. // 1 name 2 code 3 salesrep_id
  5576. char *name = (char *)sqlite3_column_text(stmt, 1);
  5577. char *code = (char *)sqlite3_column_text(stmt, 2);
  5578. int salesrep_id = sqlite3_column_int(stmt, 3);
  5579. if (name == NULL) {
  5580. name = "";
  5581. }
  5582. if (code == NULL) {
  5583. code = "";
  5584. }
  5585. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5586. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5587. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5588. // 比较code 相等则check
  5589. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5590. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5591. }
  5592. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5593. }] mutableCopy];
  5594. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5595. return ret;
  5596. }
  5597. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5598. zipcode = [self translateSingleQuote:zipcode];
  5599. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5600. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5601. char *country = (char *)sqlite3_column_text(stmt, 0);
  5602. char *state = (char *)sqlite3_column_text(stmt, 1);
  5603. char *city = (char *)sqlite3_column_text(stmt, 2);
  5604. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5605. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5606. if (country == NULL) {
  5607. country = "";
  5608. }
  5609. if (state == NULL) {
  5610. state = "";
  5611. }
  5612. if (city == NULL) {
  5613. city = "";
  5614. }
  5615. if (country_code == NULL) {
  5616. country_code = "";
  5617. }
  5618. if (state_code == NULL) {
  5619. state_code = "";
  5620. }
  5621. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5622. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5623. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5624. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5625. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5626. }] mutableCopy];
  5627. return ret;
  5628. }
  5629. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5630. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5631. [item setValue:value forKey:valueKey];
  5632. [dic setValue:item forKey:itemKey];
  5633. }
  5634. + (NSString *)countryCodeByid:(NSString *)code_id {
  5635. NSString *ret = nil;
  5636. code_id = [self translateSingleQuote:code_id];
  5637. sqlite3 *db = [iSalesDB get_db];
  5638. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5639. sqlite3_stmt * statement;
  5640. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5641. while (sqlite3_step(statement) == SQLITE_ROW) {
  5642. char *code = (char *)sqlite3_column_text(statement, 0);
  5643. if (code == NULL) {
  5644. code = "";
  5645. }
  5646. ret = [NSString stringWithUTF8String:code];
  5647. }
  5648. sqlite3_finalize(statement);
  5649. }
  5650. [iSalesDB close_db:db];
  5651. return ret;
  5652. }
  5653. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5654. NSString *ret = nil;
  5655. code = [self translateSingleQuote:code];
  5656. sqlite3 *db = [iSalesDB get_db];
  5657. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5658. sqlite3_stmt * statement;
  5659. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5660. while (sqlite3_step(statement) == SQLITE_ROW) {
  5661. char *_id = (char *)sqlite3_column_text(statement, 0);
  5662. if (_id == NULL) {
  5663. _id = "";
  5664. }
  5665. ret = [NSString stringWithFormat:@"%s",_id];
  5666. }
  5667. sqlite3_finalize(statement);
  5668. }
  5669. [iSalesDB close_db:db];
  5670. return ret;
  5671. }
  5672. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5673. NSString *name = nil;
  5674. codeId = [self translateSingleQuote:codeId];
  5675. sqlite3 *db = [iSalesDB get_db];
  5676. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5677. sqlite3_stmt * statement;
  5678. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5679. while (sqlite3_step(statement) == SQLITE_ROW) {
  5680. char *value = (char *)sqlite3_column_text(statement, 0);
  5681. if (value == NULL) {
  5682. value = "";
  5683. }
  5684. name = [NSString stringWithUTF8String:value];
  5685. }
  5686. sqlite3_finalize(statement);
  5687. }
  5688. [iSalesDB close_db:db];
  5689. return name;
  5690. }
  5691. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5692. NSString *ret = nil;
  5693. sqlite3 *db = [iSalesDB get_db];
  5694. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5695. sqlite3_stmt * statement;
  5696. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5697. while (sqlite3_step(statement) == SQLITE_ROW) {
  5698. char *name = (char *)sqlite3_column_text(statement, 0);
  5699. if (name == NULL) {
  5700. name = "";
  5701. }
  5702. ret = [NSString stringWithUTF8String:name];
  5703. }
  5704. sqlite3_finalize(statement);
  5705. }
  5706. [iSalesDB close_db:db];
  5707. return ret;
  5708. }
  5709. + (NSString *)salesRepCodeById:(NSString *)_id {
  5710. NSString *ret = nil;
  5711. _id = [self translateSingleQuote:_id];
  5712. sqlite3 *db = [iSalesDB get_db];
  5713. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5714. sqlite3_stmt * statement;
  5715. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5716. while (sqlite3_step(statement) == SQLITE_ROW) {
  5717. char *rep = (char *)sqlite3_column_text(statement, 0);
  5718. if (rep == NULL) {
  5719. rep = "";
  5720. }
  5721. ret = [NSString stringWithUTF8String:rep];
  5722. }
  5723. sqlite3_finalize(statement);
  5724. }
  5725. [iSalesDB close_db:db];
  5726. return ret;
  5727. }
  5728. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5729. char *tx = (char *)sqlite3_column_text(stmt, col);
  5730. if (tx == NULL) {
  5731. tx = "";
  5732. }
  5733. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5734. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5735. if (!text) {
  5736. text = @"";
  5737. }
  5738. // 将字符全部为' '的字符串干掉
  5739. int spaceCount = 0;
  5740. for (int i = 0; i < text.length; i++) {
  5741. if ([text characterAtIndex:i] == ' ') {
  5742. spaceCount++;
  5743. }
  5744. }
  5745. if (spaceCount == text.length) {
  5746. text = @"";
  5747. }
  5748. return text;
  5749. }
  5750. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5751. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5752. NSData *data = [NSData dataWithContentsOfFile:path];
  5753. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5754. return ret;
  5755. }
  5756. + (NSString *)textFileName:(NSString *)name {
  5757. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5758. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5759. if (!text) {
  5760. text = @"";
  5761. }
  5762. return text;
  5763. }
  5764. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5765. return [[dic objectForKey:key] mutableCopy];
  5766. }
  5767. + (id)translateSingleQuote:(NSString *)string {
  5768. if ([string isKindOfClass:[NSString class]])
  5769. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5770. return string;
  5771. }
  5772. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5773. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5774. }
  5775. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5776. NSString* ret= nil;
  5777. NSString *sqlQuery = nil;
  5778. // 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
  5779. 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];
  5780. sqlite3_stmt * statement;
  5781. // int count=0;
  5782. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5783. {
  5784. if (sqlite3_step(statement) == SQLITE_ROW)
  5785. {
  5786. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5787. if(imgurl==nil)
  5788. imgurl="";
  5789. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5790. ret=nsimgurl;
  5791. }
  5792. sqlite3_finalize(statement);
  5793. }
  5794. else
  5795. {
  5796. [ret setValue:@"8" forKey:@"result"];
  5797. }
  5798. // [iSalesDB close_db:db];
  5799. // DebugLog(@"data string: %@",ret );
  5800. return ret;
  5801. }
  5802. #pragma mark contact Advanced search
  5803. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5804. {
  5805. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5806. return [RAConvertor dict2data:contactAdvanceDic];
  5807. }
  5808. #pragma mark create new contact
  5809. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5810. assert(params[@"user"]!=nil);
  5811. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5812. NSData *data = [NSData dataWithContentsOfFile:path];
  5813. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5814. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5815. NSString *countryCode = nil;
  5816. NSString *countryCode_id = nil;
  5817. NSString *stateCode = nil;
  5818. NSString *city = nil;
  5819. NSString *zipCode = nil;
  5820. NSString *carrierCode = nil;
  5821. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5822. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5823. NSString *code_id = params[@"country"];
  5824. countryCode_id = code_id;
  5825. countryCode = [self countryCodeByid:code_id];
  5826. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5827. NSString *zip_code = params[@"zipcode"];
  5828. // 剔除全部为空格
  5829. int spaceCount = 0;
  5830. for (int i = 0; i < zip_code.length; i++) {
  5831. if ([zip_code characterAtIndex:i] == ' ') {
  5832. spaceCount++;
  5833. }
  5834. }
  5835. if (spaceCount == zip_code.length) {
  5836. zip_code = @"";
  5837. }
  5838. zipCode = zip_code;
  5839. if (zipCode.length > 0) {
  5840. countryCode_id = params[@"country"];
  5841. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5842. countryCode = [dic valueForKey:@"country_code"];
  5843. if (!countryCode) {
  5844. // countryCode = @"US";
  5845. NSString *code_id = params[@"country"];
  5846. countryCode = [self countryCodeByid:code_id];
  5847. }
  5848. stateCode = [dic valueForKey:@"state_code"];
  5849. if (!stateCode.length) {
  5850. stateCode = params[@"state"];
  5851. }
  5852. city = [dic valueForKey:@"city"];
  5853. if (!city.length) {
  5854. city = params[@"city"];
  5855. }
  5856. // zip code
  5857. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5858. [zipDic setValue:zipCode forKey:@"value"];
  5859. [section_0 setValue:zipDic forKey:@"item_11"];
  5860. } else {
  5861. NSString *code_id = params[@"country"];
  5862. countryCode = [self countryCodeByid:code_id];
  5863. stateCode = params[@"state"];
  5864. city = params[@"city"];
  5865. }
  5866. }
  5867. } else {
  5868. // default: US United States
  5869. countryCode = @"US";
  5870. countryCode_id = @"228";
  5871. }
  5872. // country
  5873. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5874. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5875. // state
  5876. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5877. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5878. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5879. // city
  5880. if (city) {
  5881. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5882. [cityDic setValue:city forKey:@"value"];
  5883. [section_0 setValue:cityDic forKey:@"item_13"];
  5884. }
  5885. // price type
  5886. NSDictionary *priceDic = [self offline_getPrice];
  5887. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5888. // contact type
  5889. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5890. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5891. // Sales Rep
  5892. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5893. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5894. #ifdef BUILD_CONTRAST
  5895. //carrier
  5896. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5897. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5898. #endif
  5899. [ret setValue:section_0 forKey:@"section_0"];
  5900. return [RAConvertor dict2data:ret];
  5901. }
  5902. #pragma mark save
  5903. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5904. NSString *addr = nil;
  5905. NSString *contact_name = nil;
  5906. // int carrier = [params[@"carrier"] intValue];
  5907. NSString *companyName = [params objectForKey:@"company"];
  5908. if (companyName) {
  5909. companyName = [AESCrypt fastencrypt:companyName];
  5910. } else {
  5911. companyName = @"";
  5912. }
  5913. DebugLog(@"company");
  5914. companyName = [self translateSingleQuote:companyName];
  5915. NSString *addr1 = [params objectForKey:@"address"];
  5916. NSString *addr2 = [params objectForKey:@"address2"];
  5917. NSString *addr3 = [params objectForKey:@"address_3"];
  5918. NSString *addr4 = [params objectForKey:@"address_4"];
  5919. if (!addr2) {
  5920. addr2 = @"";
  5921. }
  5922. if (!addr3) {
  5923. addr3 = @"";
  5924. }
  5925. if (!addr4) {
  5926. addr4 = @"";
  5927. }
  5928. if (!addr1) {
  5929. addr1 = @"";
  5930. }
  5931. DebugLog(@"addr");
  5932. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5933. addr = [AESCrypt fastencrypt:addr];
  5934. addr = [self translateSingleQuote:addr];
  5935. if (addr1 && ![addr1 isEqualToString:@""]) {
  5936. addr1 = [AESCrypt fastencrypt:addr1];
  5937. }
  5938. addr1 = [self translateSingleQuote:addr1];
  5939. addr2 = [self translateSingleQuote:addr2];
  5940. addr3 = [self translateSingleQuote:addr3];
  5941. addr4 = [self translateSingleQuote:addr4];
  5942. NSString *country = [params objectForKey:@"country"];
  5943. if (country) {
  5944. country = [self countryNameByCountryCodeId:country];
  5945. } else {
  5946. country = @"";
  5947. }
  5948. int carrier = [params[@"carrier"] intValue];
  5949. DebugLog(@"country");
  5950. country = [self translateSingleQuote:country];
  5951. NSString *state = [params objectForKey:@"state"];
  5952. if (!state) {
  5953. state = @"";
  5954. }
  5955. DebugLog(@"state");
  5956. state = [self translateSingleQuote:state];
  5957. NSString *city = [params objectForKey:@"city"];
  5958. if (!city) {
  5959. city = @"";
  5960. }
  5961. city = [self translateSingleQuote:city];
  5962. NSString *zipcode = [params objectForKey:@"zipcode"];
  5963. if (!zipcode) {
  5964. zipcode = @"";
  5965. }
  5966. DebugLog(@"zip");
  5967. zipcode = [self translateSingleQuote:zipcode];
  5968. NSString *fistName = [params objectForKey:@"firstname"];
  5969. if (!fistName) {
  5970. fistName = @"";
  5971. }
  5972. NSString *lastName = [params objectForKey:@"lastname"];
  5973. if (!lastName) {
  5974. lastName = @"";
  5975. }
  5976. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5977. DebugLog(@"contact_name");
  5978. contact_name = [self translateSingleQuote:contact_name];
  5979. fistName = [self translateSingleQuote:fistName];
  5980. lastName = [self translateSingleQuote:lastName];
  5981. NSString *phone = [params objectForKey:@"phone"];
  5982. if (phone) {
  5983. phone = [AESCrypt fastencrypt:phone];
  5984. } else {
  5985. phone = @"";
  5986. }
  5987. DebugLog(@"PHONE");
  5988. phone = [self translateSingleQuote:phone];
  5989. NSString *fax = [params objectForKey:@"fax"];
  5990. if (!fax) {
  5991. fax = @"";
  5992. }
  5993. DebugLog(@"FAX");
  5994. fax = [self translateSingleQuote:fax];
  5995. NSString *email = [params objectForKey:@"email"];
  5996. if (!email) {
  5997. email = @"";
  5998. }
  5999. DebugLog(@"EMAIL:%@",email);
  6000. email = [self translateSingleQuote:email];
  6001. NSString *notes = [params objectForKey:@"contact_notes"];
  6002. if (!notes) {
  6003. notes = @"";
  6004. }
  6005. DebugLog(@"NOTE:%@",notes);
  6006. notes = [self translateSingleQuote:notes];
  6007. NSString *price = [params objectForKey:@"price_name"];
  6008. if (price) {
  6009. price = [self priceNameByPriceId:price];
  6010. } else {
  6011. price = @"";
  6012. }
  6013. DebugLog(@"PRICE");
  6014. price = [self translateSingleQuote:price];
  6015. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6016. if (salesRep) {
  6017. salesRep = [self salesRepCodeById:salesRep];
  6018. } else {
  6019. salesRep = @"";
  6020. }
  6021. salesRep = [self translateSingleQuote:salesRep];
  6022. NSString *img = [params objectForKey:@"business_card"];
  6023. NSArray *array = [img componentsSeparatedByString:@","];
  6024. NSString *img_0 = array[0];
  6025. if (!img_0) {
  6026. img_0 = @"";
  6027. }
  6028. img_0 = [self translateSingleQuote:img_0];
  6029. NSString *img_1 = array[1];
  6030. if (!img_1) {
  6031. img_1 = @"";
  6032. }
  6033. img_1 = [self translateSingleQuote:img_1];
  6034. NSString *img_2 = array[2];
  6035. if (!img_2) {
  6036. img_2 = @"";
  6037. }
  6038. img_2 = [self translateSingleQuote:img_2];
  6039. NSString *contact_id = [NSUUID UUID].UUIDString;
  6040. NSString *contact_type = [params objectForKey:@"type_name"];
  6041. if (!contact_type) {
  6042. contact_type = @"";
  6043. }
  6044. contact_type = [self translateSingleQuote:contact_type];
  6045. // 判断更新时是否为customer
  6046. if (update) {
  6047. contact_id = [params objectForKey:@"contact_id"];
  6048. if (!contact_id) {
  6049. contact_id = @"";
  6050. }
  6051. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6052. __block int customer = 0;
  6053. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6054. customer = sqlite3_column_int(stmt, 0);
  6055. }];
  6056. isCustomer = customer ? YES : NO;
  6057. }
  6058. NSMutableDictionary *sync_dic = [params mutableCopy];
  6059. if (isCustomer) {
  6060. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6061. } else {
  6062. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6063. }
  6064. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  6065. NSString *sync_data = nil;
  6066. NSString *sql = nil;
  6067. if (update){
  6068. contact_id = [params objectForKey:@"contact_id"];
  6069. if (!contact_id) {
  6070. contact_id = @"";
  6071. }
  6072. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6073. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6074. sync_data = [RAConvertor dict2string:sync_dic];
  6075. sync_data = [self translateSingleQuote:sync_data];
  6076. 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];
  6077. } else {
  6078. contact_id = [self translateSingleQuote:contact_id];
  6079. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6080. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6081. sync_data = [RAConvertor dict2string:sync_dic];
  6082. sync_data = [self translateSingleQuote:sync_data];
  6083. 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];
  6084. }
  6085. int result = [iSalesDB execSql:sql];
  6086. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6087. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6088. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6089. }
  6090. #pragma mark save new contact
  6091. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6092. {
  6093. return [self offline_saveContact:params update:NO isCustomer:YES];
  6094. }
  6095. #pragma mark edit contact
  6096. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6097. {
  6098. assert(params[@"user"]!=nil);
  6099. // {
  6100. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6101. // password = 123456;
  6102. // user = EvanK;
  6103. // }
  6104. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6105. NSData *data = [NSData dataWithContentsOfFile:path];
  6106. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6107. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6108. NSString *countryCode = nil;
  6109. NSString *countryCode_id = nil;
  6110. NSString *stateCode = nil;
  6111. __block int carrierCode = -1;
  6112. /*------contact infor------*/
  6113. __block NSString *country = nil;
  6114. __block NSString *company_name = nil;
  6115. __block NSString *contact_id = params[@"contact_id"];
  6116. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6117. __block NSString *zipcode = nil;
  6118. __block NSString *state = nil; // state_code
  6119. __block NSString *city = nil; //
  6120. __block NSString *firt_name,*last_name;
  6121. __block NSString *phone,*fax,*email;
  6122. __block NSString *notes,*price_type,*sales_rep;
  6123. __block NSString *img_0,*img_1,*img_2;
  6124. __block NSString *contact_type;
  6125. contact_id = [self translateSingleQuote:contact_id];
  6126. 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];
  6127. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6128. country = [self textAtColumn:0 statement:stmt]; // country name
  6129. company_name = [self textAtColumn:1 statement:stmt];
  6130. addr_1 = [self textAtColumn:2 statement:stmt];
  6131. addr_2 = [self textAtColumn:3 statement:stmt];
  6132. addr_3 = [self textAtColumn:4 statement:stmt];
  6133. addr_4 = [self textAtColumn:5 statement:stmt];
  6134. zipcode = [self textAtColumn:6 statement:stmt];
  6135. state = [self textAtColumn:7 statement:stmt]; // state code
  6136. city = [self textAtColumn:8 statement:stmt];
  6137. firt_name = [self textAtColumn:9 statement:stmt];
  6138. last_name = [self textAtColumn:10 statement:stmt];
  6139. phone = [self textAtColumn:11 statement:stmt];
  6140. fax = [self textAtColumn:12 statement:stmt];
  6141. email = [self textAtColumn:13 statement:stmt];
  6142. notes = [self textAtColumn:14 statement:stmt];
  6143. price_type = [self textAtColumn:15 statement:stmt]; // name
  6144. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6145. img_0 = [self textAtColumn:17 statement:stmt];
  6146. img_1 = [self textAtColumn:18 statement:stmt];
  6147. img_2 = [self textAtColumn:19 statement:stmt];
  6148. contact_type = [self textAtColumn:20 statement:stmt];
  6149. carrierCode = sqlite3_column_int(stmt, 21);
  6150. }];
  6151. // decrypt
  6152. if (company_name) {
  6153. company_name = [AESCrypt fastdecrypt:company_name];
  6154. }
  6155. if (addr_1) {
  6156. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6157. }
  6158. if (phone) {
  6159. phone = [AESCrypt fastdecrypt:phone];
  6160. }
  6161. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6162. countryCode = [iSalesDB jk_queryText:countrySql];
  6163. stateCode = state;
  6164. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6165. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6166. NSString *code_id = params[@"country"];
  6167. countryCode_id = code_id;
  6168. countryCode = [self countryCodeByid:code_id];
  6169. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6170. NSString *zip_code = params[@"zipcode"];
  6171. // 剔除全部为空格
  6172. int spaceCount = 0;
  6173. for (int i = 0; i < zip_code.length; i++) {
  6174. if ([zip_code characterAtIndex:i] == ' ') {
  6175. spaceCount++;
  6176. }
  6177. }
  6178. if (spaceCount == zip_code.length) {
  6179. zip_code = @"";
  6180. }
  6181. if (zipcode.length > 0) {
  6182. zipcode = zip_code;
  6183. countryCode_id = params[@"country"];
  6184. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6185. countryCode = [dic valueForKey:@"country_code"];
  6186. if (!countryCode) {
  6187. NSString *code_id = params[@"country"];
  6188. countryCode = [self countryCodeByid:code_id];
  6189. }
  6190. stateCode = [dic valueForKey:@"state_code"];
  6191. if (!stateCode.length) {
  6192. stateCode = params[@"state"];
  6193. }
  6194. city = [dic valueForKey:@"city"];
  6195. if (!city.length) {
  6196. city = params[@"city"];
  6197. }
  6198. // zip code
  6199. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6200. [zipDic setValue:zipcode forKey:@"value"];
  6201. [section_0 setValue:zipDic forKey:@"item_8"];
  6202. } else {
  6203. NSString *code_id = params[@"country"];
  6204. countryCode = [self countryCodeByid:code_id];
  6205. stateCode = params[@"state"];
  6206. city = params[@"city"];
  6207. }
  6208. }
  6209. }
  6210. // 0 Country
  6211. // 1 Company Name
  6212. // 2 Contact ID
  6213. // 3 Picture
  6214. // 4 Address 1
  6215. // 5 Address 2
  6216. // 6 Address 3
  6217. // 7 Address 4
  6218. // 8 Zip Code
  6219. // 9 State/Province
  6220. // 10 City
  6221. // 11 Contact First Name
  6222. // 12 Contact Last Name
  6223. // 13 Phone
  6224. // 14 Fax
  6225. // 15 Email
  6226. // 16 Contact Notes
  6227. // 17 Price Type
  6228. // 18 Contact Type
  6229. // 19 Sales Rep
  6230. // country
  6231. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6232. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6233. // company
  6234. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6235. // contact_id
  6236. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6237. // picture
  6238. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6239. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6240. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6241. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6242. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6243. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6244. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6245. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6246. // addr 1 2 3 4
  6247. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6248. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6249. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6250. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6251. // zip code
  6252. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6253. // state
  6254. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6255. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6256. // city
  6257. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6258. // first last
  6259. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6260. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6261. // phone fax email
  6262. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6263. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6264. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6265. // notes
  6266. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6267. // price
  6268. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6269. for (NSString *key in priceDic.allKeys) {
  6270. if ([key containsString:@"val_"]) {
  6271. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6272. if ([dic[@"value"] isEqualToString:price_type]) {
  6273. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6274. [priceDic setValue:dic forKey:key];
  6275. }
  6276. }
  6277. }
  6278. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6279. // Contact Rep
  6280. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6281. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6282. // Sales Rep
  6283. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6284. for (NSString *key in repDic.allKeys) {
  6285. if ([key containsString:@"val_"]) {
  6286. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6287. NSString *value = dic[@"value"];
  6288. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6289. if (code && [code isEqualToString:sales_rep]) {
  6290. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6291. [repDic setValue:dic forKey:key];
  6292. }
  6293. }
  6294. }
  6295. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6296. #ifdef BUILD_CONTRAST
  6297. //carrier
  6298. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6299. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6300. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6301. #endif
  6302. [ret setValue:section_0 forKey:@"section_0"];
  6303. return [RAConvertor dict2data:ret];
  6304. }
  6305. #pragma mark save contact
  6306. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6307. {
  6308. return [self offline_saveContact:params update:YES isCustomer:YES];
  6309. }
  6310. #pragma mark category
  6311. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6312. if (ck) {
  6313. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6314. for (NSString *key in res.allKeys) {
  6315. if (![key isEqualToString:@"count"]) {
  6316. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6317. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6318. if ([val[@"value"] isEqualToString:ck]) {
  6319. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6320. }
  6321. [res setValue:val forKey:key];
  6322. }
  6323. }
  6324. [dic setValue:res forKey:valueKey];
  6325. }
  6326. }
  6327. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6328. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6329. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6330. NSString* category = [params valueForKey:@"category"];
  6331. if (!category || [category isEqualToString:@""]) {
  6332. category = @"%";
  6333. }
  6334. category = [self translateSingleQuote:category];
  6335. int limit = [[params valueForKey:@"limit"] intValue];
  6336. int offset = [[params valueForKey:@"offset"] intValue];
  6337. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6338. NSString *limit_str = @"";
  6339. if (limited) {
  6340. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6341. }
  6342. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6343. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6344. sqlite3 *db = [iSalesDB get_db];
  6345. // [iSalesDB AddExFunction:db];
  6346. int count;
  6347. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6348. 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];
  6349. double price_min = 0;
  6350. double price_max = 0;
  6351. if ([params.allKeys containsObject:@"alert"]) {
  6352. // alert
  6353. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6354. NSString *alert = params[@"alert"];
  6355. if ([alert isEqualToString:@"Display All"]) {
  6356. alert = [NSString stringWithFormat:@""];
  6357. } else {
  6358. alert = [self translateSingleQuote:alert];
  6359. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6360. }
  6361. // available
  6362. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6363. NSString *available = params[@"available"];
  6364. NSString *available_condition;
  6365. if ([available isEqualToString:@"Display All"]) {
  6366. available_condition = @"";
  6367. } else if ([available isEqualToString:@"Available Now"]) {
  6368. available_condition = @"and availability > 0";
  6369. } else {
  6370. available_condition = @"and availability == 0";
  6371. }
  6372. // best seller
  6373. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6374. NSString *best_seller = @"";
  6375. NSString *order_best_seller = @"m.name asc";
  6376. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6377. best_seller = @"and best_seller > 0";
  6378. order_best_seller = @"m.best_seller desc,m.name asc";
  6379. }
  6380. // price
  6381. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6382. NSString *price = params[@"price"];
  6383. price_min = 0;
  6384. price_max = MAXFLOAT;
  6385. if (params[@"user"] && price != nil) {
  6386. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6387. NSMutableString *priceName = [NSMutableString string];
  6388. for (int i = 0; i < priceTypeArray.count; i++) {
  6389. NSString *pricetype = priceTypeArray[i];
  6390. pricetype = [self translateSingleQuote:pricetype];
  6391. if (i == 0) {
  6392. [priceName appendFormat:@"'%@'",pricetype];
  6393. } else {
  6394. [priceName appendFormat:@",'%@'",pricetype];
  6395. }
  6396. }
  6397. if ([price isEqualToString:@"Display All"]) {
  6398. price = [NSString stringWithFormat:@""];
  6399. } else if([price containsString:@"+"]){
  6400. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6401. price_min = [price doubleValue];
  6402. 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];
  6403. } else {
  6404. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6405. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6406. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6407. 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];
  6408. }
  6409. } else {
  6410. price = @"";
  6411. }
  6412. // sold_by_qty : Sold in quantities of %@
  6413. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6414. NSString *qty = params[@"sold_by_qty"];
  6415. if ([qty isEqualToString:@"Display All"]) {
  6416. qty = @"";
  6417. } else {
  6418. qty = [self translateSingleQuote:qty];
  6419. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6420. }
  6421. // model name;
  6422. NSString* modelname =params[@"modelName"];
  6423. if(modelname.length==0)
  6424. {
  6425. modelname=@"";
  6426. }
  6427. else
  6428. {
  6429. modelname = modelname.lowercaseString;
  6430. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6431. }
  6432. //modelDescription
  6433. NSString* modelDescription =params[@"modelDescription"];
  6434. if(modelDescription.length==0)
  6435. {
  6436. modelDescription=@"";
  6437. }
  6438. else
  6439. {
  6440. modelDescription = modelDescription.lowercaseString;
  6441. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6442. }
  6443. // cate
  6444. // category = [self translateSingleQuote:category];
  6445. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6446. // cate mutiple selection
  6447. NSString *category_id = params[@"category"];
  6448. NSMutableArray *cate_id_array = nil;
  6449. NSMutableString *cateWhere = [NSMutableString string];
  6450. if ([category_id isEqualToString:@""] || !category_id) {
  6451. [cateWhere appendString:@"1 = 1"];
  6452. } else {
  6453. if ([category_id containsString:@","]) {
  6454. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6455. } else {
  6456. cate_id_array = [@[category_id] mutableCopy];
  6457. }
  6458. [cateWhere appendString:@"("];
  6459. for (int i = 0; i < cate_id_array.count; i++) {
  6460. if (i == 0) {
  6461. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6462. } else {
  6463. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6464. }
  6465. }
  6466. [cateWhere appendString:@")"];
  6467. }
  6468. // where bestseller > 0 order by bestseller desc
  6469. // sql query: alert availability(int) best_seller(int) price qty
  6470. 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];
  6471. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6472. }
  6473. DebugLog(@"offline category where: %@",where);
  6474. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6475. if (!params[@"user"]) {
  6476. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6477. }
  6478. [ret setValue:filter forKey:@"filter"];
  6479. DebugLog(@"offline_category sql:%@",sqlQuery);
  6480. sqlite3_stmt * statement;
  6481. [ret setValue:@"2" forKey:@"result"];
  6482. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6483. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6484. // int count=0;
  6485. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6486. {
  6487. int i=0;
  6488. while (sqlite3_step(statement) == SQLITE_ROW)
  6489. {
  6490. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6491. char *name = (char*)sqlite3_column_text(statement, 0);
  6492. if(name==nil)
  6493. name="";
  6494. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6495. char *description = (char*)sqlite3_column_text(statement, 1);
  6496. if(description==nil)
  6497. description="";
  6498. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6499. int product_id = sqlite3_column_int(statement, 2);
  6500. int wid = sqlite3_column_int(statement, 3);
  6501. int closeout = sqlite3_column_int(statement, 4);
  6502. int cid = sqlite3_column_int(statement, 5);
  6503. int wisdelete = sqlite3_column_int(statement, 6);
  6504. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6505. int more_color = sqlite3_column_int(statement, 8);
  6506. // Defaul Category ID
  6507. __block NSString *categoryID = nil;
  6508. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6509. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6510. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6511. if(default_category==nil)
  6512. default_category="";
  6513. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6514. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6515. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6516. categoryID = nsdefault_category;
  6517. }];
  6518. if (!categoryID.length) {
  6519. NSString *cateIDs = params[@"category"];
  6520. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6521. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6522. for (NSString *cateID in requestCategoryArr) {
  6523. BOOL needBreak = NO;
  6524. for (NSString *itemCateIDBox in itemCategoryArr) {
  6525. if (itemCateIDBox.length > 4) {
  6526. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6527. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6528. if ([itemCategoryID isEqualToString:cateID]) {
  6529. needBreak = YES;
  6530. categoryID = itemCategoryID;
  6531. break;
  6532. }
  6533. }
  6534. }
  6535. if (needBreak) {
  6536. break;
  6537. }
  6538. }
  6539. }
  6540. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6541. if(wid !=0 && wisdelete != 1)
  6542. [item setValue:@"true" forKey:@"wish_exists"];
  6543. else
  6544. [item setValue:@"false" forKey:@"wish_exists"];
  6545. if(closeout==0)
  6546. [item setValue:@"false" forKey:@"is_closeout"];
  6547. else
  6548. [item setValue:@"true" forKey:@"is_closeout"];
  6549. if(cid==0)
  6550. [item setValue:@"false" forKey:@"cart_exists"];
  6551. else
  6552. [item setValue:@"true" forKey:@"cart_exists"];
  6553. if (more_color == 0) {
  6554. [item setObject:@(false) forKey:@"more_color"];
  6555. } else if (more_color == 1) {
  6556. [item setObject:@(true) forKey:@"more_color"];
  6557. }
  6558. [item addEntriesFromDictionary:imgjson];
  6559. // [item setValue:nsurl forKey:@"img"];
  6560. [item setValue:nsname forKey:@"name"];
  6561. [item setValue:nsdescription forKey:@"description"];
  6562. if (categoryID) {
  6563. [item setValue:categoryID forKey:@"item_category_id"];
  6564. }
  6565. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6566. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6567. i++;
  6568. }
  6569. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6570. [ret setObject:items forKey:@"items"];
  6571. sqlite3_finalize(statement);
  6572. } else {
  6573. DebugLog(@"nothing...");
  6574. }
  6575. DebugLog(@"count:%d",count);
  6576. [iSalesDB close_db:db];
  6577. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6578. return ret;
  6579. }
  6580. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6581. {
  6582. return [self categoryList:params limited:YES];
  6583. }
  6584. # pragma mark item search
  6585. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6586. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6587. // assert(params[@"order_code"]);
  6588. // params[@"user"]
  6589. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6590. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6591. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6592. // category
  6593. NSDictionary *category_menu = [self offline_category_menu];
  6594. [filter setValue:category_menu forKey:@"category"];
  6595. NSString* where= nil;
  6596. NSString* orderby= @"m.name";
  6597. if (!filterSearch) {
  6598. int covertype = [[params valueForKey:@"covertype"] intValue];
  6599. switch (covertype) {
  6600. case 0:
  6601. {
  6602. where=@"m.category like'%%#005#%%'";
  6603. break;
  6604. }
  6605. case 1:
  6606. {
  6607. where=@"m.alert like '%QS%'";
  6608. break;
  6609. }
  6610. case 2:
  6611. {
  6612. where=@"m.availability>0";
  6613. break;
  6614. }
  6615. case 3:
  6616. {
  6617. where=@"m.best_seller>0";
  6618. orderby=@"m.best_seller desc,m.name asc";
  6619. break;
  6620. }
  6621. default:
  6622. where=@"1=1";
  6623. break;
  6624. }
  6625. }
  6626. int limit = [[params valueForKey:@"limit"] intValue];
  6627. int offset = [[params valueForKey:@"offset"] intValue];
  6628. NSString *limit_str = @"";
  6629. if (limited) {
  6630. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6631. }
  6632. sqlite3 *db = [iSalesDB get_db];
  6633. // [iSalesDB AddExFunction:db];
  6634. int count;
  6635. NSString *sqlQuery = nil;
  6636. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6637. 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];
  6638. double price_min = 0;
  6639. double price_max = 0;
  6640. if (filterSearch) {
  6641. // alert
  6642. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6643. NSString *alert = params[@"alert"];
  6644. if ([alert isEqualToString:@"Display All"]) {
  6645. alert = [NSString stringWithFormat:@""];
  6646. } else {
  6647. alert = [self translateSingleQuote:alert];
  6648. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6649. }
  6650. // available
  6651. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6652. NSString *available = params[@"available"];
  6653. NSString *available_condition;
  6654. if ([available isEqualToString:@"Display All"]) {
  6655. available_condition = @"";
  6656. } else if ([available isEqualToString:@"Available Now"]) {
  6657. available_condition = @"and availability > 0";
  6658. } else {
  6659. available_condition = @"and availability == 0";
  6660. }
  6661. // best seller
  6662. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6663. NSString *best_seller = @"";
  6664. NSString *order_best_seller = @"m.name asc";
  6665. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6666. best_seller = @"and best_seller > 0";
  6667. order_best_seller = @"m.best_seller desc,m.name asc";
  6668. }
  6669. // price
  6670. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6671. NSString *price = params[@"price"];
  6672. price_min = 0;
  6673. price_max = MAXFLOAT;
  6674. if (params[@"user"]) {
  6675. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6676. NSMutableString *priceName = [NSMutableString string];
  6677. for (int i = 0; i < priceTypeArray.count; i++) {
  6678. NSString *pricetype = priceTypeArray[i];
  6679. pricetype = [self translateSingleQuote:pricetype];
  6680. if (i == 0) {
  6681. [priceName appendFormat:@"'%@'",pricetype];
  6682. } else {
  6683. [priceName appendFormat:@",'%@'",pricetype];
  6684. }
  6685. }
  6686. if ([price isEqualToString:@"Display All"]) {
  6687. price = [NSString stringWithFormat:@""];
  6688. } else if([price containsString:@"+"]){
  6689. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6690. price_min = [price doubleValue];
  6691. 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];
  6692. } else {
  6693. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6694. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6695. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6696. 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];
  6697. }
  6698. } else {
  6699. price = @"";
  6700. }
  6701. // sold_by_qty : Sold in quantities of %@
  6702. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6703. NSString *qty = params[@"sold_by_qty"];
  6704. if ([qty isEqualToString:@"Display All"]) {
  6705. qty = @"";
  6706. } else {
  6707. qty = [self translateSingleQuote:qty];
  6708. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6709. }
  6710. // model name;
  6711. NSString* modelname =params[@"modelName"];
  6712. if(modelname.length==0)
  6713. {
  6714. modelname=@"";
  6715. }
  6716. else
  6717. {
  6718. modelname = modelname.lowercaseString;
  6719. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6720. }
  6721. //modelDescription
  6722. NSString* modelDescription =params[@"modelDescription"];
  6723. if(modelDescription.length==0)
  6724. {
  6725. modelDescription=@"";
  6726. }
  6727. else
  6728. {
  6729. modelDescription = modelDescription.lowercaseString;
  6730. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6731. }
  6732. //lower(description) like'%%%@%%'
  6733. // category
  6734. NSString *category_id = params[@"ctgId"];
  6735. NSMutableArray *cate_id_array = nil;
  6736. NSMutableString *cateWhere = [NSMutableString string];
  6737. if ([category_id isEqualToString:@""] || !category_id) {
  6738. [cateWhere appendString:@"1 = 1"];
  6739. } else {
  6740. if ([category_id containsString:@","]) {
  6741. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6742. } else {
  6743. cate_id_array = [@[category_id] mutableCopy];
  6744. }
  6745. /*-----------*/
  6746. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6747. [cateWhere appendString:@"("];
  6748. for (int i = 0; i < cate_id_array.count; i++) {
  6749. for (NSString *key0 in cateDic.allKeys) {
  6750. if ([key0 containsString:@"category_"]) {
  6751. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6752. for (NSString *key1 in category0.allKeys) {
  6753. if ([key1 containsString:@"category_"]) {
  6754. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6755. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6756. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6757. cate_id_array[i] = [category1 objectForKey:@"id"];
  6758. if (i == 0) {
  6759. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6760. } else {
  6761. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6762. }
  6763. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6764. [category0 setValue:category1 forKey:key1];
  6765. [cateDic setValue:category0 forKey:key0];
  6766. }
  6767. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6768. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6769. cate_id_array[i] = [category0 objectForKey:@"id"];
  6770. if (i == 0) {
  6771. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6772. } else {
  6773. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6774. }
  6775. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6776. [cateDic setValue:category0 forKey:key0];
  6777. }
  6778. }
  6779. }
  6780. }
  6781. }
  6782. }
  6783. [cateWhere appendString:@")"];
  6784. [filter setValue:cateDic forKey:@"category"];
  6785. }
  6786. // where bestseller > 0 order by bestseller desc
  6787. // sql query: alert availability(int) best_seller(int) price qty
  6788. 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];
  6789. // count
  6790. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6791. }
  6792. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6793. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6794. if (!params[@"user"]) {
  6795. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6796. }
  6797. [ret setValue:filter forKey:@"filter"];
  6798. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6799. sqlite3_stmt * statement;
  6800. [ret setValue:@"2" forKey:@"result"];
  6801. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6802. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6803. // int count=0;
  6804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6805. {
  6806. int i=0;
  6807. while (sqlite3_step(statement) == SQLITE_ROW)
  6808. {
  6809. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6810. char *name = (char*)sqlite3_column_text(statement, 0);
  6811. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6812. char *description = (char*)sqlite3_column_text(statement, 1);
  6813. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6814. int product_id = sqlite3_column_int(statement, 2);
  6815. int wid = sqlite3_column_int(statement, 3);
  6816. int closeout = sqlite3_column_int(statement, 4);
  6817. int cid = sqlite3_column_int(statement, 5);
  6818. int wisdelete = sqlite3_column_int(statement, 6);
  6819. int more_color = sqlite3_column_int(statement, 7);
  6820. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6821. if(wid !=0 && wisdelete != 1)
  6822. [item setValue:@"true" forKey:@"wish_exists"];
  6823. else
  6824. [item setValue:@"false" forKey:@"wish_exists"];
  6825. if(closeout==0)
  6826. [item setValue:@"false" forKey:@"is_closeout"];
  6827. else
  6828. [item setValue:@"true" forKey:@"is_closeout"];
  6829. if(cid==0)
  6830. [item setValue:@"false" forKey:@"cart_exists"];
  6831. else
  6832. [item setValue:@"true" forKey:@"cart_exists"];
  6833. if (more_color == 0) {
  6834. [item setObject:@(false) forKey:@"more_color"];
  6835. } else if (more_color == 1) {
  6836. [item setObject:@(true) forKey:@"more_color"];
  6837. }
  6838. [item addEntriesFromDictionary:imgjson];
  6839. // [item setValue:nsurl forKey:@"img"];
  6840. [item setValue:nsname forKey:@"fash_name"];
  6841. [item setValue:nsdescription forKey:@"description"];
  6842. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6843. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6844. i++;
  6845. }
  6846. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6847. [ret setObject:items forKey:@"items"];
  6848. sqlite3_finalize(statement);
  6849. }
  6850. [iSalesDB close_db:db];
  6851. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6852. return ret;
  6853. }
  6854. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6855. {
  6856. return [self itemsearch:params limited:YES];
  6857. }
  6858. #pragma mark order detail
  6859. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6860. if (str1.length == 0) {
  6861. str1 = @"&nbsp";
  6862. }
  6863. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6864. return str;
  6865. }
  6866. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6867. {
  6868. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6869. [fromformatter setDateFormat:from];
  6870. NSDate *date = [fromformatter dateFromString:datetime];
  6871. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6872. [toformatter setDateFormat:to];
  6873. NSString * ret = [toformatter stringFromDate:date];
  6874. return ret;
  6875. }
  6876. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6877. // 把毫秒去掉
  6878. if ([dateTime containsString:@"."]) {
  6879. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6880. }
  6881. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6882. formatter.dateFormat = formate;
  6883. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6884. NSDate *date = [formatter dateFromString:dateTime];
  6885. formatter.dateFormat = newFormate;
  6886. NSString *result = [formatter stringFromDate:date];
  6887. return result ? result : @"";
  6888. }
  6889. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6890. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6891. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6892. }
  6893. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6894. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6895. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6896. }
  6897. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6898. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6899. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6900. }
  6901. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6902. {
  6903. assert(params[@"mode"]!=nil);
  6904. assert(params[@"user"]!=nil);
  6905. DebugLog(@"offline oderdetail params: %@",params);
  6906. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6907. int orderId = [params[@"orderId"] intValue];
  6908. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6909. // decrypt card number and card security code
  6910. // 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 ];
  6911. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country,o.must_call,discount from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via,must_call,discount from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  6912. sqlite3 *db = [iSalesDB get_db];
  6913. sqlite3_stmt * statement;
  6914. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6915. NSString *nssoid = @"";
  6916. NSString* orderinfo = @"";
  6917. NSString *moreInfo = @"";
  6918. double handlingFee = 0;
  6919. double payments_and_credist = 0;
  6920. double totalPrice = 0;
  6921. double shippingFee = 0;
  6922. double lift_gate = 0;
  6923. NSString *customer_contact = @"";
  6924. NSString *customer_email = @"";
  6925. NSString *customer_fax = @"";
  6926. NSString *customer_phone = @"";
  6927. NSString *customer_city = @"";
  6928. NSString *customer_state = @"";
  6929. NSString *customer_zipcode = @"";
  6930. NSString *customer_country = @"";
  6931. BOOL must_call = NO;
  6932. double discount = 0;
  6933. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6934. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6935. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6936. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6937. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6938. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6939. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6940. {
  6941. if (sqlite3_step(statement) == SQLITE_ROW)
  6942. {
  6943. // int order_id = sqlite3_column_int(statement, 0);
  6944. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6945. // ret[@"sign_url"] = sign_url;
  6946. section_1 = @{
  6947. @"data":sign_url,
  6948. @"title":@"Signature",
  6949. @"type":@"sign_url"
  6950. }.mutableCopy;
  6951. [ret setObject:section_1 forKey:@"section_1"];
  6952. customer_contact = [self textAtColumn:52 statement:statement];
  6953. customer_email = [self textAtColumn:53 statement:statement];
  6954. customer_phone = [self textAtColumn:54 statement:statement];
  6955. customer_fax = [self textAtColumn:55 statement:statement];
  6956. customer_city = [self textAtColumn:60 statement:statement];
  6957. customer_state = [self textAtColumn:61 statement:statement];
  6958. customer_zipcode = [self textAtColumn:62 statement:statement];
  6959. customer_country = [self textAtColumn:63 statement:statement];
  6960. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6961. discount =sqlite3_column_double(statement, 65);
  6962. int offline_edit=sqlite3_column_int(statement, 56);
  6963. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6964. char *soid = (char*)sqlite3_column_text(statement, 1);
  6965. if(soid==nil)
  6966. soid= "";
  6967. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6968. // so#
  6969. ret[@"so#"] = nssoid;
  6970. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6971. if(poNumber==nil)
  6972. poNumber= "";
  6973. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6974. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6975. if(create_time==nil)
  6976. create_time= "";
  6977. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6978. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6979. int status = sqlite3_column_int(statement, 4);
  6980. int erpStatus = sqlite3_column_int(statement, 49);
  6981. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6982. // status
  6983. if (status > 1 && status != 3) {
  6984. status = erpStatus;
  6985. } else if (status == 3) {
  6986. status = 15;
  6987. }
  6988. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6989. ret[@"order_status"] = nsstatus;
  6990. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6991. if(company_name==nil)
  6992. company_name= "";
  6993. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6994. // company name
  6995. ret[@"company_name"] = nscompany_name;
  6996. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6997. if(customer_contact==nil)
  6998. customer_contact= "";
  6999. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7000. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7001. if(addr_1==nil)
  7002. addr_1="";
  7003. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7004. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7005. if(addr_2==nil)
  7006. addr_2="";
  7007. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7008. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7009. if(addr_3==nil)
  7010. addr_3="";
  7011. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7012. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7013. if(addr_4==nil)
  7014. addr_4="";
  7015. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7016. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7017. [arr_addr addObject:nsaddr_1];
  7018. [arr_addr addObject:nsaddr_2];
  7019. [arr_addr addObject:nsaddr_3];
  7020. [arr_addr addObject:nsaddr_4];
  7021. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7022. [arr_addr addObject:customer_state];
  7023. [arr_addr addObject:customer_zipcode];
  7024. [arr_addr addObject:customer_country];
  7025. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7026. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7027. char *logist = (char*)sqlite3_column_text(statement, 11);
  7028. if(logist==nil)
  7029. logist= "";
  7030. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7031. if (/*status == -11 || */status == 10 || status == 11) {
  7032. nslogist = [self textAtColumn:59 statement:statement];
  7033. };
  7034. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7035. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7036. shipping = @"Shipping To Be Quoted";
  7037. } else {
  7038. shippingFee = sqlite3_column_double(statement, 12);
  7039. }
  7040. // Shipping
  7041. // ret[@"Shipping"] = shipping;
  7042. NSDictionary *shipping_item = @{
  7043. @"title":@"Shipping",
  7044. @"value":shipping
  7045. };
  7046. [price_data setObject:shipping_item forKey:@"item_1"];
  7047. int have_lift_gate = sqlite3_column_int(statement, 17);
  7048. lift_gate = sqlite3_column_double(statement, 13);
  7049. // Liftgate Fee(No loading dock)
  7050. if (!have_lift_gate) {
  7051. lift_gate = 0;
  7052. }
  7053. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7054. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7055. if (sqlite3_column_int(statement, 57)) {
  7056. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7057. liftgate_value = @"Shipping To Be Quoted";
  7058. }
  7059. NSDictionary *liftgate_item = @{
  7060. @"title":@"Liftgate Fee(No loading dock)",
  7061. @"value":liftgate_value
  7062. };
  7063. [price_data setObject:liftgate_item forKey:@"item_2"];
  7064. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7065. // [ret removeObjectForKey:@"Shipping"];
  7066. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7067. // }
  7068. //
  7069. // if (have_lift_gate) {
  7070. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7071. // }
  7072. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7073. if(general_notes==nil)
  7074. general_notes= "";
  7075. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7076. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7077. if(internal_notes==nil)
  7078. internal_notes= "";
  7079. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7080. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7081. if(payment_type==nil)
  7082. payment_type= "";
  7083. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7084. // order info
  7085. orderinfo = [self textFileName:@"order_info.html"];
  7086. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7087. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7088. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7089. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7090. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7091. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7092. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7093. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7094. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7095. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7096. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7097. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7098. NSString *payment = nil;
  7099. // id -> show
  7100. __block NSString *show_pay_type = nspayment_type;
  7101. [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) {
  7102. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7103. if (show_typ_ch != NULL) {
  7104. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7105. }
  7106. }];
  7107. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7108. {
  7109. payment = [self textFileName:@"creditcardpayment.html"];
  7110. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7111. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7112. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7113. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7114. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7115. NSString *card_type = [self textAtColumn:42 statement:statement];
  7116. if (card_type.length > 0) { // 显示星号
  7117. card_type = @"****";
  7118. }
  7119. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7120. if (card_number.length > 0 && card_number.length > 4) {
  7121. for (int i = 0; i < card_number.length - 4; i++) {
  7122. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7123. }
  7124. } else {
  7125. card_number = @"";
  7126. }
  7127. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7128. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7129. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7130. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7131. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7132. card_expiration = @"****";
  7133. }
  7134. NSString *card_city = [self textAtColumn:46 statement:statement];
  7135. NSString *card_state = [self textAtColumn:47 statement:statement];
  7136. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7137. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7138. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7139. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7140. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7141. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7142. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7143. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7144. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7145. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7146. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7147. }
  7148. else
  7149. {
  7150. payment=[self textFileName:@"normalpayment.html"];
  7151. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7152. }
  7153. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7154. ret[@"result"]= [NSNumber numberWithInt:2];
  7155. // more info
  7156. moreInfo = [self textFileName:@"more_info.html"];
  7157. /*****ship to******/
  7158. // ShipToCompany_or_&nbsp
  7159. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7160. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7161. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7162. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7163. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7164. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7165. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7166. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7167. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7168. /*****ship from******/
  7169. // ShipFromCompany_or_&nbsp
  7170. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7171. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7172. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7173. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7174. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7175. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7176. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7177. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7178. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7179. /*****freight to******/
  7180. // FreightBillToCompany_or_&nbsp
  7181. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7182. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7183. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7184. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7185. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7186. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7187. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7188. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7189. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7190. /*****merchandise to******/
  7191. // MerchandiseBillToCompany_or_&nbsp
  7192. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7193. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7194. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7195. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7196. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7197. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7198. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7199. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7200. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7201. /*****return to******/
  7202. // ReturnToCompany_or_&nbsp
  7203. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7204. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7205. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7206. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7207. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7208. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7209. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7210. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7211. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7212. //
  7213. // DebugLog(@"more info : %@",moreInfo);
  7214. // handling fee
  7215. handlingFee = sqlite3_column_double(statement, 33);
  7216. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7217. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7218. if (sqlite3_column_int(statement, 58)) {
  7219. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7220. handling_fee_value = @"Shipping To Be Quoted";
  7221. }
  7222. NSDictionary *handling_fee_item = @{
  7223. @"title":@"Handling Fee",
  7224. @"value":handling_fee_value
  7225. };
  7226. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7227. //
  7228. // customer info
  7229. customerID = [self textAtColumn:36 statement:statement];
  7230. // mode
  7231. ret[@"mode"] = params[@"mode"];
  7232. // model_count
  7233. ret[@"model_count"] = @(0);
  7234. }
  7235. sqlite3_finalize(statement);
  7236. }
  7237. [iSalesDB close_db:db];
  7238. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7239. // "customer_email" = "Shui Hu";
  7240. // "customer_fax" = "";
  7241. // "customer_first_name" = F;
  7242. // "customer_last_name" = L;
  7243. // "customer_name" = ",da He Xiang Dong Liu A";
  7244. // "customer_phone" = "Hey Xuan Feng";
  7245. contactInfo[@"customer_phone"] = customer_phone;
  7246. contactInfo[@"customer_fax"] = customer_fax;
  7247. contactInfo[@"customer_email"] = customer_email;
  7248. NSString *first_name = @"";
  7249. NSString *last_name = @"";
  7250. if ([customer_contact isEqualToString:@""]) {
  7251. } else if ([customer_contact containsString:@" "]) {
  7252. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7253. first_name = [customer_contact substringToIndex:first_space_index];
  7254. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7255. }
  7256. contactInfo[@"customer_first_name"] = first_name;
  7257. contactInfo[@"customer_last_name"] = last_name;
  7258. ret[@"customerInfo"] = contactInfo;
  7259. // models
  7260. if (nssoid) {
  7261. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7262. __block double TotalCuft = 0;
  7263. __block double TotalWeight = 0;
  7264. __block int TotalCarton = 0;
  7265. __block double allItemPrice = 0;
  7266. 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];
  7267. sqlite3 *db1 = [iSalesDB get_db];
  7268. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7269. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7270. int product_id = sqlite3_column_int(stmt, 0);
  7271. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7272. int item_id = sqlite3_column_int(stmt, 7);
  7273. NSString* Price=nil;
  7274. if(str_price==nil)
  7275. {
  7276. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7277. if(price==nil)
  7278. Price=@"No Price.";
  7279. else
  7280. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7281. }
  7282. else
  7283. {
  7284. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7285. }
  7286. double discount = sqlite3_column_double(stmt, 2);
  7287. int item_count = sqlite3_column_int(stmt, 3);
  7288. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7289. NSString *nsline_note=nil;
  7290. if(line_note!=nil)
  7291. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7292. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7293. // NSString *nsname = nil;
  7294. // if(name!=nil)
  7295. // nsname= [[NSString alloc]initWithUTF8String:name];
  7296. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7297. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7298. // NSString *nsdescription=nil;
  7299. // if(description!=nil)
  7300. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7301. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7302. // int stockUom = sqlite3_column_int(stmt, 8);
  7303. // int _id = sqlite3_column_int(stmt, 9);
  7304. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7305. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7306. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7307. int carton=[bsubtotaljson[@"carton"] intValue];
  7308. TotalCuft += cuft;
  7309. TotalWeight += weight;
  7310. TotalCarton += carton;
  7311. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7312. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7313. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7314. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7315. itemjson[@"note"]=nsline_note;
  7316. itemjson[@"origin_price"] = Price;
  7317. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7318. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7319. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7320. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7321. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7322. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7323. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7324. if(itemjson[@"combine"] != nil)
  7325. {
  7326. // int citem=0;
  7327. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7328. for(int bc=0;bc<bcount;bc++)
  7329. {
  7330. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7331. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7332. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7333. subTotal += uprice * modulus * item_count;
  7334. }
  7335. }
  7336. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7337. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7338. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7339. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7340. itemjson[@"type"] = @"order_item";
  7341. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7342. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7343. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7344. allItemPrice += subTotal;
  7345. }];
  7346. section_3[@"data"] = model_data;
  7347. section_3[@"type"] = @"sub_order";
  7348. section_3[@"title"] = @"Models";
  7349. section_3[@"switch"] = @(false);
  7350. ret[@"section_3"] = section_3;
  7351. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7352. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7353. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7354. // payments/Credits
  7355. // payments_and_credist = sqlite3_column_double(statement, 34);
  7356. payments_and_credist = allItemPrice;
  7357. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7358. NSDictionary *sub_total_item = @{
  7359. @"title":@"Sub-Total",
  7360. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7361. };
  7362. [price_data setObject:sub_total_item forKey:@"item_0"];
  7363. // // total
  7364. // totalPrice = sqlite3_column_double(statement, 35);
  7365. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7366. } else {
  7367. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7368. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7369. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7370. // payments/Credits
  7371. payments_and_credist = 0;
  7372. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7373. NSDictionary *sub_total_item = @{
  7374. @"title":@"Sub-Total",
  7375. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7376. };
  7377. [price_data setObject:sub_total_item forKey:@"item_0"];
  7378. }
  7379. // total
  7380. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7381. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7382. if(discount>0)
  7383. {
  7384. NSDictionary *total_item = @{
  7385. @"title":@"Total",
  7386. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice*(1-(discount/100.0))]
  7387. };
  7388. NSDictionary *discount_item = @{
  7389. @"title":@"Order Discount",
  7390. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7391. };
  7392. [price_data setObject:discount_item forKey:@"item_4"];
  7393. [price_data setObject:total_item forKey:@"item_5"];
  7394. }
  7395. else
  7396. {
  7397. NSDictionary *total_item = @{
  7398. @"title":@"Total",
  7399. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7400. };
  7401. [price_data setObject:total_item forKey:@"item_4"];
  7402. }
  7403. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7404. section_4 = @{
  7405. @"data":price_data,
  7406. @"title":@"Price Info",
  7407. @"type":@"price_info"
  7408. }.mutableCopy;
  7409. ret[@"section_4"] = section_4;
  7410. // ret[@"order_info"]= orderinfo;
  7411. section_0 = @{
  7412. @"data":orderinfo,
  7413. @"title":@"Order Info",
  7414. @"type":@"order_info"
  7415. }.mutableCopy;
  7416. ret[@"section_0"] = section_0;
  7417. // ret[@"more_order_info"] = moreInfo;
  7418. section_2 = @{
  7419. @"data":moreInfo,
  7420. @"title":@"More Info",
  7421. @"type":@"more_order_info"
  7422. }.mutableCopy;
  7423. ret[@"section_2"] = section_2;
  7424. ret[@"count"] = @(5);
  7425. return [RAConvertor dict2data:ret];
  7426. }
  7427. #pragma mark order list
  7428. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7429. NSString *nsstatus = @"";
  7430. switch (status) {
  7431. case 0:
  7432. {
  7433. nsstatus=@"Temp Order";
  7434. break;
  7435. }
  7436. case 1:
  7437. {
  7438. nsstatus=@"Saved Order";
  7439. break;
  7440. }
  7441. case 2: {
  7442. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7443. break;
  7444. }
  7445. case 3:
  7446. case 15:
  7447. {
  7448. nsstatus=@"Cancelled";
  7449. break;
  7450. }
  7451. case 10:
  7452. {
  7453. nsstatus=@"Quote Submitted";
  7454. break;
  7455. }
  7456. case 11:
  7457. {
  7458. nsstatus=@"Sales Order Submitted";
  7459. break;
  7460. }
  7461. case 12:
  7462. {
  7463. nsstatus=@"Processing";
  7464. break;
  7465. }
  7466. case 13:
  7467. {
  7468. nsstatus=@"Shipped";
  7469. break;
  7470. }
  7471. case 14:
  7472. {
  7473. nsstatus=@"Closed";
  7474. break;
  7475. }
  7476. case -11:
  7477. {
  7478. nsstatus = @"Ready For Submit";
  7479. break;
  7480. }
  7481. default:
  7482. break;
  7483. }
  7484. return nsstatus;
  7485. }
  7486. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7487. {
  7488. double total = 0;
  7489. __block double payments_and_credist = 0;
  7490. __block double allItemPrice = 0;
  7491. // sqlite3 *db1 = [iSalesDB get_db];
  7492. if (so_id) {
  7493. // 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];
  7494. 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];
  7495. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7496. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7497. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7498. int product_id = sqlite3_column_int(stmt, 0);
  7499. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7500. int discount = sqlite3_column_int(stmt, 2);
  7501. int item_count = sqlite3_column_int(stmt, 3);
  7502. // int item_id = sqlite3_column_int(stmt, 7);
  7503. int item_id = sqlite3_column_int(stmt, 4);
  7504. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7505. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7506. NSString* Price=nil;
  7507. if(str_price==nil)
  7508. {
  7509. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7510. if(price==nil)
  7511. Price=@"No Price.";
  7512. else
  7513. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7514. }
  7515. else
  7516. {
  7517. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7518. }
  7519. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7520. itemjson[@"The unit price"]=Price;
  7521. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7522. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7523. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7524. if(itemjson[@"combine"] != nil)
  7525. {
  7526. // int citem=0;
  7527. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7528. for(int bc=0;bc<bcount;bc++)
  7529. {
  7530. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7531. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7532. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7533. subTotal += uprice * modulus * item_count;
  7534. }
  7535. }
  7536. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7537. allItemPrice += subTotal;
  7538. }];
  7539. payments_and_credist = allItemPrice;
  7540. } else {
  7541. // payments/Credits
  7542. payments_and_credist = 0;
  7543. }
  7544. // lift_gate handlingFee shippingFee
  7545. __block double lift_gate = 0;
  7546. __block double handlingFee = 0;
  7547. __block double shippingFee = 0;
  7548. __block double order_discount = 0;
  7549. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7550. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7551. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7552. if (have_lift_gate) {
  7553. lift_gate = sqlite3_column_double(stmt, 1);
  7554. }
  7555. handlingFee = sqlite3_column_double(stmt, 2);
  7556. shippingFee = sqlite3_column_double(stmt, 3);
  7557. order_discount = sqlite3_column_double(stmt, 4);
  7558. }];
  7559. if(order_discount>0)
  7560. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7561. // total
  7562. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7563. if (close) {
  7564. [iSalesDB close_db:db1];
  7565. }
  7566. return total;
  7567. }
  7568. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7569. {
  7570. assert(params[@"user"]!=nil);
  7571. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7572. int limit = [[params valueForKey:@"limit"] intValue];
  7573. int offset = [[params valueForKey:@"offset"] intValue];
  7574. NSString* keyword = [params valueForKey:@"keyWord"];
  7575. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7576. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7577. NSString* where=@"1 = 1";
  7578. if(keyword.length>0)
  7579. 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]];
  7580. if (orderStatus.length > 0) {
  7581. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7582. if (order_status_array.count == 1) {
  7583. int status_value = [[order_status_array firstObject] intValue];
  7584. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7585. if (status_value == 15 || status_value == 3) {
  7586. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7587. } else {
  7588. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7589. }
  7590. } else {
  7591. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7592. }
  7593. } else if (order_status_array.count > 1) {
  7594. for (int i = 0; i < order_status_array.count;i++) {
  7595. NSString *status = order_status_array[i];
  7596. NSString *condition = @" or";
  7597. if (i == 0) {
  7598. condition = @" and (";
  7599. }
  7600. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7601. int status_value = [status intValue];
  7602. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7603. if (status_value == 15 || status_value == 3) {
  7604. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7605. } else {
  7606. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7607. }
  7608. } else {
  7609. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7610. }
  7611. }
  7612. where = [where stringByAppendingString:@" )"];
  7613. }
  7614. }
  7615. 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];
  7616. // DebugLog(@"order list sql: %@",sqlQuery);
  7617. sqlite3 *db = [iSalesDB get_db];
  7618. sqlite3_stmt * statement;
  7619. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7620. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7621. {
  7622. int count=0;
  7623. while (sqlite3_step(statement) == SQLITE_ROW)
  7624. {
  7625. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7626. int order_id = sqlite3_column_double(statement, 0);
  7627. char *soid = (char*)sqlite3_column_text(statement, 1);
  7628. if(soid==nil)
  7629. soid= "";
  7630. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7631. int status = sqlite3_column_double(statement, 2);
  7632. int erpStatus = sqlite3_column_double(statement, 9);
  7633. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7634. if(sales_rep==nil)
  7635. sales_rep= "";
  7636. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7637. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7638. if(create_by==nil)
  7639. create_by= "";
  7640. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7641. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7642. if(company_name==nil)
  7643. company_name= "";
  7644. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7645. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7646. if(create_time==nil)
  7647. create_time= "";
  7648. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7649. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7650. // double total_price = sqlite3_column_double(statement, 7);
  7651. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7652. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7653. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7654. int offline_edit = sqlite3_column_int(statement, 10);
  7655. // ": "JH",
  7656. // "": "$8307.00",
  7657. // "": "MOB1608050001",
  7658. // "": "ArpithaT",
  7659. // "": "1st Stage Property Transformations",
  7660. // "": "JANICE SUTTON",
  7661. // "": 2255,
  7662. // "": "08/02/2016 09:49:18",
  7663. // "": 1,
  7664. // "": "Saved Order"
  7665. // "": "1470384050483",
  7666. // "model_count": "6 / 28"
  7667. item[@"sales_rep"]= nssales_rep;
  7668. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7669. item[@"so#"]= nssoid;
  7670. item[@"create_by"]= nscreate_by;
  7671. item[@"customer_name"]= nscompany_name;
  7672. item[@"customer_contact"] = customer_contact;
  7673. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7674. item[@"purchase_time"]= nscreate_time;
  7675. int statusCode = status;
  7676. if (statusCode == 2) {
  7677. statusCode = erpStatus;
  7678. } else if (statusCode == 3) {
  7679. statusCode = 15;
  7680. }
  7681. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7682. item[@"order_status"]= nsstatus;
  7683. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7684. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7685. // item[@"model_count"]
  7686. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7687. count++;
  7688. }
  7689. ret[@"count"]= [NSNumber numberWithInt:count];
  7690. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7691. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7692. ret[@"result"]= [NSNumber numberWithInt:2];
  7693. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7694. ret[@"time_zone"] = @"PST";
  7695. sqlite3_finalize(statement);
  7696. }
  7697. 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];
  7698. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7699. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7700. [iSalesDB close_db:db];
  7701. return [RAConvertor dict2data:ret];
  7702. }
  7703. #pragma mark update gnotes
  7704. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7705. {
  7706. DebugLog(@"params: %@",params);
  7707. // comments = Meyoyoyoyoyoyoy;
  7708. // orderCode = MOB1608110001;
  7709. // password = 123456;
  7710. // user = EvanK;
  7711. 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]];
  7712. int ret = [iSalesDB execSql:sql];
  7713. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7714. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7715. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7716. // [dic setValue:@"160409" forKey:@"min_ver"];
  7717. return [RAConvertor dict2data:dic];
  7718. }
  7719. #pragma mark move to wishlist
  7720. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7721. {
  7722. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7723. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7724. _id = [NSString stringWithFormat:@"(%@)",_id];
  7725. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7726. sqlite3 *db = [iSalesDB get_db];
  7727. __block NSString *product_id = @"";
  7728. __block NSString *item_count_str = @"";
  7729. // __block NSString *item_id = nil;
  7730. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7731. // product_id = [self textAtColumn:0 statement:stmt];
  7732. int item_count = sqlite3_column_int(stmt, 1);
  7733. // item_id = [self textAtColumn:2 statement:stmt];
  7734. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7735. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7736. if (p_id.length) {
  7737. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7738. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7739. }
  7740. }];
  7741. [iSalesDB close_db:db];
  7742. // 去除第一个,
  7743. if (product_id.length > 1) {
  7744. product_id = [product_id substringFromIndex:1];
  7745. }
  7746. if (item_count_str.length > 1) {
  7747. item_count_str = [item_count_str substringFromIndex:1];
  7748. }
  7749. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7750. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7751. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7752. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7753. sqlite3 *db1 = [iSalesDB get_db];
  7754. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7755. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7756. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7757. // 删除
  7758. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7759. int ret = [iSalesDB execSql:deleteSql db:db1];
  7760. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7761. [iSalesDB close_db:db1];
  7762. return [RAConvertor dict2data:dic];
  7763. }
  7764. #pragma mark cart delete
  7765. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7766. {
  7767. // cartItemId = 548;
  7768. // orderCode = MOB1608110001;
  7769. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7770. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7771. _id = [NSString stringWithFormat:@"(%@)",_id];
  7772. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7773. int ret = [iSalesDB execSql:sql];
  7774. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7775. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7776. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7777. // [dic setValue:@"160409" forKey:@"min_ver"];
  7778. return [RAConvertor dict2data:dic];
  7779. }
  7780. #pragma mark set order discount
  7781. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7782. //{
  7783. // DebugLog(@"cart set price params: %@",params);
  7784. // // "cartitem_id" = 1;
  7785. // // discount = "0.000000";
  7786. // // "item_note" = "";
  7787. // // price = "269.000000";
  7788. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7789. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7790. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7791. //// NSString *price = [self valueInParams:params key:@"price"];
  7792. //
  7793. // // bool badd_price_changed=false;
  7794. // // sqlite3* db=[iSalesDB get_db];
  7795. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7796. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7797. // // NSRange range;
  7798. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7799. // //
  7800. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7801. // // badd_price_changed=true;
  7802. // // [iSalesDB close_db:db];
  7803. // //
  7804. // // if(badd_price_changed)
  7805. // // {
  7806. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7807. // // }
  7808. // //
  7809. //
  7810. //
  7811. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7812. //
  7813. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7814. //
  7815. // int ret = [iSalesDB execSql:sql];
  7816. //
  7817. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7818. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7819. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7820. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7821. //
  7822. //// return [RAConvertor dict2data:dic];
  7823. // return dic;
  7824. //
  7825. //}
  7826. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7827. {
  7828. DebugLog(@"order set discount params: %@",params);
  7829. // "cartitem_id" = 1;
  7830. // discount = "0.000000";
  7831. // "item_note" = "";
  7832. // price = "269.000000";
  7833. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7834. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7835. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7836. // NSString *price = [self valueInParams:params key:@"price"];
  7837. // bool badd_price_changed=false;
  7838. // sqlite3* db=[iSalesDB get_db];
  7839. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7840. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7841. // NSRange range;
  7842. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7843. //
  7844. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7845. // badd_price_changed=true;
  7846. // [iSalesDB close_db:db];
  7847. //
  7848. // if(badd_price_changed)
  7849. // {
  7850. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7851. // }
  7852. //
  7853. // sqlite3 *db = [iSalesDB get_db];
  7854. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7855. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7856. //NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f, total_price=%f where so_id = '%@'",discount.doubleValue,total_price*(1-discount.doubleValue/100.0),order_code];
  7857. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7858. int ret = [iSalesDB execSql:sql];
  7859. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7860. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7861. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7862. // [dic setValue:@"160409" forKey:@"min_ver"];
  7863. // return [RAConvertor dict2data:dic];
  7864. return dic;
  7865. }
  7866. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7867. {
  7868. DebugLog(@"cart set price params: %@",params);
  7869. // "cartitem_id" = 1;
  7870. // discount = "0.000000";
  7871. // "item_note" = "";
  7872. // price = "269.000000";
  7873. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7874. NSString *notes = [self valueInParams:params key:@"item_note"];
  7875. NSString *discount = [self valueInParams:params key:@"discount"];
  7876. NSString *price = [self valueInParams:params key:@"price"];
  7877. // bool badd_price_changed=false;
  7878. // sqlite3* db=[iSalesDB get_db];
  7879. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7880. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7881. // NSRange range;
  7882. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7883. //
  7884. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7885. // badd_price_changed=true;
  7886. // [iSalesDB close_db:db];
  7887. //
  7888. // if(badd_price_changed)
  7889. // {
  7890. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7891. // }
  7892. //
  7893. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7894. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7895. int ret = [iSalesDB execSql:sql];
  7896. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7897. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7898. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7899. // [dic setValue:@"160409" forKey:@"min_ver"];
  7900. return [RAConvertor dict2data:dic];
  7901. }
  7902. #pragma mark set line notes
  7903. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7904. {
  7905. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7906. NSString *notes = [self valueInParams:params key:@"notes"];
  7907. notes = [self translateSingleQuote:notes];
  7908. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7909. int ret = [iSalesDB execSql:sql];
  7910. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7911. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7912. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7913. // [dic setValue:@"160409" forKey:@"min_ver"];
  7914. return [RAConvertor dict2data:dic];
  7915. }
  7916. #pragma mark set qty
  7917. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7918. {
  7919. assert(params[@"can_create_backorder"]!=nil);
  7920. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7921. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7922. int item_count = [params[@"inputInt"] intValue];
  7923. // 购买检查数量大于库存
  7924. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7925. if (!params[@"can_create_backorder"]) {
  7926. 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];
  7927. __block BOOL out_of_stock = NO;
  7928. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7929. int availability = sqlite3_column_int(stmt, 0);
  7930. if (availability < item_count) {
  7931. out_of_stock = YES;
  7932. }
  7933. }];
  7934. if (out_of_stock) { // 缺货
  7935. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7936. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7937. return [RAConvertor dict2data:dic];
  7938. }
  7939. }
  7940. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7941. int ret = [iSalesDB execSql:sql];
  7942. __block int item_id = 0;
  7943. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7944. item_id = sqlite3_column_int(stmt, 0);
  7945. }];
  7946. sqlite3 *db = [iSalesDB get_db];
  7947. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7948. [iSalesDB close_db:db];
  7949. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7950. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7951. // [dic setValue:@"160409" forKey:@"min_ver"];
  7952. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7953. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7954. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7955. return [RAConvertor dict2data:dic];
  7956. }
  7957. #pragma mark place order
  7958. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7959. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7960. if (aname) {
  7961. [dic setValue:aname forKey:@"aname"];
  7962. }
  7963. if (control) {
  7964. [dic setValue:control forKey:@"control"];
  7965. }
  7966. if (keyboard) {
  7967. [dic setValue:keyboard forKey:@"keyboard"];
  7968. }
  7969. if (name) {
  7970. [dic setValue:name forKey:@"name"];
  7971. }
  7972. if (value) {
  7973. [dic setValue:value forKey:@"value"];
  7974. }
  7975. return dic;
  7976. }
  7977. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7978. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7979. orderCode = [self translateSingleQuote:orderCode];
  7980. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7981. 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];
  7982. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7983. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7984. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7985. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7986. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7987. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7988. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7989. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7990. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7991. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7992. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7993. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7994. NSString *email = [self textAtColumn:11 statement:stmt];
  7995. NSString *phone = [self textAtColumn:12 statement:stmt];
  7996. NSString *fax = [self textAtColumn:13 statement:stmt];
  7997. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7998. NSString *city = [self textAtColumn:15 statement:stmt];
  7999. NSString *state = [self textAtColumn:16 statement:stmt];
  8000. NSString *country = [self textAtColumn:17 statement:stmt];
  8001. NSString *name = [self textAtColumn:18 statement:stmt];
  8002. // contact id
  8003. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  8004. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  8005. [contact_id_dic setValue:@"text" forKey:@"control"];
  8006. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  8007. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  8008. [contact_id_dic setValue:@"true" forKey:@"required"];
  8009. [contact_id_dic setValue:contact_id forKey:@"value"];
  8010. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  8011. // business card
  8012. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  8013. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  8014. [business_card_dic setValue:@"img" forKey:@"control"];
  8015. [business_card_dic setValue:@"1" forKey:@"disable"];
  8016. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  8017. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  8018. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  8019. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  8020. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  8021. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  8022. [business_card_dic setValue:@"business_card" forKey:@"name"];
  8023. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  8024. // fax
  8025. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8026. [customer_dic setValue:fax_dic forKey:@"item_10"];
  8027. // zipcode
  8028. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  8029. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  8030. // city
  8031. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  8032. [customer_dic setValue:city_dic forKey:@"item_12"];
  8033. // state
  8034. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  8035. [customer_dic setValue:state_dic forKey:@"item_13"];
  8036. // country
  8037. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  8038. [customer_dic setValue:country_dic forKey:@"item_14"];
  8039. // company name
  8040. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  8041. [customer_dic setValue:company_dic forKey:@"item_2"];
  8042. // addr_1
  8043. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  8044. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  8045. // addr_2
  8046. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  8047. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  8048. // addr_3
  8049. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  8050. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  8051. // addr_4
  8052. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  8053. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  8054. // Contact
  8055. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  8056. [customer_dic setValue:contact_dic forKey:@"item_7"];
  8057. // email
  8058. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8059. [customer_dic setValue:email_dic forKey:@"item_8"];
  8060. // phone
  8061. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8062. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8063. // title
  8064. [customer_dic setValue:@"Customer" forKey:@"title"];
  8065. // count
  8066. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8067. }];
  8068. // setting
  8069. NSDictionary *setting = params[@"setting"];
  8070. NSNumber *hide = setting[@"CustomerHide"];
  8071. [customer_dic setValue:hide forKey:@"hide"];
  8072. return customer_dic;
  8073. }
  8074. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8075. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8076. // setting
  8077. NSDictionary *setting = params[@"setting"];
  8078. NSNumber *hide = setting[@"ShipToHide"];
  8079. [dic setValue:hide forKey:@"hide"];
  8080. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8081. orderCode = [self translateSingleQuote:orderCode];
  8082. 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];
  8083. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8084. NSString *cid = [self textAtColumn:0 statement:stmt];
  8085. NSString *name = [self textAtColumn:1 statement:stmt];
  8086. NSString *ext = [self textAtColumn:2 statement:stmt];
  8087. NSString *contact = [self textAtColumn:3 statement:stmt];
  8088. NSString *email = [self textAtColumn:4 statement:stmt];
  8089. NSString *fax = [self textAtColumn:5 statement:stmt];
  8090. NSString *phone = [self textAtColumn:6 statement:stmt];
  8091. // count
  8092. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8093. // title
  8094. [dic setValue:@"Ship To" forKey:@"title"];
  8095. // choose
  8096. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8097. [choose_dic setValue:@"choose" forKey:@"aname"];
  8098. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8099. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8100. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8101. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8102. @"receive_contact" : @"customer_contact",
  8103. @"receive_email" : @"customer_email",
  8104. @"receive_ext" : @"customer_contact_ext",
  8105. @"receive_fax" : @"customer_fax",
  8106. @"receive_name" : @"customer_name",
  8107. @"receive_phone" : @"customer_phone"},
  8108. @"refresh" : [NSNumber numberWithInteger:1],
  8109. @"type" : @"pull"};
  8110. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8111. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8112. @"name" : @"Add new address",
  8113. @"refresh" : [NSNumber numberWithInteger:1],
  8114. @"value" : @"new_addr"
  8115. };
  8116. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8117. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8118. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8119. @"receive_contact" : @"customer_contact",
  8120. @"receive_email" : @"customer_email",
  8121. @"receive_ext" : @"customer_contact_ext",
  8122. @"receive_fax" : @"customer_fax",
  8123. @"receive_name" : @"customer_name",
  8124. @"receive_phone" : @"customer_phone"},
  8125. @"name" : @"select_ship_to",
  8126. @"refresh" : [NSNumber numberWithInteger:1],
  8127. @"value" : @"Sales_Order_Ship_To"};
  8128. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8129. [dic setValue:choose_dic forKey:@"item_0"];
  8130. // contact id
  8131. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8132. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8133. [contact_id_dic setValue:@"true" forKey:@"required"];
  8134. [dic setValue:contact_id_dic forKey:@"item_1"];
  8135. // company name
  8136. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8137. [dic setValue:company_name_dic forKey:@"item_2"];
  8138. // address
  8139. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8140. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8141. if ([required integerValue]) {
  8142. [ext_dic setValue:@"true" forKey:@"required"];
  8143. }
  8144. [dic setValue:ext_dic forKey:@"item_3"];
  8145. // contact
  8146. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8147. [dic setValue:contact_dic forKey:@"item_4"];
  8148. // phone
  8149. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8150. [dic setValue:phone_dic forKey:@"item_5"];
  8151. // fax
  8152. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8153. [dic setValue:fax_dic forKey:@"item_6"];
  8154. // email
  8155. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8156. [dic setValue:email_dic forKey:@"item_7"];
  8157. }];
  8158. return dic;
  8159. }
  8160. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8161. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8162. // setting
  8163. NSDictionary *setting = params[@"setting"];
  8164. NSNumber *hide = setting[@"ShipFromHide"];
  8165. [dic setValue:hide forKey:@"hide"];
  8166. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8167. orderCode = [self translateSingleQuote:orderCode];
  8168. 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];
  8169. 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';";
  8170. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8171. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8172. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8173. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8174. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8175. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8176. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8177. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8178. if (!cid.length) {
  8179. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8180. cid = [self textAtColumn:0 statement:statment];
  8181. name = [self textAtColumn:1 statement:statment];
  8182. ext = [self textAtColumn:2 statement:statment];
  8183. contact = [self textAtColumn:3 statement:statment];
  8184. email = [self textAtColumn:4 statement:statment];
  8185. fax = [self textAtColumn:5 statement:statment];
  8186. phone = [self textAtColumn:6 statement:statment];
  8187. }];
  8188. }
  8189. // count
  8190. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8191. // title
  8192. [dic setValue:@"Ship From" forKey:@"title"];
  8193. // hide
  8194. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8195. // choose
  8196. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8197. [choose_dic setValue:@"choose" forKey:@"aname"];
  8198. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8199. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8200. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8201. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8202. @"sender_contact" : @"customer_contact",
  8203. @"sender_email" : @"customer_email",
  8204. @"sender_ext" : @"customer_contact_ext",
  8205. @"sender_fax" : @"customer_fax",
  8206. @"sender_name" : @"customer_name",
  8207. @"sender_phone" : @"customer_phone"},
  8208. @"name" : @"select_cid",
  8209. @"refresh" : [NSNumber numberWithInteger:0],
  8210. @"value" : @"Sales_Order_Ship_From"};
  8211. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8212. [dic setValue:choose_dic forKey:@"item_0"];
  8213. // contact id
  8214. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8215. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8216. [contact_id_dic setValue:@"true" forKey:@"required"];
  8217. [dic setValue:contact_id_dic forKey:@"item_1"];
  8218. // company name
  8219. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8220. [dic setValue:company_name_dic forKey:@"item_2"];
  8221. // address
  8222. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8223. [dic setValue:ext_dic forKey:@"item_3"];
  8224. // contact
  8225. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8226. [dic setValue:contact_dic forKey:@"item_4"];
  8227. // phone
  8228. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8229. [dic setValue:phone_dic forKey:@"item_5"];
  8230. // fax
  8231. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8232. [dic setValue:fax_dic forKey:@"item_6"];
  8233. // email
  8234. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8235. [dic setValue:email_dic forKey:@"item_7"];
  8236. }];
  8237. return dic;
  8238. }
  8239. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8240. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8241. // setting
  8242. NSDictionary *setting = params[@"setting"];
  8243. NSNumber *hide = setting[@"FreightBillToHide"];
  8244. [dic setValue:hide forKey:@"hide"];
  8245. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8246. orderCode = [self translateSingleQuote:orderCode];
  8247. 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];
  8248. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8249. NSString *cid = [self textAtColumn:0 statement:stmt];
  8250. NSString *name = [self textAtColumn:1 statement:stmt];
  8251. NSString *ext = [self textAtColumn:2 statement:stmt];
  8252. NSString *contact = [self textAtColumn:3 statement:stmt];
  8253. NSString *email = [self textAtColumn:4 statement:stmt];
  8254. NSString *fax = [self textAtColumn:5 statement:stmt];
  8255. NSString *phone = [self textAtColumn:6 statement:stmt];
  8256. // count
  8257. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8258. // title
  8259. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8260. // hide
  8261. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8262. // choose
  8263. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8264. [choose_dic setValue:@"choose" forKey:@"aname"];
  8265. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8266. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8267. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8268. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8269. @"shipping_billto_contact" : @"receive_contact",
  8270. @"shipping_billto_email" : @"receive_email",
  8271. @"shipping_billto_ext" : @"receive_ext",
  8272. @"shipping_billto_fax" : @"receive_fax",
  8273. @"shipping_billto_name" : @"receive_name",
  8274. @"shipping_billto_phone" : @"receive_phone"},
  8275. @"type" : @"pull"};
  8276. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8277. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8278. @"type" : @"pull",
  8279. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8280. @"shipping_billto_contact" : @"customer_contact",
  8281. @"shipping_billto_email" : @"customer_email",
  8282. @"shipping_billto_ext" : @"customer_contact_ext",
  8283. @"shipping_billto_fax" : @"customer_fax",
  8284. @"shipping_billto_name" : @"customer_name",
  8285. @"shipping_billto_phone" : @"customer_phone"}
  8286. };
  8287. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8288. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8289. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8290. @"shipping_billto_contact" : @"sender_contact",
  8291. @"shipping_billto_email" : @"sender_email",
  8292. @"shipping_billto_ext" : @"sender_ext",
  8293. @"shipping_billto_fax" : @"sender_fax",
  8294. @"shipping_billto_name" : @"sender_name",
  8295. @"shipping_billto_phone" : @"sender_phone"},
  8296. @"type" : @"pull"
  8297. };
  8298. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8299. NSDictionary *select_freight_bill_to_dic = @{
  8300. @"aname" : @"Select freight bill to",
  8301. @"name" : @"select_cid",
  8302. @"refresh" : [NSNumber numberWithInteger:0],
  8303. @"value" : @"Sales_Order_Freight_Bill_To",
  8304. @"key_map" : @{
  8305. @"shipping_billto_cid" : @"customer_cid",
  8306. @"shipping_billto_contact" : @"customer_contact",
  8307. @"shipping_billto_email" : @"customer_email",
  8308. @"shipping_billto_ext" : @"customer_contact_ext",
  8309. @"shipping_billto_fax" : @"customer_fax",
  8310. @"shipping_billto_name" : @"customer_name",
  8311. @"shipping_billto_phone" : @"customer_phone"
  8312. }
  8313. };
  8314. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8315. [dic setValue:choose_dic forKey:@"item_0"];
  8316. // contact id
  8317. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8318. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8319. [contact_id_dic setValue:@"true" forKey:@"required"];
  8320. [dic setValue:contact_id_dic forKey:@"item_1"];
  8321. // company name
  8322. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8323. [dic setValue:company_name_dic forKey:@"item_2"];
  8324. // address
  8325. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8326. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8327. if ([ext_required integerValue]) {
  8328. [ext_dic setValue:@"true" forKey:@"required"];
  8329. }
  8330. [dic setValue:ext_dic forKey:@"item_3"];
  8331. // contact
  8332. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8333. [dic setValue:contact_dic forKey:@"item_4"];
  8334. // phone
  8335. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8336. [dic setValue:phone_dic forKey:@"item_5"];
  8337. // fax
  8338. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8339. [dic setValue:fax_dic forKey:@"item_6"];
  8340. // email
  8341. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8342. [dic setValue:email_dic forKey:@"item_7"];
  8343. }];
  8344. return dic;
  8345. }
  8346. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8347. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8348. // setting
  8349. NSDictionary *setting = params[@"setting"];
  8350. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8351. [dic setValue:hide forKey:@"hide"];
  8352. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8353. orderCode = [self translateSingleQuote:orderCode];
  8354. 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];
  8355. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8356. NSString *cid = [self textAtColumn:0 statement:stmt];
  8357. NSString *name = [self textAtColumn:1 statement:stmt];
  8358. NSString *ext = [self textAtColumn:2 statement:stmt];
  8359. NSString *contact = [self textAtColumn:3 statement:stmt];
  8360. NSString *email = [self textAtColumn:4 statement:stmt];
  8361. NSString *fax = [self textAtColumn:5 statement:stmt];
  8362. NSString *phone = [self textAtColumn:6 statement:stmt];
  8363. // count
  8364. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8365. // title
  8366. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8367. // hide
  8368. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8369. // choose
  8370. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8371. [choose_dic setValue:@"choose" forKey:@"aname"];
  8372. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8373. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8374. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8375. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8376. @"billing_contact" : @"receive_contact",
  8377. @"billing_email" : @"receive_email",
  8378. @"billing_ext" : @"receive_ext",
  8379. @"billing_fax" : @"receive_fax",
  8380. @"billing_name" : @"receive_name",
  8381. @"billing_phone" : @"receive_phone"},
  8382. @"type" : @"pull"};
  8383. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8384. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8385. @"type" : @"pull",
  8386. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8387. @"billing_contact" : @"customer_contact",
  8388. @"billing_email" : @"customer_email",
  8389. @"billing_ext" : @"customer_contact_ext",
  8390. @"billing_fax" : @"customer_fax",
  8391. @"billing_name" : @"customer_name",
  8392. @"billing_phone" : @"customer_phone"}
  8393. };
  8394. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8395. NSDictionary *select_bill_to_dic = @{
  8396. @"aname" : @"Select bill to",
  8397. @"name" : @"select_cid",
  8398. @"refresh" : [NSNumber numberWithInteger:0],
  8399. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8400. @"key_map" : @{
  8401. @"billing_cid" : @"customer_cid",
  8402. @"billing_contact" : @"customer_contact",
  8403. @"billing_email" : @"customer_email",
  8404. @"billing_ext" : @"customer_contact_ext",
  8405. @"billing_fax" : @"customer_fax",
  8406. @"billing_name" : @"customer_name",
  8407. @"billing_phone" : @"customer_phone"
  8408. }
  8409. };
  8410. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8411. [dic setValue:choose_dic forKey:@"item_0"];
  8412. // contact id
  8413. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8414. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8415. [contact_id_dic setValue:@"true" forKey:@"required"];
  8416. [dic setValue:contact_id_dic forKey:@"item_1"];
  8417. // company name
  8418. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8419. [dic setValue:company_name_dic forKey:@"item_2"];
  8420. // address
  8421. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8422. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8423. if ([ext_required integerValue]) {
  8424. [ext_dic setValue:@"true" forKey:@"required"];
  8425. }
  8426. [dic setValue:ext_dic forKey:@"item_3"];
  8427. // contact
  8428. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8429. [dic setValue:contact_dic forKey:@"item_4"];
  8430. // phone
  8431. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8432. [dic setValue:phone_dic forKey:@"item_5"];
  8433. // fax
  8434. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8435. [dic setValue:fax_dic forKey:@"item_6"];
  8436. // email
  8437. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8438. [dic setValue:email_dic forKey:@"item_7"];
  8439. }];
  8440. return dic;
  8441. }
  8442. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8443. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8444. // setting
  8445. NSDictionary *setting = params[@"setting"];
  8446. NSNumber *hide = setting[@"ReturnToHide"];
  8447. [dic setValue:hide forKey:@"hide"];
  8448. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8449. orderCode = [self translateSingleQuote:orderCode];
  8450. 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];
  8451. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8452. NSString *cid = [self textAtColumn:0 statement:stmt];
  8453. NSString *name = [self textAtColumn:1 statement:stmt];
  8454. NSString *ext = [self textAtColumn:2 statement:stmt];
  8455. NSString *contact = [self textAtColumn:3 statement:stmt];
  8456. NSString *email = [self textAtColumn:4 statement:stmt];
  8457. NSString *fax = [self textAtColumn:5 statement:stmt];
  8458. NSString *phone = [self textAtColumn:6 statement:stmt];
  8459. // count
  8460. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8461. // title
  8462. [dic setValue:@"Return To" forKey:@"title"];
  8463. // hide
  8464. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8465. // choose
  8466. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8467. [choose_dic setValue:@"choose" forKey:@"aname"];
  8468. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8469. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8470. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8471. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8472. @"returnto_contact" : @"sender_contact",
  8473. @"returnto_email" : @"sender_email",
  8474. @"returnto_ext" : @"sender_ext",
  8475. @"returnto_fax" : @"sender_fax",
  8476. @"returnto_name" : @"sender_name",
  8477. @"returnto_phone" : @"sender_phone"},
  8478. @"type" : @"pull"};
  8479. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8480. NSDictionary *select_return_to_dic = @{
  8481. @"aname" : @"Select return to",
  8482. @"name" : @"select_cid",
  8483. @"refresh" : [NSNumber numberWithInteger:0],
  8484. @"value" : @"Contact_Return_To",
  8485. @"key_map" : @{
  8486. @"returnto_cid" : @"customer_cid",
  8487. @"returnto_contact" : @"customer_contact",
  8488. @"returnto_email" : @"customer_email",
  8489. @"returnto_ext" : @"customer_contact_ext",
  8490. @"returnto_fax" : @"customer_fax",
  8491. @"returnto_name" : @"customer_name",
  8492. @"returnto_phone" : @"customer_phone"
  8493. }
  8494. };
  8495. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8496. [dic setValue:choose_dic forKey:@"item_0"];
  8497. // contact id
  8498. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8499. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8500. [contact_id_dic setValue:@"true" forKey:@"required"];
  8501. [dic setValue:contact_id_dic forKey:@"item_1"];
  8502. // company name
  8503. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8504. [dic setValue:company_name_dic forKey:@"item_2"];
  8505. // address
  8506. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8507. [dic setValue:ext_dic forKey:@"item_3"];
  8508. // contact
  8509. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8510. [dic setValue:contact_dic forKey:@"item_4"];
  8511. // phone
  8512. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8513. [dic setValue:phone_dic forKey:@"item_5"];
  8514. // fax
  8515. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8516. [dic setValue:fax_dic forKey:@"item_6"];
  8517. // email
  8518. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8519. [dic setValue:email_dic forKey:@"item_7"];
  8520. }];
  8521. return dic;
  8522. }
  8523. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8524. {
  8525. assert(params[@"user"]!=nil);
  8526. assert(params[@"contact_id"]!=nil);
  8527. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8528. orderCode = [self translateSingleQuote:orderCode];
  8529. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8530. __block double TotalCuft = 0;
  8531. __block double TotalWeight = 0;
  8532. __block int TotalCarton = 0;
  8533. __block double payments = 0;
  8534. // setting
  8535. NSDictionary *setting = params[@"setting"];
  8536. NSNumber *hide = setting[@"ModelInformationHide"];
  8537. [dic setValue:hide forKey:@"hide"];
  8538. 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];
  8539. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8540. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8541. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8542. // item id
  8543. int item_id = sqlite3_column_int(stmt, 0);
  8544. // count
  8545. int item_count = sqlite3_column_int(stmt, 1);
  8546. // stockUom
  8547. int stockUom = sqlite3_column_int(stmt, 2);
  8548. // unit price
  8549. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8550. NSString* Price=nil;
  8551. if([str_price isEqualToString:@""])
  8552. {
  8553. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8554. if(price==nil)
  8555. Price=@"No Price.";
  8556. else
  8557. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8558. }
  8559. else
  8560. {
  8561. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8562. }
  8563. // discount
  8564. double discount = sqlite3_column_double(stmt, 4);
  8565. // name
  8566. NSString *name = [self textAtColumn:5 statement:stmt];
  8567. // description
  8568. NSString *description = [self textAtColumn:6 statement:stmt];
  8569. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8570. // line note
  8571. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8572. int avaulability = sqlite3_column_int(stmt, 8);
  8573. // img
  8574. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8575. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8576. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8577. if(combine != nil)
  8578. {
  8579. // int citem=0;
  8580. int bcount=[[combine valueForKey:@"count"] intValue];
  8581. for(int bc=0;bc<bcount;bc++)
  8582. {
  8583. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8584. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8585. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8586. subTotal += uprice * modulus * item_count;
  8587. }
  8588. }
  8589. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8590. [model_dic setValue:@"model" forKey:@"control"];
  8591. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8592. [model_dic setValue:description forKey:@"description"];
  8593. [model_dic setValue:line_note forKey:@"note"];
  8594. [model_dic setValue:img forKey:@"img_url"];
  8595. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8596. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8597. [model_dic setValue:Price forKey:@"unit_price"];
  8598. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8599. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8600. if (combine) {
  8601. [model_dic setValue:combine forKey:@"combine"];
  8602. }
  8603. // well,what under the row is the info for total
  8604. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8605. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8606. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8607. int carton=[bsubtotaljson[@"carton"] intValue];
  8608. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8609. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8610. TotalCuft += cuft;
  8611. TotalWeight += weight;
  8612. TotalCarton += carton;
  8613. payments += subTotal;
  8614. //---------------------------
  8615. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8616. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8617. }];
  8618. [dic setValue:@"Model Information" forKey:@"title"];
  8619. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8620. return dic;
  8621. }
  8622. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8623. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8624. [dic setValue:@"Remarks Content" forKey:@"title"];
  8625. // setting
  8626. NSDictionary *setting = params[@"setting"];
  8627. NSNumber *hide = setting[@"RemarksContentHide"];
  8628. [dic setValue:hide forKey:@"hide"];
  8629. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8630. orderCode = [self translateSingleQuote:orderCode];
  8631. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8632. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8633. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8634. int mustCall = sqlite3_column_int(stmt, 1);
  8635. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8636. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8637. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8638. NSDictionary *po_dic = @{
  8639. @"aname" : @"PO#",
  8640. @"control" : @"edit",
  8641. @"keyboard" : @"text",
  8642. @"name" : @"poNumber",
  8643. @"value" : poNumber
  8644. };
  8645. #ifdef BUILD_CONTRAST
  8646. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8647. NSDictionary *schedule_dict = @{
  8648. @"aname" : @"Schdule Date",
  8649. @"control" : @"datepicker",
  8650. @"type": @"date",
  8651. @"required": @"true",
  8652. @"name" : @"schedule_date_str",
  8653. @"value" : nsdate
  8654. };
  8655. #endif
  8656. NSDictionary *must_call_dic = @{
  8657. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8658. @"control" : @"switch",
  8659. @"name" : @"must_call",
  8660. @"value" : mustCall ? @"true" : @"false"
  8661. };
  8662. NSDictionary *general_notes_dic = @{
  8663. @"aname" : @"General notes",
  8664. @"control" : @"text_view",
  8665. @"keyboard" : @"text",
  8666. @"name" : @"comments",
  8667. @"value" : generalNotes
  8668. };
  8669. // NSDictionary *internal_notes_dic = @{
  8670. // @"aname" : @"Internal notes",
  8671. // @"control" : @"text_view",
  8672. // @"keyboard" : @"text",
  8673. // @"name" : @"internal_notes",
  8674. // @"value" : internalNotes
  8675. // };
  8676. #ifdef BUILD_CONTRAST
  8677. [dic setValue:po_dic forKey:@"item_0"];
  8678. [dic setValue:schedule_dict forKey:@"item_1"];
  8679. [dic setValue:must_call_dic forKey:@"item_2"];
  8680. [dic setValue:general_notes_dic forKey:@"item_3"];
  8681. #else
  8682. [dic setValue:po_dic forKey:@"item_0"];
  8683. [dic setValue:must_call_dic forKey:@"item_1"];
  8684. [dic setValue:general_notes_dic forKey:@"item_2"];
  8685. #endif
  8686. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8687. }];
  8688. #ifdef BUILD_CONTRAST
  8689. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8690. #else
  8691. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8692. #endif
  8693. return dic;
  8694. }
  8695. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8696. // params
  8697. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8698. orderCode = [self translateSingleQuote:orderCode];
  8699. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8700. // setting
  8701. NSDictionary *setting = params[@"setting"];
  8702. NSNumber *hide = setting[@"OrderTotalHide"];
  8703. [dic setValue:hide forKey:@"hide"];
  8704. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8705. __block double lift_gate_value = 0;
  8706. __block double handling_fee = 0;
  8707. __block double shipping = 0;
  8708. __block double discount = 0;
  8709. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8710. int lift_gate = sqlite3_column_int(stmt, 0);
  8711. lift_gate_value = sqlite3_column_double(stmt, 1);
  8712. shipping = sqlite3_column_double(stmt, 2);
  8713. handling_fee = sqlite3_column_double(stmt, 3);
  8714. discount = sqlite3_column_double(stmt, 4);
  8715. if (!lift_gate) {
  8716. lift_gate_value = 0;
  8717. }
  8718. }];
  8719. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8720. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8721. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8722. double payments = [[total valueForKey:@"payments"] doubleValue];
  8723. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8724. double totalPrice = payments* (1-discount/100.0);
  8725. [dic setValue:@"Order Total" forKey:@"title"];
  8726. NSDictionary *payments_dic = @{
  8727. @"align" : @"right",
  8728. @"aname" : @"Payments/Credits",
  8729. @"control" : @"text",
  8730. @"name" : @"paymentsAndCredits",
  8731. @"type" : @"price",
  8732. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8733. };
  8734. [dic setValue:payments_dic forKey:@"item_0"];
  8735. // version 1.71 remove
  8736. // NSDictionary *handling_fee_dic = @{
  8737. // @"align" : @"right",
  8738. // @"aname" : @"Handling Fee",
  8739. // @"control" : @"text",
  8740. // @"name" : @"handling_fee_value",
  8741. // @"required" : @"true",
  8742. // @"type" : @"price",
  8743. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8744. // };
  8745. NSDictionary *shipping_dic = @{
  8746. @"align" : @"right",
  8747. @"aname" : @"Shipping*",
  8748. @"control" : @"text",
  8749. @"name" : @"shipping",
  8750. @"required" : @"true",
  8751. @"type" : @"price",
  8752. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8753. };
  8754. NSDictionary *lift_gate_dic = @{
  8755. @"align" : @"right",
  8756. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8757. @"control" : @"text",
  8758. @"name" : @"lift_gate_value",
  8759. @"required" : @"true",
  8760. @"type" : @"price",
  8761. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8762. };
  8763. int item_count = 1;
  8764. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8765. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8766. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8767. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8768. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8769. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8770. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8771. } else {
  8772. }
  8773. }
  8774. // version 1.71 remove
  8775. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8776. // NSString* strdiscount;
  8777. if(discount>0)
  8778. {
  8779. NSDictionary *discount_dict = @{
  8780. @"align" : @"right",
  8781. @"aname" : @"Order Discount",
  8782. @"control" : @"text",
  8783. @"name" : @"orderDiscount",
  8784. // @"type" : @"price",
  8785. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8786. };
  8787. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8788. }
  8789. NSDictionary *total_price_dic = @{
  8790. @"align" : @"right",
  8791. @"aname" : @"Total",
  8792. @"control" : @"text",
  8793. @"name" : @"totalPrice",
  8794. @"type" : @"price",
  8795. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8796. };
  8797. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8798. NSDictionary *total_cuft_dic = @{
  8799. @"align" : @"right",
  8800. @"aname" : @"Total Cuft",
  8801. @"control" : @"text",
  8802. @"name" : @"",
  8803. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8804. };
  8805. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8806. NSDictionary *total_weight_dic = @{
  8807. @"align" : @"right",
  8808. @"aname" : @"Total Weight",
  8809. @"control" : @"text",
  8810. @"name" : @"",
  8811. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8812. };
  8813. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8814. NSDictionary *total_carton_dic = @{
  8815. @"align" : @"right",
  8816. @"aname" : @"Total Carton",
  8817. @"control" : @"text",
  8818. @"name" : @"",
  8819. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8820. };
  8821. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8822. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8823. return dic;
  8824. }
  8825. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8826. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8827. orderCode = [self translateSingleQuote:orderCode];
  8828. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8829. // setting
  8830. NSDictionary *setting = params[@"setting"];
  8831. NSNumber *hide = setting[@"SignatureHide"];
  8832. [dic setValue:hide forKey:@"hide"];
  8833. [dic setValue:@"Signature" forKey:@"title"];
  8834. __block NSString *pic_path = @"";
  8835. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8836. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8837. pic_path = [self textAtColumn:0 statement:stmt];
  8838. }];
  8839. NSDictionary *pic_dic = @{
  8840. @"aname" : @"Signature",
  8841. @"avalue" :pic_path,
  8842. @"control" : @"signature",
  8843. @"name" : @"sign_picpath",
  8844. @"value" : pic_path
  8845. };
  8846. [dic setValue:pic_dic forKey:@"item_0"];
  8847. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8848. return dic;
  8849. }
  8850. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8851. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8852. // setting
  8853. NSDictionary *setting = params[@"setting"];
  8854. NSNumber *hide = setting[@"ShippingMethodHide"];
  8855. [dic setValue:hide forKey:@"hide"];
  8856. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8857. orderCode = [self translateSingleQuote:orderCode];
  8858. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8859. __block NSString *logist = @"";
  8860. __block NSString *carrier = @"";
  8861. __block NSString *lift_gate = @"";
  8862. __block int lift_gate_integer = 0;
  8863. __block NSString *logistic_note = @"";
  8864. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8865. logist = [self textAtColumn:0 statement:stmt];
  8866. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8867. logistic_note = [self textAtColumn:2 statement:stmt];
  8868. carrier = [self textAtColumn:3 statement:stmt];
  8869. }];
  8870. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8871. [dic setValue:@"Shipping Method" forKey:@"title"];
  8872. // val_0
  8873. int PERSONAL_PICK_UP_check = 0;
  8874. int USE_MY_CARRIER_check = 0;
  8875. NSString *logistic_note_text = @"";
  8876. int will_call_check = 0;
  8877. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8878. will_call_check = 1;
  8879. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8880. PERSONAL_PICK_UP_check = 1;
  8881. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8882. USE_MY_CARRIER_check = 1;
  8883. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8884. logistic_note = [logistic_note_array firstObject];
  8885. if (logistic_note_array.count > 1) {
  8886. logistic_note_text = [logistic_note_array lastObject];
  8887. }
  8888. }
  8889. }
  8890. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8891. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8892. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8893. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8894. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8895. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8896. NSDictionary *val0_subItem_item0 = @{
  8897. @"aname" : @"Option",
  8898. @"cadedate" : @{
  8899. @"count" : [NSNumber numberWithInteger:2],
  8900. @"val_0" : @{
  8901. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8902. @"refresh" : [NSNumber numberWithInteger:0],
  8903. @"value" : @"PERSONAL PICK UP",
  8904. @"value_id" : @"PERSONAL PICK UP"
  8905. },
  8906. @"val_1" : @{
  8907. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8908. @"refresh" : [NSNumber numberWithInteger:0],
  8909. @"sub_item" : @{
  8910. @"count" : [NSNumber numberWithInteger:1],
  8911. @"item_0" : @{
  8912. @"aname" : @"BOL",
  8913. @"control" : @"edit",
  8914. @"keyboard" : @"text",
  8915. @"name" : @"logist_note_text",
  8916. @"refresh" : [NSNumber numberWithInteger:0],
  8917. @"required" : @"false",
  8918. @"value" : logistic_note_text
  8919. }
  8920. },
  8921. @"value" : @"USE MY CARRIER",
  8922. @"value_id" : @"USE MY CARRIER"
  8923. }
  8924. },
  8925. @"control" : @"enum",
  8926. @"name" : @"logistic_note",
  8927. @"required" : @"true",
  8928. @"single_select" : @"true"
  8929. };
  8930. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8931. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8932. // val_1
  8933. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8934. if([lift_gate isEqualToString:@""]) {
  8935. if (lift_gate_integer == 1) {
  8936. lift_gate = @"true";
  8937. } else {
  8938. lift_gate = @"false";
  8939. }
  8940. }
  8941. // carrier = [self valueInParams:params key:@"carrier"];
  8942. // if([carrier isEqualToString:@""]) {
  8943. //
  8944. // if (lift_gate_integer == 1) {
  8945. // lift_gate = @"true";
  8946. // } else {
  8947. // lift_gate = @"false";
  8948. // }
  8949. //
  8950. // }
  8951. int common_carrier_check = 0;
  8952. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8953. common_carrier_check = 1;
  8954. [params setValue:lift_gate forKey:@"lift_gate"];
  8955. #ifdef BUILD_CONTRAST
  8956. [params setValue:carrier forKey:@"carrier"];
  8957. #endif
  8958. }
  8959. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8960. // NSString * code = [iSalesDB jk_queryText:Sql];
  8961. // [self offline_getCarrierCode:offline_getCarrierCode];
  8962. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8963. NSDictionary *val_1 = @{
  8964. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8965. @"sub_item" : @{
  8966. @"item_0" : @{
  8967. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8968. @"control" : @"switch",
  8969. @"name" : @"lift_gate",
  8970. @"refresh" : [NSNumber numberWithInteger:1],
  8971. @"required" : @"true",
  8972. @"value" : lift_gate
  8973. },
  8974. #ifdef BUILD_CONTRAST
  8975. @"count" : [NSNumber numberWithInteger:2],
  8976. @"item_1" : @{
  8977. @"aname" : @"Carrier",
  8978. @"cadedate" :carrierDic,
  8979. @"control" : @"enum",
  8980. @"name" : @"carrier",
  8981. @"required" : @"false",
  8982. @"value" : @"",
  8983. @"single_select" : @"true"
  8984. }
  8985. #else
  8986. @"count" : [NSNumber numberWithInteger:1]
  8987. #endif
  8988. },
  8989. @"value" : @"COMMON CARRIER",
  8990. @"value_id" : @"COMMON CARRIER"
  8991. };
  8992. // cadedate
  8993. NSDictionary *cadedate = @{
  8994. @"count" : [NSNumber numberWithInteger:2],
  8995. @"val_0" : val_0,
  8996. @"val_1" : val_1
  8997. };
  8998. // item_0
  8999. NSMutableDictionary *item_0 = @{
  9000. @"aname" : @"Shipping",
  9001. @"cadedate" : cadedate,
  9002. @"control" : @"enum",
  9003. @"name" : @"logist",
  9004. @"refresh" : [NSNumber numberWithInteger:1],
  9005. @"single_select" : @"true",
  9006. }.mutableCopy;
  9007. NSNumber *required = setting[@"ShippingMethodRequire"];
  9008. if ([required integerValue]) {
  9009. [item_0 setValue:@"true" forKey:@"required"];
  9010. }
  9011. [dic setValue:item_0 forKey:@"item_0"];
  9012. if ([logist isEqualToString:@"WILL CALL"]) {
  9013. [dic removeObjectForKey:@"lift_gate"];
  9014. }
  9015. return dic;
  9016. }
  9017. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  9018. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9019. orderCode = [self translateSingleQuote:orderCode];
  9020. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  9021. __block int submit_as_integer = 0;
  9022. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9023. submit_as_integer = sqlite3_column_int(stmt, 0);
  9024. }];
  9025. int check11 = 0;
  9026. int check10 = 0;
  9027. if (submit_as_integer == 11) {
  9028. check11 = 1;
  9029. }
  9030. if (submit_as_integer == 10) {
  9031. check10 = 1;
  9032. }
  9033. // section_0
  9034. NSMutableDictionary *dic = @{
  9035. @"count" : @(1),
  9036. @"item_0" : @{
  9037. @"aname" : @"Submit Order As",
  9038. @"cadedate" : @{
  9039. @"count" : @(2),
  9040. @"val_0" : @{
  9041. @"check" : [NSNumber numberWithInteger:check11],
  9042. @"value" : @"Sales Order",
  9043. @"value_id" : @(11)
  9044. },
  9045. @"val_1" : @{
  9046. @"check" : [NSNumber numberWithInteger:check10],
  9047. @"value" : @"Quote",
  9048. @"value_id" : @(10)
  9049. }
  9050. },
  9051. @"control" : @"enum",
  9052. @"name" : @"erpOrderStatus",
  9053. @"required" : @"true",
  9054. @"single_select" : @"true"
  9055. },
  9056. @"title" : @"Order Type"
  9057. }.mutableCopy;
  9058. // setting
  9059. NSDictionary *setting = params[@"setting"];
  9060. NSNumber *hide = setting[@"OrderTypeHide"];
  9061. [dic setValue:hide forKey:@"hide"];
  9062. return dic;
  9063. }
  9064. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  9065. countryCode = [self translateSingleQuote:countryCode];
  9066. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  9067. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9068. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  9069. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  9070. if (name == NULL) {
  9071. name = "";
  9072. }
  9073. if (code == NULL) {
  9074. code = "";
  9075. }
  9076. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9077. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  9078. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  9079. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9080. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  9081. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9082. }
  9083. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  9084. [container setValue:stateDic forKey:key];
  9085. }] mutableCopy];
  9086. [ret removeObjectForKey:@"result"];
  9087. // failure 可以不用了,一样的
  9088. if (ret.allKeys.count == 0) {
  9089. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9090. [stateDic setValue:@"Other" forKey:@"value"];
  9091. [stateDic setValue:@"" forKey:@"value_id"];
  9092. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9093. if (state_code && [@"" isEqualToString:state_code]) {
  9094. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9095. }
  9096. NSString *key = [NSString stringWithFormat:@"val_0"];
  9097. [ret setValue:stateDic forKey:key];
  9098. }
  9099. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  9100. return ret;
  9101. }
  9102. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  9103. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9104. orderCode = [self translateSingleQuote:orderCode];
  9105. 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];
  9106. __block NSString *payType = @"";
  9107. __block NSString *firstName = @"";
  9108. __block NSString *lastName = @"";
  9109. __block NSString *addr1 = @"";
  9110. __block NSString *addr2 = @"";
  9111. __block NSString *zipcode = @"";
  9112. __block NSString *cardType = @"";
  9113. __block NSString *cardNumber = @"";
  9114. __block NSString *securityCode = @"";
  9115. __block NSString *month = @"";
  9116. __block NSString *year = @"";
  9117. __block NSString *city = @"";
  9118. __block NSString *state = @"";
  9119. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9120. payType = [self textAtColumn:0 statement:stmt];
  9121. firstName = [self textAtColumn:1 statement:stmt];
  9122. lastName = [self textAtColumn:2 statement:stmt];
  9123. addr1 = [self textAtColumn:3 statement:stmt];
  9124. addr2 = [self textAtColumn:4 statement:stmt];
  9125. zipcode = [self textAtColumn:5 statement:stmt];
  9126. cardType = [self textAtColumn:6 statement:stmt];
  9127. cardNumber = [self textAtColumn:7 statement:stmt];
  9128. securityCode = [self textAtColumn:8 statement:stmt];
  9129. month = [self textAtColumn:9 statement:stmt];
  9130. year = [self textAtColumn:10 statement:stmt];
  9131. city = [self textAtColumn:11 statement:stmt];
  9132. state = [self textAtColumn:12 statement:stmt];
  9133. }];
  9134. NSString *required = @"true";
  9135. // setting
  9136. NSDictionary *setting = params[@"setting"];
  9137. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9138. if ([requiredNumber integerValue]) {
  9139. required = @"true";
  9140. } else {
  9141. required = @"false";
  9142. }
  9143. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9144. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9145. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9146. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9147. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9148. NSString *type = [self textAtColumn:1 statement:stmt];
  9149. NSMutableDictionary *val = @{
  9150. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9151. @"value" : type,
  9152. @"value_id" : type_id
  9153. }.mutableCopy;
  9154. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9155. NSDictionary *sub_item =nil;
  9156. #ifdef BUILD_CONTRAST
  9157. sub_item = @{
  9158. @"count" : @(3),
  9159. @"item_0" : @{
  9160. @"aname" : @"Number",
  9161. @"control" : @"edit",
  9162. @"keyboard" : @"int",
  9163. @"length" : @"16",
  9164. @"name" : @"credit_card_number",
  9165. @"required" : @"false",
  9166. @"value" : cardNumber
  9167. },
  9168. @"item_1" : @{
  9169. @"aname" : @"Expiration Date",
  9170. @"control" : @"monthpicker",
  9171. @"name" : @"credit_card_expiration",
  9172. @"required" : @"false",
  9173. @"type" : @"date",
  9174. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9175. },
  9176. @"item_2" : @{
  9177. @"aname" : @"Security Code",
  9178. @"control" : @"edit",
  9179. @"keyboard" : @"int",
  9180. @"length" : @"4",
  9181. @"name" : @"credit_card_security_code",
  9182. @"required" : @"false",
  9183. @"value" : securityCode
  9184. },
  9185. };
  9186. #else
  9187. sub_item= @{
  9188. @"count" : @(3),
  9189. @"item_0" : @{
  9190. @"aname" : @"choose",
  9191. @"control" : @"multi_action",
  9192. @"count" : @(1),
  9193. @"item_0" : @{
  9194. @"aname" : @"Same as customer",
  9195. @"key_map" : @{
  9196. @"credit_card_address1" : @"customer_address1",
  9197. @"credit_card_address2" : @"customer_address2",
  9198. @"credit_card_city" : @"customer_city",
  9199. @"credit_card_first_name" : @"customer_first_name",
  9200. @"credit_card_last_name" : @"customer_last_name",
  9201. @"credit_card_state" : @"customer_state",
  9202. @"credit_card_zipcode" : @"customer_zipcode"
  9203. },
  9204. @"type" : @"pull"
  9205. }
  9206. },
  9207. @"item_1" : @{
  9208. @"aname" : @"",
  9209. @"color" : @"red",
  9210. @"control" : @"text",
  9211. @"name" : @"",
  9212. @"value" : @"USA Credit cards only"
  9213. },
  9214. @"item_2" : @{
  9215. @"aname" : @"Fill",
  9216. @"cadedate" : @{
  9217. @"count" : @(2),
  9218. @"val_0" : @{
  9219. @"check" : @(1),
  9220. @"sub_item" : @{
  9221. @"count" : @(11),
  9222. @"item_0" : @{
  9223. @"aname" : @"Type",
  9224. @"cadedate" : @{
  9225. @"count" : @(2),
  9226. @"val_0" : @{
  9227. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9228. @"value" : @"VISA",
  9229. @"value_id" : @"VISA"/*@(0)*/
  9230. },
  9231. @"val_1" : @{
  9232. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9233. @"value" : @"MASTER CARD",
  9234. @"value_id" : @"MASTER CARD"/*@(1)*/
  9235. }
  9236. },
  9237. @"control" : @"enum",
  9238. @"name" : @"credit_card_type",
  9239. @"required" : @"true",
  9240. @"single_select" : @"true"
  9241. },
  9242. @"item_1" : @{
  9243. @"aname" : @"Number",
  9244. @"control" : @"edit",
  9245. @"keyboard" : @"int",
  9246. @"length" : @"16",
  9247. @"name" : @"credit_card_number",
  9248. @"required" : @"true",
  9249. @"value" : cardNumber
  9250. },
  9251. @"item_10" : @{
  9252. @"aname" : @"State",
  9253. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9254. @"control" : @"enum",
  9255. @"enum" : @"true",
  9256. @"name" : @"credit_card_state",
  9257. @"required" : @"true",
  9258. @"single_select" : @"true"
  9259. },
  9260. @"item_2" : @{
  9261. @"aname" : @"Expiration Date",
  9262. @"control" : @"monthpicker",
  9263. @"name" : @"credit_card_expiration",
  9264. @"required" : @"true",
  9265. @"type" : @"date",
  9266. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9267. },
  9268. @"item_3" : @{
  9269. @"aname" : @"Security Code",
  9270. @"control" : @"edit",
  9271. @"keyboard" : @"int",
  9272. @"length" : @"3",
  9273. @"name" : @"credit_card_security_code",
  9274. @"required" : @"true",
  9275. @"value" : securityCode
  9276. },
  9277. @"item_4" : @{
  9278. @"aname" : @"First Name",
  9279. @"control" : @"edit",
  9280. @"keyboard" : @"text",
  9281. @"name" : @"credit_card_first_name",
  9282. @"required" : @"true",
  9283. @"value" : firstName
  9284. },
  9285. @"item_5" : @{
  9286. @"aname" : @"Last Name",
  9287. @"control" : @"edit",
  9288. @"keyboard" : @"text",
  9289. @"name" : @"credit_card_last_name",
  9290. @"required" : @"true",
  9291. @"value" : lastName
  9292. },
  9293. @"item_6" : @{
  9294. @"aname" : @"Address 1",
  9295. @"control" : @"edit",
  9296. @"keyboard" : @"text",
  9297. @"name" : @"credit_card_address1",
  9298. @"required" : @"true",
  9299. @"value" : addr1
  9300. },
  9301. @"item_7" : @{
  9302. @"aname" : @"Address 2",
  9303. @"control" : @"edit",
  9304. @"keyboard" : @"text",
  9305. @"name" : @"credit_card_address2",
  9306. @"value" : addr2
  9307. },
  9308. @"item_8" : @{
  9309. @"aname" : @"zip code",
  9310. @"control" : @"edit",
  9311. @"keyboard" : @"text",
  9312. @"name" : @"credit_card_zipcode",
  9313. @"required" : @"true",
  9314. @"value" : zipcode
  9315. },
  9316. @"item_9" : @{
  9317. @"aname" : @"City",
  9318. @"control" : @"edit",
  9319. @"keyboard" : @"text",
  9320. @"name" : @"credit_card_city",
  9321. @"required" : @"true",
  9322. @"value" : city
  9323. }
  9324. },
  9325. @"value" : @"Fill Now",
  9326. @"value_id" : @""
  9327. },
  9328. @"val_1" : @{
  9329. @"check" : @(0),
  9330. @"value" : @"Fill Later",
  9331. @"value_id" : @""
  9332. }
  9333. },
  9334. @"control" : @"enum",
  9335. @"name" : @"",
  9336. @"single_select" : @"true"
  9337. }
  9338. };
  9339. #endif;
  9340. [val setObject:sub_item forKey:@"sub_item"];
  9341. }
  9342. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9343. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9344. }];
  9345. // "section_2"
  9346. NSMutableDictionary *dic = @{
  9347. @"count" : @(1),
  9348. @"item_0" : @{
  9349. @"aname" : @"Payment",
  9350. @"required" : required,
  9351. @"cadedate" : cadedate,
  9352. // @{
  9353. // @"count" : @(6),
  9354. // @"val_3" : @{
  9355. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9356. // @"value" : @"Check",
  9357. // @"value_id" : @"Check"
  9358. // },
  9359. // @"val_2" : @{
  9360. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9361. // @"value" : @"Cash",
  9362. // @"value_id" : @"Cash"
  9363. // },
  9364. // @"val_1" : @{
  9365. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9366. // @"value" : @"NET 60",
  9367. // @"value_id" : @"NET 30"
  9368. // },
  9369. // @"val_4" : @{
  9370. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9371. // @"value" : @"Wire Transfer",
  9372. // @"value_id" : @"Wire Transfer"
  9373. // },
  9374. // @"val_0" : @{
  9375. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9376. // @"sub_item" : @{
  9377. // @"count" : @(3),
  9378. // @"item_0" : @{
  9379. // @"aname" : @"choose",
  9380. // @"control" : @"multi_action",
  9381. // @"count" : @(1),
  9382. // @"item_0" : @{
  9383. // @"aname" : @"Same as customer",
  9384. // @"key_map" : @{
  9385. // @"credit_card_address1" : @"customer_address1",
  9386. // @"credit_card_address2" : @"customer_address2",
  9387. // @"credit_card_city" : @"customer_city",
  9388. // @"credit_card_first_name" : @"customer_first_name",
  9389. // @"credit_card_last_name" : @"customer_last_name",
  9390. // @"credit_card_state" : @"customer_state",
  9391. // @"credit_card_zipcode" : @"customer_zipcode"
  9392. // },
  9393. // @"type" : @"pull"
  9394. // }
  9395. // },
  9396. // @"item_1" : @{
  9397. // @"aname" : @"",
  9398. // @"color" : @"red",
  9399. // @"control" : @"text",
  9400. // @"name" : @"",
  9401. // @"value" : @"USA Credit cards only"
  9402. // },
  9403. // @"item_2" : @{
  9404. // @"aname" : @"Fill",
  9405. // @"cadedate" : @{
  9406. // @"count" : @(2),
  9407. // @"val_0" : @{
  9408. // @"check" : @(1),
  9409. // @"sub_item" : @{
  9410. // @"count" : @(11),
  9411. // @"item_0" : @{
  9412. // @"aname" : @"Type",
  9413. // @"cadedate" : @{
  9414. // @"count" : @(2),
  9415. // @"val_0" : @{
  9416. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9417. // @"value" : @"VISA",
  9418. // @"value_id" : @(0)
  9419. // },
  9420. // @"val_1" : @{
  9421. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9422. // @"value" : @"MASTER CARD",
  9423. // @"value_id" : @(1)
  9424. // }
  9425. // },
  9426. // @"control" : @"enum",
  9427. // @"name" : @"credit_card_type",
  9428. // @"required" : @"true",
  9429. // @"single_select" : @"true"
  9430. // },
  9431. // @"item_1" : @{
  9432. // @"aname" : @"Number",
  9433. // @"control" : @"edit",
  9434. // @"keyboard" : @"int",
  9435. // @"length" : @"16",
  9436. // @"name" : @"credit_card_number",
  9437. // @"required" : @"true",
  9438. // @"value" : cardNumber
  9439. // },
  9440. // @"item_10" : @{
  9441. // @"aname" : @"State",
  9442. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9443. // @"control" : @"enum",
  9444. // @"enum" : @"true",
  9445. // @"name" : @"credit_card_state",
  9446. // @"required" : @"true",
  9447. // @"single_select" : @"true"
  9448. // },
  9449. // @"item_2" : @{
  9450. // @"aname" : @"Expiration Date",
  9451. // @"control" : @"monthpicker",
  9452. // @"name" : @"credit_card_expiration",
  9453. // @"required" : @"true",
  9454. // @"type" : @"date",
  9455. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9456. // },
  9457. // @"item_3" : @{
  9458. // @"aname" : @"Security Code",
  9459. // @"control" : @"edit",
  9460. // @"keyboard" : @"int",
  9461. // @"length" : @"3",
  9462. // @"name" : @"credit_card_security_code",
  9463. // @"required" : @"true",
  9464. // @"value" : securityCode
  9465. // },
  9466. // @"item_4" : @{
  9467. // @"aname" : @"First Name",
  9468. // @"control" : @"edit",
  9469. // @"keyboard" : @"text",
  9470. // @"name" : @"credit_card_first_name",
  9471. // @"required" : @"true",
  9472. // @"value" : firstName
  9473. // },
  9474. // @"item_5" : @{
  9475. // @"aname" : @"Last Name",
  9476. // @"control" : @"edit",
  9477. // @"keyboard" : @"text",
  9478. // @"name" : @"credit_card_last_name",
  9479. // @"required" : @"true",
  9480. // @"value" : lastName
  9481. // },
  9482. // @"item_6" : @{
  9483. // @"aname" : @"Address 1",
  9484. // @"control" : @"edit",
  9485. // @"keyboard" : @"text",
  9486. // @"name" : @"credit_card_address1",
  9487. // @"required" : @"true",
  9488. // @"value" : addr1
  9489. // },
  9490. // @"item_7" : @{
  9491. // @"aname" : @"Address 2",
  9492. // @"control" : @"edit",
  9493. // @"keyboard" : @"text",
  9494. // @"name" : @"credit_card_address2",
  9495. // @"value" : addr2
  9496. // },
  9497. // @"item_8" : @{
  9498. // @"aname" : @"zip code",
  9499. // @"control" : @"edit",
  9500. // @"keyboard" : @"text",
  9501. // @"name" : @"credit_card_zipcode",
  9502. // @"required" : @"true",
  9503. // @"value" : zipcode
  9504. // },
  9505. // @"item_9" : @{
  9506. // @"aname" : @"City",
  9507. // @"control" : @"edit",
  9508. // @"keyboard" : @"text",
  9509. // @"name" : @"credit_card_city",
  9510. // @"required" : @"true",
  9511. // @"value" : city
  9512. // }
  9513. // },
  9514. // @"value" : @"Fill Now",
  9515. // @"value_id" : @""
  9516. // },
  9517. // @"val_1" : @{
  9518. // @"check" : @(0),
  9519. // @"value" : @"Fill Later",
  9520. // @"value_id" : @""
  9521. // }
  9522. // },
  9523. // @"control" : @"enum",
  9524. // @"name" : @"",
  9525. // @"single_select" : @"true"
  9526. // }
  9527. // },
  9528. // @"value" : @"Visa/Master",
  9529. // @"value_id" : @"Credit Card"
  9530. // },
  9531. // @"val_5" : @{
  9532. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9533. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9534. // @"value_id" : @"FOLLOW EXISTING"
  9535. // }
  9536. // },
  9537. @"control" : @"enum",
  9538. @"name" : @"paymentType",
  9539. @"single_select" : @"true"
  9540. },
  9541. @"title" : @"Payment Information"
  9542. }.mutableCopy;
  9543. NSNumber *hide = setting[@"PaymentInformationHide"];
  9544. [dic setValue:hide forKey:@"hide"];
  9545. return dic;
  9546. }
  9547. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9548. // params
  9549. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9550. orderCode = [self translateSingleQuote:orderCode];
  9551. // 缺货检查
  9552. 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];
  9553. __block BOOL outOfStock = NO;
  9554. sqlite3 *database = db;
  9555. if (!db) {
  9556. database = [iSalesDB get_db];
  9557. }
  9558. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9559. int availability = sqlite3_column_int(stmt, 0);
  9560. int item_qty = sqlite3_column_int(stmt, 1);
  9561. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9562. }];
  9563. if (!db) {
  9564. [iSalesDB close_db:database];
  9565. }
  9566. return outOfStock;
  9567. }
  9568. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9569. {
  9570. assert(params[@"user"]!=nil);
  9571. assert(params[@"contact_id"]!=nil);
  9572. assert(params[@"can_create_backorder"]!=nil);
  9573. sqlite3 *db = [iSalesDB get_db];
  9574. // params
  9575. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9576. orderCode = [self translateSingleQuote:orderCode];
  9577. // 缺货检查
  9578. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9579. if (![params[@"can_create_backorder"] boolValue]) {
  9580. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9581. if (out_of_stock) {
  9582. [iSalesDB close_db:db];
  9583. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9584. [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"];
  9585. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9586. return [RAConvertor dict2data:resultDic];
  9587. }
  9588. }
  9589. // UISetting
  9590. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9591. NSString *cachefolder = [paths objectAtIndex:0];
  9592. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9593. NSData* json =nil;
  9594. json=[NSData dataWithContentsOfFile:img_cache];
  9595. NSError *error=nil;
  9596. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9597. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9598. [params setObject:setting forKey:@"setting"];
  9599. int section_count = 0;
  9600. // 0 Order Type 1 Shipping Method 2 Payment Information
  9601. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9602. // 0 Order Type
  9603. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9604. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9605. [ret setValue:order_type_dic forKey:key0];
  9606. // 1 Shipping Method
  9607. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9608. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9609. [ret setValue:shipping_method_dic forKey:key1];
  9610. // 2 Payment Information
  9611. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9612. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9613. [ret setValue:payment_info_dic forKey:key2];
  9614. // 3 Customer
  9615. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9616. NSDictionary *customer_dic = [self customerDic:params db:db];
  9617. [ret setValue:customer_dic forKey:key3];
  9618. // 4 Ship To
  9619. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9620. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9621. [ret setValue:ship_to_dic forKey:key4];
  9622. // 5 Ship From
  9623. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9624. if (![shipFromDisable integerValue]) {
  9625. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9626. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9627. [ret setValue:ship_from_dic forKey:key5];
  9628. }
  9629. // 6 Freight Bill To
  9630. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9631. if (![freightBillToDisable integerValue]) {
  9632. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9633. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9634. [ret setValue:freight_bill_to forKey:key6];
  9635. }
  9636. // 7 Merchandise Bill To
  9637. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9638. if (![merchandiseBillToDisable integerValue]) {
  9639. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9640. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9641. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9642. }
  9643. // 8 Return To
  9644. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9645. if (![returnToDisable integerValue]) {
  9646. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9647. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9648. [ret setValue:return_to_dic forKey:key8];
  9649. }
  9650. // 9 Model Information
  9651. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9652. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9653. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9654. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9655. [ret setValue:model_info_dic forKey:key9];
  9656. // 10 Remarks Content
  9657. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9658. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9659. [ret setValue:remarks_content_dic forKey:key10];
  9660. // 11 Order Total
  9661. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9662. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9663. [ret setValue:order_total_dic forKey:key11];
  9664. // 12 Signature
  9665. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9666. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9667. [ret setValue:sign_dic forKey:key12];
  9668. [ret setValue:@(section_count) forKey:@"section_count"];
  9669. [iSalesDB close_db:db];
  9670. return [RAConvertor dict2data:ret];
  9671. // return nil;
  9672. }
  9673. #pragma mark addr editor
  9674. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9675. NSMutableDictionary *new_item = [item mutableCopy];
  9676. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9677. return new_item;
  9678. }
  9679. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9680. {
  9681. // "is_subaction" = true;
  9682. // orderCode = MOB1608240002;
  9683. // password = 123456;
  9684. // "subaction_tag" = 1;
  9685. // user = EvanK;
  9686. // {
  9687. // "is_subaction" = true;
  9688. // orderCode = MOB1608240002;
  9689. // password = 123456;
  9690. // "refresh_trigger" = zipcode;
  9691. // "subaction_tag" = 1;
  9692. // user = EvanK;
  9693. // }
  9694. NSString *country_code = nil;
  9695. NSString *zipCode = nil;
  9696. NSString *stateCode = nil;
  9697. NSString *city = nil;
  9698. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9699. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9700. NSString *code_id = params[@"country"];
  9701. country_code = [self countryCodeByid:code_id];
  9702. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9703. NSString *zip_code = params[@"zipcode"];
  9704. // 剔除全部为空格
  9705. int spaceCount = 0;
  9706. for (int i = 0; i < zip_code.length; i++) {
  9707. if ([zip_code characterAtIndex:i] == ' ') {
  9708. spaceCount++;
  9709. }
  9710. }
  9711. if (spaceCount == zip_code.length) {
  9712. zip_code = @"";
  9713. }
  9714. zipCode = zip_code;
  9715. if (zipCode.length > 0) {
  9716. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9717. country_code = [dic valueForKey:@"country_code"];
  9718. if (!country_code) {
  9719. // country_code = @"US";
  9720. NSString *code_id = params[@"country"];
  9721. country_code = [self countryCodeByid:code_id];
  9722. }
  9723. stateCode = [dic valueForKey:@"state_code"];
  9724. if(!stateCode.length) {
  9725. stateCode = params[@"state"];
  9726. }
  9727. city = [dic valueForKey:@"city"];
  9728. if (!city.length) {
  9729. city = params[@"city"];
  9730. }
  9731. // zip code
  9732. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9733. // [zipDic setValue:zipCode forKey:@"value"];
  9734. // [section_0 setValue:zipDic forKey:@"item_11"];
  9735. } else {
  9736. NSString *code_id = params[@"country"];
  9737. country_code = [self countryCodeByid:code_id];
  9738. stateCode = params[@"state"];
  9739. city = params[@"city"];
  9740. }
  9741. }
  9742. }
  9743. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9744. // [ret removeObjectForKey:@"up_params"];
  9745. [ret setObject:@"New Address" forKey:@"title"];
  9746. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9747. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9748. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9749. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9750. // [country_dic removeObjectForKey:@"refresh"];
  9751. // [country_dic removeObjectForKey:@"restore"];
  9752. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9753. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9754. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9755. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9756. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9757. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9758. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9759. // [zip_code_dic removeObjectForKey:@"refresh"];
  9760. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9761. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9762. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9763. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9764. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9765. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9766. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9767. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9768. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9769. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9770. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9771. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9772. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9773. // country
  9774. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9775. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9776. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9777. // state
  9778. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9779. // NSDictionary *tmpDic = @{
  9780. // @"value" : @"Other",
  9781. // @"value_id" : @"",
  9782. // @"check" : [NSNumber numberWithInteger:0]
  9783. // };
  9784. //
  9785. // for (int i = 0; i < allState.allKeys.count; i++) {
  9786. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9787. //
  9788. // NSDictionary *tmp = allState[key];
  9789. // [allState setValue:tmpDic forKey:key];
  9790. // tmpDic = tmp;
  9791. // }
  9792. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9793. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9794. [ret setValue:new_section_0 forKey:@"section_0"];
  9795. return [RAConvertor dict2data:ret];
  9796. }
  9797. #pragma mark save addr
  9798. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9799. {
  9800. // NSString *companyName = [self valueInParams:params key:@"company"];
  9801. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9802. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9803. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9804. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9805. // NSString *countryId = [self valueInParams:params key:@"country"];
  9806. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9807. // NSString *city = [self valueInParams:params key:@"city"];
  9808. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9809. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9810. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9811. // NSString *phone = [self valueInParams:params key:@"phone"];
  9812. // NSString *fax = [self valueInParams:params key:@"fax"];
  9813. // NSString *email = [self valueInParams:params key:@"email"];
  9814. return [self offline_saveContact:params update:NO isCustomer:NO];
  9815. }
  9816. #pragma mark cancel order
  9817. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9818. {
  9819. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9820. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9821. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9822. if (order_id.length) {
  9823. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9824. }
  9825. int ret = [iSalesDB execSql:sql];
  9826. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9827. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9828. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9829. // [dic setValue:@"160409" forKey:@"min_ver"];
  9830. return [RAConvertor dict2data:dic];
  9831. }
  9832. #pragma mark sign order
  9833. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9834. {
  9835. //参考 offline_saveBusinesscard
  9836. DebugLog(@"sign order params: %@",params);
  9837. // orderCode = MOB1608240002;
  9838. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9839. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9840. orderCode = [self translateSingleQuote:orderCode];
  9841. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9842. picPath = [self translateSingleQuote:picPath];
  9843. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9844. int ret = [iSalesDB execSql:sql];
  9845. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9846. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9847. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9848. // [dic setValue:@"160409" forKey:@"min_ver"];
  9849. return [RAConvertor dict2data:dic];
  9850. }
  9851. #pragma mark save order
  9852. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9853. NSString *ret = [self valueInParams:params key:key];
  9854. if (translate) {
  9855. ret = [self translateSingleQuote:ret];
  9856. }
  9857. return ret;
  9858. }
  9859. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9860. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9861. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9862. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9863. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9864. if (schedule_date.length==0 ) {
  9865. schedule_date = @"";
  9866. } else {
  9867. schedule_date = [self rchangeDateFormate:schedule_date];
  9868. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9869. }
  9870. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9871. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9872. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9873. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9874. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9875. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9876. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9877. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9878. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9879. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9880. if (![total_price isEqualToString:@""]) {
  9881. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9882. } else {
  9883. total_price = @"";
  9884. }
  9885. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9886. if ([paymentsAndCredits isEqualToString:@""]) {
  9887. paymentsAndCredits = @"";
  9888. } else {
  9889. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9890. }
  9891. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9892. if ([handling_fee_value isEqualToString:@""]) {
  9893. handling_fee_value = @"";
  9894. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9895. handling_fee_value = @"";
  9896. } else {
  9897. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9898. }
  9899. NSString *handling_fee_placeholder = handling_fee_value;
  9900. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9901. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9902. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9903. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9904. NSString *lift_gate_placeholder = @"";
  9905. if ([lift_gate_value isEqualToString:@""]) {
  9906. lift_gate_placeholder = @"";
  9907. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9908. lift_gate_placeholder = @"";
  9909. } else {
  9910. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9911. }
  9912. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9913. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9914. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9915. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9916. 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];
  9917. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9918. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9919. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9920. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9921. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9922. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9923. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9924. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9925. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9926. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9927. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9928. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9929. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9930. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9931. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9932. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9933. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9934. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9935. if (!number_nil) {
  9936. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9937. }
  9938. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9939. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9940. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9941. if (!security_code_nil) {
  9942. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9943. }
  9944. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9945. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9946. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9947. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9948. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9949. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9950. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9951. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9952. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9953. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9954. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9955. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9956. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9957. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9958. NSString *contact_id = customer_cid;
  9959. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9960. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9961. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9962. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9963. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9964. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9965. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9966. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9967. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9968. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9969. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9970. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9971. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9972. //
  9973. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9974. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9975. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9976. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9977. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9978. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9979. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9980. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9981. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9982. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9983. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9984. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9985. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9986. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9987. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9988. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9989. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9990. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9991. 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];
  9992. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9993. if (receive_cid.length) {
  9994. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9995. }
  9996. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9997. if (receive_name.length) {
  9998. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9999. }
  10000. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  10001. if (receive_ext.length) {
  10002. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  10003. }
  10004. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  10005. if (receive_contact.length) {
  10006. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  10007. }
  10008. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  10009. if (receive_phone.length) {
  10010. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  10011. }
  10012. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  10013. if (receive_email.length) {
  10014. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  10015. }
  10016. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  10017. if (receive_fax.length) {
  10018. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  10019. }
  10020. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  10021. if (sender_cid.length) {
  10022. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  10023. }
  10024. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  10025. if (sender_name.length) {
  10026. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  10027. }
  10028. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  10029. if (sender_ext.length) {
  10030. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  10031. }
  10032. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  10033. if(sender_contact.length) {
  10034. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  10035. }
  10036. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  10037. if (sender_phone.length) {
  10038. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  10039. }
  10040. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  10041. if (sender_fax.length) {
  10042. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  10043. }
  10044. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  10045. if (sender_email.length) {
  10046. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  10047. }
  10048. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  10049. if (shipping_billto_cid.length) {
  10050. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  10051. }
  10052. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  10053. if (shipping_billto_name.length) {
  10054. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  10055. }
  10056. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  10057. if (shipping_billto_ext.length) {
  10058. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  10059. }
  10060. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  10061. if (shipping_billto_contact.length) {
  10062. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  10063. }
  10064. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  10065. if (shipping_billto_phone.length) {
  10066. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  10067. }
  10068. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  10069. if (shipping_billto_fax.length) {
  10070. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  10071. }
  10072. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  10073. if (shipping_billto_email.length) {
  10074. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  10075. }
  10076. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  10077. if (billing_cid.length) {
  10078. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  10079. }
  10080. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  10081. if (billing_name.length) {
  10082. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  10083. }
  10084. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  10085. if (billing_ext.length) {
  10086. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  10087. }
  10088. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  10089. if (billing_contact.length) {
  10090. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  10091. }
  10092. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  10093. if (billing_phone.length) {
  10094. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  10095. }
  10096. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  10097. if (billing_fax.length) {
  10098. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  10099. }
  10100. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  10101. if (billing_email.length) {
  10102. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  10103. }
  10104. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  10105. if (returnto_cid.length) {
  10106. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  10107. }
  10108. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10109. if (returnto_name.length) {
  10110. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10111. }
  10112. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10113. if (returnto_ext.length) {
  10114. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10115. }
  10116. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10117. if (returnto_contact.length) {
  10118. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10119. }
  10120. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10121. if (returnto_phone.length) {
  10122. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10123. }
  10124. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10125. if (returnto_fax.length) {
  10126. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10127. }
  10128. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10129. if (returnto_email.length) {
  10130. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10131. }
  10132. NSString *order_status = @"status = 1,";
  10133. if (submit) {
  10134. order_status = @"status = -11,";
  10135. }
  10136. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10137. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10138. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10139. NSString *sync_sql = @"";
  10140. if (submit) {
  10141. param[@"truck_carrier"]=carrier;
  10142. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10143. [param setValue:sales_rep forKey:@"sales_rep"];
  10144. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10145. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10146. }
  10147. 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];
  10148. DebugLog(@"save order contactSql: %@",contactSql);
  10149. DebugLog(@"save order orderSql: %@",orderSql);
  10150. // int contact_ret = [iSalesDB execSql:contactSql];
  10151. int order_ret = [iSalesDB execSql:orderSql];
  10152. int ret = order_ret;
  10153. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10154. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10155. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10156. // [dic setValue:@"160409" forKey:@"min_ver"];
  10157. [dic setObject:so_id forKey:@"so#"];
  10158. return [RAConvertor dict2data:dic];
  10159. }
  10160. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10161. {
  10162. // id foo = nil;
  10163. // NSMutableArray *a = @[].mutableCopy;
  10164. // [a addObject:foo];
  10165. return [self saveorder:param submit:NO];
  10166. }
  10167. #pragma mark add to cart by name
  10168. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10169. {
  10170. NSString *orderCode = [params objectForKey:@"orderCode"];
  10171. NSDictionary *newParams = @{
  10172. @"product_id" : params[@"product_id_string"],
  10173. @"orderCode" : orderCode,
  10174. @"can_create_backorder":params[@"can_create_backorder"]
  10175. };
  10176. return [self offline_add2cart:[newParams mutableCopy]];
  10177. }
  10178. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10179. {
  10180. NSString *orderCode = [params objectForKey:@"orderCode"];
  10181. NSString *upccode = [params objectForKey:@"upc_code"];
  10182. // product_name = [self translateSingleQuote:product_name];
  10183. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10184. // bool search_upc = [params[@"search_upc"] boolValue];
  10185. sqlite3 *db = [iSalesDB get_db];
  10186. __block NSMutableString *product_id_string = [NSMutableString string];
  10187. // NSString *name = [product_name_array objectAtIndex:i];
  10188. NSString *sql =nil;
  10189. 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];
  10190. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10191. int product_id = sqlite3_column_int(stmt, 0);
  10192. [product_id_string appendFormat:@"%d",product_id];
  10193. }];
  10194. if (!orderCode) {
  10195. orderCode = @"";
  10196. }
  10197. NSDictionary *newParams = @{
  10198. @"product_id" : product_id_string,
  10199. @"orderCode" : orderCode,
  10200. @"can_create_backorder":params[@"can_create_backorder"]
  10201. };
  10202. [iSalesDB close_db:db];
  10203. return [self offline_add2cart:[newParams mutableCopy]];
  10204. }
  10205. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10206. {
  10207. NSString *orderCode = [params objectForKey:@"orderCode"];
  10208. NSString *product_name = [params objectForKey:@"product_name"];
  10209. product_name = [self translateSingleQuote:product_name];
  10210. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10211. bool search_upc = [params[@"search_upc"] boolValue];
  10212. sqlite3 *db = [iSalesDB get_db];
  10213. __block NSMutableString *product_id_string = [NSMutableString string];
  10214. for (int i = 0; i < product_name_array.count; i++) {
  10215. NSString *name = [product_name_array objectAtIndex:i];
  10216. NSString *sql =nil;
  10217. if(search_upc)
  10218. 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];
  10219. else
  10220. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10221. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10222. int product_id = sqlite3_column_int(stmt, 0);
  10223. if (i == product_name_array.count - 1) {
  10224. [product_id_string appendFormat:@"%d",product_id];
  10225. } else {
  10226. [product_id_string appendFormat:@"%d,",product_id];
  10227. }
  10228. }];
  10229. }
  10230. if (!orderCode) {
  10231. orderCode = @"";
  10232. }
  10233. NSDictionary *newParams = @{
  10234. @"product_id" : product_id_string,
  10235. @"orderCode" : orderCode,
  10236. @"can_create_backorder":params[@"can_create_backorder"]
  10237. };
  10238. [iSalesDB close_db:db];
  10239. return [self offline_add2cart:[newParams mutableCopy]];
  10240. }
  10241. #pragma mark reset order
  10242. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10243. {
  10244. // UIApplication * app = [UIApplication sharedApplication];
  10245. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10246. [iSalesDB disable_trigger];
  10247. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10248. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10249. [iSalesDB enable_trigger];
  10250. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10251. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10252. return [RAConvertor dict2data:dic];
  10253. }
  10254. #pragma mark submit order
  10255. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10256. {
  10257. return [self saveorder:params submit:YES];
  10258. }
  10259. #pragma mark copy order
  10260. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10261. {
  10262. NSMutableDictionary *ret = @{}.mutableCopy;
  10263. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10264. sqlite3 *db = [iSalesDB get_db];
  10265. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10266. // 首先查看联系人是否active
  10267. 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];
  10268. __block int customer_is_active = 1;
  10269. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10270. customer_is_active = sqlite3_column_int(stmt, 0);
  10271. }];
  10272. if (!customer_is_active) {
  10273. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10274. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10275. [iSalesDB close_db:db];
  10276. return [RAConvertor dict2data:ret];
  10277. }
  10278. // new order
  10279. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10280. NSString *new_order_code = [self get_offline_soid:db];
  10281. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10282. user = [self translateSingleQuote:user];
  10283. 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
  10284. __block int result = 1;
  10285. result = [iSalesDB execSql:insert_order_sql db:db];
  10286. if (!result) {
  10287. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10288. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10289. [iSalesDB close_db:db];
  10290. return [RAConvertor dict2data:ret];
  10291. }
  10292. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10293. // __block NSString *product_id = @"";
  10294. __weak typeof(self) weakSelf = self;
  10295. 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];
  10296. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10297. int is_active = sqlite3_column_int(stmt, 1);
  10298. if (is_active) {
  10299. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10300. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10301. 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];
  10302. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10303. }
  10304. }];
  10305. // product_id = [product_id substringFromIndex:1];
  10306. //
  10307. // [self offline_add2cart:@{}.mutableCopy];
  10308. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10309. [iSalesDB close_db:db];
  10310. if (result) {
  10311. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10312. } else {
  10313. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10314. }
  10315. [ret setObject:new_order_code forKey:@"so_id"];
  10316. return [RAConvertor dict2data:ret];
  10317. }
  10318. #pragma mark move wish list to cart
  10319. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10320. assert(params[@"can_create_backorder"]!=nil);
  10321. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10322. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10323. NSString *collectId = params[@"collectId"];
  10324. 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];
  10325. __block NSString *product_id = @"";
  10326. __block NSString *qty = @"";
  10327. __block int number_of_outOfStock = 0;
  10328. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10329. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10330. int productId = sqlite3_column_int(stmt, 0);
  10331. int item_qty = sqlite3_column_int(stmt, 1);
  10332. int availability = sqlite3_column_int(stmt, 2);
  10333. int _id = sqlite3_column_int(stmt, 3);
  10334. if (![params[@"can_create_backorder"] boolValue]) {
  10335. // 库存小于购买量为缺货
  10336. if (availability >= item_qty) {
  10337. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10338. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10339. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10340. } else {
  10341. number_of_outOfStock++;
  10342. }
  10343. } else {
  10344. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10345. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10346. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10347. }
  10348. }];
  10349. NSMutableDictionary *retDic = nil;
  10350. if (![params[@"can_create_backorder"] boolValue]) {
  10351. if (delete_collectId.count == 0) {
  10352. retDic = [NSMutableDictionary dictionary];
  10353. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10354. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10355. return [RAConvertor dict2data:retDic];
  10356. }
  10357. }
  10358. if (product_id.length > 1) {
  10359. product_id = [product_id substringFromIndex:1];
  10360. }
  10361. if (qty.length > 1) {
  10362. qty = [qty substringFromIndex:1];
  10363. }
  10364. NSString *orderCode = params[@"orderCode"];
  10365. if (!orderCode) {
  10366. orderCode = @"";
  10367. }
  10368. NSDictionary *newParams = @{
  10369. @"product_id" : product_id,
  10370. @"orderCode" : orderCode,
  10371. @"qty" : qty,
  10372. @"can_create_backorder":params[@"can_create_backorder"]
  10373. };
  10374. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10375. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10376. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10377. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10378. NSInteger ret = [wish_return[@"result"] intValue];
  10379. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10380. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10381. }
  10382. if (![params[@"can_create_backorder"] boolValue]) {
  10383. if (number_of_outOfStock > 0) {
  10384. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10385. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10386. }
  10387. }
  10388. return [RAConvertor dict2data:retDic];
  10389. }
  10390. #pragma mark - portfolio
  10391. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10392. // assert(params[@"contact_id"]!=nil);
  10393. assert(params[@"user"]!=nil);
  10394. assert(params[@"can_see_price"]!=nil);
  10395. int sort = [[params valueForKey:@"sort"] intValue];
  10396. int offset = [[params valueForKey:@"offset"] intValue];
  10397. int limit = [[params valueForKey:@"limit"] intValue];
  10398. NSString *orderBy = @"";
  10399. switch (sort) {
  10400. case 0:{
  10401. orderBy = @"p.modify_time desc";
  10402. }
  10403. break;
  10404. case 1:{
  10405. orderBy = @"modify_time asc";
  10406. }
  10407. break;
  10408. case 2:{
  10409. orderBy = @"p.name asc";
  10410. }
  10411. break;
  10412. case 3:{
  10413. orderBy = @"p.name desc";
  10414. }
  10415. break;
  10416. case 4:{
  10417. orderBy = @"p.description asc";
  10418. }
  10419. break;
  10420. case 5: {
  10421. orderBy = @"m.default_category asc";
  10422. }
  10423. default:
  10424. break;
  10425. }
  10426. // 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];
  10427. 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];
  10428. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10429. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10430. sqlite3 *db = [iSalesDB get_db];
  10431. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10432. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10433. int product_id = sqlite3_column_int(stmt, 0);
  10434. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10435. NSString *name = [self textAtColumn:1 statement:stmt];
  10436. NSString *description = [self textAtColumn:2 statement:stmt];
  10437. double price = sqlite3_column_double(stmt, 3);
  10438. double discount = sqlite3_column_double(stmt,4);
  10439. int qty = sqlite3_column_int(stmt, 5);
  10440. int percentage = sqlite3_column_int(stmt, 6);
  10441. int item_id = sqlite3_column_int(stmt, 7);
  10442. // int fashion_id = sqlite3_column_int(stmt, 8);
  10443. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10444. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10445. double percent = sqlite3_column_double(stmt, 11);
  10446. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10447. if ([price_null isEqualToString:@"null"]) {
  10448. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10449. }
  10450. NSMutableDictionary *item = @{
  10451. @"linenotes": line_note,
  10452. @"check": @(1),
  10453. @"product_id": product_id_string,
  10454. @"available_qty": @(qty),
  10455. @"available_percent" : @(percent),
  10456. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10457. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10458. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10459. @"img": img_path,
  10460. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10461. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10462. }.mutableCopy;
  10463. if (percentage) {
  10464. [item removeObjectForKey:@"available_qty"];
  10465. } else {
  10466. [item removeObjectForKey:@"available_percent"];
  10467. }
  10468. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10469. if ([qty_null isEqualToString:@"null"]) {
  10470. [item removeObjectForKey:@"available_qty"];
  10471. }
  10472. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10473. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10474. }];
  10475. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10476. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10477. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10478. }
  10479. [iSalesDB close_db:db];
  10480. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10481. [dic setValue:@"" forKey:@"email_content"];
  10482. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10483. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10484. return [RAConvertor dict2data:dic];
  10485. }
  10486. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10487. __block int qty = 0;
  10488. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10489. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10490. qty = sqlite3_column_int(stmt, 0);
  10491. }];
  10492. return qty;
  10493. }
  10494. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10495. {
  10496. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10497. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10498. // NSMutableDictionary * values = params[@"replaceValue"];
  10499. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10500. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10501. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10502. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10503. NSString *pdf_path = @"";
  10504. if (direct) {
  10505. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10506. } else {
  10507. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10508. }
  10509. NSString *create_user = [self valueInParams:params key:@"user"];
  10510. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10511. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10512. // model info
  10513. // 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];
  10514. // V1.90 more color
  10515. 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];
  10516. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10517. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10518. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10519. sqlite3 *db = [iSalesDB get_db];
  10520. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10521. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10522. int product_id = sqlite3_column_int(stmt, 0);
  10523. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10524. double price = sqlite3_column_double(stmt, 1);
  10525. double discount = sqlite3_column_double(stmt,2);
  10526. int qty = sqlite3_column_int(stmt, 3);
  10527. int percentage = sqlite3_column_int(stmt, 4);
  10528. int item_id = sqlite3_column_int(stmt, 5);
  10529. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10530. double percent = sqlite3_column_double(stmt, 7);
  10531. int more_color = sqlite3_column_int(stmt, 8);
  10532. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10533. /* if ([price_null isEqualToString:@"null"]) {
  10534. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10535. }
  10536. */
  10537. [product_ids_string appendFormat:@"%@,",product_id_string];
  10538. // Regular Price
  10539. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10540. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10541. // QTY
  10542. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10543. if ([qty_null isEqualToString:@"null"]) {
  10544. // 查available
  10545. qty = [self model_QTY:product_id_string db:db];
  10546. }
  10547. if (percentage) {
  10548. qty = qty * percent / 100;
  10549. }
  10550. // Special Price
  10551. if ([price_null isEqualToString:@"null"]) {
  10552. // price = regular price
  10553. price = [regular_price_str doubleValue];
  10554. }
  10555. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10556. if (![discount_null isEqualToString:@"null"]) {
  10557. price = price * (1 - discount / 100.0);
  10558. }
  10559. NSMutableDictionary *item = @{
  10560. @"line_note": line_note,
  10561. @"product_id": product_id_string,
  10562. @"available_qty": @(qty),
  10563. @"more_color":@(more_color),
  10564. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10565. @"regular_price" : regular_price_str,
  10566. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10567. }.mutableCopy;
  10568. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10569. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10570. }];
  10571. [iSalesDB close_db:db];
  10572. if (product_ids_string.length > 0) {
  10573. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10574. }
  10575. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10576. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10577. return [RAConvertor dict2data:resultDictionary];
  10578. }
  10579. NSString *model_info = [RAConvertor dict2string:dic];
  10580. // 创建PDF
  10581. // 在preview情况下保存,则不需要新建了
  10582. if (direct) {
  10583. NSMutableDictionary *tear_sheet_params = params;
  10584. // if (tear_sheet_id) {
  10585. // tear_sheet_params = values;
  10586. // }
  10587. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10588. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10589. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10590. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10591. resultDictionary = pdfInfo.mutableCopy;
  10592. } else { // 创建PDF失败
  10593. return pdfData;
  10594. }
  10595. } else {
  10596. // pdf_path 就是本地路径
  10597. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10598. }
  10599. // 将文件移动到PDF Cache文件夹
  10600. NSString *newPath = [pdf_path lastPathComponent];
  10601. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10602. NSString *cachefolder = [paths objectAtIndex:0];
  10603. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10604. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10605. NSFileManager *fileManager = [NSFileManager defaultManager];
  10606. NSError *error = nil;
  10607. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10608. if (error) { // 移动文件失败
  10609. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10610. return [RAConvertor dict2data:resultDictionary];
  10611. }
  10612. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10613. pdf_path = [newPath lastPathComponent];
  10614. // 保存信息
  10615. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10616. NSString *off_params = [RAConvertor dict2string:params];
  10617. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10618. pdf_path = [self translateSingleQuote:pdf_path];
  10619. create_user = [self translateSingleQuote:create_user];
  10620. tear_note = [self translateSingleQuote:tear_note];
  10621. tear_name = [self translateSingleQuote:tear_name];
  10622. model_info = [self translateSingleQuote:model_info];
  10623. configureParams = [self translateSingleQuote:configureParams];
  10624. off_params = [self translateSingleQuote:off_params];
  10625. 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];
  10626. if (tear_sheet_id) {
  10627. int _id = [tear_sheet_id intValue];
  10628. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10629. }
  10630. int result = [iSalesDB execSql:save_pdf_sql];
  10631. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10632. //
  10633. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10634. if (remove_Item) {
  10635. // portfolioId
  10636. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10637. }
  10638. return [RAConvertor dict2data:resultDictionary];
  10639. }
  10640. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10641. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10642. }
  10643. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10644. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10645. }
  10646. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10647. int offset = [[params valueForKey:@"offset"] intValue];
  10648. int limit = [[params valueForKey:@"limit"] intValue];
  10649. NSString *keyword = [params valueForKey:@"keyWord"];
  10650. NSString *where = @"where is_delete is null or is_delete = 0";
  10651. if (keyword.length) {
  10652. keyword = [self translateSingleQuote:keyword];
  10653. 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];
  10654. }
  10655. 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
  10656. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10657. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10658. NSString *cachefolder = [paths objectAtIndex:0];
  10659. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10660. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10661. NSString *name = [self textAtColumn:0 statement:stmt];
  10662. NSString *note = [self textAtColumn:1 statement:stmt];
  10663. NSString *time = [self textAtColumn:2 statement:stmt];
  10664. NSString *user = [self textAtColumn:3 statement:stmt];
  10665. NSString *path = [self textAtColumn:4 statement:stmt];
  10666. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10667. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10668. path = [pdfFolder stringByAppendingPathComponent:path];
  10669. BOOL bdir=NO;
  10670. NSFileManager* fileManager = [NSFileManager defaultManager];
  10671. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10672. {
  10673. //pdf文件不存在
  10674. path=nil;
  10675. }
  10676. time = [self changeDateTimeFormate:time];
  10677. time = [time stringByAppendingString:@" PST"];
  10678. int sheet_id = sqlite3_column_int(stmt, 5);
  10679. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10680. item[@"tearsheetsId"]=@(sheet_id);
  10681. item[@"pdf_path"]=path;
  10682. item[@"create_time"]=time;
  10683. item[@"create_user"]=user;
  10684. item[@"tear_note"]=note;
  10685. item[@"tear_name"]=name;
  10686. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10687. item[@"model_info"]=model_info;
  10688. item[@"off_params"]=off_params;
  10689. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10690. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10691. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10692. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10693. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10694. }];
  10695. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10696. return [RAConvertor dict2data:dic];
  10697. }
  10698. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10699. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10700. // NSString *user = [params objectForKey:@"user"];
  10701. // 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];
  10702. 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];
  10703. int result = [iSalesDB execSql:sql];
  10704. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10705. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10706. if (result == RESULT_TRUE) {
  10707. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10708. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10709. sqlite3 *db = [iSalesDB get_db];
  10710. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10711. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10712. [iSalesDB close_db:db];
  10713. }
  10714. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10715. return [RAConvertor dict2data:dic];
  10716. }
  10717. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10718. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10719. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10720. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10721. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10722. NSDictionary *company_item = @{
  10723. @"control": @"text",
  10724. @"keyboard": @"text",
  10725. @"name": @"company_name",
  10726. @"value": COMPANY_FULL_NAME,
  10727. @"aname": @"Company Name"
  10728. };
  10729. [section_0 setObject:company_item forKey:@"item_0"];
  10730. [dic setObject:section_0 forKey:@"section_0"];
  10731. // Regurlar Price
  10732. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10733. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10734. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10735. __block long val_count = 0;
  10736. NSString *sql = @"select name,type,order_by from price order by order_by";
  10737. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10738. NSString *name = [self textAtColumn:0 statement:stmt];
  10739. int type = sqlite3_column_int(stmt, 1);
  10740. int order_by = sqlite3_column_int(stmt, 2);
  10741. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10742. NSDictionary *price_dic = @{
  10743. @"value" : name,
  10744. @"value_id" : [NSNumber numberWithInteger:type],
  10745. @"check" : order_by == 0 ? @(1) : @(0)
  10746. };
  10747. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10748. val_count = ++(*count);
  10749. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10750. val_count = 0;
  10751. }];
  10752. [cadedate setObject:@{@"value" : @"None",
  10753. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10754. val_count++;
  10755. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10756. [price setObject:cadedate forKey:@"cadedate"];
  10757. [section1 setObject:price forKey:@"item_2"];
  10758. [dic setObject:section1 forKey:@"section_1"];
  10759. return [RAConvertor dict2data:dic];
  10760. }
  10761. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10762. NSString *product_id = [params objectForKey:@"fashionId"];
  10763. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10764. __block int result = RESULT_TRUE;
  10765. __block int qty = 0;
  10766. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10767. qty = sqlite3_column_int(stmt, 0);
  10768. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10769. result = RESULT_FALSE;
  10770. }];
  10771. NSMutableDictionary *dic = @{
  10772. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10773. @"mode" : @"Regular Mode",
  10774. @"quantity_available" : @(qty),
  10775. @"result" : @(result),
  10776. }.mutableCopy;
  10777. return [RAConvertor dict2data:dic];
  10778. }
  10779. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10780. NSString *item_ids = [params objectForKey:@"item_id"];
  10781. NSString *line_notes = [params objectForKey:@"notes"];
  10782. NSString *price_str = [params objectForKey:@"price"];
  10783. NSString *discount_str = [params objectForKey:@"discount"];
  10784. NSString *percent = [params objectForKey:@"available_percent"];
  10785. NSString *qty = [params objectForKey:@"available_qty"];
  10786. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10787. int dot = 0;
  10788. if (line_notes) {
  10789. line_notes = [self translateSingleQuote:line_notes];
  10790. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10791. sql = [sql stringByAppendingString:line_notes];
  10792. dot = 1;
  10793. } else {
  10794. line_notes = @"";
  10795. }
  10796. if (price_str) {
  10797. if (dot) {
  10798. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10799. } else {
  10800. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10801. dot = 1;
  10802. }
  10803. sql = [sql stringByAppendingString:price_str];
  10804. } else {
  10805. price_str = @"";
  10806. }
  10807. if (discount_str) {
  10808. if (dot) {
  10809. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10810. } else {
  10811. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10812. dot = 1;
  10813. }
  10814. sql = [sql stringByAppendingString:discount_str];
  10815. } else {
  10816. discount_str = @"";
  10817. }
  10818. if (percent) {
  10819. if (dot) {
  10820. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10821. } else {
  10822. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10823. dot = 1;
  10824. }
  10825. sql = [sql stringByAppendingString:percent];
  10826. } else {
  10827. percent = @"";
  10828. }
  10829. if (qty) {
  10830. if (dot) {
  10831. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10832. } else {
  10833. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10834. dot = 1;
  10835. }
  10836. sql = [sql stringByAppendingString:qty];
  10837. } else {
  10838. qty = @"";
  10839. }
  10840. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10841. sql = [sql stringByAppendingString:where];
  10842. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10843. int result = [iSalesDB execSql:sql];
  10844. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10845. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10846. return [RAConvertor dict2data:dic];
  10847. }
  10848. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10849. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10850. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10851. sqlite3 *db = [iSalesDB get_db];
  10852. 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];
  10853. int result = [iSalesDB execSql:sql db:db];
  10854. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10855. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10856. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10857. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10858. [iSalesDB close_db:db];
  10859. return [RAConvertor dict2data:dic];
  10860. }
  10861. + (void)offline_removePDFWithName:(NSString *)name {
  10862. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10863. NSString *cachefolder = [paths objectAtIndex:0];
  10864. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10865. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10866. NSFileManager *fileManager = [NSFileManager defaultManager];
  10867. [fileManager removeItemAtPath:path error:nil];
  10868. }
  10869. + (void)offline_clear_PDFCache {
  10870. NSFileManager *fileManager = [NSFileManager defaultManager];
  10871. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10872. NSString *cachefolder = [paths objectAtIndex:0];
  10873. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10874. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10875. for (NSString *path in pdf_files) {
  10876. [self offline_removePDFWithName:[path lastPathComponent]];
  10877. }
  10878. }
  10879. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10880. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10881. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10882. NSString *user = [params objectForKey:@"user"];
  10883. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10884. if (![create_user isEqualToString:user]) {
  10885. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10886. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10887. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10888. return [RAConvertor dict2data:dic];
  10889. }
  10890. 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]];
  10891. __block int is_local = 0;
  10892. __block NSString *path = @"";
  10893. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10894. is_local = sqlite3_column_int(stmt, 0);
  10895. path = [self textAtColumn:1 statement:stmt];
  10896. }];
  10897. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10898. if (is_local == 1) {
  10899. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10900. // 删除文件
  10901. [self offline_removePDFWithName:path];
  10902. }
  10903. int result = [iSalesDB execSql:sql];
  10904. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10905. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10906. return [RAConvertor dict2data:dic];
  10907. }
  10908. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10909. {
  10910. // assert(add_params[@"contact_id"]!=nil);
  10911. assert(add_params[@"user"]!=nil);
  10912. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10913. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10914. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10915. NSString* where=@"1=1";
  10916. if (ver!=nil) {
  10917. where=@"is_dirty=1";
  10918. }
  10919. 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];
  10920. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10921. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10922. sqlite3 *db = [iSalesDB get_db];
  10923. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10924. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10925. NSInteger _id = sqlite3_column_int(stmt, 0);
  10926. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10927. NSString *name = [self textAtColumn:2 statement:stmt];
  10928. NSString *desc = [self textAtColumn:3 statement:stmt];
  10929. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10930. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10931. NSInteger qty = sqlite3_column_int(stmt, 6);
  10932. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10933. double percent = sqlite3_column_double(stmt, 8);
  10934. double price = sqlite3_column_double(stmt, 9);
  10935. double discount = sqlite3_column_double(stmt, 10);
  10936. NSString *img = [self textAtColumn:11 statement:stmt];
  10937. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10938. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10939. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10940. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10941. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10942. if ([price_null isEqualToString:@"null"]) {
  10943. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10944. }
  10945. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10946. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10947. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10948. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10949. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10950. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10951. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10952. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10953. if ([qty_null isEqualToString:@"null"]) {
  10954. // [item setValue:@"null" forKey:@"available_qty"];
  10955. } else {
  10956. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10957. }
  10958. if ([is_percent_null isEqualToString:@"null"]) {
  10959. // [item setValue:@"null" forKey:@"percentage"];
  10960. } else {
  10961. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10962. }
  10963. if ([percent_null isEqualToString:@"null"]) {
  10964. // [item setValue:@"null" forKey:@"percent"];
  10965. } else {
  10966. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10967. }
  10968. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10969. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10970. [item setValue:name forKey:@"name"];
  10971. [item setValue:desc forKey:@"description"];
  10972. [item setValue:img forKey:@"img"];
  10973. [item setValue:line_note forKey:@"line_note"];
  10974. [item setValue:create_time forKey:@"createtime"];
  10975. [item setValue:modify_time forKey:@"modifytime"];
  10976. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10977. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10978. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10979. }];
  10980. [iSalesDB close_db:db];
  10981. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10982. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10983. }
  10984. return ret;
  10985. }
  10986. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10987. {
  10988. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10989. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10990. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10991. NSString* where=@"1=1";
  10992. if (ver!=nil) {
  10993. where=@"is_dirty=1";
  10994. }
  10995. 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];
  10996. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10997. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10998. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10999. NSInteger _id = sqlite3_column_int(stmt, 0);
  11000. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  11001. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  11002. NSString *create_user = [self textAtColumn:3 statement:stmt];
  11003. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  11004. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  11005. NSString *model_info = [self textAtColumn:6 statement:stmt];
  11006. NSString *createtime = [self textAtColumn:7 statement:stmt];
  11007. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  11008. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  11009. NSString *off_params = [self textAtColumn:10 statement:stmt];
  11010. NSString *uuid = [NSUUID UUID].UUIDString;
  11011. int is_delete = sqlite3_column_int(stmt, 11);
  11012. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  11013. int is_local = sqlite3_column_int(stmt, 12);
  11014. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11015. if (![tearsheet_id_null isEqualToString:@"null"]) {
  11016. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  11017. }
  11018. [item setObject:pdf_path forKey:@"pdf_path"];
  11019. [item setObject:create_user forKey:@"create_user"];
  11020. [item setObject:tear_note forKey:@"tear_note"];
  11021. [item setObject:tear_name forKey:@"tear_name"];
  11022. [item setObject:model_info forKey:@"model_info"];
  11023. [item setObject:createtime forKey:@"createtime"];
  11024. [item setObject:modifytime forKey:@"modifytime"];
  11025. [item setObject:urlParams forKey:@"urlParams"];
  11026. [item setObject:off_params forKey:@"off_params"];
  11027. [item setObject:uuid forKey:@"pdf_token"];
  11028. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  11029. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  11030. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  11031. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  11032. } else {
  11033. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11034. }
  11035. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11036. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11037. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  11038. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11039. }];
  11040. return ret;
  11041. }
  11042. #pragma mark 2020
  11043. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11044. {
  11045. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11046. sqlite3 *db = [iSalesDB get_db];
  11047. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  11048. if (contactType) {
  11049. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  11050. } else {
  11051. contactType = @"1 = 1";
  11052. }
  11053. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  11054. DebugLog(@"offline contact list keyword: %@",keyword);
  11055. // advanced search
  11056. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  11057. if (contact_name) {
  11058. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11059. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  11060. } else {
  11061. contact_name = @"";
  11062. }
  11063. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  11064. if (customer_phone) {
  11065. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11066. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  11067. } else {
  11068. customer_phone = @"";
  11069. }
  11070. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  11071. if (customer_fax) {
  11072. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11073. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  11074. } else {
  11075. customer_fax = @"";
  11076. }
  11077. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  11078. if (customer_zipcode) {
  11079. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11080. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  11081. } else {
  11082. customer_zipcode = @"";
  11083. }
  11084. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  11085. if (customer_sales_rep) {
  11086. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11087. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  11088. } else {
  11089. customer_sales_rep = @"";
  11090. }
  11091. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  11092. if (customer_state) {
  11093. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11094. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  11095. } else {
  11096. customer_state = @"";
  11097. }
  11098. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  11099. if (customer_name) {
  11100. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11101. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  11102. } else {
  11103. customer_name = @"";
  11104. }
  11105. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  11106. if (customer_country) {
  11107. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11108. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  11109. } else {
  11110. customer_country = @"";
  11111. }
  11112. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11113. if (customer_cid) {
  11114. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11115. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11116. } else {
  11117. customer_cid = @"";
  11118. }
  11119. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11120. if (customer_city) {
  11121. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11122. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11123. } else {
  11124. customer_city = @"";
  11125. }
  11126. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11127. if (customer_address) {
  11128. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11129. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11130. } else {
  11131. customer_address = @"";
  11132. }
  11133. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11134. if (customer_email) {
  11135. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11136. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11137. } else {
  11138. customer_email = @"";
  11139. }
  11140. NSString *price_name = [params valueForKey:@"price_name"];
  11141. if (price_name) {
  11142. if ([price_name containsString:@","]) {
  11143. // 首先从 price表中查处name
  11144. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11145. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11146. for (int i = 1;i < pArray.count;i++) {
  11147. NSString *p = pArray[i];
  11148. [mutablePStr appendFormat:@" or type = %@ ",p];
  11149. }
  11150. [mutablePStr appendString:@";"];
  11151. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11152. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11153. char *name = (char *)sqlite3_column_text(stmt, 0);
  11154. if (!name)
  11155. name = "";
  11156. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11157. }];
  11158. // 再根据name 拼sql
  11159. NSMutableString *mutable_price_name = [NSMutableString string];
  11160. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11161. for (int i = 1; i < price_name_array.count; i++) {
  11162. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11163. }
  11164. [mutable_price_name appendString:@")"];
  11165. price_name = mutable_price_name;
  11166. } else {
  11167. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11168. if ([price_name isEqualToString:@""]) {
  11169. price_name = @"";
  11170. } else {
  11171. __block NSString *price;
  11172. price_name = [self translateSingleQuote:price_name];
  11173. [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) {
  11174. char *p = (char *)sqlite3_column_text(stmt, 0);
  11175. if (p == NULL) {
  11176. p = "";
  11177. }
  11178. price = [NSString stringWithUTF8String:p];
  11179. }];
  11180. if ([price isEqualToString:@""]) {
  11181. price_name = @"";
  11182. } else {
  11183. price = [self translateSingleQuote:price];
  11184. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11185. }
  11186. }
  11187. }
  11188. } else {
  11189. price_name = @"";
  11190. }
  11191. int limit = [[params valueForKey:@"limit"] intValue];
  11192. int offset = [[params valueForKey:@"offset"] intValue];
  11193. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11194. 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];
  11195. 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];
  11196. // int result= [iSalesDB AddExFunction:db];
  11197. int count =0;
  11198. NSString *sqlQuery = nil;
  11199. if(keyword.length==0)
  11200. {
  11201. // 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];
  11202. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11203. sqlQuery = sql;
  11204. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11205. }
  11206. else
  11207. {
  11208. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11209. keyword = keyword.lowercaseString;
  11210. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11211. 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];
  11212. 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]];
  11213. }
  11214. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11215. sqlite3_stmt * statement;
  11216. [ret setValue:@"2" forKey:@"result"];
  11217. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11218. // 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";
  11219. int i = 0;
  11220. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11221. {
  11222. while (sqlite3_step(statement) == SQLITE_ROW)
  11223. {
  11224. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11225. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11226. int editable = sqlite3_column_int(statement, 0);
  11227. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11228. NSString *nscompany_name =nil;
  11229. if(company_name==nil)
  11230. nscompany_name=@"";
  11231. else
  11232. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11233. char *country = (char*)sqlite3_column_text(statement, 2);
  11234. if(country==nil)
  11235. country="";
  11236. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11237. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11238. // if(addr==nil)
  11239. // addr="";
  11240. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11241. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11242. if(zipcode==nil)
  11243. zipcode="";
  11244. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11245. char *state = (char*)sqlite3_column_text(statement, 5);
  11246. if(state==nil)
  11247. state="";
  11248. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11249. char *city = (char*)sqlite3_column_text(statement, 6);
  11250. if(city==nil)
  11251. city="";
  11252. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11253. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11254. // NSString *nscontact_name = nil;
  11255. // if(contact_name==nil)
  11256. // nscontact_name=@"";
  11257. // else
  11258. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11259. char *phone = (char*)sqlite3_column_text(statement, 8);
  11260. NSString *nsphone = nil;
  11261. if(phone==nil)
  11262. nsphone=@"";
  11263. else
  11264. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11265. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11266. if(contact_id==nil)
  11267. contact_id="";
  11268. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11269. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11270. if(addr_1==nil)
  11271. addr_1="";
  11272. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11273. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11274. if(addr_2==nil)
  11275. addr_2="";
  11276. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11277. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11278. if(addr_3==nil)
  11279. addr_3="";
  11280. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11281. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11282. if(addr_4==nil)
  11283. addr_4="";
  11284. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11285. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11286. if(first_name==nil)
  11287. first_name="";
  11288. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11289. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11290. if(last_name==nil)
  11291. last_name="";
  11292. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11293. char *fax = (char*)sqlite3_column_text(statement, 16);
  11294. NSString *nsfax = nil;
  11295. if(fax==nil)
  11296. nsfax=@"";
  11297. else
  11298. {
  11299. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11300. if(nsfax.length>0)
  11301. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11302. }
  11303. char *email = (char*)sqlite3_column_text(statement, 17);
  11304. NSString *nsemail = nil;
  11305. if(email==nil)
  11306. nsemail=@"";
  11307. else
  11308. {
  11309. nsemail= [[NSString alloc]initWithUTF8String:email];
  11310. if(nsemail.length>0)
  11311. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11312. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11313. }
  11314. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11315. [arr_name addObject:nsfirst_name];
  11316. [arr_name addObject:nslast_name];
  11317. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11318. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11319. {
  11320. // decrypt
  11321. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11322. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11323. nsphone=[AESCrypt fastdecrypt:nsphone];
  11324. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11325. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11326. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11327. }
  11328. [arr_addr addObject:nscompany_name];
  11329. [arr_addr addObject:nscontact_name];
  11330. [arr_addr addObject:@"<br>"];
  11331. [arr_addr addObject:nsaddr_1];
  11332. [arr_addr addObject:nsaddr_2];
  11333. [arr_addr addObject:nsaddr_3];
  11334. [arr_addr addObject:nsaddr_4];
  11335. //[arr_addr addObject:nsaddr];
  11336. [arr_addr addObject:nszipcode];
  11337. [arr_addr addObject:nscity];
  11338. [arr_addr addObject:nsstate];
  11339. [arr_addr addObject:nscountry];
  11340. [arr_addr addObject:@"<br>"];
  11341. [arr_addr addObject:nsphone];
  11342. [arr_addr addObject:nsfax];
  11343. [arr_addr addObject:nsemail];
  11344. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11345. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11346. [item setValue:name forKey:@"name"];
  11347. [item setValue:nscontact_id forKey:@"contact_id"];
  11348. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11349. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11350. i++;
  11351. }
  11352. sqlite3_finalize(statement);
  11353. }
  11354. [iSalesDB close_db:db];
  11355. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11356. dispatch_async(dispatch_get_main_queue(), ^{
  11357. UIApplication * app = [UIApplication sharedApplication];
  11358. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11359. [ret setValue:appDelegate.mode forKey:@"mode"];
  11360. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11361. result(ret);
  11362. });
  11363. return ;
  11364. });
  11365. }
  11366. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11367. //{
  11368. //
  11369. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11370. //
  11371. // NSString* contactId = [params valueForKey:@"contactId"];
  11372. //
  11373. //
  11374. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11375. //
  11376. //
  11377. //
  11378. //
  11379. //
  11380. //
  11381. // sqlite3 *db = [iSalesDB get_db];
  11382. //
  11383. //
  11384. //
  11385. //
  11386. //
  11387. // NSString *sqlQuery = nil;
  11388. //
  11389. //
  11390. // {
  11391. // 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];
  11392. //
  11393. // }
  11394. //
  11395. //
  11396. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11397. // sqlite3_stmt * statement;
  11398. //
  11399. //
  11400. // [ret setValue:@"2" forKey:@"result"];
  11401. //
  11402. //
  11403. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11404. // {
  11405. //
  11406. // //int i = 0;
  11407. // if (sqlite3_step(statement) == SQLITE_ROW)
  11408. // {
  11409. //
  11410. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11411. //
  11412. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11413. //
  11414. //
  11415. // // int editable = sqlite3_column_int(statement, 0);
  11416. //
  11417. //
  11418. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11419. // NSString *nscompany_name =nil;
  11420. // if(company_name==nil)
  11421. // nscompany_name=@"";
  11422. // else
  11423. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11424. //
  11425. //
  11426. // char *country = (char*)sqlite3_column_text(statement, 2);
  11427. // if(country==nil)
  11428. // country="";
  11429. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11430. //
  11431. //
  11432. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11433. // // if(addr==nil)
  11434. // // addr="";
  11435. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11436. //
  11437. //
  11438. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11439. // if(zipcode==nil)
  11440. // zipcode="";
  11441. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11442. //
  11443. //
  11444. // char *state = (char*)sqlite3_column_text(statement, 5);
  11445. // if(state==nil)
  11446. // state="";
  11447. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11448. //
  11449. // char *city = (char*)sqlite3_column_text(statement, 6);
  11450. // if(city==nil)
  11451. // city="";
  11452. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11453. //
  11454. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11455. // // NSString *nscontact_name = nil;
  11456. // // if(contact_name==nil)
  11457. // // nscontact_name=@"";
  11458. // // else
  11459. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11460. //
  11461. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11462. // NSString *nsphone = nil;
  11463. // if(phone==nil)
  11464. // nsphone=@"";
  11465. // else
  11466. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11467. //
  11468. //
  11469. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11470. // // if(contact_id==nil)
  11471. // // contact_id="";
  11472. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11473. //
  11474. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11475. // if(addr_1==nil)
  11476. // addr_1="";
  11477. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11478. //
  11479. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11480. // if(addr_2==nil)
  11481. // addr_2="";
  11482. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11483. //
  11484. //
  11485. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11486. // if(addr_3==nil)
  11487. // addr_3="";
  11488. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11489. //
  11490. //
  11491. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11492. // if(addr_4==nil)
  11493. // addr_4="";
  11494. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11495. //
  11496. //
  11497. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11498. // if(first_name==nil)
  11499. // first_name="";
  11500. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11501. //
  11502. //
  11503. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11504. // if(last_name==nil)
  11505. // last_name="";
  11506. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11507. //
  11508. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11509. // NSString *nsfax = nil;
  11510. // if(fax==nil)
  11511. // nsfax=@"";
  11512. // else
  11513. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11514. //
  11515. // char *email = (char*)sqlite3_column_text(statement, 17);
  11516. // NSString *nsemail = nil;
  11517. // if(email==nil)
  11518. // nsemail=@"";
  11519. // else
  11520. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11521. //
  11522. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11523. // NSString *nsimg_0 = nil;
  11524. // if(img_0==nil)
  11525. // nsimg_0=@"";
  11526. // else
  11527. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11528. //
  11529. // [self copy_bcardImg:nsimg_0];
  11530. //
  11531. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11532. // NSString *nsimg_1 = nil;
  11533. // if(img_1==nil)
  11534. // nsimg_1=@"";
  11535. // else
  11536. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11537. // [self copy_bcardImg:nsimg_1];
  11538. //
  11539. //
  11540. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11541. // NSString *nsimg_2 = nil;
  11542. // if(img_2==nil)
  11543. // nsimg_2=@"";
  11544. // else
  11545. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11546. // [self copy_bcardImg:nsimg_2];
  11547. //
  11548. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11549. // NSString *nsprice_type = nil;
  11550. // if(price_type==nil)
  11551. // nsprice_type=@"";
  11552. // else
  11553. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11554. //
  11555. //
  11556. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11557. // NSString *nsnotes = nil;
  11558. // if(notes==nil)
  11559. // nsnotes=@"";
  11560. // else
  11561. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11562. //
  11563. //
  11564. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11565. // NSString *nssalesrep = nil;
  11566. // if(salesrep==nil)
  11567. // nssalesrep=@"";
  11568. // else
  11569. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11570. //
  11571. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11572. //
  11573. //
  11574. // {
  11575. // // decrypt
  11576. //
  11577. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11578. //
  11579. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11580. //
  11581. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11582. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11583. //
  11584. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11585. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11586. //
  11587. // }
  11588. //
  11589. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11590. //
  11591. // [arr_name addObject:nsfirst_name];
  11592. // [arr_name addObject:nslast_name];
  11593. //
  11594. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11595. //
  11596. //
  11597. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11598. //
  11599. // [arr_ext addObject:nsaddr_1];
  11600. // [arr_ext addObject:nsaddr_2];
  11601. // [arr_ext addObject:nsaddr_3];
  11602. // [arr_ext addObject:nsaddr_4];
  11603. // [arr_ext addObject:@"\r\n"];
  11604. //
  11605. // [arr_ext addObject:nscity];
  11606. // [arr_ext addObject:nsstate];
  11607. // [arr_ext addObject:nszipcode];
  11608. // [arr_ext addObject:nscountry];
  11609. //
  11610. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11611. //
  11612. //
  11613. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11614. //
  11615. //
  11616. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11617. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11618. // [item setValue:nscountry forKey:@"customer_country"];
  11619. // [item setValue:nsphone forKey:@"customer_phone"];
  11620. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11621. // [item setValue:nscompany_name forKey:@"customer_name"];
  11622. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11623. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11624. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11625. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11626. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11627. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11628. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11629. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11630. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11631. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11632. // [item setValue:nscity forKey:@"customer_city"];
  11633. // [item setValue:nsstate forKey:@"customer_state"];
  11634. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11635. // [item setValue:contactId forKey:@"customer_cid"];
  11636. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11637. // [item setValue:nsfax forKey:@"customer_fax"];
  11638. // [item setValue:nsemail forKey:@"customer_email"];
  11639. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11640. //
  11641. //
  11642. // [ret setObject:item forKey:@"customerInfo"];
  11643. // // i++;
  11644. //
  11645. //
  11646. //
  11647. // }
  11648. //
  11649. //
  11650. //
  11651. //
  11652. // sqlite3_finalize(statement);
  11653. // }
  11654. //
  11655. //
  11656. //
  11657. // [iSalesDB close_db:db];
  11658. //
  11659. //
  11660. //
  11661. //
  11662. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11663. // dispatch_async(dispatch_get_main_queue(), ^{
  11664. // UIApplication * app = [UIApplication sharedApplication];
  11665. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11666. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11667. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11668. // result(ret);
  11669. // });
  11670. //
  11671. // return ;
  11672. // });
  11673. //}
  11674. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11675. {
  11676. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11677. NSString* order_code= appDelegate.order_code;
  11678. NSString* user = appDelegate.user;
  11679. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11680. NSString* category = [params valueForKey:@"category"];
  11681. if (!category || [category isEqualToString:@""]) {
  11682. category = @"%";
  11683. }
  11684. category = [self translateSingleQuote:category];
  11685. int limit = [[params valueForKey:@"limit"] intValue];
  11686. int offset = [[params valueForKey:@"offset"] intValue];
  11687. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11688. NSString *limit_str = @"";
  11689. if (limited) {
  11690. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11691. }
  11692. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11693. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11694. sqlite3 *db = [iSalesDB get_db];
  11695. // [iSalesDB AddExFunction:db];
  11696. int count;
  11697. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11698. 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];
  11699. double price_min = 0;
  11700. double price_max = 0;
  11701. if ([params.allKeys containsObject:@"alert"]) {
  11702. // alert
  11703. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11704. NSString *alert = params[@"alert"];
  11705. if ([alert isEqualToString:@"Display All"]) {
  11706. alert = [NSString stringWithFormat:@""];
  11707. } else {
  11708. alert = [self translateSingleQuote:alert];
  11709. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11710. }
  11711. // available
  11712. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11713. NSString *available = params[@"available"];
  11714. NSString *available_condition;
  11715. if ([available isEqualToString:@"Display All"]) {
  11716. available_condition = @"";
  11717. } else if ([available isEqualToString:@"Available Now"]) {
  11718. available_condition = @"and availability > 0";
  11719. } else {
  11720. available_condition = @"and availability == 0";
  11721. }
  11722. // best seller
  11723. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11724. NSString *best_seller = @"";
  11725. NSString *order_best_seller = @"m.name asc";
  11726. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11727. best_seller = @"and best_seller > 0";
  11728. order_best_seller = @"m.best_seller desc,m.name asc";
  11729. }
  11730. // price
  11731. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11732. NSString *price = params[@"price"];
  11733. price_min = 0;
  11734. price_max = MAXFLOAT;
  11735. if (user && price != nil) {
  11736. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11737. NSMutableString *priceName = [NSMutableString string];
  11738. for (int i = 0; i < priceTypeArray.count; i++) {
  11739. NSString *pricetype = priceTypeArray[i];
  11740. pricetype = [self translateSingleQuote:pricetype];
  11741. if (i == 0) {
  11742. [priceName appendFormat:@"'%@'",pricetype];
  11743. } else {
  11744. [priceName appendFormat:@",'%@'",pricetype];
  11745. }
  11746. }
  11747. if ([price isEqualToString:@"Display All"]) {
  11748. price = [NSString stringWithFormat:@""];
  11749. } else if([price containsString:@"+"]){
  11750. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11751. price_min = [price doubleValue];
  11752. 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];
  11753. } else {
  11754. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11755. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11756. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11757. 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];
  11758. }
  11759. } else {
  11760. price = @"";
  11761. }
  11762. // sold_by_qty : Sold in quantities of %@
  11763. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11764. NSString *qty = params[@"sold_by_qty"];
  11765. if ([qty isEqualToString:@"Display All"]) {
  11766. qty = @"";
  11767. } else {
  11768. qty = [self translateSingleQuote:qty];
  11769. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11770. }
  11771. // cate
  11772. // category = [self translateSingleQuote:category];
  11773. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11774. // cate mutiple selection
  11775. NSString *category_id = params[@"category"];
  11776. NSMutableArray *cate_id_array = nil;
  11777. NSMutableString *cateWhere = [NSMutableString string];
  11778. if ([category_id isEqualToString:@""] || !category_id) {
  11779. [cateWhere appendString:@"1 = 1"];
  11780. } else {
  11781. if ([category_id containsString:@","]) {
  11782. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11783. } else {
  11784. cate_id_array = [@[category_id] mutableCopy];
  11785. }
  11786. [cateWhere appendString:@"("];
  11787. for (int i = 0; i < cate_id_array.count; i++) {
  11788. if (i == 0) {
  11789. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11790. } else {
  11791. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11792. }
  11793. }
  11794. [cateWhere appendString:@")"];
  11795. }
  11796. // where bestseller > 0 order by bestseller desc
  11797. // sql query: alert availability(int) best_seller(int) price qty
  11798. 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];
  11799. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11800. }
  11801. DebugLog(@"offline category where: %@",where);
  11802. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11803. if (!user) {
  11804. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11805. }
  11806. [ret setValue:filter forKey:@"filter"];
  11807. DebugLog(@"offline_category sql:%@",sqlQuery);
  11808. sqlite3_stmt * statement;
  11809. [ret setValue:@"2" forKey:@"result"];
  11810. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11811. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11812. // int count=0;
  11813. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11814. {
  11815. int i=0;
  11816. while (sqlite3_step(statement) == SQLITE_ROW)
  11817. {
  11818. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11819. char *name = (char*)sqlite3_column_text(statement, 0);
  11820. if(name==nil)
  11821. name="";
  11822. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11823. char *description = (char*)sqlite3_column_text(statement, 1);
  11824. if(description==nil)
  11825. description="";
  11826. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11827. int product_id = sqlite3_column_int(statement, 2);
  11828. int wid = sqlite3_column_int(statement, 3);
  11829. int closeout = sqlite3_column_int(statement, 4);
  11830. int cid = sqlite3_column_int(statement, 5);
  11831. int wisdelete = sqlite3_column_int(statement, 6);
  11832. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11833. int more_color = sqlite3_column_int(statement, 8);
  11834. // Defaul Category ID
  11835. __block NSString *categoryID = nil;
  11836. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11837. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11838. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11839. if(default_category==nil)
  11840. default_category="";
  11841. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11842. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11843. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11844. categoryID = nsdefault_category;
  11845. }];
  11846. if (!categoryID.length) {
  11847. NSString *cateIDs = params[@"category"];
  11848. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11849. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11850. for (NSString *cateID in requestCategoryArr) {
  11851. BOOL needBreak = NO;
  11852. for (NSString *itemCateIDBox in itemCategoryArr) {
  11853. if (itemCateIDBox.length > 4) {
  11854. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11855. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11856. if ([itemCategoryID isEqualToString:cateID]) {
  11857. needBreak = YES;
  11858. categoryID = itemCategoryID;
  11859. break;
  11860. }
  11861. }
  11862. }
  11863. if (needBreak) {
  11864. break;
  11865. }
  11866. }
  11867. }
  11868. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11869. if(wid !=0 && wisdelete != 1)
  11870. [item setValue:@"true" forKey:@"wish_exists"];
  11871. else
  11872. [item setValue:@"false" forKey:@"wish_exists"];
  11873. if(closeout==0)
  11874. [item setValue:@"false" forKey:@"is_closeout"];
  11875. else
  11876. [item setValue:@"true" forKey:@"is_closeout"];
  11877. if(cid==0)
  11878. [item setValue:@"false" forKey:@"cart_exists"];
  11879. else
  11880. [item setValue:@"true" forKey:@"cart_exists"];
  11881. if (more_color == 0) {
  11882. [item setObject:@(false) forKey:@"more_color"];
  11883. } else if (more_color == 1) {
  11884. [item setObject:@(true) forKey:@"more_color"];
  11885. }
  11886. [item addEntriesFromDictionary:imgjson];
  11887. // [item setValue:nsurl forKey:@"img"];
  11888. [item setValue:nsname forKey:@"name"];
  11889. [item setValue:nsdescription forKey:@"description"];
  11890. if (categoryID) {
  11891. [item setValue:categoryID forKey:@"item_category_id"];
  11892. }
  11893. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11894. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11895. i++;
  11896. }
  11897. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11898. [ret setObject:items forKey:@"items"];
  11899. sqlite3_finalize(statement);
  11900. } else {
  11901. DebugLog(@"nothing...");
  11902. }
  11903. DebugLog(@"count:%d",count);
  11904. [iSalesDB close_db:db];
  11905. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11906. dispatch_async(dispatch_get_main_queue(), ^{
  11907. // UIApplication * app = [UIApplication sharedApplication];
  11908. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11909. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11910. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11911. result(ret);
  11912. });
  11913. });
  11914. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11915. }
  11916. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11917. {
  11918. [self categoryList:params limited:YES completionHandler:result];
  11919. }
  11920. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11921. {
  11922. UIApplication * app = [UIApplication sharedApplication];
  11923. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11924. NSString* order_code = appDelegate.order_code;
  11925. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11926. NSString* keyword = [params valueForKey:@"keyword"];
  11927. keyword=keyword.lowercaseString;
  11928. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11929. int limit = [[params valueForKey:@"limit"] intValue];
  11930. int offset = [[params valueForKey:@"offset"] intValue];
  11931. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11932. NSString *limit_str = @"";
  11933. if (limited) {
  11934. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11935. }
  11936. sqlite3 *db = [iSalesDB get_db];
  11937. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11938. NSString *sqlQuery = nil;
  11939. if(exactMatch )
  11940. 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 ;
  11941. else
  11942. 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
  11943. DebugLog(@"offline_search sql:%@",sqlQuery);
  11944. sqlite3_stmt * statement;
  11945. [ret setValue:@"2" forKey:@"result"];
  11946. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11947. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11948. // int count=0;
  11949. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11950. {
  11951. int i=0;
  11952. while (sqlite3_step(statement) == SQLITE_ROW)
  11953. {
  11954. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11955. // char *name = (char*)sqlite3_column_text(statement, 1);
  11956. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11957. char *name = (char*)sqlite3_column_text(statement, 0);
  11958. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11959. char *description = (char*)sqlite3_column_text(statement, 1);
  11960. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11961. int product_id = sqlite3_column_int(statement, 2);
  11962. // char *url = (char*)sqlite3_column_text(statement, 3);
  11963. // if(url==nil)
  11964. // url="";
  11965. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11966. int wid = sqlite3_column_int(statement, 3);
  11967. int closeout = sqlite3_column_int(statement, 4);
  11968. int cid = sqlite3_column_int(statement, 5);
  11969. int wisdelete = sqlite3_column_int(statement, 6);
  11970. int more_color = sqlite3_column_int(statement, 7);
  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:@"fash_name"];
  11992. [item setValue:nsdescription forKey:@"description"];
  11993. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11994. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11995. i++;
  11996. }
  11997. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11998. [ret setObject:items forKey:@"items"];
  11999. sqlite3_finalize(statement);
  12000. }
  12001. DebugLog(@"count:%d",count);
  12002. [iSalesDB close_db:db];
  12003. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12004. dispatch_async(dispatch_get_main_queue(), ^{
  12005. result(ret);
  12006. });
  12007. });
  12008. }
  12009. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12010. {
  12011. [self search:params limited:YES completionHandler:result];
  12012. }
  12013. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12014. //{
  12015. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  12016. // NSString* order_code = appDelegate.order_code;
  12017. // NSString* user = appDelegate.user;
  12018. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12019. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  12020. //
  12021. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  12022. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  12023. //
  12024. // // category
  12025. // NSDictionary *category_menu = [self offline_category_menu];
  12026. // [filter setValue:category_menu forKey:@"category"];
  12027. //
  12028. // NSString* where= nil;
  12029. // NSString* orderby= @"m.name";
  12030. // if (!filterSearch) {
  12031. // int covertype = [[params valueForKey:@"covertype"] intValue];
  12032. //
  12033. // switch (covertype) {
  12034. // case 0:
  12035. // {
  12036. // where=@"m.category like'%%#005#%%'";
  12037. // break;
  12038. // }
  12039. // case 1:
  12040. // {
  12041. // where=@"m.alert like '%QS%'";
  12042. // break;
  12043. // }
  12044. // case 2:
  12045. // {
  12046. // where=@"m.availability>0";
  12047. // break;
  12048. // }
  12049. // case 3:
  12050. // {
  12051. // where=@"m.best_seller>0";
  12052. // orderby=@"m.best_seller desc,m.name asc";
  12053. // break;
  12054. // }
  12055. // default:
  12056. // where=@"1=1";
  12057. // break;
  12058. // }
  12059. //
  12060. // }
  12061. //
  12062. //
  12063. // int limit = [[params valueForKey:@"limit"] intValue];
  12064. // int offset = [[params valueForKey:@"offset"] intValue];
  12065. //
  12066. // NSString *limit_str = @"";
  12067. // if (limited) {
  12068. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  12069. // }
  12070. //
  12071. //
  12072. // sqlite3 *db = [iSalesDB get_db];
  12073. // // [iSalesDB AddExFunction:db];
  12074. //
  12075. // int count;
  12076. //
  12077. // NSString *sqlQuery = nil;
  12078. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  12079. // 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];
  12080. //
  12081. //
  12082. // double price_min = 0;
  12083. // double price_max = 0;
  12084. // if (filterSearch) {
  12085. // // alert
  12086. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  12087. // NSString *alert = params[@"alert"];
  12088. // if ([alert isEqualToString:@"Display All"]) {
  12089. // alert = [NSString stringWithFormat:@""];
  12090. // } else {
  12091. // alert = [self translateSingleQuote:alert];
  12092. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  12093. // }
  12094. //
  12095. // // available
  12096. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  12097. // NSString *available = params[@"available"];
  12098. // NSString *available_condition;
  12099. // if ([available isEqualToString:@"Display All"]) {
  12100. // available_condition = @"";
  12101. // } else if ([available isEqualToString:@"Available Now"]) {
  12102. // available_condition = @"and availability > 0";
  12103. // } else {
  12104. // available_condition = @"and availability == 0";
  12105. // }
  12106. //
  12107. // // best seller
  12108. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  12109. // NSString *best_seller = @"";
  12110. // NSString *order_best_seller = @"m.name asc";
  12111. //
  12112. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12113. // best_seller = @"and best_seller > 0";
  12114. // order_best_seller = @"m.best_seller desc,m.name asc";
  12115. // }
  12116. //
  12117. // // price
  12118. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12119. // NSString *price = params[@"price"];
  12120. // price_min = 0;
  12121. // price_max = MAXFLOAT;
  12122. // if (user) {
  12123. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12124. // NSMutableString *priceName = [NSMutableString string];
  12125. // for (int i = 0; i < priceTypeArray.count; i++) {
  12126. // NSString *pricetype = priceTypeArray[i];
  12127. // pricetype = [self translateSingleQuote:pricetype];
  12128. // if (i == 0) {
  12129. // [priceName appendFormat:@"'%@'",pricetype];
  12130. // } else {
  12131. // [priceName appendFormat:@",'%@'",pricetype];
  12132. // }
  12133. // }
  12134. //
  12135. // if ([price isEqualToString:@"Display All"]) {
  12136. // price = [NSString stringWithFormat:@""];
  12137. // } else if([price containsString:@"+"]){
  12138. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12139. // price_min = [price doubleValue];
  12140. // 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];
  12141. // } else {
  12142. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12143. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12144. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12145. // 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];
  12146. // }
  12147. // } else {
  12148. // price = @"";
  12149. // }
  12150. //
  12151. // // sold_by_qty : Sold in quantities of %@
  12152. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12153. // NSString *qty = params[@"sold_by_qty"];
  12154. // if ([qty isEqualToString:@"Display All"]) {
  12155. // qty = @"";
  12156. // } else {
  12157. // qty = [self translateSingleQuote:qty];
  12158. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12159. // }
  12160. //
  12161. // // category
  12162. // NSString *category_id = params[@"ctgId"];
  12163. // NSMutableArray *cate_id_array = nil;
  12164. // NSMutableString *cateWhere = [NSMutableString string];
  12165. //
  12166. // if ([category_id isEqualToString:@""] || !category_id) {
  12167. // [cateWhere appendString:@"1 = 1"];
  12168. // } else {
  12169. //
  12170. // if ([category_id containsString:@","]) {
  12171. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12172. // } else {
  12173. // cate_id_array = [@[category_id] mutableCopy];
  12174. // }
  12175. // /*-----------*/
  12176. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12177. //
  12178. // [cateWhere appendString:@"("];
  12179. // for (int i = 0; i < cate_id_array.count; i++) {
  12180. //
  12181. // for (NSString *key0 in cateDic.allKeys) {
  12182. //
  12183. // if ([key0 containsString:@"category_"]) {
  12184. //
  12185. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12186. //
  12187. // for (NSString *key1 in category0.allKeys) {
  12188. //
  12189. // if ([key1 containsString:@"category_"]) {
  12190. //
  12191. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12192. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12193. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12194. //
  12195. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12196. // if (i == 0) {
  12197. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12198. // } else {
  12199. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12200. // }
  12201. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12202. // [category0 setValue:category1 forKey:key1];
  12203. // [cateDic setValue:category0 forKey:key0];
  12204. //
  12205. // }
  12206. //
  12207. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12208. //
  12209. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12210. //
  12211. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12212. // if (i == 0) {
  12213. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12214. // } else {
  12215. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12216. // }
  12217. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12218. // [cateDic setValue:category0 forKey:key0];
  12219. //
  12220. // }
  12221. //
  12222. // }
  12223. //
  12224. // }
  12225. //
  12226. // }
  12227. //
  12228. // }
  12229. //
  12230. // }
  12231. // [cateWhere appendString:@")"];
  12232. //
  12233. // [filter setValue:cateDic forKey:@"category"];
  12234. // }
  12235. //
  12236. // // where bestseller > 0 order by bestseller desc
  12237. // // sql query: alert availability(int) best_seller(int) price qty
  12238. //
  12239. // 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];
  12240. //
  12241. //
  12242. // // count
  12243. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12244. //
  12245. // }
  12246. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12247. //
  12248. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12249. //
  12250. //
  12251. //
  12252. //
  12253. // if (!user) {
  12254. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12255. // }
  12256. //
  12257. // [ret setValue:filter forKey:@"filter"];
  12258. //
  12259. //
  12260. //
  12261. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12262. //
  12263. // sqlite3_stmt * statement;
  12264. // [ret setValue:@"2" forKey:@"result"];
  12265. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12266. //
  12267. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12268. // // int count=0;
  12269. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12270. // {
  12271. //
  12272. // int i=0;
  12273. // while (sqlite3_step(statement) == SQLITE_ROW)
  12274. // {
  12275. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12276. //
  12277. //
  12278. // char *name = (char*)sqlite3_column_text(statement, 0);
  12279. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12280. //
  12281. // char *description = (char*)sqlite3_column_text(statement, 1);
  12282. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12283. //
  12284. //
  12285. //
  12286. // int product_id = sqlite3_column_int(statement, 2);
  12287. //
  12288. //
  12289. // int wid = sqlite3_column_int(statement, 3);
  12290. // int closeout = sqlite3_column_int(statement, 4);
  12291. // int cid = sqlite3_column_int(statement, 5);
  12292. // int wisdelete = sqlite3_column_int(statement, 6);
  12293. // int more_color = sqlite3_column_int(statement, 7);
  12294. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12295. //
  12296. // if(wid !=0 && wisdelete != 1)
  12297. // [item setValue:@"true" forKey:@"wish_exists"];
  12298. // else
  12299. // [item setValue:@"false" forKey:@"wish_exists"];
  12300. //
  12301. // if(closeout==0)
  12302. // [item setValue:@"false" forKey:@"is_closeout"];
  12303. // else
  12304. // [item setValue:@"true" forKey:@"is_closeout"];
  12305. //
  12306. // if(cid==0)
  12307. // [item setValue:@"false" forKey:@"cart_exists"];
  12308. // else
  12309. // [item setValue:@"true" forKey:@"cart_exists"];
  12310. //
  12311. // if (more_color == 0) {
  12312. // [item setObject:@(false) forKey:@"more_color"];
  12313. // } else if (more_color == 1) {
  12314. // [item setObject:@(true) forKey:@"more_color"];
  12315. // }
  12316. //
  12317. // [item addEntriesFromDictionary:imgjson];
  12318. //
  12319. //
  12320. // // [item setValue:nsurl forKey:@"img"];
  12321. // [item setValue:nsname forKey:@"fash_name"];
  12322. // [item setValue:nsdescription forKey:@"description"];
  12323. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12324. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12325. // i++;
  12326. // }
  12327. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12328. // [ret setObject:items forKey:@"items"];
  12329. // sqlite3_finalize(statement);
  12330. // }
  12331. //
  12332. // [iSalesDB close_db:db];
  12333. //
  12334. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12335. //
  12336. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12337. // dispatch_async(dispatch_get_main_queue(), ^{
  12338. //
  12339. // result(ret);
  12340. // });
  12341. //
  12342. // });
  12343. //
  12344. //
  12345. //
  12346. //}
  12347. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12348. //{
  12349. // [self itemsearch:params limited:YES completionHandler:result];
  12350. //}
  12351. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12352. {
  12353. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12354. NSString* offline_command=params[@"offline_Command"];
  12355. NSMutableDictionary* ret=nil;
  12356. if([offline_command isEqualToString:@"model_NIYMAL"])
  12357. {
  12358. NSString* category = params[@"category"];
  12359. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12360. }
  12361. dispatch_async(dispatch_get_main_queue(), ^{
  12362. result(ret);
  12363. });
  12364. });
  12365. }
  12366. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12367. {
  12368. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12369. int sort = [[params objectForKey:@"sort"] intValue];
  12370. NSString *sort_str = @"";
  12371. switch (sort) {
  12372. case 0:{
  12373. sort_str = @"order by w.modify_time desc";
  12374. }
  12375. break;
  12376. case 1:{
  12377. sort_str = @"order by w.modify_time asc";
  12378. }
  12379. break;
  12380. case 2:{
  12381. sort_str = @"order by m.name asc";
  12382. }
  12383. break;
  12384. case 3:{
  12385. sort_str = @"order by m.name desc";
  12386. }
  12387. break;
  12388. case 4:{
  12389. sort_str = @"order by m.description asc";
  12390. }
  12391. break;
  12392. default:
  12393. break;
  12394. }
  12395. // NSString* user = appDelegate.user;
  12396. sqlite3 *db = [iSalesDB get_db];
  12397. // order by w.create_time
  12398. 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];
  12399. // 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];
  12400. sqlite3_stmt * statement;
  12401. NSDate *date1 = [NSDate date];
  12402. // NSDate *date2 = nil;
  12403. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12404. int count=0;
  12405. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12406. {
  12407. while (sqlite3_step(statement) == SQLITE_ROW)
  12408. {
  12409. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12410. int product_id = sqlite3_column_double(statement, 0);
  12411. char *description = (char*)sqlite3_column_text(statement, 1);
  12412. if(description==nil)
  12413. description= "";
  12414. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12415. int item_id = sqlite3_column_double(statement, 2);
  12416. NSDate *date_image = [NSDate date];
  12417. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12418. printf("image : ");
  12419. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12420. // char *url = (char*)sqlite3_column_text(statement, 3);
  12421. // if(url==nil)
  12422. // url="";
  12423. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12424. int qty = sqlite3_column_int(statement, 3);
  12425. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12426. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12427. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12428. item[@"description"]= nsdescription;
  12429. item[@"img"]= nsurl;
  12430. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12431. count++;
  12432. }
  12433. printf("total time:");
  12434. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12435. ret[@"count"]= [NSNumber numberWithInt:count];
  12436. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12437. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12438. ret[@"result"]= [NSNumber numberWithInt:2];
  12439. sqlite3_finalize(statement);
  12440. }
  12441. [iSalesDB close_db:db];
  12442. dispatch_async(dispatch_get_main_queue(), ^{
  12443. UIApplication * app = [UIApplication sharedApplication];
  12444. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12445. appDelegate.wish_count =count;
  12446. [appDelegate update_count_mark];
  12447. result(ret);
  12448. });
  12449. });
  12450. }
  12451. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12452. {
  12453. // UIApplication * app = [UIApplication sharedApplication];
  12454. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12455. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12456. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12457. sqlite3 *db = [iSalesDB get_db];
  12458. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12459. NSString* product_id=params[@"product_id"];
  12460. NSString *item_count_str = params[@"item_count"];
  12461. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12462. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12463. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12464. // NSString *sql = @"";
  12465. for(int i=0;i<arr.count;i++)
  12466. {
  12467. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12468. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12469. __block int cart_count = 0;
  12470. if (!item_count_str) {
  12471. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12472. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12473. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12474. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12475. cart_count = [[model_set_components lastObject] intValue];
  12476. }];
  12477. }
  12478. if(count==0)
  12479. {
  12480. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12481. sqlite3_stmt *stmt;
  12482. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12483. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12484. if (item_count_arr) {
  12485. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12486. } else {
  12487. sqlite3_bind_int(stmt,2,cart_count);
  12488. }
  12489. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12490. [iSalesDB execSql:@"ROLLBACK" db:db];
  12491. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12492. [iSalesDB close_db:db];
  12493. DebugLog(@"add to wishlist error");
  12494. dispatch_async(dispatch_get_main_queue(), ^{
  12495. result(ret);
  12496. });
  12497. }
  12498. } else {
  12499. int qty = 0;
  12500. if (item_count_arr) {
  12501. qty = [item_count_arr[i] intValue];
  12502. } else {
  12503. qty = cart_count;
  12504. }
  12505. 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]];
  12506. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12507. [iSalesDB execSql:@"ROLLBACK" db:db];
  12508. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12509. [iSalesDB close_db:db];
  12510. DebugLog(@"add to wishlist error");
  12511. dispatch_async(dispatch_get_main_queue(), ^{
  12512. result(ret);
  12513. });
  12514. }
  12515. }
  12516. }
  12517. // [iSalesDB execSql:sql db:db];
  12518. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12519. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12520. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12521. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12522. [iSalesDB close_db:db];
  12523. ret[@"result"]= [NSNumber numberWithInt:2];
  12524. dispatch_async(dispatch_get_main_queue(), ^{
  12525. UIApplication * app = [UIApplication sharedApplication];
  12526. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12527. appDelegate.wish_count =count;
  12528. [appDelegate update_count_mark];
  12529. result(ret);
  12530. });
  12531. });
  12532. }
  12533. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12534. {
  12535. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12536. bool bLogin = appDelegate.bLogin;
  12537. __block NSString* contact_id = appDelegate.contact_id;
  12538. __block NSString* user = appDelegate.user;
  12539. __block NSString* order_code = appDelegate.order_code;
  12540. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12541. NSString* model_name = [params valueForKey:@"product_name"];
  12542. NSString* product_id = [params valueForKey:@"product_id"];
  12543. NSString* upc_code = [params valueForKey:@"upc_code"];
  12544. NSString* category = [params valueForKey:@"category"];
  12545. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12546. if(category==nil) {
  12547. category = default_category_id;
  12548. } else {
  12549. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12550. // 参数重有多个category id
  12551. if (arr_0.count > 1) {
  12552. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12553. // 取交集
  12554. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12555. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12556. [set_0 intersectSet:set_1];
  12557. if (set_0.count == 1) {
  12558. category = (NSString *)[set_0 anyObject];
  12559. } else {
  12560. if ([set_0 containsObject:default_category_id]) {
  12561. category = default_category_id;
  12562. } else {
  12563. category = (NSString *)[set_0 anyObject];
  12564. }
  12565. }
  12566. }
  12567. }
  12568. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12569. sqlite3 *db = [iSalesDB get_db];
  12570. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12571. NSString *sqlQuery = nil;
  12572. if(product_id==nil)
  12573. if(model_name==nil)
  12574. {
  12575. 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='%@';
  12576. }
  12577. else
  12578. {
  12579. 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='%@';
  12580. }
  12581. else
  12582. 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=%@;
  12583. sqlite3_stmt * statement;
  12584. [ret setValue:@"2" forKey:@"result"];
  12585. [ret setValue:@"3" forKey:@"detail_section_count"];
  12586. // int count=0;
  12587. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12588. {
  12589. if (sqlite3_step(statement) == SQLITE_ROW)
  12590. {
  12591. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12592. char *name = (char*)sqlite3_column_text(statement, 0);
  12593. if(name==nil)
  12594. name="";
  12595. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12596. char *description = (char*)sqlite3_column_text(statement, 1);
  12597. if(description==nil)
  12598. description="";
  12599. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12600. int product_id = sqlite3_column_int(statement, 2);
  12601. char *color = (char*)sqlite3_column_text(statement, 3);
  12602. if(color==nil)
  12603. color="";
  12604. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12605. //
  12606. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12607. // if(legcolor==nil)
  12608. // legcolor="";
  12609. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12610. //
  12611. //
  12612. int availability = sqlite3_column_int(statement, 5);
  12613. //
  12614. int incoming_stock = sqlite3_column_int(statement, 6);
  12615. char *demension = (char*)sqlite3_column_text(statement, 7);
  12616. if(demension==nil)
  12617. demension="";
  12618. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12619. // ,,,,,,,,,
  12620. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12621. if(seat_height==nil)
  12622. seat_height="";
  12623. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12624. char *material = (char*)sqlite3_column_text(statement, 9);
  12625. if(material==nil)
  12626. material="";
  12627. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12628. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12629. if(box_dim==nil)
  12630. box_dim="";
  12631. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12632. char *volume = (char*)sqlite3_column_text(statement, 11);
  12633. if(volume==nil)
  12634. volume="";
  12635. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12636. double weight = sqlite3_column_double(statement, 12);
  12637. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12638. if(model_set==nil)
  12639. model_set="";
  12640. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12641. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12642. if(load_ability==nil)
  12643. load_ability="";
  12644. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12645. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12646. // if(default_category==nil)
  12647. // default_category="";
  12648. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12649. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12650. if(fabric_content==nil)
  12651. fabric_content="";
  12652. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12653. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12654. if(assembling==nil)
  12655. assembling="";
  12656. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12657. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12658. if(made_in==nil)
  12659. made_in="";
  12660. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12661. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12662. if(special_remarks==nil)
  12663. special_remarks="";
  12664. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12665. int stockUcom = sqlite3_column_double(statement, 20);
  12666. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12667. if(product_group==nil)
  12668. product_group="";
  12669. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12670. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12671. // if(fashion_selector==nil)
  12672. // fashion_selector="";
  12673. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12674. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12675. if(selector_field==nil)
  12676. selector_field="";
  12677. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12678. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12679. if(property_field==nil)
  12680. property_field="";
  12681. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12682. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12683. if(packaging==nil)
  12684. packaging="";
  12685. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12686. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12687. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12688. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12689. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12690. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12691. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12692. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12693. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12694. NSString* Availability=nil;
  12695. if(availability>0)
  12696. Availability=[NSString stringWithFormat:@"%d",availability];
  12697. else
  12698. Availability = @"Out of Stock";
  12699. [img_section setValue:Availability forKey:@"Availability"];
  12700. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12701. char *eta = (char*)sqlite3_column_text(statement, 25);
  12702. if(eta==nil)
  12703. eta="";
  12704. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12705. if ([nseta isEqualToString:@"null"]) {
  12706. nseta = @"";
  12707. }
  12708. if (availability <= 0) {
  12709. [img_section setValue:nseta forKey:@"ETA"];
  12710. }
  12711. int item_id = sqlite3_column_int(statement, 26);
  12712. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12713. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12714. NSString* Price=nil;
  12715. if(bLogin==false)
  12716. Price=@"Must Sign in.";
  12717. else
  12718. {
  12719. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12720. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12721. if(price==nil)
  12722. Price=@"No Price.";
  12723. else
  12724. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12725. }
  12726. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12727. [img_section setValue:Price forKey:@"price"];
  12728. [img_section setValue:nsname forKey:@"model_name"];
  12729. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12730. if (order_code) { // 离线order code即so#
  12731. 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];
  12732. __block int cartQTY = 0;
  12733. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12734. cartQTY = sqlite3_column_int(stmt, 0);
  12735. }];
  12736. if (cartQTY > 0) {
  12737. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12738. }
  12739. }
  12740. [ret setObject:img_section forKey:@"img_section"];
  12741. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12742. int detail0_item_count=0;
  12743. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12744. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12745. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12746. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12747. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12748. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12749. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12750. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12751. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12752. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12753. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12754. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12755. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12756. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12757. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12758. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12759. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12760. {
  12761. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12762. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12763. }
  12764. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12765. [detail0_section setValue:@"kv" forKey:@"type"];
  12766. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12767. [ret setObject:detail0_section forKey:@"detail_0"];
  12768. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12769. // [detail1_section setValue:@"detail" forKey:@"target"];
  12770. // [detail1_section setValue:@"popup" forKey:@"action"];
  12771. // [detail1_section setValue:@"content" forKey:@"type"];
  12772. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12773. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12774. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12775. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12776. [ret setObject:detail1_section forKey:@"detail_1"];
  12777. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12778. [detail2_section setValue:@"detail" forKey:@"target"];
  12779. [detail2_section setValue:@"popup" forKey:@"action"];
  12780. [detail2_section setValue:@"content" forKey:@"type"];
  12781. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12782. [detail2_section setValue:@"true" forKey:@"single_row"];
  12783. [detail2_section setValue:@"local" forKey:@"data"];
  12784. [ret setObject:detail2_section forKey:@"detail_2"];
  12785. }
  12786. sqlite3_finalize(statement);
  12787. }
  12788. else
  12789. {
  12790. [ret setValue:@"8" forKey:@"result"];
  12791. }
  12792. // DebugLog(@"count:%d",count);
  12793. [iSalesDB close_db:db];
  12794. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12795. dispatch_async(dispatch_get_main_queue(), ^{
  12796. result(ret);
  12797. });
  12798. });
  12799. }
  12800. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12801. //{
  12802. //
  12803. // UIApplication * app = [UIApplication sharedApplication];
  12804. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12805. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12806. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12807. // sqlite3 *db = [iSalesDB get_db];
  12808. // NSString* collectId=params[@"collectId"];
  12809. //
  12810. //
  12811. //
  12812. //
  12813. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12814. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12815. // [iSalesDB execSql:sqlQuery db:db];
  12816. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12817. // [iSalesDB close_db:db];
  12818. //
  12819. //
  12820. // ret[@"result"]= [NSNumber numberWithInt:2];
  12821. // dispatch_async(dispatch_get_main_queue(), ^{
  12822. //
  12823. // appDelegate.wish_count =count;
  12824. //
  12825. // [appDelegate update_count_mark];
  12826. // result(ret);
  12827. // });
  12828. //
  12829. // });
  12830. //}
  12831. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12832. {
  12833. UIApplication * app = [UIApplication sharedApplication];
  12834. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12835. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12836. [iSalesDB disable_trigger];
  12837. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12838. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12839. [iSalesDB enable_trigger];
  12840. dispatch_async(dispatch_get_main_queue(), ^{
  12841. //
  12842. // NSString* user = [params valueForKey:@"user"];
  12843. //
  12844. // NSString* password = [params valueForKey:@"password"];
  12845. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12846. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12847. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12848. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12849. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12850. [appDelegate update_count_mark];
  12851. appDelegate.can_show_price =false;
  12852. appDelegate.can_see_price =false;
  12853. appDelegate.can_create_portfolio =false;
  12854. appDelegate.can_create_order =false;
  12855. appDelegate.can_cancel_order =false;
  12856. appDelegate.can_set_cart_price =false;
  12857. appDelegate.can_delete_order =false;
  12858. appDelegate.can_submit_order =false;
  12859. appDelegate.can_set_tearsheet_price =false;
  12860. appDelegate.can_update_contact_info = false;
  12861. appDelegate.save_order_logout = false;
  12862. appDelegate.submit_order_logout = false;
  12863. appDelegate.alert_sold_in_quantities = false;
  12864. appDelegate.ipad_perm =nil ;
  12865. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12866. appDelegate.OrderFilter= nil;
  12867. [appDelegate SetSo:nil];
  12868. [appDelegate set_main_button_panel];
  12869. result(ret);
  12870. });
  12871. });
  12872. }
  12873. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12874. {
  12875. // iSalesDB.
  12876. // UIApplication * app = [UIApplication sharedApplication];
  12877. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12878. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12879. [iSalesDB disable_trigger];
  12880. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12881. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12882. [iSalesDB enable_trigger];
  12883. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12884. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12885. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12886. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12887. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12888. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12889. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12890. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12891. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12892. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12893. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12894. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12895. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12896. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12897. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12898. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12899. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12900. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12901. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12902. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12903. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12904. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12905. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12906. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12907. [arr_name addObject:customer_first_name];
  12908. [arr_name addObject:customer_last_name];
  12909. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12910. // default ship from
  12911. 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';";
  12912. __block NSString *cid = @"";
  12913. __block NSString *name = @"";
  12914. __block NSString *ext = @"";
  12915. __block NSString *contact = @"";
  12916. __block NSString *email = @"";
  12917. __block NSString *fax = @"";
  12918. __block NSString *phone = @"";
  12919. sqlite3 *db = [iSalesDB get_db];
  12920. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12921. cid = [self textAtColumn:0 statement:statment];
  12922. name = [self textAtColumn:1 statement:statment];
  12923. ext = [self textAtColumn:2 statement:statment];
  12924. contact = [self textAtColumn:3 statement:statment];
  12925. email = [self textAtColumn:4 statement:statment];
  12926. fax = [self textAtColumn:5 statement:statment];
  12927. phone = [self textAtColumn:6 statement:statment];
  12928. }];
  12929. // default carrier
  12930. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12931. __block NSString *carrier = @"";
  12932. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12933. carrier = [self textAtColumn:0 statement:statment];
  12934. }];
  12935. NSString* so_id = [self get_offline_soid:db];
  12936. if(so_id==nil)
  12937. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12938. 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];
  12939. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12940. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12941. //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'
  12942. //soId
  12943. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12944. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12945. sqlite3_stmt * statement;
  12946. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12947. {
  12948. if (sqlite3_step(statement) == SQLITE_ROW)
  12949. {
  12950. // char *name = (char*)sqlite3_column_text(statement, 1);
  12951. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12952. //ret = sqlite3_column_int(statement, 0);
  12953. char *soId = (char*)sqlite3_column_text(statement, 0);
  12954. if(soId==nil)
  12955. soId="";
  12956. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12957. [ret setValue:nssoId forKey:@"soId"];
  12958. [ret setValue:nssoId forKey:@"orderCode"];
  12959. }
  12960. sqlite3_finalize(statement);
  12961. }
  12962. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12963. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12964. [iSalesDB close_db:db];
  12965. dispatch_async(dispatch_get_main_queue(), ^{
  12966. result(ret);
  12967. });
  12968. });
  12969. }
  12970. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12971. {
  12972. UIApplication * app = [UIApplication sharedApplication];
  12973. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12974. bool can_create_backorder= appDelegate.can_create_backorder;
  12975. NSString* user = appDelegate.user;
  12976. NSString* contact_id = appDelegate.contact_id;
  12977. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12978. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12979. sqlite3 *db = [iSalesDB get_db];
  12980. NSString* orderCode=params[@"orderCode"];
  12981. int sort = [[params objectForKey:@"sort"] intValue];
  12982. NSString *sort_str = @"";
  12983. switch (sort) {
  12984. case 0:{
  12985. sort_str = @"order by c.modify_time desc";
  12986. }
  12987. break;
  12988. case 1:{
  12989. sort_str = @"order by c.modify_time asc";
  12990. }
  12991. break;
  12992. case 2:{
  12993. sort_str = @"order by m.name asc";
  12994. }
  12995. break;
  12996. case 3:{
  12997. sort_str = @"order by m.name desc";
  12998. }
  12999. break;
  13000. case 4:{
  13001. sort_str = @"order by m.description asc";
  13002. }
  13003. break;
  13004. default:
  13005. break;
  13006. }
  13007. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  13008. sqlite3_stmt * statement1;
  13009. double orderdiscount=0;
  13010. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  13011. if ( dbresult1== SQLITE_OK)
  13012. {
  13013. if (sqlite3_step(statement1) == SQLITE_ROW)
  13014. {
  13015. // NSDate *row_date = [NSDate date];
  13016. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13017. orderdiscount = sqlite3_column_double(statement1, 0);
  13018. }
  13019. sqlite3_finalize(statement1);
  13020. }
  13021. 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 ];
  13022. // 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 ];
  13023. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  13024. DebugLog(@"offline_login sql:%@",sqlQuery);
  13025. sqlite3_stmt * statement;
  13026. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  13027. NSDate *date1 = [NSDate date];
  13028. int count=0;
  13029. int cart_count=0;
  13030. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  13031. if ( dbresult== SQLITE_OK)
  13032. {
  13033. while (sqlite3_step(statement) == SQLITE_ROW)
  13034. {
  13035. // NSDate *row_date = [NSDate date];
  13036. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13037. int product_id = sqlite3_column_int(statement, 0);
  13038. char *str_price = (char*)sqlite3_column_text(statement, 1);
  13039. int item_id = sqlite3_column_int(statement, 7);
  13040. NSString* Price=nil;
  13041. if(str_price==nil)
  13042. {
  13043. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  13044. // NSDate *price_date = [NSDate date];
  13045. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  13046. // DebugLog(@"price time interval");
  13047. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  13048. if(price==nil)
  13049. Price=@"No Price.";
  13050. else
  13051. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  13052. }
  13053. else
  13054. {
  13055. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  13056. }
  13057. double discount = sqlite3_column_double(statement, 2);
  13058. int item_count = sqlite3_column_int(statement, 3);
  13059. char *line_note = (char*)sqlite3_column_text(statement, 4);
  13060. NSString *nsline_note=nil;
  13061. if(line_note!=nil)
  13062. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  13063. char *name = (char*)sqlite3_column_text(statement, 5);
  13064. NSString *nsname=nil;
  13065. if(name!=nil)
  13066. nsname= [[NSString alloc]initWithUTF8String:name];
  13067. char *description = (char*)sqlite3_column_text(statement, 6);
  13068. NSString *nsdescription=nil;
  13069. if(description!=nil)
  13070. nsdescription= [[NSString alloc]initWithUTF8String:description];
  13071. int stockUom = sqlite3_column_int(statement, 8);
  13072. int _id = sqlite3_column_int(statement, 9);
  13073. int availability = sqlite3_column_int(statement, 10);
  13074. // NSDate *subtotal_date = [NSDate date];
  13075. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  13076. // DebugLog(@"subtotal_date time interval");
  13077. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  13078. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  13079. double weight=[bsubtotaljson[@"weight"] doubleValue];
  13080. int carton=[bsubtotaljson[@"carton"] intValue];
  13081. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  13082. int mpack =[bsubtotaljson[@"mpack"] intValue];
  13083. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  13084. // NSDate *img_date = [NSDate date];
  13085. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  13086. // DebugLog(@"img_date time interval");
  13087. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  13088. itemjson[@"model"]=nsname;
  13089. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  13090. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  13091. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  13092. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  13093. itemjson[@"check"]=@"true";
  13094. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  13095. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  13096. itemjson[@"unit_price"]=Price;
  13097. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  13098. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  13099. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  13100. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  13101. itemjson[@"note"]=nsline_note;
  13102. if (!can_create_backorder) {
  13103. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  13104. }
  13105. // NSDate *date2 = [NSDate date];
  13106. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  13107. // DebugLog(@"model_bundle time interval");
  13108. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  13109. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  13110. count++;
  13111. // DebugLog(@"row time interval");
  13112. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  13113. }
  13114. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  13115. sqlite3_finalize(statement);
  13116. }
  13117. DebugLog(@"request cart total time interval");
  13118. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  13119. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  13120. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  13121. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  13122. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  13123. ret[@"count"]=[NSNumber numberWithInt:count ];
  13124. if(orderdiscount>0)
  13125. ret[@"orderDiscount"]=@(orderdiscount);
  13126. ret[@"mode"]=@"Regular Mode";
  13127. [iSalesDB close_db:db];
  13128. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  13129. DebugLog(@"general notes :%@",general_note);
  13130. ret[@"general_note"]= general_note;
  13131. dispatch_async(dispatch_get_main_queue(), ^{
  13132. result(ret);
  13133. });
  13134. });
  13135. }
  13136. @end