OLDataProvider.m 688 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. //#import "UIProgressView+AFNetworking.h"
  15. //#import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "RASingleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. #import "ActiveViewController.h"
  24. #import "RANetwork.h"
  25. @interface OLDataProvider ()
  26. @end
  27. @implementation OLDataProvider
  28. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  29. {
  30. 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];
  31. NSString* ret=@"No Price.";
  32. sqlite3_stmt * statement;
  33. // int count=0;
  34. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  35. {
  36. if (sqlite3_step(statement) == SQLITE_ROW)
  37. {
  38. char *price = (char*)sqlite3_column_text(statement, 0);
  39. if(price==nil)
  40. price="";
  41. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  42. double dp= [nsprice doubleValue];
  43. ret=[NSString stringWithFormat:@"%.2f",dp];
  44. }
  45. sqlite3_finalize(statement);
  46. }
  47. return ret;
  48. // // [iSalesDB close_db:db];
  49. //
  50. // return nil;
  51. //// if(dprice==DBL_MAX)
  52. //// ret= nil;
  53. //// else
  54. //// ret= [NSNumber numberWithDouble:dprice];
  55. //// return ret;
  56. }
  57. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  58. {
  59. NSString* ret= nil;
  60. NSString *sqlQuery = nil;
  61. // 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
  62. 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];
  63. sqlite3_stmt * statement;
  64. // int count=0;
  65. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  66. {
  67. if (sqlite3_step(statement) == SQLITE_ROW)
  68. {
  69. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  70. if(imgurl==nil)
  71. imgurl="";
  72. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  73. ret=nsimgurl;
  74. }
  75. sqlite3_finalize(statement);
  76. }
  77. else
  78. {
  79. ret=@"";
  80. }
  81. // [iSalesDB close_db:db];
  82. // DebugLog(@"data string: %@",ret );
  83. return ret;
  84. }
  85. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  86. {
  87. // assert(params[@"contact_id"]!=nil);
  88. assert(params[@"user"]!=nil);
  89. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  90. //生成portfolio pdf需要的数据
  91. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  92. data[@"npd_url"]=COMPANY_WEB;
  93. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  94. [formatter setDateFormat:@"MMMM yyyy"];
  95. NSString* date = [formatter stringFromDate:[NSDate date]];
  96. data[@"create_month"]=date;
  97. data[@"company_name"]=COMPANY_FULL_NAME;
  98. data[@"catalog_name"]=params[@"catalog_name"];
  99. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  100. data[@"TOTAL_PAGE"]=0;
  101. data[@"CURRENT_PAGE"]=0;
  102. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  103. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  104. bool show_stockout=[params[@"show_stockout"] boolValue];
  105. NSString *sql = nil;
  106. // v1.90 more color
  107. NSString *group_by = @"";
  108. if(values==nil)
  109. {
  110. if (show_group_by) {
  111. group_by = @"group by b.product_group ";
  112. }
  113. 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];
  114. }
  115. else
  116. {
  117. if (show_group_by) {
  118. group_by = @"group by product_group ";
  119. }
  120. 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];
  121. }
  122. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  123. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  124. sqlite3 *db = [iSalesDB get_db];
  125. // NSDictionary *resultDic =
  126. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  127. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  128. int product_id = sqlite3_column_int(stmt, 0);
  129. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  130. NSString *name = [self textAtColumn:1 statement:stmt];
  131. NSString *description = [self textAtColumn:2 statement:stmt];
  132. double price = sqlite3_column_double(stmt, 3);
  133. NSString *s_price = [self textAtColumn:3 statement:stmt];
  134. double discount = sqlite3_column_double(stmt,4);
  135. int qty = sqlite3_column_int(stmt, 5);
  136. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  137. int is_percentage = sqlite3_column_int(stmt, 6);
  138. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  139. int item_id = sqlite3_column_int(stmt, 7);
  140. // int fashion_id = sqlite3_column_int(stmt, 8);
  141. NSString *line_note = [self textAtColumn:9 statement:stmt];
  142. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  143. double percent = sqlite3_column_double(stmt, 10);
  144. // int portfolio_id = sqlite3_column_int(stmt, 11);
  145. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  146. int availability = sqlite3_column_int(stmt, 12);
  147. NSString *color = [self textAtColumn:13 statement:stmt];
  148. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  149. NSString *demension = [self textAtColumn:15 statement:stmt];
  150. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  151. NSString *material = [self textAtColumn:17 statement:stmt];
  152. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  153. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  154. double volume = sqlite3_column_double(stmt, 19);
  155. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  156. double weight = sqlite3_column_double(stmt, 20);
  157. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  158. int model_set = sqlite3_column_int(stmt, 21);
  159. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  160. double load_ability = sqlite3_column_double(stmt, 22);
  161. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  162. NSString *assembling = [self textAtColumn:24 statement:stmt];
  163. NSString *made_in = [self textAtColumn:25 statement:stmt];
  164. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  165. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  166. float bprice=0;
  167. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  168. for(int b=0;b< [bundle[@"count"] intValue];b++)
  169. {
  170. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  171. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  172. }
  173. if(gprice!=nil)
  174. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  175. if(s_percent.length==0||is_percentage==0)
  176. {
  177. percent=100.0;
  178. if([s_qty isEqualToString:@"null"])
  179. qty=availability;
  180. }
  181. else
  182. {
  183. qty=availability;
  184. }
  185. qty=qty*percent/100+0.5;
  186. for(int i=0;i<[values[@"count"] intValue];i++)
  187. {
  188. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  189. if([valueitem[@"product_id"] intValue]==product_id)
  190. {
  191. line_note=valueitem[@"line_note"];
  192. qty=[valueitem[@"available_qty"] intValue];
  193. if([[valueitem allKeys] containsObject:@"available_qty"])
  194. s_qty=[NSString stringWithFormat:@"%d",qty];
  195. else
  196. s_qty=@"null";
  197. if(valueitem[@"regular_price"]!=nil)
  198. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  199. s_price=valueitem[@"special_price"];
  200. price=[valueitem[@"special_price"] floatValue];
  201. discount=0;
  202. // v1.90
  203. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  204. }
  205. }
  206. if(is_percentage==0&&qty==0&&!show_stockout)
  207. {
  208. return;
  209. }
  210. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  211. {
  212. return;
  213. }
  214. NSString* set_price=@"";
  215. if([params[@"entered_price"] boolValue])
  216. {
  217. if (s_price==nil ) {
  218. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  219. }
  220. else
  221. {
  222. if(price*(1-discount/100.0)!=gprice.floatValue)
  223. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  224. }
  225. }
  226. NSString* get_price=@"";
  227. {
  228. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  229. // DebugLog(@"price time interval");
  230. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  231. if(gprice==nil)
  232. get_price=@"Price:No Price.";
  233. else
  234. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  235. }
  236. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  237. // if ([qty_null isEqualToString:@"null"]) {
  238. // qty=availability;
  239. // }
  240. //
  241. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  242. // if ([qty_percent_null isEqualToString:@"null"]) {
  243. // percentage=1;
  244. // }
  245. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  246. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  247. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  248. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  249. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  250. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  251. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  252. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  253. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  254. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  255. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  256. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  257. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  258. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  259. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  260. if([params[@"availability"] boolValue]==false)
  261. str_availability=@"";
  262. else
  263. {
  264. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  265. str_availability=@"<b>Availability:</b> In Production";
  266. }
  267. if([params[@"color"] boolValue]==false || color.length==0 )
  268. str_color=@"";
  269. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  270. str_model_set=@"";
  271. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  272. str_legcolor=@"";
  273. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  274. str_demension=@"";
  275. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  276. str_seat_height=@"";
  277. if([params[@"material"] boolValue]==false || material.length==0 )
  278. str_meterial=@"";
  279. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  280. str_box_dim=@"";
  281. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  282. str_volume=@"";
  283. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  284. str_weight=@"";
  285. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  286. str_load_ability=@"";
  287. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  288. str_fabric_content=@"";
  289. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  290. str_assembling=@"";
  291. if([params[@"made"] boolValue]==false || made_in.length==0 )
  292. str_made_in=@"";
  293. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  294. str_line_note=@"";
  295. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  296. [arr_detail addObject:str_availability];
  297. [arr_detail addObject:str_color];
  298. [arr_detail addObject:str_model_set];
  299. [arr_detail addObject:str_legcolor];
  300. [arr_detail addObject:str_demension];
  301. [arr_detail addObject:str_seat_height];
  302. [arr_detail addObject:str_meterial];
  303. [arr_detail addObject:str_box_dim];
  304. [arr_detail addObject:str_volume];
  305. [arr_detail addObject:str_weight];
  306. [arr_detail addObject:str_load_ability];
  307. [arr_detail addObject:str_fabric_content];
  308. [arr_detail addObject:str_assembling];
  309. [arr_detail addObject:str_made_in];
  310. [arr_detail addObject:str_line_note];
  311. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  312. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  313. //model image;
  314. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  315. // qr image
  316. NSString* qrpath=nil;
  317. if([params[@"show_barcode"] boolValue])
  318. {
  319. NSString* temp = NSTemporaryDirectory();
  320. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  321. qrpath=[temp stringByAppendingPathComponent:filename];
  322. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  323. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  324. }
  325. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  326. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  327. item[@"img"]=img_path;
  328. item[@"img_qr"]=qrpath;
  329. item[@"special_price"]=set_price;
  330. item[@"price"]=get_price;
  331. item[@"name"]=name;
  332. item[@"description"]=description;
  333. item[@"detail"]=detail;
  334. item[@"img"]=img_path;
  335. if (show_group_by && more_color_int != 0) {
  336. item[@"more_color"] = more_color_path;
  337. }
  338. // @{
  339. // //@"linenotes": line_note,
  340. //// @"product_id": product_id_string,
  341. //// @"available_qty": @(qty),
  342. //// @"available_percent" : @(percent),
  343. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  344. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  345. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"": ,
  352. // @"":
  353. // }.mutableCopy;
  354. // if (percentage) {
  355. // [item removeObjectForKey:@"available_qty"];
  356. // } else {
  357. // [item removeObjectForKey:@"available_percent"];
  358. // }
  359. //
  360. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  361. // if ([qty_null isEqualToString:@"null"]) {
  362. // [item removeObjectForKey:@"available_qty"];
  363. // }
  364. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  365. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  366. }];
  367. [iSalesDB close_db:db];
  368. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  369. // [dic setValue:@"" forKey:@"email_content"];
  370. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  371. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  372. //
  373. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  374. // cell[@"count"]=[NSNumber numberWithInt:10];
  375. grid[@"cell0"]=dic;
  376. data[@"grid0"]=grid;
  377. return data;
  378. }
  379. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  380. {
  381. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  382. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  383. NSString *cache_folder=[paths objectAtIndex:0];
  384. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  385. BOOL bdir=NO;
  386. NSFileManager* fileManager = [NSFileManager defaultManager];
  387. if(PDF_DEBUG)
  388. {
  389. NSData *data = [NSData dataWithContentsOfFile:default_path];
  390. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  391. return ret;
  392. }
  393. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  394. {
  395. NSError * error=nil;
  396. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  397. {
  398. return nil;
  399. }
  400. }
  401. NSData *data = [NSData dataWithContentsOfFile:template_path];
  402. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  403. return ret;
  404. }
  405. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  406. {
  407. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  408. 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 ];
  409. DebugLog(@"offline_login sql:%@",sqlQuery);
  410. sqlite3_stmt * statement;
  411. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  412. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  413. {
  414. if (sqlite3_step(statement) == SQLITE_ROW)
  415. {
  416. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  417. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  418. // int can_show_price = sqlite3_column_int(statement, 0);
  419. // int can_see_price = sqlite3_column_int(statement, 1);
  420. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  421. // if(contact_id==nil)
  422. // contact_id="";
  423. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  424. // int user_type = sqlite3_column_int(statement, 3);
  425. // int can_cancel_order = sqlite3_column_int(statement, 4);
  426. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  427. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  428. // int can_delete_order = sqlite3_column_int(statement, 7);
  429. // int can_submit_order = sqlite3_column_int(statement, 8);
  430. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  431. // int can_create_order = sqlite3_column_int(statement, 10);
  432. // char *mode = (char*)sqlite3_column_text(statement, 11);
  433. // if(mode==nil)
  434. // mode="";
  435. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  436. // char *username = (char*)sqlite3_column_text(statement, 12);
  437. // if(username==nil)
  438. // username="";
  439. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  440. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  441. char *first_name = (char*)sqlite3_column_text(statement, 14);
  442. if(first_name==nil)
  443. first_name="";
  444. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  445. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  446. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  447. // [header setValue:nscontact_id forKey:@"contact_id"];
  448. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  449. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  450. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  451. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  452. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  454. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  455. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  456. //
  457. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  458. //
  459. // [header setValue:nsusername forKey:@"username"];
  460. //
  461. // NSError* error=nil;
  462. //
  463. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  464. // [header setValue:statusFilter forKey:@"statusFilter"];
  465. //
  466. //
  467. //
  468. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  469. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  470. //
  471. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  472. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  473. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  474. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  475. //
  476. // [ret setObject:header forKey:@"header"];
  477. [ret setValue:nsfirst_name forKey:@"first_name"];
  478. }
  479. sqlite3_finalize(statement);
  480. }
  481. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  482. return ret;
  483. }
  484. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  485. {
  486. assert(user!=nil);
  487. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  488. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  489. data[@"npd_url"]=COMPANY_WEB;
  490. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  491. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  492. NSString* date = [formatter stringFromDate:[NSDate date]];
  493. data[@"print_date"]=date;
  494. data[@"company_name"]=COMPANY_FULL_NAME;
  495. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  496. data[@"TOTAL_PAGE"]=0;
  497. data[@"CURRENT_PAGE"]=0;
  498. // [formatter setDateFormat:@"MM/dd/yyyy"];
  499. // date = [formatter stringFromDate:[NSDate date]];
  500. // data[@"creat_date"]=date;
  501. NSString* temp = NSTemporaryDirectory();
  502. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  503. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  504. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  505. // bool bwrite=
  506. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  507. data[@"img_barcode"]=qrpath;
  508. data[@"so_id"]=order_code;
  509. data[@"company"]=COMPANY_FULL_NAME;
  510. // 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];
  511. 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];
  512. sqlite3 *db = [iSalesDB get_db];
  513. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  514. sqlite3_stmt * statement;
  515. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  516. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  517. row0[@"count"]=[NSNumber numberWithInt:1];
  518. datasource[@"row0"]=row0;
  519. NSString* print_status=@"";
  520. NSString* order_type=@"Offline Quote";
  521. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  522. {
  523. if (sqlite3_step(statement) == SQLITE_ROW)
  524. {
  525. int erpOrderStatus = sqlite3_column_int(statement, 0);
  526. int status = sqlite3_column_int(statement, 26);
  527. if(status==2)
  528. {
  529. switch (erpOrderStatus) {
  530. case 10:
  531. // order_type=@"Quoted Order";
  532. print_status=@"Quote";
  533. break;
  534. case 11:
  535. print_status=@"Booked";
  536. break;
  537. case 12:
  538. print_status=@"Picked";
  539. break;
  540. case 13:
  541. print_status=@"Shipped";
  542. break;
  543. case 14:
  544. print_status=@"Closed";
  545. break;
  546. case 15:
  547. print_status=@"Canceled";
  548. break;
  549. case 16:
  550. print_status=@"All";
  551. break;
  552. case 18:
  553. print_status=@"Processing";
  554. break;
  555. default:
  556. break;
  557. }
  558. }
  559. else{
  560. switch (status) {
  561. case 0:
  562. {
  563. print_status=@"Quote";
  564. break;
  565. }
  566. case 1:
  567. {
  568. print_status=@"Saved";
  569. break;
  570. }
  571. case 3:
  572. {
  573. //canceled
  574. print_status=@"Canceled";
  575. break;
  576. }
  577. default:
  578. break;
  579. }
  580. }
  581. NSString* sender_name = [self textAtColumn:1 statement:statement];
  582. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  583. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  584. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  585. NSString* sender_email = [self textAtColumn:5 statement:statement];
  586. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  587. NSString* billing_name = [self textAtColumn:7 statement:statement];
  588. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  589. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  590. if(billing_phone.length>0)
  591. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  592. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  593. if(billing_fax.length>0)
  594. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  595. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  596. [billto_arr0 addObject:billing_phone];
  597. [billto_arr0 addObject:billing_fax];
  598. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  599. NSString* billing_email = [self textAtColumn:11 statement:statement];
  600. if(billing_email.length>0)
  601. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  602. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  603. if(billing_contact.length>0)
  604. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  605. NSString* receive_name = [self textAtColumn:12 statement:statement];
  606. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  607. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  608. if(receive_phone.length>0)
  609. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  610. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  611. if(receive_fax.length>0)
  612. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  613. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  614. [receive_arr0 addObject:receive_phone];
  615. [receive_arr0 addObject:receive_fax];
  616. NSString* receive_email = [self textAtColumn:16 statement:statement];
  617. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  618. if(receive_email.length>0)
  619. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  620. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  621. if(receive_contact.length>0)
  622. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  623. NSString* poNumber = [self textAtColumn:17 statement:statement];
  624. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  625. NSString* carrier = [self textAtColumn:19 statement:statement];
  626. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  627. NSString* terms = [self textAtColumn:21 statement:statement];
  628. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  629. NSString* port_destination = [self textAtColumn:23 statement:statement];
  630. NSString* etd = [self textAtColumn:24 statement:statement];
  631. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  632. if(sign_picpath.length==0)
  633. sign_picpath=nil;
  634. NSString* create_date = [self textAtColumn:27 statement:statement];
  635. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  636. NSString* general_notes=[self textAtColumn:33 statement:statement];
  637. NSString *customerID = [self textAtColumn:34 statement:statement];
  638. if (customerID.length == 0) {
  639. customerID = nil;
  640. }
  641. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  642. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  643. if(general_notes.length>0)
  644. {
  645. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  646. row2[@"item_0"]=@{@"g_notes":general_notes };
  647. row2[@"count"]=[NSNumber numberWithInt:1];
  648. datasource[@"row2"]=row2;
  649. }
  650. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  651. [billto_arr addObject:billing_ext];
  652. [billto_arr addObject:billing_pf];
  653. [billto_arr addObject:billing_contact];
  654. [billto_arr addObject:billing_email];
  655. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  656. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  657. [receive_arr addObject:receive_ext];
  658. [receive_arr addObject:receive_pf];
  659. [receive_arr addObject:receive_contact];
  660. [receive_arr addObject:receive_email];
  661. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  662. float shipping = sqlite3_column_double(statement, 28);
  663. float lift_gate_value = sqlite3_column_double(statement, 29);
  664. float handling_fee_value = sqlite3_column_double(statement, 30);
  665. data[@"company"]=sender_name;
  666. data[@"shipfrom_addr"]=sender_ext;
  667. data[@"shipfrom_phone"]=sender_phone;
  668. data[@"shipfrom_fax"]=sender_fax;
  669. data[@"shipfrom_email"]=sender_email;
  670. data[@"warehouse_name"]=warehouse_name;
  671. data[@"bill_to_name"]=billing_name;
  672. data[@"bill_to_ext"]=billto;
  673. data[@"ship_to_name"]=receive_name;
  674. data[@"ship_to_ext"]=receive;
  675. data[@"po_no"]=poNumber;
  676. data[@"rep"]=sales_rep;
  677. data[@"e_schdule"]=etd;
  678. data[@"sales_terms"]=sales_terms;
  679. data[@"port_destination"]=port_destination;
  680. data[@"terms"]=terms;
  681. data[@"carrier"]=carrier;
  682. data[@"vendor_no"]=vendor_no;
  683. data[@"customer_sign"]=sign_picpath;
  684. data[@"print_status"]=print_status;
  685. data[@"create_date"]=create_date;
  686. 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];;
  687. sqlite3_stmt * statement_cart;
  688. // NSDate *date1 = [NSDate date];
  689. int count=0;
  690. // int cart_count=0;
  691. float credits=0;
  692. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  693. if ( dbresult== SQLITE_OK)
  694. {
  695. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  696. int row1count=0;
  697. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  698. {
  699. // NSDate *row_date = [NSDate date];
  700. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  701. // int product_id = sqlite3_column_int(statement_cart, 0);
  702. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  703. int item_id = sqlite3_column_int(statement_cart, 7);
  704. double discount = sqlite3_column_double(statement_cart, 2);
  705. NSString* Price=nil;
  706. if(str_price==nil)
  707. {
  708. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  709. // NSDate *price_date = [NSDate date];
  710. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  711. // DebugLog(@"price time interval");
  712. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  713. if(price==nil)
  714. Price=@"No Price.";
  715. else
  716. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  717. }
  718. else
  719. {
  720. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  721. }
  722. float dunitprice=0;
  723. int item_count = sqlite3_column_int(statement_cart, 3);
  724. NSString* Amount=@"";
  725. if(![Price isEqualToString:@"No Price."])
  726. {
  727. dunitprice=[Price floatValue];
  728. dunitprice=dunitprice* (1-discount/100.0);
  729. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  730. credits+=dunitprice*item_count;
  731. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  732. }
  733. else
  734. {
  735. Price=@"";
  736. }
  737. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  738. // NSString *nsline_note=nil;
  739. // if(line_note!=nil)
  740. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  741. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  742. NSString *nsname=nil;
  743. if(name!=nil)
  744. nsname= [[NSString alloc]initWithUTF8String:name];
  745. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  746. NSString *nsdescription=nil;
  747. if(description!=nil)
  748. nsdescription= [[NSString alloc]initWithUTF8String:description];
  749. // int stockUom = sqlite3_column_int(statement_cart, 8);
  750. // int _id = sqlite3_column_int(statement_cart, 9);
  751. // NSDate *subtotal_date = [NSDate date];
  752. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  753. // DebugLog(@"subtotal_date time interval");
  754. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  755. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  756. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  757. // int carton=[bsubtotaljson[@"carton"] intValue];
  758. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  759. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  760. // NSDate *img_date = [NSDate date];
  761. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  762. // DebugLog(@"img_date time interval");
  763. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  764. itemjson[@"model"]=nsname;
  765. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  766. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  767. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  768. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  769. // itemjson[@"check"]=@"true";
  770. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  771. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  772. itemjson[@"unit_price"]=Price;
  773. itemjson[@"amount"]=Amount;
  774. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  775. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  776. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  777. // itemjson[@"note"]=nsline_note;
  778. // NSDate *date2 = [NSDate date];
  779. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  780. // DebugLog(@"model_bundle time interval");
  781. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  782. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  783. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  784. row1count++;
  785. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  786. for(int c=0;c<[combine[@"count"] intValue];c++)
  787. {
  788. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  789. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  790. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  791. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  792. credits+=combine_amount;
  793. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  794. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  795. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  796. row1count++;
  797. }
  798. count++;
  799. // DebugLog(@"row time interval");
  800. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  801. }
  802. row1[@"count"]=[NSNumber numberWithInt:row1count];
  803. datasource[@"row1"]=row1;
  804. data[@"table_model"]=datasource;
  805. sqlite3_finalize(statement_cart);
  806. }
  807. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  808. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  809. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  810. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  811. // if(credits>0)
  812. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  813. // else
  814. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  815. if(shipping>0)
  816. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  817. else
  818. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  819. if(lift_gate_value>0)
  820. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  821. else
  822. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  823. if(handling_fee_value>0)
  824. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  825. else
  826. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  827. float total = credits+shipping+lift_gate_value+handling_fee_value;
  828. if(total>0)
  829. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  830. else
  831. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  832. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  833. fee[@"row0"]=fee_row0;
  834. data[@"table_fee"]=fee;
  835. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  836. // ret[@"sign_url"] = sign_url;
  837. //
  838. // customer_contact = [self textAtColumn:52 statement:statement];
  839. // customer_email = [self textAtColumn:53 statement:statement];
  840. // customer_phone = [self textAtColumn:54 statement:statement];
  841. // customer_fax = [self textAtColumn:55 statement:statement];
  842. //
  843. //
  844. // int offline_edit=sqlite3_column_int(statement, 56);
  845. //
  846. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  847. //
  848. // char *soid = (char*)sqlite3_column_text(statement, 1);
  849. // if(soid==nil)
  850. // soid= "";
  851. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  852. // // so#
  853. // ret[@"so#"] = nssoid;
  854. //
  855. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  856. // if(poNumber==nil)
  857. // poNumber= "";
  858. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  859. //
  860. //
  861. //
  862. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  863. // if(create_time==nil)
  864. // create_time= "";
  865. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  866. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  867. //
  868. // int status = sqlite3_column_int(statement, 4);
  869. // int erpStatus = sqlite3_column_int(statement, 49);
  870. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  871. //
  872. // // status
  873. // if (status > 1 && status != 3) {
  874. // status = erpStatus;
  875. // } else if (status == 3) {
  876. // status = 15;
  877. // }
  878. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  879. // ret[@"order_status"] = nsstatus;
  880. //
  881. //
  882. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  883. // if(company_name==nil)
  884. // company_name= "";
  885. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  886. // // company name
  887. // ret[@"company_name"] = nscompany_name;
  888. //
  889. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  890. // if(customer_contact==nil)
  891. // customer_contact= "";
  892. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  893. //
  894. //
  895. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  896. // if(addr_1==nil)
  897. // addr_1="";
  898. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  899. //
  900. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  901. // if(addr_2==nil)
  902. // addr_2="";
  903. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  904. //
  905. //
  906. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  907. // if(addr_3==nil)
  908. // addr_3="";
  909. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  910. //
  911. //
  912. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  913. // if(addr_4==nil)
  914. // addr_4="";
  915. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  916. //
  917. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  918. // [arr_addr addObject:nsaddr_1];
  919. // [arr_addr addObject:nsaddr_2];
  920. // [arr_addr addObject:nsaddr_3];
  921. // [arr_addr addObject:nsaddr_4];
  922. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  923. //
  924. //
  925. // char *logist = (char*)sqlite3_column_text(statement, 11);
  926. // if(logist==nil)
  927. // logist= "";
  928. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  929. // if (status == -11 || status == 10 || status == 11) {
  930. // nslogist = [self textAtColumn:59 statement:statement];
  931. // };
  932. //
  933. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  934. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  935. // shipping = @"Shipping To Be Quoted";
  936. // } else {
  937. // shippingFee = sqlite3_column_double(statement, 12);
  938. // }
  939. //
  940. // // Shipping
  941. // ret[@"Shipping"] = shipping;
  942. //
  943. // int have_lift_gate = sqlite3_column_int(statement, 17);
  944. // lift_gate = sqlite3_column_double(statement, 13);
  945. // // Liftgate Fee(No loading dock)
  946. // if (!have_lift_gate) {
  947. // lift_gate = 0;
  948. // }
  949. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  950. // if (sqlite3_column_int(statement, 57)) {
  951. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  952. // }
  953. //
  954. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  955. // if(general_notes==nil)
  956. // general_notes= "";
  957. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  958. //
  959. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  960. // if(internal_notes==nil)
  961. // internal_notes= "";
  962. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  963. //
  964. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  965. // if(payment_type==nil)
  966. // payment_type= "";
  967. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  968. //
  969. //
  970. //
  971. // // order info
  972. // orderinfo = [self textFileName:@"order_info.html"];
  973. //
  974. //
  975. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  979. //
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  987. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  988. //
  989. // NSString *payment = nil;
  990. // if([nspayment_type isEqualToString:@"Credit Card"])
  991. // {
  992. // payment = [self textFileName:@"creditcardpayment.html"];
  993. //
  994. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  995. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  996. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  997. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  998. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  999. // NSString *card_type = [self textAtColumn:42 statement:statement];
  1000. // if (card_type.length > 0) { // 显示星号
  1001. // card_type = @"****";
  1002. // }
  1003. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1004. // if (card_number.length > 0 && card_number.length > 4) {
  1005. // for (int i = 0; i < card_number.length - 4; i++) {
  1006. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1007. // }
  1008. // } else {
  1009. // card_number = @"";
  1010. // }
  1011. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1012. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1013. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1014. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1015. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1016. // card_expiration = @"****";
  1017. // }
  1018. //
  1019. //
  1020. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1021. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1022. //
  1023. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1024. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1025. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1026. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1029. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1030. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1031. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1032. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1033. //
  1034. //
  1035. // }
  1036. // else
  1037. // {
  1038. // payment=[self textFileName:@"normalpayment.html"];
  1039. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1040. // }
  1041. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1042. //
  1043. // ret[@"result"]= [NSNumber numberWithInt:2];
  1044. //
  1045. // // more info
  1046. // moreInfo = [self textFileName:@"more_info.html"];
  1047. //
  1048. //
  1049. // // ShipToCompany_or_&nbsp
  1050. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1052. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1054. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1056. //
  1057. //
  1058. // // ShipFromCompany_or_&nbsp
  1059. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1060. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1061. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1063. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1065. //
  1066. //
  1067. // // FreightBillToCompany_or_&nbsp
  1068. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1069. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1070. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1072. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1074. //
  1075. //
  1076. // // MerchandiseBillToCompany_or_&nbsp
  1077. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1078. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1079. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1080. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1081. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1082. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1083. //
  1084. //
  1085. // // ReturnToCompany_or_&nbsp
  1086. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1087. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1088. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1089. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1090. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1091. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1092. // //
  1093. //
  1094. // // DebugLog(@"more info : %@",moreInfo);
  1095. //
  1096. // // handling fee
  1097. // handlingFee = sqlite3_column_double(statement, 33);
  1098. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1099. // if (sqlite3_column_int(statement, 58)) {
  1100. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1101. // }
  1102. // //
  1103. //
  1104. // // customer info
  1105. // customerID = [self textAtColumn:36 statement:statement];
  1106. //
  1107. // // mode
  1108. // ret[@"mode"] = appDelegate.mode;
  1109. //
  1110. // // model_count
  1111. // ret[@"model_count"] = @(0);
  1112. }
  1113. sqlite3_finalize(statement);
  1114. }
  1115. [iSalesDB close_db:db];
  1116. data[@"order_type"]=order_type;
  1117. /*
  1118. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1119. // "customer_email" = "Shui Hu";
  1120. // "customer_fax" = "";
  1121. // "customer_first_name" = F;
  1122. // "customer_last_name" = L;
  1123. // "customer_name" = ",da He Xiang Dong Liu A";
  1124. // "customer_phone" = "Hey Xuan Feng";
  1125. contactInfo[@"customer_phone"] = customer_phone;
  1126. contactInfo[@"customer_fax"] = customer_fax;
  1127. contactInfo[@"customer_email"] = customer_email;
  1128. NSString *first_name = @"";
  1129. NSString *last_name = @"";
  1130. if ([customer_contact isEqualToString:@""]) {
  1131. } else if ([customer_contact containsString:@" "]) {
  1132. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1133. first_name = [customer_contact substringToIndex:first_space_index];
  1134. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1135. }
  1136. contactInfo[@"customer_first_name"] = first_name;
  1137. contactInfo[@"customer_last_name"] = last_name;
  1138. ret[@"customerInfo"] = contactInfo;
  1139. // models
  1140. if (nssoid) {
  1141. __block double TotalCuft = 0;
  1142. __block double TotalWeight = 0;
  1143. __block int TotalCarton = 0;
  1144. __block double allItemPrice = 0;
  1145. 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];
  1146. sqlite3 *db1 = [iSalesDB get_db];
  1147. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1148. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1149. int product_id = sqlite3_column_int(stmt, 0);
  1150. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1151. int item_id = sqlite3_column_int(stmt, 7);
  1152. NSString* Price=nil;
  1153. if(str_price==nil)
  1154. {
  1155. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1156. if(price==nil)
  1157. Price=@"No Price.";
  1158. else
  1159. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1160. }
  1161. else
  1162. {
  1163. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1164. }
  1165. double discount = sqlite3_column_double(stmt, 2);
  1166. int item_count = sqlite3_column_int(stmt, 3);
  1167. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1168. NSString *nsline_note=nil;
  1169. if(line_note!=nil)
  1170. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1171. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1172. // NSString *nsname = nil;
  1173. // if(name!=nil)
  1174. // nsname= [[NSString alloc]initWithUTF8String:name];
  1175. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1176. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1177. // NSString *nsdescription=nil;
  1178. // if(description!=nil)
  1179. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1180. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1181. // int stockUom = sqlite3_column_int(stmt, 8);
  1182. // int _id = sqlite3_column_int(stmt, 9);
  1183. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1184. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1185. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1186. int carton=[bsubtotaljson[@"carton"] intValue];
  1187. TotalCuft += cuft;
  1188. TotalWeight += weight;
  1189. TotalCarton += carton;
  1190. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1191. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1192. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1193. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1194. itemjson[@"note"]=nsline_note;
  1195. itemjson[@"origin_price"] = Price;
  1196. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1198. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1199. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1200. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1201. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1202. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1203. if(itemjson[@"combine"] != nil)
  1204. {
  1205. // int citem=0;
  1206. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1207. for(int bc=0;bc<bcount;bc++)
  1208. {
  1209. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1210. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1211. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1212. subTotal += uprice * modulus * item_count;
  1213. }
  1214. }
  1215. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1216. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1217. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1218. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1219. allItemPrice += subTotal;
  1220. }];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1223. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1224. // payments/Credits
  1225. // payments_and_credist = sqlite3_column_double(statement, 34);
  1226. payments_and_credist = allItemPrice;
  1227. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1228. // // total
  1229. // totalPrice = sqlite3_column_double(statement, 35);
  1230. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1231. } else {
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1234. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1235. // payments/Credits
  1236. payments_and_credist = 0;
  1237. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1238. }
  1239. // total
  1240. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1241. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1242. ret[@"order_info"]= orderinfo;
  1243. ret[@"more_order_info"] = moreInfo;
  1244. return [RAConvertor dict2data:ret];
  1245. */
  1246. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1247. return data;
  1248. }
  1249. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1250. {
  1251. assert(params[@"user"]!=nil);
  1252. NSString* template_file=nil;
  1253. switch([params[@"thumb"] intValue])
  1254. {
  1255. case 0:
  1256. template_file= @"so.json";
  1257. break;
  1258. case 1:
  1259. template_file= @"so_thumb.json";
  1260. break;
  1261. default:
  1262. template_file= @"so.json";
  1263. }
  1264. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1265. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1266. if(data[@"table_model"][@"row2"]==nil)
  1267. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1268. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1269. DebugLog(@"%@",file);
  1270. // return nil;
  1271. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1272. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1273. if (file) {
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1275. [dic setObject:file forKey:@"pdf_path"];
  1276. dic[@"isLocalFile"]=@"true";
  1277. return [RAConvertor dict2data:dic];
  1278. }
  1279. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1280. dic[@"isLocalFile"]=@"true";
  1281. return [RAConvertor dict2data:dic];
  1282. }
  1283. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1284. {
  1285. // assert(params[@"contact_id"]!=nil);
  1286. // assert(params[@"user"]!=nil);
  1287. if(params[@"user"]==nil)
  1288. {
  1289. AppDelegate *appDelegate = nil;
  1290. //some UI methods ej
  1291. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1292. if(appDelegate.user!=nil)
  1293. [params setValue:appDelegate.user forKey:@"user"];
  1294. }
  1295. NSString* template_file=nil;
  1296. switch([params[@"pdf_style"] intValue])
  1297. {
  1298. case 0:
  1299. template_file= @"portfolio_2x3.json";
  1300. break;
  1301. case 1:
  1302. template_file= @"portfolio_3x2.json";
  1303. break;
  1304. default:
  1305. template_file= @"portfolio_3x2.json";
  1306. }
  1307. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1308. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1309. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1310. DebugLog(@"%@",file);
  1311. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1312. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1313. if (file) {
  1314. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1315. [dic setObject:file forKey:@"pdf_path"];
  1316. dic[@"isLocalFile"]=@"true";
  1317. return [RAConvertor dict2data:dic];
  1318. }
  1319. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1320. dic[@"isLocalFile"]=@"true";
  1321. return [RAConvertor dict2data:dic];
  1322. }
  1323. +(NSString*) get_offline_soid:(sqlite3*)db
  1324. {
  1325. NSString* soid=nil;
  1326. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1327. [formatter setDateFormat:@"yyMMdd"];
  1328. NSString* date = [formatter stringFromDate:[NSDate date]];
  1329. for(int i=1;i<999;i++)
  1330. {
  1331. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1332. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1333. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1334. if(count==0)
  1335. return soid;
  1336. }
  1337. return nil;
  1338. }
  1339. +(NSArray*) enumOfflineOrder
  1340. {
  1341. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1342. sqlite3 *db = [iSalesDB get_db];
  1343. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1344. sqlite3_stmt * statement;
  1345. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1346. if ( dbresult== SQLITE_OK)
  1347. {
  1348. while (sqlite3_step(statement) == SQLITE_ROW)
  1349. {
  1350. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1351. NSString* nsso_id=nil;
  1352. if(so_id!=nil)
  1353. {
  1354. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1355. }
  1356. [ret addObject:nsso_id];
  1357. }
  1358. sqlite3_finalize(statement);
  1359. }
  1360. [iSalesDB close_db:db];
  1361. return ret;
  1362. }
  1363. //+(void) uploadFile:(NSString*) file
  1364. //{
  1365. // NSData* data = [NSData dataWithContentsOfFile: file];
  1366. // UIApplication * app = [UIApplication sharedApplication];
  1367. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1368. //
  1369. //
  1370. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1371. //
  1372. //
  1373. //
  1374. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1375. //
  1376. // if(appDelegate.user!=nil)
  1377. // [params setValue:appDelegate.user forKey:@"user"];
  1378. // // if(appDelegate.contact_id!=nil)
  1379. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1380. // if(appDelegate.password!=nil)
  1381. // [params setValue:appDelegate.password forKey:@"password"];
  1382. //
  1383. //
  1384. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1385. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1386. // } error:nil];
  1387. //
  1388. //
  1389. //
  1390. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1391. //
  1392. // NSProgress *progress = nil;
  1393. //
  1394. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1395. //
  1396. //
  1397. //
  1398. // if (error) {
  1399. //
  1400. // NSString* err_msg = [error localizedDescription];
  1401. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1402. //
  1403. //
  1404. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1405. //
  1406. // DebugLog(@"data string: %@",str);
  1407. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1408. //
  1409. // } else {
  1410. // DebugLog(@"response ");
  1411. //
  1412. //
  1413. //
  1414. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1415. //
  1416. // // 再将NSData转为字符串
  1417. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1418. //
  1419. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1420. //
  1421. // DebugLog(@"data string: %@",jsonStr);
  1422. //
  1423. // NSDictionary* json = responseObject;
  1424. //
  1425. //
  1426. // if([[json valueForKey:@"result"] intValue]==2)
  1427. // {
  1428. //// NSString* img_url_down = json[@"img_url_aname"];
  1429. //// NSString* img_url_up = json[@"img_url"];
  1430. //
  1431. //
  1432. // }
  1433. // else
  1434. // {
  1435. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1436. // }
  1437. //
  1438. //
  1439. // }
  1440. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1441. // }];
  1442. //
  1443. //
  1444. //
  1445. //
  1446. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1447. // //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. //
  1454. // [uploadTask resume];
  1455. //
  1456. //}
  1457. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1458. //{
  1459. //
  1460. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1461. // NSString *documents = [paths objectAtIndex:0];
  1462. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1463. //
  1464. //
  1465. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1466. //
  1467. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1468. //
  1469. //
  1470. //
  1471. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1472. //
  1473. //
  1474. //
  1475. //
  1476. //
  1477. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1478. //
  1479. //
  1480. //
  1481. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1482. //
  1483. //
  1484. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1485. //
  1486. //
  1487. //
  1488. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1489. //
  1490. //
  1491. //
  1492. //
  1493. // NSMutableDictionary* orderlist = nil;
  1494. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1495. //
  1496. //
  1497. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1498. //
  1499. //
  1500. // orderlist = [NSMutableDictionary new];
  1501. //
  1502. //// [orderlist addObject:appDelegate.order_code];
  1503. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1504. //
  1505. // }
  1506. // else
  1507. // {
  1508. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1509. //
  1510. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1511. //
  1512. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1513. // }
  1514. //
  1515. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1516. // orderitem[@"create_by"] = appDelegate.user;
  1517. // orderitem[@"price"] = upparams[@"total_price"];
  1518. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1519. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1520. // orderitem[@"order_status"] = @"Saved Order";
  1521. // orderitem[@"order_code"] = appDelegate.order_code;
  1522. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1523. //
  1524. //
  1525. // int count = [orderlist[@"count"] intValue];
  1526. //
  1527. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1528. //
  1529. // newlist[@"item_0"]=orderitem;
  1530. // int newcount = 1;
  1531. // for(int i=0;i<count;i++)
  1532. // {
  1533. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1534. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1535. // continue;
  1536. // else
  1537. // {
  1538. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1539. // newcount++;
  1540. // }
  1541. // }
  1542. // newlist[@"count"] = @(newcount);
  1543. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1544. //
  1545. // return;
  1546. //}
  1547. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1548. {
  1549. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1550. // NSString *documents = [paths objectAtIndex:0];
  1551. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1552. NSString *userdir = [OLDataProvider getUserPath];
  1553. NSMutableDictionary* orderlist = nil;
  1554. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1555. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1556. // order list 为空,不用维护
  1557. #ifdef RA_NOTIFICATION
  1558. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1559. #else
  1560. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1561. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1562. #endif
  1563. return;
  1564. // [orderlist addObject:appDelegate.order_code];
  1565. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1566. }
  1567. else
  1568. {
  1569. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1570. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1571. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1572. }
  1573. //==================== order list 删除
  1574. int count = [orderlist[@"count"] intValue];
  1575. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1576. NSMutableDictionary* submititem =nil;
  1577. int newcount = 0;
  1578. for(int i=0;i<count;i++)
  1579. {
  1580. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1581. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1582. {
  1583. submititem = item;
  1584. continue;
  1585. }
  1586. else
  1587. {
  1588. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1589. newcount++;
  1590. }
  1591. }
  1592. newlist[@"count"] = @(newcount);
  1593. [RAUtils dicttofile:orderlistPath dict:newlist];
  1594. //////////////////---=============================== submitlist 添加
  1595. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1596. NSMutableDictionary* submitlist = nil;
  1597. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1598. submitlist = [NSMutableDictionary new];
  1599. }
  1600. else
  1601. {
  1602. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1603. }
  1604. int submitcount =[submitlist[@"count"] intValue];
  1605. submititem[@"order_status"] = soid;//@"Submitted Order";
  1606. submititem[@"customer_name"] = company;//@"Submitted Order";
  1607. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1608. submitlist[@"count"] = @(submitcount+1);
  1609. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1610. #ifdef RA_NOTIFICATION
  1611. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1612. #else
  1613. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1614. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1615. #endif
  1616. }
  1617. +(void) save2submitScanOrder1
  1618. {
  1619. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1620. // NSString *documents = [paths objectAtIndex:0];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. NSString *userdir = [OLDataProvider getUserPath];
  1623. NSMutableDictionary* orderlist = nil;
  1624. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1625. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1626. // order list 为空,不用维护
  1627. #ifdef RA_NOTIFICATION
  1628. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1629. #else
  1630. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1631. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1632. #endif
  1633. return;
  1634. // [orderlist addObject:appDelegate.order_code];
  1635. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1636. }
  1637. else
  1638. {
  1639. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1640. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1641. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1642. }
  1643. int count = [orderlist[@"count"] intValue];
  1644. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1645. int newcount = 0;
  1646. for(int i=0;i<count;i++)
  1647. {
  1648. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1649. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1650. continue;
  1651. else
  1652. {
  1653. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1654. newcount++;
  1655. }
  1656. }
  1657. newlist[@"count"] = @(newcount);
  1658. [RAUtils dicttofile:orderlistPath dict:newlist];
  1659. #ifdef RA_NOTIFICATION
  1660. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1661. #else
  1662. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1663. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1664. #endif
  1665. }
  1666. +(void) createScanTempFolder
  1667. {
  1668. NSString* scanFilePath = [self getScanTempPath];
  1669. BOOL bdir=YES;
  1670. NSFileManager* fileManager = [NSFileManager defaultManager];
  1671. if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1672. {
  1673. NSError *error = nil;
  1674. bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1675. if(!bsuccess)
  1676. DebugLog(@"Create cache folder failed");
  1677. // if(bsuccess)
  1678. // {
  1679. // sqlite3 *db = [self get_db];
  1680. //
  1681. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1682. // [iSalesDB close_db:db];
  1683. // }
  1684. }
  1685. }
  1686. +(NSString*) getScanOrderPath
  1687. {
  1688. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1689. NSString *documents = [paths objectAtIndex:0];
  1690. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1691. NSString* servername = addressDic[@"name"];
  1692. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1693. NSString* user = appDelegate.user;
  1694. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1695. orderpath = [orderpath stringByAppendingPathComponent:user];
  1696. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1697. BOOL bdir=YES;
  1698. NSFileManager* fileManager = [NSFileManager defaultManager];
  1699. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1700. {
  1701. NSError *error = nil;
  1702. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1703. if(!bsuccess)
  1704. DebugLog(@"Create cache folder failed");
  1705. // if(bsuccess)
  1706. // {
  1707. // sqlite3 *db = [self get_db];
  1708. //
  1709. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1710. // [iSalesDB close_db:db];
  1711. // }
  1712. }
  1713. return orderpath;
  1714. }
  1715. +(NSString*) getScanTempPath
  1716. {
  1717. if(RASingleton.sharedInstance.scan_temp_code == nil)
  1718. RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1719. return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1720. }
  1721. +(void) moveScanTemp2Order
  1722. {
  1723. NSString *orderdir = [OLDataProvider getScanOrderPath];
  1724. NSError *error = nil;
  1725. NSString* tempdir = [OLDataProvider getScanTempPath];
  1726. // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1727. // for(int i=0;i<arr_files.count;i++)
  1728. // {
  1729. // NSString* file=arr_files[i];
  1730. //
  1731. //
  1732. // }
  1733. [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1734. }
  1735. +(NSString*) getUserPath
  1736. {
  1737. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1738. NSString *documents = [paths objectAtIndex:0];
  1739. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1740. NSString* servername = addressDic[@"name"];
  1741. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1742. NSString* user = appDelegate.user;
  1743. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1744. userpath = [userpath stringByAppendingPathComponent:user];
  1745. return userpath;
  1746. }
  1747. +(NSString*) getScanPath
  1748. {
  1749. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1750. if(appDelegate.order_code!=nil)
  1751. {
  1752. return [self getScanOrderPath];
  1753. }
  1754. else
  1755. {
  1756. [self createScanTempFolder];
  1757. return [self getScanTempPath];
  1758. }
  1759. }
  1760. +(NSString*) createScanTempCode
  1761. {
  1762. return [[NSUUID new] UUIDString];
  1763. }
  1764. +(NSString*) scanTemplatePath:(NSString*)file
  1765. {
  1766. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1767. NSString *documents = [paths objectAtIndex:0];
  1768. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1769. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1770. return templatefile;
  1771. }
  1772. +(void) guestLogin
  1773. {
  1774. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1775. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1776. }
  1777. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1778. {
  1779. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1780. NSString *documents = [paths objectAtIndex:0];
  1781. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1782. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1783. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1784. return [dict mutableCopy];
  1785. }
  1786. +(void) deleteSubmittedOrder:(NSString*) order_code
  1787. {
  1788. NSString *userdir = [OLDataProvider getUserPath];
  1789. if(order_code.length==0)
  1790. return;
  1791. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1792. // NSString *documents = [paths objectAtIndex:0];
  1793. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1794. if([appDelegate.order_code isEqualToString:order_code])
  1795. {
  1796. //要删除的订单是当前打开的订单;
  1797. appDelegate.order_code = nil;
  1798. appDelegate.customerInfo = nil;
  1799. RASingleton.sharedInstance.scan_cart = nil;
  1800. appDelegate.contact_id = nil;
  1801. // [appDelegate updateScanButton:false];
  1802. [appDelegate update_count_mark];
  1803. }
  1804. //删除订单目录
  1805. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1806. //维护订单列表
  1807. NSMutableDictionary* orderlist = nil;
  1808. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1809. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1810. // order list 为空,不用维护
  1811. #ifdef RA_NOTIFICATION
  1812. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1813. #else
  1814. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1815. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1816. #endif
  1817. return;
  1818. // [orderlist addObject:appDelegate.order_code];
  1819. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1820. }
  1821. else
  1822. {
  1823. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1824. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1825. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1826. }
  1827. int count = [orderlist[@"count"] intValue];
  1828. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1829. int newcount = 0;
  1830. for(int i=0;i<count;i++)
  1831. {
  1832. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1833. if([item[@"order_code"] isEqualToString: order_code ])
  1834. continue;
  1835. else
  1836. {
  1837. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1838. newcount++;
  1839. }
  1840. }
  1841. newlist[@"count"] = @(newcount);
  1842. [RAUtils dicttofile:orderlistPath dict:newlist];
  1843. #ifdef RA_NOTIFICATION
  1844. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1845. #else
  1846. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1847. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1848. #endif
  1849. }
  1850. +(void) updateScanOrder:(NSString*) order_code
  1851. {
  1852. NSString *userdir = [OLDataProvider getUserPath];
  1853. if(order_code.length==0)
  1854. return;
  1855. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1856. // NSString *documents = [paths objectAtIndex:0];
  1857. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1858. NSMutableDictionary* orderlist = nil;
  1859. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1860. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1861. // order list 为空,不用维护
  1862. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1863. return;
  1864. // [orderlist addObject:appDelegate.order_code];
  1865. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1866. }
  1867. else
  1868. {
  1869. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1870. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1871. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1872. }
  1873. int count = [orderlist[@"count"] intValue];
  1874. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1875. float p =0;
  1876. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1877. {
  1878. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1879. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1880. }
  1881. // int newcount = 0;
  1882. for(int i=0;i<count;i++)
  1883. {
  1884. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1885. if([item[@"order_code"] isEqualToString: order_code ])
  1886. {
  1887. item[@"price"] = [NSString stringWithFormat:@"%.2f",p];
  1888. item[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1889. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1890. }
  1891. }
  1892. // newlist[@"count"] = @(newcount);
  1893. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1894. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1895. }
  1896. +(void) deleteScanOrder:(NSString*) order_code
  1897. {
  1898. NSString *userdir = [OLDataProvider getUserPath];
  1899. if(order_code.length==0)
  1900. return;
  1901. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1902. // NSString *documents = [paths objectAtIndex:0];
  1903. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1904. if([appDelegate.order_code isEqualToString:order_code])
  1905. {
  1906. //要删除的订单是当前打开的订单;
  1907. appDelegate.order_code = nil;
  1908. appDelegate.customerInfo = nil;
  1909. RASingleton.sharedInstance.scan_cart = nil;
  1910. appDelegate.contact_id = nil;
  1911. // [appDelegate updateScanButton:false];
  1912. [appDelegate update_count_mark];
  1913. }
  1914. //删除订单目录
  1915. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1916. //维护订单列表
  1917. NSMutableDictionary* orderlist = nil;
  1918. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1919. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1920. // order list 为空,不用维护
  1921. #ifdef RA_NOTIFICATION
  1922. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1923. #else
  1924. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1925. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1926. #endif
  1927. return;
  1928. // [orderlist addObject:appDelegate.order_code];
  1929. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1930. }
  1931. else
  1932. {
  1933. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1934. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1935. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1936. }
  1937. int count = [orderlist[@"count"] intValue];
  1938. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1939. int newcount = 0;
  1940. for(int i=0;i<count;i++)
  1941. {
  1942. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1943. if([item[@"order_code"] isEqualToString: order_code ])
  1944. continue;
  1945. else
  1946. {
  1947. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1948. newcount++;
  1949. }
  1950. }
  1951. newlist[@"count"] = @(newcount);
  1952. [RAUtils dicttofile:orderlistPath dict:newlist];
  1953. #ifdef RA_NOTIFICATION
  1954. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1955. #else
  1956. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1957. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1958. #endif
  1959. }
  1960. +(void) saveScanCart:(NSMutableDictionary*) cart
  1961. {
  1962. if(cart==nil)
  1963. return;
  1964. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1965. // NSString *documents = [paths objectAtIndex:0];
  1966. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1967. NSString *orderdir = [self getScanPath];
  1968. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1969. [RAUtils dicttofile:cartpath dict:cart];
  1970. }
  1971. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  1972. {
  1973. assert(add_params[@"user"]!=nil);
  1974. // assert(add_params[@"contact_id"]!=nil);
  1975. // assert(add_params[@"password"]!=nil);
  1976. NSString* serial= [[NSUUID UUID] UUIDString];
  1977. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1978. NSString *cachefolder = [paths objectAtIndex:0];
  1979. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1980. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1981. NSFileManager* fileManager = [NSFileManager defaultManager];
  1982. BOOL bdir=YES;
  1983. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1984. {
  1985. NSError *error = nil;
  1986. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1987. if(!bsuccess)
  1988. DebugLog(@"Create UPLOAD folder failed");
  1989. }
  1990. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1991. ret[@"contact"]=[self prepareContact:serial];
  1992. ret[@"wishlist"]=[self prepareWishlist:serial];
  1993. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  1994. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  1995. ret[@"view_portfolio"] = [self preparePDF:serial];
  1996. NSString* str= [RAConvertor dict2string:ret];
  1997. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1998. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1999. NSError *error=nil;
  2000. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2001. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2002. ZipArchive* zip = [[ZipArchive alloc] init];
  2003. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2004. for(int i=0;i<arr_files.count;i++)
  2005. {
  2006. NSString* file=arr_files[i];
  2007. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2008. }
  2009. if( ![zip CloseZipFile2] )
  2010. {
  2011. zippath = @"";
  2012. }
  2013. ret[@"file"]=zippath;
  2014. return ret;
  2015. }
  2016. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2017. {
  2018. if(filename.length==0)
  2019. return false;
  2020. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2021. bool ret=false;
  2022. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2023. NSString *cachefolder = [paths objectAtIndex:0];
  2024. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2025. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2026. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2027. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2028. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2029. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2030. NSFileManager* fileManager = [NSFileManager defaultManager];
  2031. BOOL bdir=NO;
  2032. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2033. {
  2034. NSError *error = nil;
  2035. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2036. {
  2037. ret=false;
  2038. }
  2039. else
  2040. {
  2041. ret=true;
  2042. }
  2043. }
  2044. return ret;
  2045. }
  2046. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2047. {
  2048. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2049. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2050. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2051. NSString* where=@" 1=1";
  2052. if (ver!=nil) {
  2053. where=@"is_dirty=1";
  2054. }
  2055. sqlite3 *db = [iSalesDB get_db];
  2056. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2057. sqlite3_stmt * statement;
  2058. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2059. int count=0;
  2060. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2061. if ( dbresult== SQLITE_OK)
  2062. {
  2063. while (sqlite3_step(statement) == SQLITE_ROW)
  2064. {
  2065. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2066. int _id = sqlite3_column_int(statement, 0);
  2067. int product_id = sqlite3_column_int(statement, 1);
  2068. int item_id = sqlite3_column_int(statement, 2);
  2069. int qty = sqlite3_column_int(statement, 3);
  2070. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2071. NSString* nscreate_time=nil;
  2072. if(create_time!=nil)
  2073. {
  2074. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2075. }
  2076. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2077. NSString* nsmodify_time=nil;
  2078. if(modify_time!=nil)
  2079. {
  2080. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2081. }
  2082. int is_delete = sqlite3_column_int(statement, 6);
  2083. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2084. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2085. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2086. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2087. itemjson[@"create_time"]=nscreate_time;
  2088. itemjson[@"modify_time"]=nsmodify_time;
  2089. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2090. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2091. count++;
  2092. }
  2093. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2094. sqlite3_finalize(statement);
  2095. }
  2096. ret[@"count"]=[NSNumber numberWithInt:count ];
  2097. [iSalesDB close_db:db];
  2098. return ret;
  2099. }
  2100. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2101. {
  2102. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2103. // UIApplication * app = [UIApplication sharedApplication];
  2104. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2105. assert(add_params[@"user"]!=nil);
  2106. // assert(add_params[@"password"]!=nil);
  2107. 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 ];
  2108. // 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 ];
  2109. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2110. DebugLog(@"offline_login sql:%@",sqlQuery);
  2111. sqlite3_stmt * statement;
  2112. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2113. int count=0;
  2114. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2115. if ( dbresult== SQLITE_OK)
  2116. {
  2117. while (sqlite3_step(statement) == SQLITE_ROW)
  2118. {
  2119. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2120. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2121. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2122. int product_id = sqlite3_column_int(statement, 0);
  2123. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2124. int item_id = sqlite3_column_int(statement, 7);
  2125. NSString* Price=nil;
  2126. if(str_price==nil)
  2127. {
  2128. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2129. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2130. if(price==nil)
  2131. Price=@"No Price.";
  2132. else
  2133. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2134. }
  2135. else
  2136. {
  2137. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2138. }
  2139. double discount = sqlite3_column_double(statement, 2);
  2140. int item_count = sqlite3_column_int(statement, 3);
  2141. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2142. NSString *nsline_note=nil;
  2143. if(line_note!=nil)
  2144. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2145. char *name = (char*)sqlite3_column_text(statement, 5);
  2146. NSString *nsname=nil;
  2147. if(name!=nil)
  2148. nsname= [[NSString alloc]initWithUTF8String:name];
  2149. char *description = (char*)sqlite3_column_text(statement, 6);
  2150. NSString *nsdescription=nil;
  2151. if(description!=nil)
  2152. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2153. // int stockUom = sqlite3_column_int(statement, 8);
  2154. // int _id = sqlite3_column_int(statement, 9);
  2155. //
  2156. //
  2157. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2158. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2159. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2160. // int carton=[bsubtotaljson[@"carton"] intValue];
  2161. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2162. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2163. itemjson[@"model"]=nsname;
  2164. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2165. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2166. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2167. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2168. // itemjson[@"check"]=@"true";
  2169. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2170. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2171. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2172. itemjson[@"unit_price"]=Price;
  2173. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2174. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2175. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2176. itemjson[@"note"]=nsline_note;
  2177. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2178. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2179. count++;
  2180. }
  2181. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2182. sqlite3_finalize(statement);
  2183. }
  2184. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2185. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2186. return ret;
  2187. }
  2188. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2189. {
  2190. assert(add_params[@"user"]!=nil);
  2191. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2192. sqlite3 *db = [iSalesDB get_db];
  2193. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2194. // for(int i=0;i<arr_soid.count;i++)
  2195. sqlite3_stmt * statement;
  2196. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2197. //int count=0;
  2198. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2199. if ( dbresult== SQLITE_OK)
  2200. {
  2201. int count=0;
  2202. while (sqlite3_step(statement) == SQLITE_ROW)
  2203. {
  2204. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2205. int _id = sqlite3_column_int(statement, 0);
  2206. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2207. NSString* nssync_data=nil;
  2208. if(sync_data!=nil)
  2209. {
  2210. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2211. }
  2212. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2213. NSString* nsimg_1=nil;
  2214. if(img_1!=nil)
  2215. {
  2216. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2217. [self copy_upImg:serial file:nsimg_1];
  2218. }
  2219. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2220. NSString* nsso_no=nil;
  2221. if(so_no!=nil)
  2222. {
  2223. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2224. }
  2225. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2226. itemjson[@"order_type"]=@"submit order";
  2227. else
  2228. itemjson[@"order_type"]=@"archive order";
  2229. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2230. itemjson[@"json_data"]= nssync_data;
  2231. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2232. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2233. count++;
  2234. }
  2235. ret[@"count"]=[NSNumber numberWithInt:count ];
  2236. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2237. }
  2238. sqlite3_finalize(statement);
  2239. [iSalesDB close_db:db];
  2240. return ret;
  2241. }
  2242. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2243. {
  2244. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2245. sqlite3 *db = [iSalesDB get_db];
  2246. // UIApplication * app = [UIApplication sharedApplication];
  2247. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2248. 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";
  2249. sqlite3_stmt * statement;
  2250. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2251. int count=0;
  2252. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2253. if ( dbresult== SQLITE_OK)
  2254. {
  2255. while (sqlite3_step(statement) == SQLITE_ROW)
  2256. {
  2257. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2258. int _id = sqlite3_column_int(statement, 0);
  2259. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2260. NSString* nsimg_0=nil;
  2261. if(img_0!=nil)
  2262. {
  2263. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2264. [self copy_upImg:serial file:nsimg_0];
  2265. }
  2266. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2267. NSString* nsimg_1=nil;
  2268. if(img_1!=nil)
  2269. {
  2270. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2271. [self copy_upImg:serial file:nsimg_1];
  2272. }
  2273. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2274. NSString* nsimg_2=nil;
  2275. if(img_2!=nil)
  2276. {
  2277. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2278. [self copy_upImg:serial file:nsimg_2];
  2279. }
  2280. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2281. NSString* nssync_data=nil;
  2282. if(sync_data!=nil)
  2283. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2284. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2285. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2286. itemjson[@"json_data"]= nssync_data;
  2287. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2288. count++;
  2289. }
  2290. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2291. sqlite3_finalize(statement);
  2292. }
  2293. ret[@"count"]=[NSNumber numberWithInt:count ];
  2294. [iSalesDB close_db:db];
  2295. return ret;
  2296. }
  2297. +(bool) check_offlinedata
  2298. {
  2299. // UIApplication * app = [UIApplication sharedApplication];
  2300. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2301. sqlite3 *db = [iSalesDB get_db];
  2302. NSString * where=@"1=1";
  2303. // if(appDelegate.user!=nil)
  2304. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2305. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2306. [iSalesDB close_db:db];
  2307. if(count==0)
  2308. {
  2309. return false;
  2310. }
  2311. return true;
  2312. //
  2313. //[iSalesDB close_db:db];
  2314. }
  2315. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2316. {
  2317. // UIApplication * app = [UIApplication sharedApplication];
  2318. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2319. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2320. sqlite3 *db = [iSalesDB get_db];
  2321. NSString* collectId=params[@"collectId"];
  2322. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2323. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2324. [iSalesDB execSql:sqlQuery db:db];
  2325. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2326. [iSalesDB close_db:db];
  2327. // appDelegate.wish_count =count;
  2328. //
  2329. // [appDelegate update_count_mark];
  2330. ret[@"result"]= [NSNumber numberWithInt:2];
  2331. ret[@"wish_count"]=@(count);
  2332. return ret;
  2333. }
  2334. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2335. //{
  2336. //
  2337. // UIApplication * app = [UIApplication sharedApplication];
  2338. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2339. //
  2340. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2341. // sqlite3 *db = [iSalesDB get_db];
  2342. // NSString* product_id=params[@"product_id"];
  2343. //
  2344. //
  2345. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2346. //
  2347. //
  2348. // for(int i=0;i<arr.count;i++)
  2349. // {
  2350. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2351. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2352. // if(count==0)
  2353. // {
  2354. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2355. // [iSalesDB execSql:sqlQuery db:db];
  2356. // }
  2357. // }
  2358. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2359. // [iSalesDB close_db:db];
  2360. //
  2361. // appDelegate.wish_count =count;
  2362. //
  2363. // [appDelegate update_count_mark];
  2364. // ret[@"result"]= [NSNumber numberWithInt:2];
  2365. // return ret;
  2366. // //
  2367. // //return ret;
  2368. //}
  2369. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2370. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2371. DebugLog(@"time interval: %lf",interval);
  2372. }
  2373. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2374. //{
  2375. // UIApplication * app = [UIApplication sharedApplication];
  2376. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2377. //
  2378. // int sort = [[params objectForKey:@"sort"] intValue];
  2379. // NSString *sort_str = @"";
  2380. // switch (sort) {
  2381. // case 0:{
  2382. // sort_str = @"order by w.modify_time desc";
  2383. // }
  2384. // break;
  2385. // case 1:{
  2386. // sort_str = @"order by w.modify_time asc";
  2387. // }
  2388. // break;
  2389. // case 2:{
  2390. // sort_str = @"order by m.name asc";
  2391. // }
  2392. // break;
  2393. // case 3:{
  2394. // sort_str = @"order by m.name desc";
  2395. // }
  2396. // break;
  2397. // case 4:{
  2398. // sort_str = @"order by m.description asc";
  2399. // }
  2400. // break;
  2401. //
  2402. // default:
  2403. // break;
  2404. //
  2405. // }
  2406. //
  2407. //
  2408. //// NSString* user = appDelegate.user;
  2409. //
  2410. // sqlite3 *db = [iSalesDB get_db];
  2411. //
  2412. // // order by w.create_time
  2413. // 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];
  2414. //
  2415. //// 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];
  2416. //
  2417. //
  2418. // sqlite3_stmt * statement;
  2419. //
  2420. // NSDate *date1 = [NSDate date];
  2421. //// NSDate *date2 = nil;
  2422. //
  2423. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2424. // int count=0;
  2425. //
  2426. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2427. // {
  2428. //
  2429. //
  2430. // while (sqlite3_step(statement) == SQLITE_ROW)
  2431. // {
  2432. //
  2433. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2434. //
  2435. // int product_id = sqlite3_column_double(statement, 0);
  2436. //
  2437. //
  2438. //
  2439. //
  2440. //
  2441. // char *description = (char*)sqlite3_column_text(statement, 1);
  2442. // if(description==nil)
  2443. // description= "";
  2444. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2445. //
  2446. // int item_id = sqlite3_column_double(statement, 2);
  2447. //
  2448. // NSDate *date_image = [NSDate date];
  2449. //
  2450. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2451. //
  2452. // printf("image : ");
  2453. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2454. //
  2455. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2456. // // if(url==nil)
  2457. // // url="";
  2458. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2459. //
  2460. // int qty = sqlite3_column_int(statement, 3);
  2461. //
  2462. //
  2463. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2464. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2465. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2466. // item[@"description"]= nsdescription;
  2467. // item[@"img"]= nsurl;
  2468. //
  2469. //
  2470. //
  2471. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2472. // count++;
  2473. //
  2474. // }
  2475. // printf("total time:");
  2476. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2477. //
  2478. // ret[@"count"]= [NSNumber numberWithInt:count];
  2479. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2480. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2481. // ret[@"result"]= [NSNumber numberWithInt:2];
  2482. //
  2483. //
  2484. // appDelegate.wish_count =count;
  2485. //
  2486. // [appDelegate update_count_mark];
  2487. // sqlite3_finalize(statement);
  2488. //
  2489. //
  2490. //
  2491. //
  2492. // }
  2493. //
  2494. // [iSalesDB close_db:db];
  2495. //
  2496. // return ret;
  2497. //}
  2498. +(NSDictionary*) offline_notimpl
  2499. {
  2500. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2501. ret[@"result"]=@"8";
  2502. ret[@"err_msg"]=@"offline mode does not support this function.";
  2503. return ret;
  2504. }
  2505. +(NSDictionary*) offline_home
  2506. {
  2507. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2508. NSString *cachefolder = [paths objectAtIndex:0];
  2509. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2510. NSData* json =nil;
  2511. json=[NSData dataWithContentsOfFile:img_cache];
  2512. if(json==nil)
  2513. return nil;
  2514. NSError *error=nil;
  2515. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2516. return menu;
  2517. }
  2518. +(NSDictionary*) offline_category_menu
  2519. {
  2520. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2521. NSString *cachefolder = [paths objectAtIndex:0];
  2522. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2523. NSData* json =nil;
  2524. json=[NSData dataWithContentsOfFile:img_cache];
  2525. if(json==nil)
  2526. return nil;
  2527. NSError *error=nil;
  2528. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2529. return menu;
  2530. }
  2531. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2532. {
  2533. NSString* offline_command=params[@"offline_Command"];
  2534. NSDictionary* ret=nil;
  2535. if([offline_command isEqualToString:@"model_NIYMAL"])
  2536. {
  2537. NSString* category = params[@"category"];
  2538. ret = [self refresh_model_NIYMAL:category];
  2539. }
  2540. return ret;
  2541. }
  2542. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2543. {
  2544. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2545. [ret setValue:@"2" forKey:@"result"];
  2546. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2547. sqlite3* db= [iSalesDB get_db];
  2548. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2549. [iSalesDB close_db:db];
  2550. [ret setObject:detail1_section forKey:@"detail_1"];
  2551. return ret;
  2552. }
  2553. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2554. {
  2555. //assert(user!=nil);
  2556. // UIApplication * app = [UIApplication sharedApplication];
  2557. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2558. // NSArray* arr1 = [self get_user_all_price_type];
  2559. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2560. // NSSet *set1 = [NSSet setWithArray:arr1];
  2561. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2562. // if(appDelegate.contact_id==nil)
  2563. // set2=[set1 mutableCopy];
  2564. // else
  2565. // [set2 intersectsSet:set1];
  2566. // NSArray *retarr = [set2 allObjects];
  2567. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2568. // sqlite3 *db = [iSalesDB get_db];
  2569. NSString* sqlQuery = nil;
  2570. if(contact_id==nil)
  2571. {
  2572. if(!blogin)
  2573. return nil;
  2574. 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];
  2575. }
  2576. else
  2577. 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];
  2578. sqlite3_stmt * statement;
  2579. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2580. int count=0;
  2581. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2582. {
  2583. while (sqlite3_step(statement) == SQLITE_ROW)
  2584. {
  2585. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2586. char *name = (char*)sqlite3_column_text(statement, 0);
  2587. if(name==nil)
  2588. name="";
  2589. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2590. // double price = sqlite3_column_double(statement, 1);
  2591. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2592. // if(isnull==nil)
  2593. // item[nsname]= @"No Price";
  2594. // else
  2595. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2596. char *price = (char*)sqlite3_column_text(statement, 1);
  2597. if(price!=nil)
  2598. {
  2599. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2600. nsprice=[AESCrypt fastdecrypt:nsprice];
  2601. if(nsprice.length>0)
  2602. {
  2603. double dp= [nsprice doubleValue];
  2604. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2605. }
  2606. }
  2607. else
  2608. {
  2609. item[nsname]= @"No Price";
  2610. }
  2611. // int type= sqlite3_column_int(statement, 2);
  2612. //item[@"type"]=@"price";
  2613. // item[nsname]= nsprice;
  2614. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2615. count++;
  2616. }
  2617. ret[@"count"]= [NSNumber numberWithInt:count];
  2618. sqlite3_finalize(statement);
  2619. }
  2620. // [iSalesDB close_db:db];
  2621. return ret;
  2622. }
  2623. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2624. //{
  2625. // NSArray* arr1 = [self get_user_all_price_type:db];
  2626. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2627. //
  2628. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2629. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2630. // // [set2 intersectsSet:set1];
  2631. // //
  2632. // //
  2633. // // NSArray *retarr = [set2 allObjects];
  2634. //
  2635. // NSString* whereprice=nil;
  2636. // if(contact_id==nil)
  2637. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2638. // else
  2639. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2640. //
  2641. //
  2642. // // sqlite3 *db = [iSalesDB get_db];
  2643. //
  2644. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2645. // sqlite3_stmt * statement;
  2646. //
  2647. //
  2648. // NSNumber* ret = nil;
  2649. // double dprice=DBL_MAX;
  2650. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2651. // {
  2652. //
  2653. //
  2654. // while (sqlite3_step(statement) == SQLITE_ROW)
  2655. // {
  2656. //
  2657. // // double val = sqlite3_column_double(statement, 0);
  2658. // char *price = (char*)sqlite3_column_text(statement, 0);
  2659. // if(price!=nil)
  2660. // {
  2661. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2662. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2663. // if(nsprice.length>0)
  2664. // {
  2665. // double dp= [nsprice doubleValue];
  2666. // if(dp<dprice)
  2667. // dprice=dp;
  2668. // }
  2669. // }
  2670. // }
  2671. //
  2672. //
  2673. //
  2674. //
  2675. // sqlite3_finalize(statement);
  2676. //
  2677. //
  2678. //
  2679. //
  2680. // }
  2681. //
  2682. // // [iSalesDB close_db:db];
  2683. //
  2684. // if(dprice==DBL_MAX)
  2685. // ret= nil;
  2686. // else
  2687. // ret= [NSNumber numberWithDouble:dprice];
  2688. // return ret;
  2689. //}
  2690. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2691. {
  2692. assert(user!=nil);
  2693. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2694. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2695. NSString* whereprice=nil;
  2696. if(contact_id==nil)
  2697. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2698. else
  2699. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2700. // sqlite3 *db = [iSalesDB get_db];
  2701. NSString *productIdCondition = @"1 = 1";
  2702. if (product_id) {
  2703. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2704. }
  2705. NSString *itemIdCondition = @"";
  2706. if (item_id) {
  2707. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2708. }
  2709. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2710. sqlite3_stmt * statement;
  2711. NSNumber* ret = nil;
  2712. double dprice=DBL_MAX;
  2713. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2714. {
  2715. while (sqlite3_step(statement) == SQLITE_ROW)
  2716. {
  2717. // double val = sqlite3_column_double(statement, 0);
  2718. char *price = (char*)sqlite3_column_text(statement, 0);
  2719. if(price!=nil)
  2720. {
  2721. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2722. nsprice=[AESCrypt fastdecrypt:nsprice];
  2723. if(nsprice.length>0)
  2724. {
  2725. double dp= [nsprice doubleValue];
  2726. if(dp<dprice)
  2727. dprice=dp;
  2728. }
  2729. }
  2730. }
  2731. sqlite3_finalize(statement);
  2732. }
  2733. // [iSalesDB close_db:db];
  2734. if(dprice==DBL_MAX)
  2735. ret= nil;
  2736. else
  2737. ret= [NSNumber numberWithDouble:dprice];
  2738. return ret;
  2739. }
  2740. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2741. {
  2742. assert(user!=nil);
  2743. NSArray* ret=nil;
  2744. // sqlite3 *db = [iSalesDB get_db];
  2745. // no customer assigned , use login user contact_id
  2746. // UIApplication * app = [UIApplication sharedApplication];
  2747. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2748. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2749. sqlite3_stmt * statement;
  2750. // int count=0;
  2751. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2752. {
  2753. if (sqlite3_step(statement) == SQLITE_ROW)
  2754. {
  2755. char *val = (char*)sqlite3_column_text(statement, 0);
  2756. if(val==nil)
  2757. val="";
  2758. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2759. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2760. }
  2761. sqlite3_finalize(statement);
  2762. }
  2763. // [iSalesDB close_db:db];
  2764. return ret;
  2765. }
  2766. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2767. {
  2768. // sqlite3 *db = [iSalesDB get_db];
  2769. if(contact_id==nil)
  2770. {
  2771. // no customer assigned , use login user contact_id
  2772. // UIApplication * app = [UIApplication sharedApplication];
  2773. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2774. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2775. sqlite3_stmt * statement;
  2776. // int count=0;
  2777. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2778. {
  2779. if (sqlite3_step(statement) == SQLITE_ROW)
  2780. {
  2781. char *val = (char*)sqlite3_column_text(statement, 0);
  2782. if(val==nil)
  2783. val="";
  2784. contact_id = [[NSString alloc]initWithUTF8String:val];
  2785. }
  2786. sqlite3_finalize(statement);
  2787. }
  2788. if(contact_id.length<=0)
  2789. {
  2790. // [iSalesDB close_db:db];
  2791. return nil;
  2792. }
  2793. }
  2794. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2795. sqlite3_stmt * statement;
  2796. NSArray* ret=nil;
  2797. // int count=0;
  2798. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2799. {
  2800. if (sqlite3_step(statement) == SQLITE_ROW)
  2801. {
  2802. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2803. if(price_type==nil)
  2804. price_type="";
  2805. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2806. if(nsprice_type.length>0)
  2807. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2808. else
  2809. ret=nil;
  2810. }
  2811. sqlite3_finalize(statement);
  2812. }
  2813. // [iSalesDB close_db:db];
  2814. return ret;
  2815. }
  2816. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2817. {
  2818. NSString* ret= nil;
  2819. // sqlite3 *db = [iSalesDB get_db];
  2820. NSString *sqlQuery = nil;
  2821. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2822. // 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;
  2823. // 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
  2824. if(product_id==nil && model_name)
  2825. 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;
  2826. else if (product_id)
  2827. 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
  2828. sqlite3_stmt * statement;
  2829. // int count=0;
  2830. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2831. {
  2832. if (sqlite3_step(statement) == SQLITE_ROW)
  2833. {
  2834. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2835. if(imgurl==nil)
  2836. imgurl="";
  2837. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2838. ret=nsimgurl;
  2839. }
  2840. sqlite3_finalize(statement);
  2841. }
  2842. else
  2843. {
  2844. [ret setValue:@"8" forKey:@"result"];
  2845. }
  2846. // [iSalesDB close_db:db];
  2847. DebugLog(@"data string: %@",ret );
  2848. return ret;
  2849. }
  2850. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2851. {
  2852. NSString* ret= nil;
  2853. sqlite3 *db = [iSalesDB get_db];
  2854. NSString *sqlQuery = nil;
  2855. if(product_id==nil)
  2856. if(upc_code==nil)
  2857. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2858. else
  2859. 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='%@';
  2860. else
  2861. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2862. sqlite3_stmt * statement;
  2863. // int count=0;
  2864. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2865. {
  2866. if (sqlite3_step(statement) == SQLITE_ROW)
  2867. {
  2868. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2869. if(default_category==nil)
  2870. default_category="";
  2871. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2872. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2873. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2874. ret=nsdefault_category;
  2875. }
  2876. sqlite3_finalize(statement);
  2877. }
  2878. else
  2879. {
  2880. [ret setValue:@"8" forKey:@"result"];
  2881. }
  2882. [iSalesDB close_db:db];
  2883. DebugLog(@"data string: %@",ret );
  2884. return ret;
  2885. }
  2886. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2887. NSString *sql = nil;
  2888. if (product_id != nil) {
  2889. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2890. } else {
  2891. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2892. }
  2893. NSString *result_set = [iSalesDB jk_queryText:sql];
  2894. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2895. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2896. return result_set;
  2897. }
  2898. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2899. //{
  2900. //
  2901. //
  2902. // assert(params[@"user"]!=nil);
  2903. //
  2904. //
  2905. //
  2906. // NSString* model_name = [params valueForKey:@"product_name"];
  2907. //
  2908. // NSString* product_id = [params valueForKey:@"product_id"];
  2909. //
  2910. // NSString* category = [params valueForKey:@"category"];
  2911. //
  2912. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2913. // if(category==nil) {
  2914. // category = default_category_id;
  2915. // } else {
  2916. //
  2917. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2918. // // 参数重有多个category id
  2919. // if (arr_0.count > 1) {
  2920. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2921. //
  2922. // // 取交集
  2923. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2924. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2925. // [set_0 intersectSet:set_1];
  2926. //
  2927. // if (set_0.count == 1) {
  2928. // category = (NSString *)[set_0 anyObject];
  2929. // } else {
  2930. // if ([set_0 containsObject:default_category_id]) {
  2931. // category = default_category_id;
  2932. // } else {
  2933. // category = (NSString *)[set_0 anyObject];
  2934. // }
  2935. // }
  2936. // }
  2937. // }
  2938. //
  2939. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2940. //
  2941. //
  2942. //
  2943. //
  2944. //
  2945. //
  2946. // sqlite3 *db = [iSalesDB get_db];
  2947. //
  2948. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2949. //
  2950. //
  2951. // NSString *sqlQuery = nil;
  2952. //
  2953. // if(product_id==nil)
  2954. // 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='%@';
  2955. // else
  2956. //
  2957. // 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=%@;
  2958. //
  2959. //
  2960. // sqlite3_stmt * statement;
  2961. // [ret setValue:@"2" forKey:@"result"];
  2962. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2963. //
  2964. // // int count=0;
  2965. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2966. // {
  2967. //
  2968. //
  2969. // if (sqlite3_step(statement) == SQLITE_ROW)
  2970. // {
  2971. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2972. //
  2973. //
  2974. // char *name = (char*)sqlite3_column_text(statement, 0);
  2975. // if(name==nil)
  2976. // name="";
  2977. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2978. //
  2979. // char *description = (char*)sqlite3_column_text(statement, 1);
  2980. // if(description==nil)
  2981. // description="";
  2982. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2983. //
  2984. //
  2985. // int product_id = sqlite3_column_int(statement, 2);
  2986. //
  2987. //
  2988. // char *color = (char*)sqlite3_column_text(statement, 3);
  2989. // if(color==nil)
  2990. // color="";
  2991. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2992. // //
  2993. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2994. // // if(legcolor==nil)
  2995. // // legcolor="";
  2996. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2997. // //
  2998. // //
  2999. // int availability = sqlite3_column_int(statement, 5);
  3000. // //
  3001. // int incoming_stock = sqlite3_column_int(statement, 6);
  3002. //
  3003. //
  3004. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3005. // if(demension==nil)
  3006. // demension="";
  3007. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3008. //
  3009. //
  3010. //
  3011. // // ,,,,,,,,,
  3012. //
  3013. //
  3014. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3015. // if(seat_height==nil)
  3016. // seat_height="";
  3017. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3018. //
  3019. //
  3020. // char *material = (char*)sqlite3_column_text(statement, 9);
  3021. // if(material==nil)
  3022. // material="";
  3023. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3024. //
  3025. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3026. // if(box_dim==nil)
  3027. // box_dim="";
  3028. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3029. //
  3030. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3031. // if(volume==nil)
  3032. // volume="";
  3033. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3034. //
  3035. // double weight = sqlite3_column_double(statement, 12);
  3036. //
  3037. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3038. // if(model_set==nil)
  3039. // model_set="";
  3040. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3041. //
  3042. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3043. // if(load_ability==nil)
  3044. // load_ability="";
  3045. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3046. //
  3047. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3048. //// if(default_category==nil)
  3049. //// default_category="";
  3050. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3051. //
  3052. //
  3053. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3054. // if(fabric_content==nil)
  3055. // fabric_content="";
  3056. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3057. //
  3058. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3059. // if(assembling==nil)
  3060. // assembling="";
  3061. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3062. //
  3063. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3064. // if(made_in==nil)
  3065. // made_in="";
  3066. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3067. //
  3068. //
  3069. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3070. // if(special_remarks==nil)
  3071. // special_remarks="";
  3072. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3073. //
  3074. //
  3075. // int stockUcom = sqlite3_column_double(statement, 20);
  3076. //
  3077. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3078. // if(product_group==nil)
  3079. // product_group="";
  3080. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3081. //
  3082. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3083. // // if(fashion_selector==nil)
  3084. // // fashion_selector="";
  3085. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3086. //
  3087. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3088. // if(selector_field==nil)
  3089. // selector_field="";
  3090. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3091. //
  3092. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3093. // if(property_field==nil)
  3094. // property_field="";
  3095. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3096. //
  3097. //
  3098. //
  3099. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3100. // if(packaging==nil)
  3101. // packaging="";
  3102. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3103. //
  3104. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3105. //
  3106. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3107. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3108. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3109. //
  3110. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3111. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3112. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3113. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3114. //
  3115. //
  3116. //
  3117. //
  3118. //
  3119. // NSString* Availability=nil;
  3120. // if(availability>0)
  3121. // Availability=[NSString stringWithFormat:@"%d",availability];
  3122. // else
  3123. // Availability = @"Out of Stock";
  3124. //
  3125. // [img_section setValue:Availability forKey:@"Availability"];
  3126. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3127. //
  3128. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3129. //
  3130. //
  3131. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3132. // if(eta==nil)
  3133. // eta="";
  3134. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3135. // if ([nseta isEqualToString:@"null"]) {
  3136. // nseta = @"";
  3137. // }
  3138. // if (availability <= 0) {
  3139. // [img_section setValue:nseta forKey:@"ETA"];
  3140. // }
  3141. //
  3142. //
  3143. // int item_id = sqlite3_column_int(statement, 26);
  3144. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3145. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3146. //
  3147. // NSString* Price=nil;
  3148. // if(appDelegate.bLogin==false)
  3149. // Price=@"Must Sign in.";
  3150. // else
  3151. // {
  3152. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3153. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3154. // if(price==nil)
  3155. // Price=@"No Price.";
  3156. // else
  3157. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3158. // }
  3159. //
  3160. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3161. // [img_section setValue:Price forKey:@"price"];
  3162. // [img_section setValue:nsname forKey:@"model_name"];
  3163. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3164. //
  3165. // if (appDelegate.order_code) { // 离线order code即so#
  3166. //
  3167. // 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];
  3168. // __block int cartQTY = 0;
  3169. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3170. //
  3171. // cartQTY = sqlite3_column_int(stmt, 0);
  3172. //
  3173. // }];
  3174. //
  3175. // if (cartQTY > 0) {
  3176. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3177. // }
  3178. // }
  3179. //
  3180. // [ret setObject:img_section forKey:@"img_section"];
  3181. //
  3182. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3183. // int detail0_item_count=0;
  3184. //
  3185. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3186. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3187. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3188. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3189. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3190. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3191. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3192. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3193. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3194. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3195. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3196. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3197. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3198. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3199. //
  3200. //
  3201. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3202. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3203. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3204. // {
  3205. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3206. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3207. // }
  3208. //
  3209. //
  3210. //
  3211. //
  3212. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3213. // [detail0_section setValue:@"kv" forKey:@"type"];
  3214. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3215. //
  3216. // [ret setObject:detail0_section forKey:@"detail_0"];
  3217. //
  3218. //
  3219. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3220. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3221. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3222. // // [detail1_section setValue:@"content" forKey:@"type"];
  3223. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3224. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3225. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3226. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3227. // [ret setObject:detail1_section forKey:@"detail_1"];
  3228. //
  3229. //
  3230. //
  3231. //
  3232. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3233. // [detail2_section setValue:@"detail" forKey:@"target"];
  3234. // [detail2_section setValue:@"popup" forKey:@"action"];
  3235. // [detail2_section setValue:@"content" forKey:@"type"];
  3236. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3237. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3238. // [detail2_section setValue:@"local" forKey:@"data"];
  3239. // [ret setObject:detail2_section forKey:@"detail_2"];
  3240. // }
  3241. //
  3242. //
  3243. //
  3244. //
  3245. // sqlite3_finalize(statement);
  3246. // }
  3247. // else
  3248. // {
  3249. // [ret setValue:@"8" forKey:@"result"];
  3250. // }
  3251. //// DebugLog(@"count:%d",count);
  3252. //
  3253. //
  3254. // [iSalesDB close_db:db];
  3255. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3256. //
  3257. // return ret;
  3258. //}
  3259. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3260. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3261. NSString* keyword = [params valueForKey:@"keyword"];
  3262. keyword=keyword.lowercaseString;
  3263. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3264. int limit = [[params valueForKey:@"limit"] intValue];
  3265. int offset = [[params valueForKey:@"offset"] intValue];
  3266. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3267. NSString *limit_str = @"";
  3268. if (limited) {
  3269. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3270. }
  3271. sqlite3 *db = [iSalesDB get_db];
  3272. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3273. // UIApplication * app = [UIApplication sharedApplication];
  3274. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3275. NSString *sqlQuery = nil;
  3276. if(exactMatch )
  3277. 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 ;
  3278. else
  3279. 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
  3280. DebugLog(@"offline_search sql:%@",sqlQuery);
  3281. sqlite3_stmt * statement;
  3282. [ret setValue:@"2" forKey:@"result"];
  3283. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3284. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3285. // int count=0;
  3286. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3287. {
  3288. int i=0;
  3289. while (sqlite3_step(statement) == SQLITE_ROW)
  3290. {
  3291. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3292. // char *name = (char*)sqlite3_column_text(statement, 1);
  3293. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3294. char *name = (char*)sqlite3_column_text(statement, 0);
  3295. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3296. char *description = (char*)sqlite3_column_text(statement, 1);
  3297. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3298. int product_id = sqlite3_column_int(statement, 2);
  3299. // char *url = (char*)sqlite3_column_text(statement, 3);
  3300. // if(url==nil)
  3301. // url="";
  3302. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3303. int wid = sqlite3_column_int(statement, 3);
  3304. int closeout = sqlite3_column_int(statement, 4);
  3305. int cid = sqlite3_column_int(statement, 5);
  3306. int wisdelete = sqlite3_column_int(statement, 6);
  3307. int more_color = sqlite3_column_int(statement, 7);
  3308. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3309. if(wid !=0 && wisdelete != 1)
  3310. [item setValue:@"true" forKey:@"wish_exists"];
  3311. else
  3312. [item setValue:@"false" forKey:@"wish_exists"];
  3313. if(closeout==0)
  3314. [item setValue:@"false" forKey:@"is_closeout"];
  3315. else
  3316. [item setValue:@"true" forKey:@"is_closeout"];
  3317. if(cid==0)
  3318. [item setValue:@"false" forKey:@"cart_exists"];
  3319. else
  3320. [item setValue:@"true" forKey:@"cart_exists"];
  3321. if (more_color == 0) {
  3322. [item setObject:@(false) forKey:@"more_color"];
  3323. } else if (more_color == 1) {
  3324. [item setObject:@(true) forKey:@"more_color"];
  3325. }
  3326. [item addEntriesFromDictionary:imgjson];
  3327. // [item setValue:nsurl forKey:@"img"];
  3328. [item setValue:nsname forKey:@"fash_name"];
  3329. [item setValue:nsdescription forKey:@"description"];
  3330. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3331. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3332. i++;
  3333. }
  3334. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3335. [ret setObject:items forKey:@"items"];
  3336. sqlite3_finalize(statement);
  3337. }
  3338. DebugLog(@"count:%d",count);
  3339. [iSalesDB close_db:db];
  3340. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3341. return ret;
  3342. }
  3343. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3344. {
  3345. return [self search:params limited:YES];
  3346. }
  3347. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3348. //{
  3349. // UIApplication * app = [UIApplication sharedApplication];
  3350. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3351. //
  3352. //// [iSalesDB disable_trigger]
  3353. // [iSalesDB disable_trigger];
  3354. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3355. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3356. //
  3357. // [iSalesDB enable_trigger];
  3358. //// [iSalesDB enable_trigger]
  3359. // //
  3360. // // NSString* user = [params valueForKey:@"user"];
  3361. // //
  3362. // // NSString* password = [params valueForKey:@"password"];
  3363. //
  3364. //
  3365. //
  3366. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3367. //
  3368. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3369. //
  3370. //
  3371. //
  3372. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3373. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3374. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3375. //
  3376. // [appDelegate update_count_mark];
  3377. //
  3378. //
  3379. // appDelegate.can_show_price =false;
  3380. // appDelegate.can_see_price =false;
  3381. // appDelegate.can_create_portfolio =false;
  3382. // appDelegate.can_create_order =false;
  3383. //
  3384. //
  3385. // appDelegate.can_cancel_order =false;
  3386. // appDelegate.can_set_cart_price =false;
  3387. // appDelegate.can_delete_order =false;
  3388. // appDelegate.can_submit_order =false;
  3389. // appDelegate.can_set_tearsheet_price =false;
  3390. // appDelegate.can_update_contact_info = false;
  3391. //
  3392. // appDelegate.save_order_logout = false;
  3393. // appDelegate.submit_order_logout = false;
  3394. // appDelegate.alert_sold_in_quantities = false;
  3395. //
  3396. // appDelegate.ipad_perm =nil ;
  3397. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3398. // appDelegate.OrderFilter= nil;
  3399. // [appDelegate SetSo:nil];
  3400. // [appDelegate set_main_button_panel];
  3401. //
  3402. //
  3403. // // sqlite3 *db = [iSalesDB get_db];
  3404. // //
  3405. // //
  3406. // //
  3407. // //
  3408. // //
  3409. // // 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"]];
  3410. // //
  3411. // //
  3412. // //
  3413. // //
  3414. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3415. // // sqlite3_stmt * statement;
  3416. // //
  3417. // //
  3418. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3419. // //
  3420. // //
  3421. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3422. // // {
  3423. // //
  3424. // //
  3425. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3426. // // {
  3427. // //
  3428. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3429. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3430. // //
  3431. // //
  3432. // //
  3433. // // int can_show_price = sqlite3_column_int(statement, 0);
  3434. // // int can_see_price = sqlite3_column_int(statement, 1);
  3435. // //
  3436. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3437. // // if(contact_id==nil)
  3438. // // contact_id="";
  3439. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3440. // //
  3441. // // int user_type = sqlite3_column_int(statement, 3);
  3442. // //
  3443. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3444. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3445. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3446. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3447. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3448. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3449. // // int can_create_order = sqlite3_column_int(statement, 10);
  3450. // //
  3451. // //
  3452. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3453. // // if(mode==nil)
  3454. // // mode="";
  3455. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3456. // //
  3457. // //
  3458. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3459. // // if(username==nil)
  3460. // // username="";
  3461. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3462. // //
  3463. // //
  3464. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3465. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3466. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3467. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3468. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3469. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3470. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3471. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3472. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3473. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3474. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3475. // //
  3476. // // [header setValue:nsusername forKey:@"username"];
  3477. // //
  3478. // //
  3479. // // [ret setObject:header forKey:@"header"];
  3480. // // [ret setValue:nsmode forKey:@"mode"];
  3481. // //
  3482. // //
  3483. // // }
  3484. // //
  3485. // //
  3486. // //
  3487. // // sqlite3_finalize(statement);
  3488. // // }
  3489. // //
  3490. // //
  3491. // //
  3492. // // [iSalesDB close_db:db];
  3493. // //
  3494. // //
  3495. // //
  3496. // //
  3497. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3498. //
  3499. // return ret;
  3500. //}
  3501. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3502. //{
  3503. //
  3504. // UIApplication * app = [UIApplication sharedApplication];
  3505. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3506. //
  3507. // [iSalesDB disable_trigger];
  3508. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3509. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3510. // [iSalesDB enable_trigger];
  3511. //
  3512. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3513. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3514. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3515. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3516. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3517. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3518. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3519. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3520. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3521. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3522. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3523. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3524. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3525. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3526. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3527. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3528. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3529. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3530. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3531. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3532. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3533. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3534. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3535. //
  3536. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3537. //
  3538. // [arr_name addObject:customer_first_name];
  3539. // [arr_name addObject:customer_last_name];
  3540. //
  3541. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3542. //
  3543. // // default ship from
  3544. // 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';";
  3545. //
  3546. // __block NSString *cid = @"";
  3547. // __block NSString *name = @"";
  3548. // __block NSString *ext = @"";
  3549. // __block NSString *contact = @"";
  3550. // __block NSString *email = @"";
  3551. // __block NSString *fax = @"";
  3552. // __block NSString *phone = @"";
  3553. //
  3554. // sqlite3 *db = [iSalesDB get_db];
  3555. //
  3556. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3557. //
  3558. // cid = [self textAtColumn:0 statement:statment];
  3559. // name = [self textAtColumn:1 statement:statment];
  3560. // ext = [self textAtColumn:2 statement:statment];
  3561. // contact = [self textAtColumn:3 statement:statment];
  3562. // email = [self textAtColumn:4 statement:statment];
  3563. // fax = [self textAtColumn:5 statement:statment];
  3564. // phone = [self textAtColumn:6 statement:statment];
  3565. //
  3566. // }];
  3567. //
  3568. // NSString* so_id = [self get_offline_soid:db];
  3569. // if(so_id==nil)
  3570. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3571. //
  3572. // 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];
  3573. //
  3574. //
  3575. //
  3576. // int result =[iSalesDB execSql:sql_neworder db:db];
  3577. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3578. //
  3579. //
  3580. //
  3581. // //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'
  3582. // //soId
  3583. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3584. //
  3585. //
  3586. //
  3587. //
  3588. //
  3589. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3590. // sqlite3_stmt * statement;
  3591. //
  3592. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3593. // {
  3594. // if (sqlite3_step(statement) == SQLITE_ROW)
  3595. // {
  3596. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3597. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3598. //
  3599. // //ret = sqlite3_column_int(statement, 0);
  3600. //
  3601. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3602. // if(soId==nil)
  3603. // soId="";
  3604. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3605. // [ret setValue:nssoId forKey:@"soId"];
  3606. // [ret setValue:nssoId forKey:@"orderCode"];
  3607. //
  3608. // }
  3609. // sqlite3_finalize(statement);
  3610. // }
  3611. //
  3612. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3613. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3614. //
  3615. //
  3616. // [iSalesDB close_db:db];
  3617. //
  3618. // return [RAConvertor dict2data:ret];
  3619. //
  3620. //}
  3621. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3622. {
  3623. //assert(params[@"order_code"]);
  3624. // assert(params[@"order_code"]);
  3625. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3626. NSString* orderCode = [params valueForKey:@"orderCode"];
  3627. NSString* app_order_code= params[@"appDelegate.order_code"];
  3628. // UIApplication * app = [UIApplication sharedApplication];
  3629. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3630. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3631. {
  3632. [iSalesDB disable_trigger];
  3633. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3634. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3635. [iSalesDB enable_trigger];
  3636. }
  3637. sqlite3 *db = [iSalesDB get_db];
  3638. int cart_count=[self query_ordercartcount:orderCode db:db];
  3639. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3640. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3641. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3642. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3643. [iSalesDB close_db:db];
  3644. return [RAConvertor dict2data:ret];
  3645. }
  3646. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3647. {
  3648. assert(params[@"can_create_backorder"]!=nil);
  3649. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3650. sqlite3 *db = [iSalesDB get_db];
  3651. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3652. int count =0;
  3653. if(params[@"count"]!=nil)
  3654. {
  3655. count = [params[@"count"] intValue];
  3656. }
  3657. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3658. NSString* product_id=params[@"product_id"];
  3659. NSString* orderCode=params[@"orderCode"];
  3660. NSString *qty = params[@"qty"];
  3661. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3662. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3663. __block int number_of_outOfStock = 0;
  3664. for(int i=0;i<arr_id.count;i++)
  3665. {
  3666. NSInteger item_qty= count;
  3667. if (qty) {
  3668. item_qty = [qty_arr[i] integerValue];
  3669. }
  3670. if(item_qty==0)
  3671. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3672. // 检查新加Model数量是否大于库存
  3673. if (![params[@"can_create_backorder"] boolValue]) {
  3674. __block BOOL needContinue = NO;
  3675. [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) {
  3676. int availability = sqlite3_column_int(stmt, 0);
  3677. // 库存小于购买量为缺货
  3678. if (availability < item_qty || availability <= 0) {
  3679. number_of_outOfStock++;
  3680. needContinue = YES;
  3681. }
  3682. }];
  3683. if (needContinue) {
  3684. continue;
  3685. }
  3686. }
  3687. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3688. NSString* sql=nil;
  3689. sqlite3_stmt *stmt = nil;
  3690. BOOL shouldStep = NO;
  3691. if(_id<0)
  3692. {
  3693. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3694. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3695. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3696. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3697. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3698. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3699. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3700. shouldStep = YES;
  3701. }
  3702. else
  3703. {
  3704. if (qty) { // wish list move to cart
  3705. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3706. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3707. sqlite3_bind_int(stmt, 1, _id);
  3708. shouldStep = YES;
  3709. } else {
  3710. 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];
  3711. __block BOOL update = YES;
  3712. if (![params[@"can_create_backorder"] boolValue]) {
  3713. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3714. int newQTY = sqlite3_column_int(stmt, 0);
  3715. int availability = sqlite3_column_int(stmt, 1);
  3716. if (newQTY > availability) { // 库存不够
  3717. update = NO;
  3718. number_of_outOfStock++;
  3719. }
  3720. }];
  3721. }
  3722. if (update) {
  3723. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3724. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3725. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3726. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3727. shouldStep = YES;
  3728. }
  3729. }
  3730. }
  3731. if (shouldStep) {
  3732. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3733. [iSalesDB execSql:@"ROLLBACK" db:db];
  3734. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3735. [iSalesDB close_db:db];
  3736. DebugLog(@"add to cart error");
  3737. return [RAConvertor dict2data:ret];
  3738. }
  3739. }
  3740. }
  3741. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3742. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3743. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3744. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3745. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3746. if (![params[@"can_create_backorder"] boolValue]) {
  3747. if (number_of_outOfStock > 0) {
  3748. ret[@"result"]=[NSNumber numberWithInt:8];
  3749. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3750. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3751. }
  3752. }
  3753. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3754. [iSalesDB close_db:db];
  3755. return [RAConvertor dict2data:ret];
  3756. }
  3757. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3758. {
  3759. // UIApplication * app = [UIApplication sharedApplication];
  3760. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3761. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3762. 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];
  3763. sqlite3_stmt * statement;
  3764. int count=0;
  3765. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3766. {
  3767. while (sqlite3_step(statement) == SQLITE_ROW)
  3768. {
  3769. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3770. int bitem_id = sqlite3_column_int(statement, 0);
  3771. int bitem_qty = sqlite3_column_int(statement, 1);
  3772. char *name = (char*)sqlite3_column_text(statement, 2);
  3773. if(name==nil)
  3774. name="";
  3775. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3776. char *description = (char*)sqlite3_column_text(statement, 3);
  3777. if(description==nil)
  3778. description="";
  3779. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3780. double unit_price = sqlite3_column_double(statement, 4);
  3781. int use_unitprice = sqlite3_column_int(statement, 5);
  3782. if(use_unitprice!=1)
  3783. {
  3784. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3785. }
  3786. itemjson[@"model"]=nsname;
  3787. itemjson[@"description"]=nsdescription;
  3788. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3789. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3790. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3791. if(compute)
  3792. {
  3793. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3794. }
  3795. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3796. count++;
  3797. }
  3798. sqlite3_finalize(statement);
  3799. }
  3800. ret[@"count"]=@(count);
  3801. if(count==0)
  3802. return nil;
  3803. else
  3804. return ret;
  3805. }
  3806. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3807. {
  3808. //compute: add part to subtotal;
  3809. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3810. dict_item[@(item_id)]=@"1";
  3811. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3812. double cuft=0;
  3813. double weight=0;
  3814. int carton=0;
  3815. 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];
  3816. sqlite3_stmt * statement;
  3817. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3818. {
  3819. if (sqlite3_step(statement) == SQLITE_ROW)
  3820. {
  3821. double ulength = sqlite3_column_double(statement, 0);
  3822. double uwidth = sqlite3_column_double(statement, 1);
  3823. double uheight = sqlite3_column_double(statement, 2);
  3824. double uweight = sqlite3_column_double(statement, 3);
  3825. double mlength = sqlite3_column_double(statement, 4);
  3826. double mwidth = sqlite3_column_double(statement, 5);
  3827. double mheight = sqlite3_column_double(statement, 6);
  3828. double mweight = sqlite3_column_double(statement, 7);
  3829. double ilength = sqlite3_column_double(statement, 8);
  3830. double iwidth = sqlite3_column_double(statement, 9);
  3831. double iheight = sqlite3_column_double(statement, 10);
  3832. double iweight = sqlite3_column_double(statement, 11);
  3833. // int pcs = sqlite3_column_int(statement,12);
  3834. int mpack = sqlite3_column_int(statement, 13);
  3835. int ipack = sqlite3_column_int(statement, 14);
  3836. double ucbf = sqlite3_column_double(statement, 15);
  3837. // double icbf = sqlite3_column_double(statement, 16);
  3838. // double mcbf = sqlite3_column_double(statement, 17);
  3839. if(ipack==0)
  3840. {
  3841. carton= count/mpack ;
  3842. weight = mweight*carton;
  3843. cuft= carton*(mlength*mwidth*mheight);
  3844. int remain=count%mpack;
  3845. if(remain==0)
  3846. {
  3847. //do nothing;
  3848. }
  3849. else
  3850. {
  3851. carton++;
  3852. weight += uweight*remain;
  3853. cuft += (ulength*uwidth*uheight)*remain;
  3854. }
  3855. }
  3856. else
  3857. {
  3858. carton = count/(mpack*ipack);
  3859. weight = mweight*carton;
  3860. cuft= carton*(mlength*mwidth*mheight);
  3861. int remain=count%(mpack*ipack);
  3862. if(remain==0)
  3863. {
  3864. // do nothing;
  3865. }
  3866. else
  3867. {
  3868. carton++;
  3869. int icarton =remain/ipack;
  3870. int iremain=remain%ipack;
  3871. weight += iweight*icarton;
  3872. cuft += (ilength*iwidth*iheight)*icarton;
  3873. if(iremain==0)
  3874. {
  3875. //do nothing;
  3876. }
  3877. else
  3878. {
  3879. weight += uweight*iremain;
  3880. cuft += (ulength*uwidth*uheight)*iremain;
  3881. }
  3882. }
  3883. }
  3884. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3885. cuft=ucbf*count;
  3886. weight= uweight*count;
  3887. #endif
  3888. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3889. cuft=ucbf*count;
  3890. weight= uweight*count;
  3891. #endif
  3892. }
  3893. sqlite3_finalize(statement);
  3894. }
  3895. if(compute)
  3896. {
  3897. NSArray * arr_count=nil;
  3898. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3899. for(int i=0;i<arr_bundle.count;i++)
  3900. {
  3901. dict_item[arr_bundle[i]]=@"1";
  3902. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3903. cuft+=[bundlejson[@"cuft"] doubleValue];
  3904. weight+=[bundlejson[@"weight"] doubleValue];
  3905. carton+=[bundlejson[@"carton"] intValue];
  3906. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3907. }
  3908. }
  3909. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3910. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3911. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3912. ret[@"items"]=dict_item;
  3913. return ret;
  3914. }
  3915. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3916. {
  3917. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3918. // 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 ];
  3919. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3920. DebugLog(@"offline_login sql:%@",sqlQuery);
  3921. sqlite3_stmt * statement;
  3922. int cart_count=0;
  3923. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3924. if ( dbresult== SQLITE_OK)
  3925. {
  3926. while (sqlite3_step(statement) == SQLITE_ROW)
  3927. {
  3928. int item_id = sqlite3_column_int(statement, 0);
  3929. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3930. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3931. }
  3932. sqlite3_finalize(statement);
  3933. }
  3934. return cart_count;
  3935. }
  3936. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3937. //{
  3938. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3939. // sqlite3 *db = [iSalesDB get_db];
  3940. // UIApplication * app = [UIApplication sharedApplication];
  3941. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3942. //
  3943. // NSString* orderCode=params[@"orderCode"];
  3944. //
  3945. // int sort = [[params objectForKey:@"sort"] intValue];
  3946. // NSString *sort_str = @"";
  3947. // switch (sort) {
  3948. // case 0:{
  3949. // sort_str = @"order by c.modify_time desc";
  3950. // }
  3951. // break;
  3952. // case 1:{
  3953. // sort_str = @"order by c.modify_time asc";
  3954. // }
  3955. // break;
  3956. // case 2:{
  3957. // sort_str = @"order by m.name asc";
  3958. // }
  3959. // break;
  3960. // case 3:{
  3961. // sort_str = @"order by m.name desc";
  3962. // }
  3963. // break;
  3964. // case 4:{
  3965. // sort_str = @"order by m.description asc";
  3966. // }
  3967. // break;
  3968. //
  3969. // default:
  3970. // break;
  3971. //
  3972. // }
  3973. //
  3974. //
  3975. //
  3976. // 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 ];
  3977. //
  3978. //
  3979. //// 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 ];
  3980. //
  3981. //
  3982. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3983. //
  3984. // DebugLog(@"offline_login sql:%@",sqlQuery);
  3985. // sqlite3_stmt * statement;
  3986. //
  3987. //
  3988. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3989. //
  3990. // NSDate *date1 = [NSDate date];
  3991. //
  3992. // int count=0;
  3993. // int cart_count=0;
  3994. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3995. // if ( dbresult== SQLITE_OK)
  3996. // {
  3997. //
  3998. //
  3999. // while (sqlite3_step(statement) == SQLITE_ROW)
  4000. // {
  4001. //// NSDate *row_date = [NSDate date];
  4002. //
  4003. //
  4004. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4005. //
  4006. //
  4007. //
  4008. //
  4009. // int product_id = sqlite3_column_int(statement, 0);
  4010. //
  4011. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4012. //
  4013. // int item_id = sqlite3_column_int(statement, 7);
  4014. //
  4015. // NSString* Price=nil;
  4016. // if(str_price==nil)
  4017. // {
  4018. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4019. //// NSDate *price_date = [NSDate date];
  4020. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4021. //// DebugLog(@"price time interval");
  4022. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4023. //
  4024. // if(price==nil)
  4025. // Price=@"No Price.";
  4026. // else
  4027. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4028. // }
  4029. // else
  4030. // {
  4031. //
  4032. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4033. // }
  4034. //
  4035. //
  4036. // double discount = sqlite3_column_double(statement, 2);
  4037. // int item_count = sqlite3_column_int(statement, 3);
  4038. //
  4039. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4040. // NSString *nsline_note=nil;
  4041. // if(line_note!=nil)
  4042. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4043. //
  4044. //
  4045. // char *name = (char*)sqlite3_column_text(statement, 5);
  4046. // NSString *nsname=nil;
  4047. // if(name!=nil)
  4048. // nsname= [[NSString alloc]initWithUTF8String:name];
  4049. //
  4050. // char *description = (char*)sqlite3_column_text(statement, 6);
  4051. // NSString *nsdescription=nil;
  4052. // if(description!=nil)
  4053. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4054. //
  4055. //
  4056. //
  4057. // int stockUom = sqlite3_column_int(statement, 8);
  4058. // int _id = sqlite3_column_int(statement, 9);
  4059. // int availability = sqlite3_column_int(statement, 10);
  4060. //
  4061. //// NSDate *subtotal_date = [NSDate date];
  4062. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4063. //// DebugLog(@"subtotal_date time interval");
  4064. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4065. //
  4066. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4067. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4068. // int carton=[bsubtotaljson[@"carton"] intValue];
  4069. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4070. //
  4071. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4072. //// NSDate *img_date = [NSDate date];
  4073. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4074. //// DebugLog(@"img_date time interval");
  4075. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4076. //
  4077. // itemjson[@"model"]=nsname;
  4078. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4079. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4080. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4081. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4082. // itemjson[@"check"]=@"true";
  4083. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4084. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4085. // itemjson[@"unit_price"]=Price;
  4086. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4087. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4088. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4089. // itemjson[@"note"]=nsline_note;
  4090. // if (!appDelegate.can_create_backorder) {
  4091. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4092. // }
  4093. //// NSDate *date2 = [NSDate date];
  4094. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4095. //// DebugLog(@"model_bundle time interval");
  4096. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4097. //
  4098. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4099. // count++;
  4100. //
  4101. //// DebugLog(@"row time interval");
  4102. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4103. // }
  4104. //
  4105. //
  4106. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4107. // sqlite3_finalize(statement);
  4108. // }
  4109. //
  4110. //
  4111. // DebugLog(@"request cart total time interval");
  4112. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4113. //
  4114. //
  4115. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4116. //
  4117. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4118. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4119. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4120. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4121. //
  4122. // ret[@"mode"]=@"Regular Mode";
  4123. //
  4124. // [iSalesDB close_db:db];
  4125. //
  4126. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4127. //
  4128. // DebugLog(@"general notes :%@",general_note);
  4129. //
  4130. // ret[@"general_note"]= general_note;
  4131. //
  4132. // return [RAConvertor dict2data:ret];
  4133. //}
  4134. +(NSData*) offline_login :(NSMutableDictionary *) params
  4135. {
  4136. NSString* user = [params valueForKey:@"user"];
  4137. NSString* password = [params valueForKey:@"password"];
  4138. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4139. sqlite3 *db = [iSalesDB get_db];
  4140. 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"]];
  4141. DebugLog(@"offline_login sql:%@",sqlQuery);
  4142. sqlite3_stmt * statement;
  4143. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4144. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4145. {
  4146. if (sqlite3_step(statement) == SQLITE_ROW)
  4147. {
  4148. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4149. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4150. int can_show_price = sqlite3_column_int(statement, 0);
  4151. int can_see_price = sqlite3_column_int(statement, 1);
  4152. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4153. if(contact_id==nil)
  4154. contact_id="";
  4155. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4156. int user_type = sqlite3_column_int(statement, 3);
  4157. int can_cancel_order = sqlite3_column_int(statement, 4);
  4158. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4159. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4160. int can_delete_order = sqlite3_column_int(statement, 7);
  4161. int can_submit_order = sqlite3_column_int(statement, 8);
  4162. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4163. int can_create_order = sqlite3_column_int(statement, 10);
  4164. char *mode = (char*)sqlite3_column_text(statement, 11);
  4165. if(mode==nil)
  4166. mode="";
  4167. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4168. char *username = (char*)sqlite3_column_text(statement, 12);
  4169. if(username==nil)
  4170. username="";
  4171. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4172. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4173. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4174. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4175. [header setValue:nscontact_id forKey:@"contact_id"];
  4176. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4177. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4178. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4179. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4180. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4181. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4182. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4183. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4184. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4185. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4186. [header setValue:nsusername forKey:@"username"];
  4187. NSError* error=nil;
  4188. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4189. [header setValue:statusFilter forKey:@"statusFilter"];
  4190. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4191. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4192. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4193. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4194. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4195. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4196. [ret setObject:header forKey:@"header"];
  4197. [ret setValue:nsmode forKey:@"mode"];
  4198. }
  4199. sqlite3_finalize(statement);
  4200. }
  4201. [iSalesDB close_db:db];
  4202. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4203. return [RAConvertor dict2data:ret];
  4204. }
  4205. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4206. {
  4207. assert(params[@"mode"]!=nil);
  4208. NSString* contactId = [params valueForKey:@"contactId"];
  4209. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4210. sqlite3 *db = [iSalesDB get_db];
  4211. NSString *sqlQuery = nil;
  4212. {
  4213. 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];
  4214. }
  4215. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4216. sqlite3_stmt * statement;
  4217. [ret setValue:@"2" forKey:@"result"];
  4218. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4219. {
  4220. //int i = 0;
  4221. if (sqlite3_step(statement) == SQLITE_ROW)
  4222. {
  4223. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4224. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4225. // int editable = sqlite3_column_int(statement, 0);
  4226. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4227. NSString *nscompany_name =nil;
  4228. if(company_name==nil)
  4229. nscompany_name=@"";
  4230. else
  4231. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4232. char *country = (char*)sqlite3_column_text(statement, 2);
  4233. if(country==nil)
  4234. country="";
  4235. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4236. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4237. // if(addr==nil)
  4238. // addr="";
  4239. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4240. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4241. if(zipcode==nil)
  4242. zipcode="";
  4243. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4244. char *state = (char*)sqlite3_column_text(statement, 5);
  4245. if(state==nil)
  4246. state="";
  4247. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4248. char *city = (char*)sqlite3_column_text(statement, 6);
  4249. if(city==nil)
  4250. city="";
  4251. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4252. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4253. // NSString *nscontact_name = nil;
  4254. // if(contact_name==nil)
  4255. // nscontact_name=@"";
  4256. // else
  4257. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4258. char *phone = (char*)sqlite3_column_text(statement, 8);
  4259. NSString *nsphone = nil;
  4260. if(phone==nil)
  4261. nsphone=@"";
  4262. else
  4263. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4264. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4265. // if(contact_id==nil)
  4266. // contact_id="";
  4267. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4268. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4269. if(addr_1==nil)
  4270. addr_1="";
  4271. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4272. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4273. if(addr_2==nil)
  4274. addr_2="";
  4275. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4276. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4277. if(addr_3==nil)
  4278. addr_3="";
  4279. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4280. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4281. if(addr_4==nil)
  4282. addr_4="";
  4283. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4284. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4285. if(first_name==nil)
  4286. first_name="";
  4287. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4288. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4289. if(last_name==nil)
  4290. last_name="";
  4291. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4292. char *fax = (char*)sqlite3_column_text(statement, 16);
  4293. NSString *nsfax = nil;
  4294. if(fax==nil)
  4295. nsfax=@"";
  4296. else
  4297. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4298. char *email = (char*)sqlite3_column_text(statement, 17);
  4299. NSString *nsemail = nil;
  4300. if(email==nil)
  4301. nsemail=@"";
  4302. else
  4303. nsemail= [[NSString alloc]initWithUTF8String:email];
  4304. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4305. NSString *nsimg_0 = nil;
  4306. if(img_0==nil)
  4307. nsimg_0=@"";
  4308. else
  4309. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4310. [self copy_bcardImg:nsimg_0];
  4311. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4312. NSString *nsimg_1 = nil;
  4313. if(img_1==nil)
  4314. nsimg_1=@"";
  4315. else
  4316. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4317. [self copy_bcardImg:nsimg_1];
  4318. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4319. NSString *nsimg_2 = nil;
  4320. if(img_2==nil)
  4321. nsimg_2=@"";
  4322. else
  4323. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4324. [self copy_bcardImg:nsimg_2];
  4325. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4326. NSString *nsprice_type = nil;
  4327. if(price_type==nil)
  4328. nsprice_type=@"";
  4329. else
  4330. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4331. char *notes = (char*)sqlite3_column_text(statement, 22);
  4332. NSString *nsnotes = nil;
  4333. if(notes==nil)
  4334. nsnotes=@"";
  4335. else
  4336. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4337. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4338. NSString *nssalesrep = nil;
  4339. if(salesrep==nil)
  4340. nssalesrep=@"";
  4341. else
  4342. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4343. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4344. {
  4345. // decrypt
  4346. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4347. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4348. nsphone=[AESCrypt fastdecrypt:nsphone];
  4349. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4350. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4351. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4352. }
  4353. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4354. [arr_name addObject:nsfirst_name];
  4355. [arr_name addObject:nslast_name];
  4356. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4357. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4358. [arr_ext addObject:nsaddr_1];
  4359. [arr_ext addObject:nsaddr_2];
  4360. [arr_ext addObject:nsaddr_3];
  4361. [arr_ext addObject:nsaddr_4];
  4362. [arr_ext addObject:@"\r\n"];
  4363. [arr_ext addObject:nscity];
  4364. [arr_ext addObject:nsstate];
  4365. [arr_ext addObject:nszipcode];
  4366. [arr_ext addObject:nscountry];
  4367. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4368. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4369. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4370. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4371. [item setValue:nscountry forKey:@"customer_country"];
  4372. [item setValue:nsphone forKey:@"customer_phone"];
  4373. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4374. [item setValue:nscompany_name forKey:@"customer_name"];
  4375. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4376. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4377. [item setValue:nsext forKey:@"customer_contact_ext"];
  4378. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4379. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4380. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4381. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4382. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4383. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4384. [item setValue:nslast_name forKey:@"customer_last_name"];
  4385. [item setValue:nscity forKey:@"customer_city"];
  4386. [item setValue:nsstate forKey:@"customer_state"];
  4387. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4388. [item setValue:contactId forKey:@"customer_cid"];
  4389. [item setValue:nscontact_name forKey:@"customer_contact"];
  4390. [item setValue:nsfax forKey:@"customer_fax"];
  4391. [item setValue:nsemail forKey:@"customer_email"];
  4392. [item setValue:contact_type forKey:@"customer_contact_type"];
  4393. [ret setObject:item forKey:@"customerInfo"];
  4394. // i++;
  4395. }
  4396. // UIApplication * app = [UIApplication sharedApplication];
  4397. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4398. [ret setValue:params[@"mode"] forKey:@"mode"];
  4399. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4400. sqlite3_finalize(statement);
  4401. }
  4402. [iSalesDB close_db:db];
  4403. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4404. return ret;
  4405. }
  4406. + (bool) copy_bcardImg:(NSString*) filename
  4407. {
  4408. if(filename.length==0)
  4409. return false;
  4410. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4411. bool ret=false;
  4412. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4413. NSString *cachefolder = [paths objectAtIndex:0];
  4414. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4415. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4416. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4417. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4418. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4419. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4420. //
  4421. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4422. NSFileManager* fileManager = [NSFileManager defaultManager];
  4423. BOOL bdir=NO;
  4424. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4425. {
  4426. NSError *error = nil;
  4427. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4428. {
  4429. ret=false;
  4430. }
  4431. else
  4432. {
  4433. ret=true;
  4434. }
  4435. // NSError *error = nil;
  4436. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4437. //
  4438. // if(!bsuccess)
  4439. // {
  4440. // DebugLog(@"Create offline_createimg folder failed");
  4441. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4442. // return [RAConvertor dict2data:ret];
  4443. // }
  4444. // if(bsuccess)
  4445. // {
  4446. // sqlite3 *db = [self get_db];
  4447. //
  4448. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4449. // [iSalesDB close_db:db];
  4450. // }
  4451. }
  4452. return ret;
  4453. //
  4454. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4455. // if(bsuccess)
  4456. // {
  4457. // NSError *error = nil;
  4458. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4459. // {
  4460. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4461. // }
  4462. // else
  4463. // {
  4464. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4465. //
  4466. // ret[@"img_url_aname"]=filename;
  4467. // ret[@"img_url"]=savedImagePath;
  4468. // }
  4469. // }
  4470. }
  4471. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4472. {
  4473. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4474. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4475. NSString *cachefolder = [paths objectAtIndex:0];
  4476. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4477. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4478. NSFileManager* fileManager = [NSFileManager defaultManager];
  4479. BOOL bdir=YES;
  4480. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4481. {
  4482. NSError *error = nil;
  4483. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4484. if(!bsuccess)
  4485. {
  4486. DebugLog(@"Create offline_createimg folder failed");
  4487. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4488. return [RAConvertor dict2data:ret];
  4489. }
  4490. // if(bsuccess)
  4491. // {
  4492. // sqlite3 *db = [self get_db];
  4493. //
  4494. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4495. // [iSalesDB close_db:db];
  4496. // }
  4497. }
  4498. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4499. {
  4500. NSError *error = nil;
  4501. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4502. if(!bsuccess)
  4503. {
  4504. DebugLog(@"Create img_cache folder failed");
  4505. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4506. return [RAConvertor dict2data:ret];
  4507. }
  4508. // if(bsuccess)
  4509. // {
  4510. // sqlite3 *db = [self get_db];
  4511. //
  4512. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4513. // [iSalesDB close_db:db];
  4514. // }
  4515. }
  4516. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4517. //JEPG格式
  4518. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4519. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4520. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4521. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4522. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4523. if(bsuccess)
  4524. {
  4525. NSError *error = nil;
  4526. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4527. {
  4528. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4529. }
  4530. else
  4531. {
  4532. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4533. ret[@"img_url_aname"]=filename;
  4534. ret[@"img_url"]=filename;
  4535. }
  4536. }
  4537. else
  4538. {
  4539. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4540. }
  4541. return [RAConvertor dict2data:ret];
  4542. }
  4543. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4544. {
  4545. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4546. category = [category substringToIndex:3];
  4547. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4548. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4549. params[@"category"]= category;
  4550. ret[@"params"]= params;
  4551. [ret setValue:@"detail" forKey:@"target"];
  4552. [ret setValue:@"popup" forKey:@"action"];
  4553. [ret setValue:@"content" forKey:@"type"];
  4554. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4555. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4556. [ret setValue:@"true" forKey:@"single_row"];
  4557. [ret setValue:@"true" forKey:@"partial_refresh"];
  4558. // sqlite3 *db = [iSalesDB get_db];
  4559. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4560. NSString *sqlQuery =nil;
  4561. #ifdef BUILD_NPD
  4562. 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 ;
  4563. #else
  4564. 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];
  4565. #endif
  4566. sqlite3_stmt * statement;
  4567. int count = 0;
  4568. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4569. // int count=0;
  4570. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4571. {
  4572. int i=0;
  4573. while (sqlite3_step(statement) == SQLITE_ROW)
  4574. {
  4575. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4576. // char *name = (char*)sqlite3_column_text(statement, 1);
  4577. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4578. char *name = (char*)sqlite3_column_text(statement, 0);
  4579. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4580. int product_id = sqlite3_column_int(statement, 1);
  4581. char *url = (char*)sqlite3_column_text(statement, 2);
  4582. if(url==nil)
  4583. url="";
  4584. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4585. [item setValue:nsurl forKey:@"picture_path"];
  4586. [item setValue:nsname forKey:@"fash_name"];
  4587. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4588. [item setValue:category forKey:@"category"];
  4589. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4590. i++;
  4591. }
  4592. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4593. sqlite3_finalize(statement);
  4594. }
  4595. DebugLog(@"count:%d",count);
  4596. // [iSalesDB close_db:db];
  4597. return ret;
  4598. }
  4599. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4600. {
  4601. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4602. [ret setValue:key forKey:@"key"];
  4603. [ret setValue:value forKey:@"val"];
  4604. [ret setValue:@"price" forKey:@"type"];
  4605. return ret;
  4606. }
  4607. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4608. {
  4609. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4610. [ret setValue:key forKey:@"key"];
  4611. [ret setValue:value forKey:@"val"];
  4612. return ret;
  4613. }
  4614. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4615. {
  4616. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4617. [ret setValue:@"0" forKey:@"img_count"];
  4618. // sqlite3 *db = [iSalesDB get_db];
  4619. 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 ;
  4620. sqlite3_stmt * statement;
  4621. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4622. {
  4623. int i=0;
  4624. if (sqlite3_step(statement) == SQLITE_ROW)
  4625. {
  4626. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4627. // char *name = (char*)sqlite3_column_text(statement, 1);
  4628. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4629. char *value = (char*)sqlite3_column_text(statement, 0);
  4630. if(value==nil)
  4631. value="";
  4632. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4633. char *key = (char*)sqlite3_column_text(statement, 1);
  4634. if(key==nil)
  4635. key="";
  4636. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4637. [item setValue:nsvalue forKey:@"val"];
  4638. [item setValue:nskey forKey:@"key"];
  4639. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4640. [ret setValue:@"1" forKey:@"count"];
  4641. i++;
  4642. }
  4643. sqlite3_finalize(statement);
  4644. }
  4645. // [iSalesDB close_db:db];
  4646. return ret;
  4647. }
  4648. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4649. {
  4650. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4651. [ret setValue:@"0" forKey:@"count"];
  4652. // sqlite3 *db = [iSalesDB get_db];
  4653. 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;
  4654. sqlite3_stmt * statement;
  4655. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4656. {
  4657. int i=0;
  4658. while (sqlite3_step(statement) == SQLITE_ROW)
  4659. {
  4660. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4661. // char *name = (char*)sqlite3_column_text(statement, 1);
  4662. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4663. char *value = (char*)sqlite3_column_text(statement, 0);
  4664. if(value==nil)
  4665. value="";
  4666. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4667. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4668. if(selector_display==nil)
  4669. selector_display="";
  4670. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4671. int product_id = sqlite3_column_int(statement, 2);
  4672. char *category = (char*)sqlite3_column_text(statement, 3);
  4673. if(category==nil)
  4674. category="";
  4675. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4676. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4677. [item setValue:nsvalue forKey:@"title"];
  4678. [item setValue:url forKey:@"pic_url"];
  4679. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4680. [params setValue:@"2" forKey:@"count"];
  4681. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4682. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4683. [param0 setValue:@"product_id" forKey:@"name"];
  4684. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4685. [param1 setValue:nscategory forKey:@"val"];
  4686. [param1 setValue:@"category" forKey:@"name"];
  4687. [params setObject:param0 forKey:@"param_0"];
  4688. [params setObject:param1 forKey:@"param_1"];
  4689. [item setObject:params forKey:@"params"];
  4690. [ret setValue:nsselector_display forKey:@"name"];
  4691. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4692. i++;
  4693. }
  4694. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4695. [ret setValue:@"switch" forKey:@"action"];
  4696. sqlite3_finalize(statement);
  4697. }
  4698. // [iSalesDB close_db:db];
  4699. return ret;
  4700. }
  4701. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4702. {
  4703. // model 在 category search 显示的图片。
  4704. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4705. // sqlite3 *db = [iSalesDB get_db];
  4706. 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];
  4707. sqlite3_stmt * statement;
  4708. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4709. {
  4710. while (sqlite3_step(statement) == SQLITE_ROW)
  4711. {
  4712. char *url = (char*)sqlite3_column_text(statement, 0);
  4713. if(url==nil)
  4714. url="";
  4715. int type = sqlite3_column_int(statement, 1);
  4716. if(type==0)
  4717. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4718. else
  4719. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4720. }
  4721. sqlite3_finalize(statement);
  4722. }
  4723. // [iSalesDB close_db:db];
  4724. return ret;
  4725. }
  4726. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4727. {
  4728. int item_id=-1;
  4729. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4730. sqlite3_stmt * statement;
  4731. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4732. {
  4733. if (sqlite3_step(statement) == SQLITE_ROW)
  4734. {
  4735. item_id = sqlite3_column_int(statement, 0);
  4736. }
  4737. sqlite3_finalize(statement);
  4738. }
  4739. return item_id;
  4740. }
  4741. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4742. {
  4743. // NSString* ret = @"";
  4744. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4745. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4746. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4747. sqlite3_stmt * statement;
  4748. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4749. {
  4750. while (sqlite3_step(statement) == SQLITE_ROW)
  4751. {
  4752. int bitem_id = sqlite3_column_int(statement, 0);
  4753. int bitem_qty = sqlite3_column_int(statement, 1);
  4754. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4755. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4756. }
  4757. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4758. sqlite3_finalize(statement);
  4759. }
  4760. // if(ret==nil)
  4761. // ret=@"";
  4762. *count=arr_count;
  4763. return arr_bundle;
  4764. }
  4765. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4766. {
  4767. // get default sold qty, return -1 if model not found;
  4768. int ret = -1;
  4769. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4770. sqlite3_stmt * statement;
  4771. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4772. {
  4773. if (sqlite3_step(statement) == SQLITE_ROW)
  4774. {
  4775. ret = sqlite3_column_int(statement, 0);
  4776. }
  4777. sqlite3_finalize(statement);
  4778. }
  4779. return ret;
  4780. }
  4781. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4782. {
  4783. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4784. [ret setValue:@"0" forKey:@"img_count"];
  4785. // sqlite3 *db = [iSalesDB get_db];
  4786. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4787. sqlite3_stmt * statement;
  4788. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4789. {
  4790. int i=0;
  4791. while (sqlite3_step(statement) == SQLITE_ROW)
  4792. {
  4793. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4794. // char *name = (char*)sqlite3_column_text(statement, 1);
  4795. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4796. char *url = (char*)sqlite3_column_text(statement, 0);
  4797. if(url==nil)
  4798. url="";
  4799. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4800. [item setValue:nsurl forKey:@"s"];
  4801. [item setValue:nsurl forKey:@"l"];
  4802. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4803. i++;
  4804. }
  4805. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4806. sqlite3_finalize(statement);
  4807. }
  4808. // [iSalesDB close_db:db];
  4809. return ret;
  4810. }
  4811. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4812. // UIApplication * app = [UIApplication sharedApplication];
  4813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4814. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4815. sqlite3 *db = [iSalesDB get_db];
  4816. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4817. NSString* product_id=params[@"product_id"];
  4818. NSString *item_count_str = params[@"item_count"];
  4819. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4820. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4821. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4822. // NSString *sql = @"";
  4823. for(int i=0;i<arr.count;i++)
  4824. {
  4825. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4826. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4827. __block int cart_count = 0;
  4828. if (!item_count_str) {
  4829. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4830. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4831. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4832. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4833. cart_count = [[model_set_components lastObject] intValue];
  4834. }];
  4835. }
  4836. if(count==0)
  4837. {
  4838. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4839. sqlite3_stmt *stmt;
  4840. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4841. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4842. if (item_count_arr) {
  4843. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4844. } else {
  4845. sqlite3_bind_int(stmt,2,cart_count);
  4846. }
  4847. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4848. [iSalesDB execSql:@"ROLLBACK" db:db];
  4849. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4850. [iSalesDB close_db:db];
  4851. DebugLog(@"add to wishlist error");
  4852. return ret;
  4853. }
  4854. } else {
  4855. int qty = 0;
  4856. if (item_count_arr) {
  4857. qty = [item_count_arr[i] intValue];
  4858. } else {
  4859. qty = cart_count;
  4860. }
  4861. 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]];
  4862. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4863. [iSalesDB execSql:@"ROLLBACK" db:db];
  4864. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4865. [iSalesDB close_db:db];
  4866. DebugLog(@"add to wishlist error");
  4867. return ret;
  4868. }
  4869. }
  4870. }
  4871. // [iSalesDB execSql:sql db:db];
  4872. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4873. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4874. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4875. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4876. [iSalesDB close_db:db];
  4877. ret[@"wish_count"]=@(count);
  4878. ret[@"result"]= [NSNumber numberWithInt:2];
  4879. return ret;
  4880. }
  4881. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4882. // 0 category
  4883. // 1 search
  4884. // 2 itemsearch
  4885. NSData *ret = nil;
  4886. NSDictionary *items = nil;
  4887. switch (from) {
  4888. case 0:{
  4889. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4890. }
  4891. break;
  4892. case 1:{
  4893. items = [[self search:params limited:NO] objectForKey:@"items"];
  4894. }
  4895. break;
  4896. case 2:{
  4897. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4898. }
  4899. break;
  4900. default:
  4901. break;
  4902. }
  4903. if (!items) {
  4904. return ret;
  4905. }
  4906. int count = [[items objectForKey:@"count"] intValue];
  4907. NSMutableString *product_id_str = [@"" mutableCopy];
  4908. for (int i = 0; i < count; i++) {
  4909. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4910. NSDictionary *item = [items objectForKey:key];
  4911. NSString *product_id = [item objectForKey:@"product_id"];
  4912. if (i == 0) {
  4913. [product_id_str appendString:product_id];
  4914. } else {
  4915. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4916. }
  4917. }
  4918. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4919. if ([add_to isEqualToString:@"cart"]) {
  4920. NSString *order_code = [params objectForKey:@"orderCode"];
  4921. if (order_code.length) {
  4922. NSDictionary *newParams = @{
  4923. @"product_id" : product_id_str,
  4924. @"orderCode" : order_code,
  4925. @"can_create_backorder":params[@"can_create_backorder"]
  4926. };
  4927. ret = [self offline_add2cart:newParams.mutableCopy];
  4928. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4929. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4930. ret = [RAConvertor dict2data:retDic];
  4931. }
  4932. } else if([add_to isEqualToString:@"wishlist"]) {
  4933. NSDictionary *newParams = @{
  4934. @"product_id" : product_id_str
  4935. };
  4936. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4937. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4938. ret = [RAConvertor dict2data:retDic];
  4939. } else if([add_to isEqualToString:@"portfolio"]) {
  4940. NSDictionary *newParams = @{
  4941. @"product_id" : product_id_str
  4942. };
  4943. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4944. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4945. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4946. ret = [RAConvertor dict2data:retDic];
  4947. }
  4948. return ret;
  4949. }
  4950. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4951. {
  4952. return [self addAll:params from:0];
  4953. }
  4954. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4955. {
  4956. return [self addAll:params from:1];
  4957. }
  4958. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4959. {
  4960. return [self addAll:params from:2];
  4961. }
  4962. #pragma mark - Jack
  4963. #warning 做SQL操作时转义!!
  4964. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4965. // "val_227" : {
  4966. // "check" : 1,
  4967. // "value" : "US United States",
  4968. // "value_id" : "228"
  4969. // },
  4970. if (!countryCode) {
  4971. countryCode = @"US";
  4972. }
  4973. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4974. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4975. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4976. int code_id = sqlite3_column_int(stmt, 3); // id
  4977. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4978. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4979. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4980. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4981. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4982. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4983. }
  4984. long n = *count;
  4985. *count = n + 1;
  4986. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4987. [container setValue:countryDic forKey:key];
  4988. }] mutableCopy];
  4989. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4990. return ret;
  4991. }
  4992. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4993. countryCode = [self translateSingleQuote:countryCode];
  4994. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4995. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4996. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4997. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4998. if (name == NULL) {
  4999. name = "";
  5000. }
  5001. if (code == NULL) {
  5002. code = "";
  5003. }
  5004. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5005. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5006. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5007. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5008. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5009. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5010. }
  5011. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5012. [container setValue:stateDic forKey:key];
  5013. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5014. DebugLog(@"query all state error: %@",err_msg);
  5015. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5016. // [stateDic setValue:@"Other" forKey:@"value"];
  5017. // [stateDic setValue:@"" forKey:@"value_id"];
  5018. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5019. //
  5020. // if (state_code && [@"" isEqualToString:state_code]) {
  5021. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5022. // }
  5023. //
  5024. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5025. // [container setValue:stateDic forKey:key];
  5026. }] mutableCopy];
  5027. [ret removeObjectForKey:@"result"];
  5028. // failure 可以不用了,一样的
  5029. if (ret.allKeys.count == 0) {
  5030. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5031. [stateDic setValue:@"Other" forKey:@"value"];
  5032. [stateDic setValue:@"" forKey:@"value_id"];
  5033. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5034. if (state_code && [@"" isEqualToString:state_code]) {
  5035. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5036. }
  5037. NSString *key = [NSString stringWithFormat:@"val_0"];
  5038. [ret setValue:stateDic forKey:key];
  5039. }
  5040. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5041. return ret;
  5042. }
  5043. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5044. codeId = [self translateSingleQuote:codeId];
  5045. 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];
  5046. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5047. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5048. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5049. if (name == NULL) {
  5050. name = "";
  5051. }
  5052. if (code == NULL) {
  5053. code = "";
  5054. }
  5055. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5056. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5057. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5058. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5059. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5060. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5061. }
  5062. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5063. [container setValue:stateDic forKey:key];
  5064. }] mutableCopy];
  5065. [ret removeObjectForKey:@"result"];
  5066. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5067. return ret;
  5068. }
  5069. + (NSDictionary *)offline_getPrice {
  5070. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5071. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5072. char *name = (char *) sqlite3_column_text(stmt, 1);
  5073. int type = sqlite3_column_int(stmt, 2);
  5074. int orderBy = sqlite3_column_int(stmt, 3);
  5075. if (name == NULL) {
  5076. name = "";
  5077. }
  5078. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5079. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5080. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5081. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5082. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5083. if (orderBy == 0) {
  5084. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5085. }
  5086. [container setValue:priceDic forKey:key];
  5087. }] mutableCopy];
  5088. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5089. return ret;
  5090. }
  5091. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5092. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5093. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5094. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5095. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5096. // int _id = sqlite3_column_int(stmt, 0);
  5097. NSString *name = [self textAtColumn:1 statement:stmt];
  5098. NSDictionary *typeDic = @{
  5099. @"value_id" : name,
  5100. @"value" : name,
  5101. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5102. };
  5103. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5104. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5105. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5106. }];
  5107. return ret;
  5108. }
  5109. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5110. // 首先从offline_login表中取出sales_code
  5111. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5112. // NSString *user = app.user;
  5113. user = [self translateSingleQuote:user];
  5114. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5115. __block NSString *user_code = @"";
  5116. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5117. char *code = (char *)sqlite3_column_text(stmt, 0);
  5118. if (code == NULL) {
  5119. code = "";
  5120. }
  5121. user_code = [NSString stringWithUTF8String:code];
  5122. }];
  5123. // 再取所有salesRep
  5124. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5125. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5126. // 1 name 2 code 3 salesrep_id
  5127. char *name = (char *)sqlite3_column_text(stmt, 1);
  5128. char *code = (char *)sqlite3_column_text(stmt, 2);
  5129. int salesrep_id = sqlite3_column_int(stmt, 3);
  5130. if (name == NULL) {
  5131. name = "";
  5132. }
  5133. if (code == NULL) {
  5134. code = "";
  5135. }
  5136. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5137. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5138. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5139. // 比较code 相等则check
  5140. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5141. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5142. }
  5143. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5144. }] mutableCopy];
  5145. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5146. return ret;
  5147. }
  5148. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5149. zipcode = [self translateSingleQuote:zipcode];
  5150. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5151. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5152. char *country = (char *)sqlite3_column_text(stmt, 0);
  5153. char *state = (char *)sqlite3_column_text(stmt, 1);
  5154. char *city = (char *)sqlite3_column_text(stmt, 2);
  5155. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5156. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5157. if (country == NULL) {
  5158. country = "";
  5159. }
  5160. if (state == NULL) {
  5161. state = "";
  5162. }
  5163. if (city == NULL) {
  5164. city = "";
  5165. }
  5166. if (country_code == NULL) {
  5167. country_code = "";
  5168. }
  5169. if (state_code == NULL) {
  5170. state_code = "";
  5171. }
  5172. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5173. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5174. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5175. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5176. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5177. }] mutableCopy];
  5178. return ret;
  5179. }
  5180. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5181. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5182. [item setValue:value forKey:valueKey];
  5183. [dic setValue:item forKey:itemKey];
  5184. }
  5185. + (NSString *)countryCodeByid:(NSString *)code_id {
  5186. NSString *ret = nil;
  5187. code_id = [self translateSingleQuote:code_id];
  5188. sqlite3 *db = [iSalesDB get_db];
  5189. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5190. sqlite3_stmt * statement;
  5191. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5192. while (sqlite3_step(statement) == SQLITE_ROW) {
  5193. char *code = (char *)sqlite3_column_text(statement, 0);
  5194. if (code == NULL) {
  5195. code = "";
  5196. }
  5197. ret = [NSString stringWithUTF8String:code];
  5198. }
  5199. sqlite3_finalize(statement);
  5200. }
  5201. [iSalesDB close_db:db];
  5202. return ret;
  5203. }
  5204. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5205. NSString *ret = nil;
  5206. code = [self translateSingleQuote:code];
  5207. sqlite3 *db = [iSalesDB get_db];
  5208. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5209. sqlite3_stmt * statement;
  5210. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5211. while (sqlite3_step(statement) == SQLITE_ROW) {
  5212. char *_id = (char *)sqlite3_column_text(statement, 0);
  5213. if (_id == NULL) {
  5214. _id = "";
  5215. }
  5216. ret = [NSString stringWithFormat:@"%s",_id];
  5217. }
  5218. sqlite3_finalize(statement);
  5219. }
  5220. [iSalesDB close_db:db];
  5221. return ret;
  5222. }
  5223. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5224. NSString *name = nil;
  5225. codeId = [self translateSingleQuote:codeId];
  5226. sqlite3 *db = [iSalesDB get_db];
  5227. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5228. sqlite3_stmt * statement;
  5229. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5230. while (sqlite3_step(statement) == SQLITE_ROW) {
  5231. char *value = (char *)sqlite3_column_text(statement, 0);
  5232. if (value == NULL) {
  5233. value = "";
  5234. }
  5235. name = [NSString stringWithUTF8String:value];
  5236. }
  5237. sqlite3_finalize(statement);
  5238. }
  5239. [iSalesDB close_db:db];
  5240. return name;
  5241. }
  5242. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5243. NSString *ret = nil;
  5244. sqlite3 *db = [iSalesDB get_db];
  5245. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5246. sqlite3_stmt * statement;
  5247. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5248. while (sqlite3_step(statement) == SQLITE_ROW) {
  5249. char *name = (char *)sqlite3_column_text(statement, 0);
  5250. if (name == NULL) {
  5251. name = "";
  5252. }
  5253. ret = [NSString stringWithUTF8String:name];
  5254. }
  5255. sqlite3_finalize(statement);
  5256. }
  5257. [iSalesDB close_db:db];
  5258. return ret;
  5259. }
  5260. + (NSString *)salesRepCodeById:(NSString *)_id {
  5261. NSString *ret = nil;
  5262. _id = [self translateSingleQuote:_id];
  5263. sqlite3 *db = [iSalesDB get_db];
  5264. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5265. sqlite3_stmt * statement;
  5266. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5267. while (sqlite3_step(statement) == SQLITE_ROW) {
  5268. char *rep = (char *)sqlite3_column_text(statement, 0);
  5269. if (rep == NULL) {
  5270. rep = "";
  5271. }
  5272. ret = [NSString stringWithUTF8String:rep];
  5273. }
  5274. sqlite3_finalize(statement);
  5275. }
  5276. [iSalesDB close_db:db];
  5277. return ret;
  5278. }
  5279. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5280. char *tx = (char *)sqlite3_column_text(stmt, col);
  5281. if (tx == NULL) {
  5282. tx = "";
  5283. }
  5284. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5285. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5286. if (!text) {
  5287. text = @"";
  5288. }
  5289. // 将字符全部为' '的字符串干掉
  5290. int spaceCount = 0;
  5291. for (int i = 0; i < text.length; i++) {
  5292. if ([text characterAtIndex:i] == ' ') {
  5293. spaceCount++;
  5294. }
  5295. }
  5296. if (spaceCount == text.length) {
  5297. text = @"";
  5298. }
  5299. return text;
  5300. }
  5301. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5302. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5303. NSData *data = [NSData dataWithContentsOfFile:path];
  5304. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5305. return ret;
  5306. }
  5307. + (NSString *)textFileName:(NSString *)name {
  5308. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5309. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5310. if (!text) {
  5311. text = @"";
  5312. }
  5313. return text;
  5314. }
  5315. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5316. return [[dic objectForKey:key] mutableCopy];
  5317. }
  5318. + (id)translateSingleQuote:(NSString *)string {
  5319. if ([string isKindOfClass:[NSString class]])
  5320. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5321. return string;
  5322. }
  5323. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5324. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5325. }
  5326. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5327. NSString* ret= nil;
  5328. NSString *sqlQuery = nil;
  5329. // 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
  5330. 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];
  5331. sqlite3_stmt * statement;
  5332. // int count=0;
  5333. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5334. {
  5335. if (sqlite3_step(statement) == SQLITE_ROW)
  5336. {
  5337. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5338. if(imgurl==nil)
  5339. imgurl="";
  5340. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5341. ret=nsimgurl;
  5342. }
  5343. sqlite3_finalize(statement);
  5344. }
  5345. else
  5346. {
  5347. [ret setValue:@"8" forKey:@"result"];
  5348. }
  5349. // [iSalesDB close_db:db];
  5350. // DebugLog(@"data string: %@",ret );
  5351. return ret;
  5352. }
  5353. #pragma mark contact Advanced search
  5354. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5355. {
  5356. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5357. return [RAConvertor dict2data:contactAdvanceDic];
  5358. }
  5359. #pragma mark create new contact
  5360. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5361. assert(params[@"user"]!=nil);
  5362. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5363. NSData *data = [NSData dataWithContentsOfFile:path];
  5364. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5365. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5366. NSString *countryCode = nil;
  5367. NSString *countryCode_id = nil;
  5368. NSString *stateCode = nil;
  5369. NSString *city = nil;
  5370. NSString *zipCode = nil;
  5371. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5372. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5373. NSString *code_id = params[@"country"];
  5374. countryCode_id = code_id;
  5375. countryCode = [self countryCodeByid:code_id];
  5376. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5377. NSString *zip_code = params[@"zipcode"];
  5378. // 剔除全部为空格
  5379. int spaceCount = 0;
  5380. for (int i = 0; i < zip_code.length; i++) {
  5381. if ([zip_code characterAtIndex:i] == ' ') {
  5382. spaceCount++;
  5383. }
  5384. }
  5385. if (spaceCount == zip_code.length) {
  5386. zip_code = @"";
  5387. }
  5388. zipCode = zip_code;
  5389. if (zipCode.length > 0) {
  5390. countryCode_id = params[@"country"];
  5391. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5392. countryCode = [dic valueForKey:@"country_code"];
  5393. if (!countryCode) {
  5394. // countryCode = @"US";
  5395. NSString *code_id = params[@"country"];
  5396. countryCode = [self countryCodeByid:code_id];
  5397. }
  5398. stateCode = [dic valueForKey:@"state_code"];
  5399. if (!stateCode.length) {
  5400. stateCode = params[@"state"];
  5401. }
  5402. city = [dic valueForKey:@"city"];
  5403. if (!city.length) {
  5404. city = params[@"city"];
  5405. }
  5406. // zip code
  5407. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5408. [zipDic setValue:zipCode forKey:@"value"];
  5409. [section_0 setValue:zipDic forKey:@"item_11"];
  5410. } else {
  5411. NSString *code_id = params[@"country"];
  5412. countryCode = [self countryCodeByid:code_id];
  5413. stateCode = params[@"state"];
  5414. city = params[@"city"];
  5415. }
  5416. }
  5417. } else {
  5418. // default: US United States
  5419. countryCode = @"US";
  5420. countryCode_id = @"228";
  5421. }
  5422. // country
  5423. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5424. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5425. // state
  5426. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5427. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5428. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5429. // city
  5430. if (city) {
  5431. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5432. [cityDic setValue:city forKey:@"value"];
  5433. [section_0 setValue:cityDic forKey:@"item_13"];
  5434. }
  5435. // price type
  5436. NSDictionary *priceDic = [self offline_getPrice];
  5437. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5438. // contact type
  5439. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5440. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5441. // Sales Rep
  5442. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5443. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5444. [ret setValue:section_0 forKey:@"section_0"];
  5445. return [RAConvertor dict2data:ret];
  5446. }
  5447. #pragma mark save
  5448. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5449. NSString *addr = nil;
  5450. NSString *contact_name = nil;
  5451. NSString *companyName = [params objectForKey:@"company"];
  5452. if (companyName) {
  5453. companyName = [AESCrypt fastencrypt:companyName];
  5454. } else {
  5455. companyName = @"";
  5456. }
  5457. DebugLog(@"company");
  5458. companyName = [self translateSingleQuote:companyName];
  5459. NSString *addr1 = [params objectForKey:@"address"];
  5460. NSString *addr2 = [params objectForKey:@"address2"];
  5461. NSString *addr3 = [params objectForKey:@"address_3"];
  5462. NSString *addr4 = [params objectForKey:@"address_4"];
  5463. if (!addr2) {
  5464. addr2 = @"";
  5465. }
  5466. if (!addr3) {
  5467. addr3 = @"";
  5468. }
  5469. if (!addr4) {
  5470. addr4 = @"";
  5471. }
  5472. if (!addr1) {
  5473. addr1 = @"";
  5474. }
  5475. DebugLog(@"addr");
  5476. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5477. addr = [AESCrypt fastencrypt:addr];
  5478. addr = [self translateSingleQuote:addr];
  5479. if (addr1 && ![addr1 isEqualToString:@""]) {
  5480. addr1 = [AESCrypt fastencrypt:addr1];
  5481. }
  5482. addr1 = [self translateSingleQuote:addr1];
  5483. addr2 = [self translateSingleQuote:addr2];
  5484. addr3 = [self translateSingleQuote:addr3];
  5485. addr4 = [self translateSingleQuote:addr4];
  5486. NSString *country = [params objectForKey:@"country"];
  5487. if (country) {
  5488. country = [self countryNameByCountryCodeId:country];
  5489. } else {
  5490. country = @"";
  5491. }
  5492. DebugLog(@"country");
  5493. country = [self translateSingleQuote:country];
  5494. NSString *state = [params objectForKey:@"state"];
  5495. if (!state) {
  5496. state = @"";
  5497. }
  5498. DebugLog(@"state");
  5499. state = [self translateSingleQuote:state];
  5500. NSString *city = [params objectForKey:@"city"];
  5501. if (!city) {
  5502. city = @"";
  5503. }
  5504. city = [self translateSingleQuote:city];
  5505. NSString *zipcode = [params objectForKey:@"zipcode"];
  5506. if (!zipcode) {
  5507. zipcode = @"";
  5508. }
  5509. DebugLog(@"zip");
  5510. zipcode = [self translateSingleQuote:zipcode];
  5511. NSString *fistName = [params objectForKey:@"firstname"];
  5512. if (!fistName) {
  5513. fistName = @"";
  5514. }
  5515. NSString *lastName = [params objectForKey:@"lastname"];
  5516. if (!lastName) {
  5517. lastName = @"";
  5518. }
  5519. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5520. DebugLog(@"contact_name");
  5521. contact_name = [self translateSingleQuote:contact_name];
  5522. fistName = [self translateSingleQuote:fistName];
  5523. lastName = [self translateSingleQuote:lastName];
  5524. NSString *phone = [params objectForKey:@"phone"];
  5525. if (phone) {
  5526. phone = [AESCrypt fastencrypt:phone];
  5527. } else {
  5528. phone = @"";
  5529. }
  5530. DebugLog(@"PHONE");
  5531. phone = [self translateSingleQuote:phone];
  5532. NSString *fax = [params objectForKey:@"fax"];
  5533. if (!fax) {
  5534. fax = @"";
  5535. }
  5536. DebugLog(@"FAX");
  5537. fax = [self translateSingleQuote:fax];
  5538. NSString *email = [params objectForKey:@"email"];
  5539. if (!email) {
  5540. email = @"";
  5541. }
  5542. DebugLog(@"EMAIL:%@",email);
  5543. email = [self translateSingleQuote:email];
  5544. NSString *notes = [params objectForKey:@"contact_notes"];
  5545. if (!notes) {
  5546. notes = @"";
  5547. }
  5548. DebugLog(@"NOTE:%@",notes);
  5549. notes = [self translateSingleQuote:notes];
  5550. NSString *price = [params objectForKey:@"price_name"];
  5551. if (price) {
  5552. price = [self priceNameByPriceId:price];
  5553. } else {
  5554. price = @"";
  5555. }
  5556. DebugLog(@"PRICE");
  5557. price = [self translateSingleQuote:price];
  5558. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5559. if (salesRep) {
  5560. salesRep = [self salesRepCodeById:salesRep];
  5561. } else {
  5562. salesRep = @"";
  5563. }
  5564. salesRep = [self translateSingleQuote:salesRep];
  5565. NSString *img = [params objectForKey:@"business_card"];
  5566. NSArray *array = [img componentsSeparatedByString:@","];
  5567. NSString *img_0 = array[0];
  5568. if (!img_0) {
  5569. img_0 = @"";
  5570. }
  5571. img_0 = [self translateSingleQuote:img_0];
  5572. NSString *img_1 = array[1];
  5573. if (!img_1) {
  5574. img_1 = @"";
  5575. }
  5576. img_1 = [self translateSingleQuote:img_1];
  5577. NSString *img_2 = array[2];
  5578. if (!img_2) {
  5579. img_2 = @"";
  5580. }
  5581. img_2 = [self translateSingleQuote:img_2];
  5582. NSString *contact_id = [NSUUID UUID].UUIDString;
  5583. NSString *contact_type = [params objectForKey:@"type_name"];
  5584. if (!contact_type) {
  5585. contact_type = @"";
  5586. }
  5587. contact_type = [self translateSingleQuote:contact_type];
  5588. // 判断更新时是否为customer
  5589. if (update) {
  5590. contact_id = [params objectForKey:@"contact_id"];
  5591. if (!contact_id) {
  5592. contact_id = @"";
  5593. }
  5594. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5595. __block int customer = 0;
  5596. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5597. customer = sqlite3_column_int(stmt, 0);
  5598. }];
  5599. isCustomer = customer ? YES : NO;
  5600. }
  5601. NSMutableDictionary *sync_dic = [params mutableCopy];
  5602. if (isCustomer) {
  5603. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5604. } else {
  5605. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5606. }
  5607. NSString *sync_data = nil;
  5608. NSString *sql = nil;
  5609. if (update){
  5610. contact_id = [params objectForKey:@"contact_id"];
  5611. if (!contact_id) {
  5612. contact_id = @"";
  5613. }
  5614. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5615. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5616. sync_data = [RAConvertor dict2string:sync_dic];
  5617. sync_data = [self translateSingleQuote:sync_data];
  5618. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  5619. } else {
  5620. contact_id = [self translateSingleQuote:contact_id];
  5621. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5622. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5623. sync_data = [RAConvertor dict2string:sync_dic];
  5624. sync_data = [self translateSingleQuote:sync_data];
  5625. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  5626. }
  5627. int result = [iSalesDB execSql:sql];
  5628. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5629. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5630. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5631. }
  5632. #pragma mark save new contact
  5633. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5634. {
  5635. return [self offline_saveContact:params update:NO isCustomer:YES];
  5636. }
  5637. #pragma mark edit contact
  5638. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5639. {
  5640. assert(params[@"user"]!=nil);
  5641. // {
  5642. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5643. // password = 123456;
  5644. // user = EvanK;
  5645. // }
  5646. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5647. NSData *data = [NSData dataWithContentsOfFile:path];
  5648. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5649. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5650. NSString *countryCode = nil;
  5651. NSString *countryCode_id = nil;
  5652. NSString *stateCode = nil;
  5653. /*------contact infor------*/
  5654. __block NSString *country = nil;
  5655. __block NSString *company_name = nil;
  5656. __block NSString *contact_id = params[@"contact_id"];
  5657. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5658. __block NSString *zipcode = nil;
  5659. __block NSString *state = nil; // state_code
  5660. __block NSString *city = nil; //
  5661. __block NSString *firt_name,*last_name;
  5662. __block NSString *phone,*fax,*email;
  5663. __block NSString *notes,*price_type,*sales_rep;
  5664. __block NSString *img_0,*img_1,*img_2;
  5665. __block NSString *contact_type;
  5666. contact_id = [self translateSingleQuote:contact_id];
  5667. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  5668. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5669. country = [self textAtColumn:0 statement:stmt]; // country name
  5670. company_name = [self textAtColumn:1 statement:stmt];
  5671. addr_1 = [self textAtColumn:2 statement:stmt];
  5672. addr_2 = [self textAtColumn:3 statement:stmt];
  5673. addr_3 = [self textAtColumn:4 statement:stmt];
  5674. addr_4 = [self textAtColumn:5 statement:stmt];
  5675. zipcode = [self textAtColumn:6 statement:stmt];
  5676. state = [self textAtColumn:7 statement:stmt]; // state code
  5677. city = [self textAtColumn:8 statement:stmt];
  5678. firt_name = [self textAtColumn:9 statement:stmt];
  5679. last_name = [self textAtColumn:10 statement:stmt];
  5680. phone = [self textAtColumn:11 statement:stmt];
  5681. fax = [self textAtColumn:12 statement:stmt];
  5682. email = [self textAtColumn:13 statement:stmt];
  5683. notes = [self textAtColumn:14 statement:stmt];
  5684. price_type = [self textAtColumn:15 statement:stmt]; // name
  5685. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5686. img_0 = [self textAtColumn:17 statement:stmt];
  5687. img_1 = [self textAtColumn:18 statement:stmt];
  5688. img_2 = [self textAtColumn:19 statement:stmt];
  5689. contact_type = [self textAtColumn:20 statement:stmt];
  5690. }];
  5691. // decrypt
  5692. if (company_name) {
  5693. company_name = [AESCrypt fastdecrypt:company_name];
  5694. }
  5695. if (addr_1) {
  5696. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5697. }
  5698. if (phone) {
  5699. phone = [AESCrypt fastdecrypt:phone];
  5700. }
  5701. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5702. countryCode = [iSalesDB jk_queryText:countrySql];
  5703. stateCode = state;
  5704. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5705. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5706. NSString *code_id = params[@"country"];
  5707. countryCode_id = code_id;
  5708. countryCode = [self countryCodeByid:code_id];
  5709. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5710. NSString *zip_code = params[@"zipcode"];
  5711. // 剔除全部为空格
  5712. int spaceCount = 0;
  5713. for (int i = 0; i < zip_code.length; i++) {
  5714. if ([zip_code characterAtIndex:i] == ' ') {
  5715. spaceCount++;
  5716. }
  5717. }
  5718. if (spaceCount == zip_code.length) {
  5719. zip_code = @"";
  5720. }
  5721. if (zipcode.length > 0) {
  5722. zipcode = zip_code;
  5723. countryCode_id = params[@"country"];
  5724. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5725. countryCode = [dic valueForKey:@"country_code"];
  5726. if (!countryCode) {
  5727. NSString *code_id = params[@"country"];
  5728. countryCode = [self countryCodeByid:code_id];
  5729. }
  5730. stateCode = [dic valueForKey:@"state_code"];
  5731. if (!stateCode.length) {
  5732. stateCode = params[@"state"];
  5733. }
  5734. city = [dic valueForKey:@"city"];
  5735. if (!city.length) {
  5736. city = params[@"city"];
  5737. }
  5738. // zip code
  5739. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5740. [zipDic setValue:zipcode forKey:@"value"];
  5741. [section_0 setValue:zipDic forKey:@"item_8"];
  5742. } else {
  5743. NSString *code_id = params[@"country"];
  5744. countryCode = [self countryCodeByid:code_id];
  5745. stateCode = params[@"state"];
  5746. city = params[@"city"];
  5747. }
  5748. }
  5749. }
  5750. // 0 Country
  5751. // 1 Company Name
  5752. // 2 Contact ID
  5753. // 3 Picture
  5754. // 4 Address 1
  5755. // 5 Address 2
  5756. // 6 Address 3
  5757. // 7 Address 4
  5758. // 8 Zip Code
  5759. // 9 State/Province
  5760. // 10 City
  5761. // 11 Contact First Name
  5762. // 12 Contact Last Name
  5763. // 13 Phone
  5764. // 14 Fax
  5765. // 15 Email
  5766. // 16 Contact Notes
  5767. // 17 Price Type
  5768. // 18 Contact Type
  5769. // 19 Sales Rep
  5770. // country
  5771. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5772. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5773. // company
  5774. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5775. // contact_id
  5776. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5777. // picture
  5778. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5779. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5780. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5781. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5782. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5783. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5784. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5785. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5786. // addr 1 2 3 4
  5787. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5788. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5789. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5790. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5791. // zip code
  5792. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5793. // state
  5794. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5795. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5796. // city
  5797. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5798. // first last
  5799. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5800. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5801. // phone fax email
  5802. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5803. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5804. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5805. // notes
  5806. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5807. // price
  5808. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5809. for (NSString *key in priceDic.allKeys) {
  5810. if ([key containsString:@"val_"]) {
  5811. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5812. if ([dic[@"value"] isEqualToString:price_type]) {
  5813. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5814. [priceDic setValue:dic forKey:key];
  5815. }
  5816. }
  5817. }
  5818. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5819. // Contact Rep
  5820. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5821. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5822. // Sales Rep
  5823. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5824. for (NSString *key in repDic.allKeys) {
  5825. if ([key containsString:@"val_"]) {
  5826. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5827. NSString *value = dic[@"value"];
  5828. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5829. if (code && [code isEqualToString:sales_rep]) {
  5830. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5831. [repDic setValue:dic forKey:key];
  5832. }
  5833. }
  5834. }
  5835. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5836. [ret setValue:section_0 forKey:@"section_0"];
  5837. return [RAConvertor dict2data:ret];
  5838. }
  5839. #pragma mark save contact
  5840. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5841. {
  5842. return [self offline_saveContact:params update:YES isCustomer:YES];
  5843. }
  5844. #pragma mark category
  5845. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5846. if (ck) {
  5847. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5848. for (NSString *key in res.allKeys) {
  5849. if (![key isEqualToString:@"count"]) {
  5850. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5851. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5852. if ([val[@"value"] isEqualToString:ck]) {
  5853. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5854. }
  5855. [res setValue:val forKey:key];
  5856. }
  5857. }
  5858. [dic setValue:res forKey:valueKey];
  5859. }
  5860. }
  5861. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5862. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5863. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5864. NSString* category = [params valueForKey:@"category"];
  5865. if (!category || [category isEqualToString:@""]) {
  5866. category = @"%";
  5867. }
  5868. category = [self translateSingleQuote:category];
  5869. int limit = [[params valueForKey:@"limit"] intValue];
  5870. int offset = [[params valueForKey:@"offset"] intValue];
  5871. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5872. NSString *limit_str = @"";
  5873. if (limited) {
  5874. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5875. }
  5876. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5877. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5878. sqlite3 *db = [iSalesDB get_db];
  5879. // [iSalesDB AddExFunction:db];
  5880. int count;
  5881. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5882. 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];
  5883. double price_min = 0;
  5884. double price_max = 0;
  5885. if ([params.allKeys containsObject:@"alert"]) {
  5886. // alert
  5887. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5888. NSString *alert = params[@"alert"];
  5889. if ([alert isEqualToString:@"Display All"]) {
  5890. alert = [NSString stringWithFormat:@""];
  5891. } else {
  5892. alert = [self translateSingleQuote:alert];
  5893. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5894. }
  5895. // available
  5896. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5897. NSString *available = params[@"available"];
  5898. NSString *available_condition;
  5899. if ([available isEqualToString:@"Display All"]) {
  5900. available_condition = @"";
  5901. } else if ([available isEqualToString:@"Available Now"]) {
  5902. available_condition = @"and availability > 0";
  5903. } else {
  5904. available_condition = @"and availability == 0";
  5905. }
  5906. // best seller
  5907. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5908. NSString *best_seller = @"";
  5909. NSString *order_best_seller = @"m.name asc";
  5910. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5911. best_seller = @"and best_seller > 0";
  5912. order_best_seller = @"m.best_seller desc,m.name asc";
  5913. }
  5914. // price
  5915. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5916. NSString *price = params[@"price"];
  5917. price_min = 0;
  5918. price_max = MAXFLOAT;
  5919. if (params[@"user"] && price != nil) {
  5920. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5921. NSMutableString *priceName = [NSMutableString string];
  5922. for (int i = 0; i < priceTypeArray.count; i++) {
  5923. NSString *pricetype = priceTypeArray[i];
  5924. pricetype = [self translateSingleQuote:pricetype];
  5925. if (i == 0) {
  5926. [priceName appendFormat:@"'%@'",pricetype];
  5927. } else {
  5928. [priceName appendFormat:@",'%@'",pricetype];
  5929. }
  5930. }
  5931. if ([price isEqualToString:@"Display All"]) {
  5932. price = [NSString stringWithFormat:@""];
  5933. } else if([price containsString:@"+"]){
  5934. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5935. price_min = [price doubleValue];
  5936. 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];
  5937. } else {
  5938. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5939. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5940. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5941. 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];
  5942. }
  5943. } else {
  5944. price = @"";
  5945. }
  5946. // sold_by_qty : Sold in quantities of %@
  5947. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5948. NSString *qty = params[@"sold_by_qty"];
  5949. if ([qty isEqualToString:@"Display All"]) {
  5950. qty = @"";
  5951. } else {
  5952. qty = [self translateSingleQuote:qty];
  5953. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5954. }
  5955. // model name;
  5956. NSString* modelname =params[@"modelName"];
  5957. if(modelname.length==0)
  5958. {
  5959. modelname=@"";
  5960. }
  5961. else
  5962. {
  5963. modelname = modelname.lowercaseString;
  5964. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5965. }
  5966. //modelDescription
  5967. NSString* modelDescription =params[@"modelDescription"];
  5968. if(modelDescription.length==0)
  5969. {
  5970. modelDescription=@"";
  5971. }
  5972. else
  5973. {
  5974. modelDescription = modelDescription.lowercaseString;
  5975. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5976. }
  5977. // cate
  5978. // category = [self translateSingleQuote:category];
  5979. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5980. // cate mutiple selection
  5981. NSString *category_id = params[@"category"];
  5982. NSMutableArray *cate_id_array = nil;
  5983. NSMutableString *cateWhere = [NSMutableString string];
  5984. if ([category_id isEqualToString:@""] || !category_id) {
  5985. [cateWhere appendString:@"1 = 1"];
  5986. } else {
  5987. if ([category_id containsString:@","]) {
  5988. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5989. } else {
  5990. cate_id_array = [@[category_id] mutableCopy];
  5991. }
  5992. [cateWhere appendString:@"("];
  5993. for (int i = 0; i < cate_id_array.count; i++) {
  5994. if (i == 0) {
  5995. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5996. } else {
  5997. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5998. }
  5999. }
  6000. [cateWhere appendString:@")"];
  6001. }
  6002. // where bestseller > 0 order by bestseller desc
  6003. // sql query: alert availability(int) best_seller(int) price qty
  6004. 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];
  6005. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6006. }
  6007. DebugLog(@"offline category where: %@",where);
  6008. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6009. if (!params[@"user"]) {
  6010. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6011. }
  6012. [ret setValue:filter forKey:@"filter"];
  6013. DebugLog(@"offline_category sql:%@",sqlQuery);
  6014. sqlite3_stmt * statement;
  6015. [ret setValue:@"2" forKey:@"result"];
  6016. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6017. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6018. // int count=0;
  6019. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6020. {
  6021. int i=0;
  6022. while (sqlite3_step(statement) == SQLITE_ROW)
  6023. {
  6024. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6025. char *name = (char*)sqlite3_column_text(statement, 0);
  6026. if(name==nil)
  6027. name="";
  6028. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6029. char *description = (char*)sqlite3_column_text(statement, 1);
  6030. if(description==nil)
  6031. description="";
  6032. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6033. int product_id = sqlite3_column_int(statement, 2);
  6034. int wid = sqlite3_column_int(statement, 3);
  6035. int closeout = sqlite3_column_int(statement, 4);
  6036. int cid = sqlite3_column_int(statement, 5);
  6037. int wisdelete = sqlite3_column_int(statement, 6);
  6038. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6039. int more_color = sqlite3_column_int(statement, 8);
  6040. // Defaul Category ID
  6041. __block NSString *categoryID = nil;
  6042. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6043. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6044. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6045. if(default_category==nil)
  6046. default_category="";
  6047. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6048. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6049. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6050. categoryID = nsdefault_category;
  6051. }];
  6052. if (!categoryID.length) {
  6053. NSString *cateIDs = params[@"category"];
  6054. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6055. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6056. for (NSString *cateID in requestCategoryArr) {
  6057. BOOL needBreak = NO;
  6058. for (NSString *itemCateIDBox in itemCategoryArr) {
  6059. if (itemCateIDBox.length > 4) {
  6060. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6061. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6062. if ([itemCategoryID isEqualToString:cateID]) {
  6063. needBreak = YES;
  6064. categoryID = itemCategoryID;
  6065. break;
  6066. }
  6067. }
  6068. }
  6069. if (needBreak) {
  6070. break;
  6071. }
  6072. }
  6073. }
  6074. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6075. if(wid !=0 && wisdelete != 1)
  6076. [item setValue:@"true" forKey:@"wish_exists"];
  6077. else
  6078. [item setValue:@"false" forKey:@"wish_exists"];
  6079. if(closeout==0)
  6080. [item setValue:@"false" forKey:@"is_closeout"];
  6081. else
  6082. [item setValue:@"true" forKey:@"is_closeout"];
  6083. if(cid==0)
  6084. [item setValue:@"false" forKey:@"cart_exists"];
  6085. else
  6086. [item setValue:@"true" forKey:@"cart_exists"];
  6087. if (more_color == 0) {
  6088. [item setObject:@(false) forKey:@"more_color"];
  6089. } else if (more_color == 1) {
  6090. [item setObject:@(true) forKey:@"more_color"];
  6091. }
  6092. [item addEntriesFromDictionary:imgjson];
  6093. // [item setValue:nsurl forKey:@"img"];
  6094. [item setValue:nsname forKey:@"name"];
  6095. [item setValue:nsdescription forKey:@"description"];
  6096. if (categoryID) {
  6097. [item setValue:categoryID forKey:@"item_category_id"];
  6098. }
  6099. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6100. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6101. i++;
  6102. }
  6103. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6104. [ret setObject:items forKey:@"items"];
  6105. sqlite3_finalize(statement);
  6106. } else {
  6107. DebugLog(@"nothing...");
  6108. }
  6109. DebugLog(@"count:%d",count);
  6110. [iSalesDB close_db:db];
  6111. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6112. return ret;
  6113. }
  6114. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6115. {
  6116. return [self categoryList:params limited:YES];
  6117. }
  6118. # pragma mark item search
  6119. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6120. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6121. // assert(params[@"order_code"]);
  6122. // params[@"user"]
  6123. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6124. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6125. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6126. // category
  6127. NSDictionary *category_menu = [self offline_category_menu];
  6128. [filter setValue:category_menu forKey:@"category"];
  6129. NSString* where= nil;
  6130. NSString* orderby= @"m.name";
  6131. if (!filterSearch) {
  6132. int covertype = [[params valueForKey:@"covertype"] intValue];
  6133. switch (covertype) {
  6134. case 0:
  6135. {
  6136. where=@"m.category like'%%#005#%%'";
  6137. break;
  6138. }
  6139. case 1:
  6140. {
  6141. where=@"m.alert like '%QS%'";
  6142. break;
  6143. }
  6144. case 2:
  6145. {
  6146. where=@"m.availability>0";
  6147. break;
  6148. }
  6149. case 3:
  6150. {
  6151. where=@"m.best_seller>0";
  6152. orderby=@"m.best_seller desc,m.name asc";
  6153. break;
  6154. }
  6155. default:
  6156. where=@"1=1";
  6157. break;
  6158. }
  6159. }
  6160. int limit = [[params valueForKey:@"limit"] intValue];
  6161. int offset = [[params valueForKey:@"offset"] intValue];
  6162. NSString *limit_str = @"";
  6163. if (limited) {
  6164. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6165. }
  6166. sqlite3 *db = [iSalesDB get_db];
  6167. // [iSalesDB AddExFunction:db];
  6168. int count;
  6169. NSString *sqlQuery = nil;
  6170. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6171. 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];
  6172. double price_min = 0;
  6173. double price_max = 0;
  6174. if (filterSearch) {
  6175. // alert
  6176. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6177. NSString *alert = params[@"alert"];
  6178. if ([alert isEqualToString:@"Display All"]) {
  6179. alert = [NSString stringWithFormat:@""];
  6180. } else {
  6181. alert = [self translateSingleQuote:alert];
  6182. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6183. }
  6184. // available
  6185. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6186. NSString *available = params[@"available"];
  6187. NSString *available_condition;
  6188. if ([available isEqualToString:@"Display All"]) {
  6189. available_condition = @"";
  6190. } else if ([available isEqualToString:@"Available Now"]) {
  6191. available_condition = @"and availability > 0";
  6192. } else {
  6193. available_condition = @"and availability == 0";
  6194. }
  6195. // best seller
  6196. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6197. NSString *best_seller = @"";
  6198. NSString *order_best_seller = @"m.name asc";
  6199. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6200. best_seller = @"and best_seller > 0";
  6201. order_best_seller = @"m.best_seller desc,m.name asc";
  6202. }
  6203. // price
  6204. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6205. NSString *price = params[@"price"];
  6206. price_min = 0;
  6207. price_max = MAXFLOAT;
  6208. if (params[@"user"]) {
  6209. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6210. NSMutableString *priceName = [NSMutableString string];
  6211. for (int i = 0; i < priceTypeArray.count; i++) {
  6212. NSString *pricetype = priceTypeArray[i];
  6213. pricetype = [self translateSingleQuote:pricetype];
  6214. if (i == 0) {
  6215. [priceName appendFormat:@"'%@'",pricetype];
  6216. } else {
  6217. [priceName appendFormat:@",'%@'",pricetype];
  6218. }
  6219. }
  6220. if ([price isEqualToString:@"Display All"]) {
  6221. price = [NSString stringWithFormat:@""];
  6222. } else if([price containsString:@"+"]){
  6223. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6224. price_min = [price doubleValue];
  6225. 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];
  6226. } else {
  6227. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6228. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6229. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6230. 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];
  6231. }
  6232. } else {
  6233. price = @"";
  6234. }
  6235. // sold_by_qty : Sold in quantities of %@
  6236. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6237. NSString *qty = params[@"sold_by_qty"];
  6238. if ([qty isEqualToString:@"Display All"]) {
  6239. qty = @"";
  6240. } else {
  6241. qty = [self translateSingleQuote:qty];
  6242. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6243. }
  6244. // model name;
  6245. NSString* modelname =params[@"modelName"];
  6246. if(modelname.length==0)
  6247. {
  6248. modelname=@"";
  6249. }
  6250. else
  6251. {
  6252. modelname = modelname.lowercaseString;
  6253. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6254. }
  6255. //modelDescription
  6256. NSString* modelDescription =params[@"modelDescription"];
  6257. if(modelDescription.length==0)
  6258. {
  6259. modelDescription=@"";
  6260. }
  6261. else
  6262. {
  6263. modelDescription = modelDescription.lowercaseString;
  6264. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6265. }
  6266. //lower(description) like'%%%@%%'
  6267. // category
  6268. NSString *category_id = params[@"ctgId"];
  6269. NSMutableArray *cate_id_array = nil;
  6270. NSMutableString *cateWhere = [NSMutableString string];
  6271. if ([category_id isEqualToString:@""] || !category_id) {
  6272. [cateWhere appendString:@"1 = 1"];
  6273. } else {
  6274. if ([category_id containsString:@","]) {
  6275. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6276. } else {
  6277. cate_id_array = [@[category_id] mutableCopy];
  6278. }
  6279. /*-----------*/
  6280. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6281. [cateWhere appendString:@"("];
  6282. for (int i = 0; i < cate_id_array.count; i++) {
  6283. for (NSString *key0 in cateDic.allKeys) {
  6284. if ([key0 containsString:@"category_"]) {
  6285. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6286. for (NSString *key1 in category0.allKeys) {
  6287. if ([key1 containsString:@"category_"]) {
  6288. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6289. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6290. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6291. cate_id_array[i] = [category1 objectForKey:@"id"];
  6292. if (i == 0) {
  6293. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6294. } else {
  6295. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6296. }
  6297. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6298. [category0 setValue:category1 forKey:key1];
  6299. [cateDic setValue:category0 forKey:key0];
  6300. }
  6301. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6302. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6303. cate_id_array[i] = [category0 objectForKey:@"id"];
  6304. if (i == 0) {
  6305. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6306. } else {
  6307. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6308. }
  6309. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6310. [cateDic setValue:category0 forKey:key0];
  6311. }
  6312. }
  6313. }
  6314. }
  6315. }
  6316. }
  6317. [cateWhere appendString:@")"];
  6318. [filter setValue:cateDic forKey:@"category"];
  6319. }
  6320. // where bestseller > 0 order by bestseller desc
  6321. // sql query: alert availability(int) best_seller(int) price qty
  6322. 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];
  6323. // count
  6324. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6325. }
  6326. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6327. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6328. if (!params[@"user"]) {
  6329. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6330. }
  6331. [ret setValue:filter forKey:@"filter"];
  6332. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6333. sqlite3_stmt * statement;
  6334. [ret setValue:@"2" forKey:@"result"];
  6335. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6336. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6337. // int count=0;
  6338. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6339. {
  6340. int i=0;
  6341. while (sqlite3_step(statement) == SQLITE_ROW)
  6342. {
  6343. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6344. char *name = (char*)sqlite3_column_text(statement, 0);
  6345. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6346. char *description = (char*)sqlite3_column_text(statement, 1);
  6347. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6348. int product_id = sqlite3_column_int(statement, 2);
  6349. int wid = sqlite3_column_int(statement, 3);
  6350. int closeout = sqlite3_column_int(statement, 4);
  6351. int cid = sqlite3_column_int(statement, 5);
  6352. int wisdelete = sqlite3_column_int(statement, 6);
  6353. int more_color = sqlite3_column_int(statement, 7);
  6354. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6355. if(wid !=0 && wisdelete != 1)
  6356. [item setValue:@"true" forKey:@"wish_exists"];
  6357. else
  6358. [item setValue:@"false" forKey:@"wish_exists"];
  6359. if(closeout==0)
  6360. [item setValue:@"false" forKey:@"is_closeout"];
  6361. else
  6362. [item setValue:@"true" forKey:@"is_closeout"];
  6363. if(cid==0)
  6364. [item setValue:@"false" forKey:@"cart_exists"];
  6365. else
  6366. [item setValue:@"true" forKey:@"cart_exists"];
  6367. if (more_color == 0) {
  6368. [item setObject:@(false) forKey:@"more_color"];
  6369. } else if (more_color == 1) {
  6370. [item setObject:@(true) forKey:@"more_color"];
  6371. }
  6372. [item addEntriesFromDictionary:imgjson];
  6373. // [item setValue:nsurl forKey:@"img"];
  6374. [item setValue:nsname forKey:@"fash_name"];
  6375. [item setValue:nsdescription forKey:@"description"];
  6376. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6377. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6378. i++;
  6379. }
  6380. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6381. [ret setObject:items forKey:@"items"];
  6382. sqlite3_finalize(statement);
  6383. }
  6384. [iSalesDB close_db:db];
  6385. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6386. return ret;
  6387. }
  6388. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6389. {
  6390. return [self itemsearch:params limited:YES];
  6391. }
  6392. #pragma mark order detail
  6393. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6394. if (str1.length == 0) {
  6395. str1 = @"&nbsp";
  6396. }
  6397. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6398. return str;
  6399. }
  6400. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6401. {
  6402. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6403. [fromformatter setDateFormat:from];
  6404. NSDate *date = [fromformatter dateFromString:datetime];
  6405. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6406. [toformatter setDateFormat:to];
  6407. NSString * ret = [toformatter stringFromDate:date];
  6408. return ret;
  6409. }
  6410. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6411. // 把毫秒去掉
  6412. if ([dateTime containsString:@"."]) {
  6413. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6414. }
  6415. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6416. formatter.dateFormat = formate;
  6417. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6418. NSDate *date = [formatter dateFromString:dateTime];
  6419. formatter.dateFormat = newFormate;
  6420. NSString *result = [formatter stringFromDate:date];
  6421. return result ? result : @"";
  6422. }
  6423. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6424. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6425. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6426. }
  6427. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6428. {
  6429. assert(params[@"mode"]!=nil);
  6430. assert(params[@"user"]!=nil);
  6431. DebugLog(@"offline oderdetail params: %@",params);
  6432. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6433. int orderId = [params[@"orderId"] intValue];
  6434. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6435. // decrypt card number and card security code
  6436. // 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 ];
  6437. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country,o.must_call from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  6438. sqlite3 *db = [iSalesDB get_db];
  6439. sqlite3_stmt * statement;
  6440. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6441. NSString *nssoid = @"";
  6442. NSString* orderinfo = @"";
  6443. NSString *moreInfo = @"";
  6444. double handlingFee = 0;
  6445. double payments_and_credist = 0;
  6446. double totalPrice = 0;
  6447. double shippingFee = 0;
  6448. double lift_gate = 0;
  6449. NSString *customer_contact = @"";
  6450. NSString *customer_email = @"";
  6451. NSString *customer_fax = @"";
  6452. NSString *customer_phone = @"";
  6453. NSString *customer_city = @"";
  6454. NSString *customer_state = @"";
  6455. NSString *customer_zipcode = @"";
  6456. NSString *customer_country = @"";
  6457. BOOL must_call = NO;
  6458. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6459. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6460. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6461. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6462. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6463. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6464. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6465. {
  6466. if (sqlite3_step(statement) == SQLITE_ROW)
  6467. {
  6468. // int order_id = sqlite3_column_int(statement, 0);
  6469. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6470. // ret[@"sign_url"] = sign_url;
  6471. section_1 = @{
  6472. @"data":sign_url,
  6473. @"title":@"Signature",
  6474. @"type":@"sign_url"
  6475. }.mutableCopy;
  6476. [ret setObject:section_1 forKey:@"section_1"];
  6477. customer_contact = [self textAtColumn:52 statement:statement];
  6478. customer_email = [self textAtColumn:53 statement:statement];
  6479. customer_phone = [self textAtColumn:54 statement:statement];
  6480. customer_fax = [self textAtColumn:55 statement:statement];
  6481. customer_city = [self textAtColumn:60 statement:statement];
  6482. customer_state = [self textAtColumn:61 statement:statement];
  6483. customer_zipcode = [self textAtColumn:62 statement:statement];
  6484. customer_country = [self textAtColumn:63 statement:statement];
  6485. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6486. int offline_edit=sqlite3_column_int(statement, 56);
  6487. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6488. char *soid = (char*)sqlite3_column_text(statement, 1);
  6489. if(soid==nil)
  6490. soid= "";
  6491. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6492. // so#
  6493. ret[@"so#"] = nssoid;
  6494. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6495. if(poNumber==nil)
  6496. poNumber= "";
  6497. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6498. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6499. if(create_time==nil)
  6500. create_time= "";
  6501. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6502. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6503. int status = sqlite3_column_int(statement, 4);
  6504. int erpStatus = sqlite3_column_int(statement, 49);
  6505. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6506. // status
  6507. if (status > 1 && status != 3) {
  6508. status = erpStatus;
  6509. } else if (status == 3) {
  6510. status = 15;
  6511. }
  6512. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6513. ret[@"order_status"] = nsstatus;
  6514. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6515. if(company_name==nil)
  6516. company_name= "";
  6517. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6518. // company name
  6519. ret[@"company_name"] = nscompany_name;
  6520. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6521. if(customer_contact==nil)
  6522. customer_contact= "";
  6523. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6524. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6525. if(addr_1==nil)
  6526. addr_1="";
  6527. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6528. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6529. if(addr_2==nil)
  6530. addr_2="";
  6531. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6532. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6533. if(addr_3==nil)
  6534. addr_3="";
  6535. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6536. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6537. if(addr_4==nil)
  6538. addr_4="";
  6539. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6540. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6541. [arr_addr addObject:nsaddr_1];
  6542. [arr_addr addObject:nsaddr_2];
  6543. [arr_addr addObject:nsaddr_3];
  6544. [arr_addr addObject:nsaddr_4];
  6545. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6546. [arr_addr addObject:customer_state];
  6547. [arr_addr addObject:customer_zipcode];
  6548. [arr_addr addObject:customer_country];
  6549. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6550. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6551. char *logist = (char*)sqlite3_column_text(statement, 11);
  6552. if(logist==nil)
  6553. logist= "";
  6554. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6555. if (/*status == -11 || */status == 10 || status == 11) {
  6556. nslogist = [self textAtColumn:59 statement:statement];
  6557. };
  6558. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6559. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6560. shipping = @"Shipping To Be Quoted";
  6561. } else {
  6562. shippingFee = sqlite3_column_double(statement, 12);
  6563. }
  6564. // Shipping
  6565. // ret[@"Shipping"] = shipping;
  6566. NSDictionary *shipping_item = @{
  6567. @"title":@"Shipping",
  6568. @"value":shipping
  6569. };
  6570. [price_data setObject:shipping_item forKey:@"item_1"];
  6571. int have_lift_gate = sqlite3_column_int(statement, 17);
  6572. lift_gate = sqlite3_column_double(statement, 13);
  6573. // Liftgate Fee(No loading dock)
  6574. if (!have_lift_gate) {
  6575. lift_gate = 0;
  6576. }
  6577. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6578. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6579. if (sqlite3_column_int(statement, 57)) {
  6580. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6581. liftgate_value = @"Shipping To Be Quoted";
  6582. }
  6583. NSDictionary *liftgate_item = @{
  6584. @"title":@"Liftgate Fee(No loading dock)",
  6585. @"value":liftgate_value
  6586. };
  6587. [price_data setObject:liftgate_item forKey:@"item_2"];
  6588. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6589. // [ret removeObjectForKey:@"Shipping"];
  6590. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6591. // }
  6592. //
  6593. // if (have_lift_gate) {
  6594. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6595. // }
  6596. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6597. if(general_notes==nil)
  6598. general_notes= "";
  6599. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6600. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6601. if(internal_notes==nil)
  6602. internal_notes= "";
  6603. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6604. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6605. if(payment_type==nil)
  6606. payment_type= "";
  6607. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6608. // order info
  6609. orderinfo = [self textFileName:@"order_info.html"];
  6610. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6611. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6612. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6613. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6614. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6615. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6616. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6617. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6618. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6619. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6620. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6621. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6622. NSString *payment = nil;
  6623. // id -> show
  6624. __block NSString *show_pay_type = nspayment_type;
  6625. [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) {
  6626. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6627. if (show_typ_ch != NULL) {
  6628. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6629. }
  6630. }];
  6631. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6632. {
  6633. payment = [self textFileName:@"creditcardpayment.html"];
  6634. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6635. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6636. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6637. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6638. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6639. NSString *card_type = [self textAtColumn:42 statement:statement];
  6640. if (card_type.length > 0) { // 显示星号
  6641. card_type = @"****";
  6642. }
  6643. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6644. if (card_number.length > 0 && card_number.length > 4) {
  6645. for (int i = 0; i < card_number.length - 4; i++) {
  6646. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6647. }
  6648. } else {
  6649. card_number = @"";
  6650. }
  6651. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6652. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6653. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6654. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6655. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6656. card_expiration = @"****";
  6657. }
  6658. NSString *card_city = [self textAtColumn:46 statement:statement];
  6659. NSString *card_state = [self textAtColumn:47 statement:statement];
  6660. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6661. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6662. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6663. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6664. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6665. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6666. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6667. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6668. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6669. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6670. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6671. }
  6672. else
  6673. {
  6674. payment=[self textFileName:@"normalpayment.html"];
  6675. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6676. }
  6677. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6678. ret[@"result"]= [NSNumber numberWithInt:2];
  6679. // more info
  6680. moreInfo = [self textFileName:@"more_info.html"];
  6681. /*****ship to******/
  6682. // ShipToCompany_or_&nbsp
  6683. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6684. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6685. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6686. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6687. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6688. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6689. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6690. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6691. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6692. /*****ship from******/
  6693. // ShipFromCompany_or_&nbsp
  6694. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6695. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6696. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6697. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6698. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6699. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6700. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6701. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6702. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6703. /*****freight to******/
  6704. // FreightBillToCompany_or_&nbsp
  6705. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6706. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6707. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6708. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6709. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6710. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6711. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6712. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6713. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6714. /*****merchandise to******/
  6715. // MerchandiseBillToCompany_or_&nbsp
  6716. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6717. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6718. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6719. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6720. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6721. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6722. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6723. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6724. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6725. /*****return to******/
  6726. // ReturnToCompany_or_&nbsp
  6727. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6728. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6729. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6730. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6731. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6732. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6733. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6734. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6735. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6736. //
  6737. // DebugLog(@"more info : %@",moreInfo);
  6738. // handling fee
  6739. handlingFee = sqlite3_column_double(statement, 33);
  6740. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6741. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6742. if (sqlite3_column_int(statement, 58)) {
  6743. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6744. handling_fee_value = @"Shipping To Be Quoted";
  6745. }
  6746. NSDictionary *handling_fee_item = @{
  6747. @"title":@"Handling Fee",
  6748. @"value":handling_fee_value
  6749. };
  6750. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6751. //
  6752. // customer info
  6753. customerID = [self textAtColumn:36 statement:statement];
  6754. // mode
  6755. ret[@"mode"] = params[@"mode"];
  6756. // model_count
  6757. ret[@"model_count"] = @(0);
  6758. }
  6759. sqlite3_finalize(statement);
  6760. }
  6761. [iSalesDB close_db:db];
  6762. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6763. // "customer_email" = "Shui Hu";
  6764. // "customer_fax" = "";
  6765. // "customer_first_name" = F;
  6766. // "customer_last_name" = L;
  6767. // "customer_name" = ",da He Xiang Dong Liu A";
  6768. // "customer_phone" = "Hey Xuan Feng";
  6769. contactInfo[@"customer_phone"] = customer_phone;
  6770. contactInfo[@"customer_fax"] = customer_fax;
  6771. contactInfo[@"customer_email"] = customer_email;
  6772. NSString *first_name = @"";
  6773. NSString *last_name = @"";
  6774. if ([customer_contact isEqualToString:@""]) {
  6775. } else if ([customer_contact containsString:@" "]) {
  6776. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6777. first_name = [customer_contact substringToIndex:first_space_index];
  6778. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6779. }
  6780. contactInfo[@"customer_first_name"] = first_name;
  6781. contactInfo[@"customer_last_name"] = last_name;
  6782. ret[@"customerInfo"] = contactInfo;
  6783. // models
  6784. if (nssoid) {
  6785. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6786. __block double TotalCuft = 0;
  6787. __block double TotalWeight = 0;
  6788. __block int TotalCarton = 0;
  6789. __block double allItemPrice = 0;
  6790. 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];
  6791. sqlite3 *db1 = [iSalesDB get_db];
  6792. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6793. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6794. int product_id = sqlite3_column_int(stmt, 0);
  6795. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6796. int item_id = sqlite3_column_int(stmt, 7);
  6797. NSString* Price=nil;
  6798. if(str_price==nil)
  6799. {
  6800. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6801. if(price==nil)
  6802. Price=@"No Price.";
  6803. else
  6804. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6805. }
  6806. else
  6807. {
  6808. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6809. }
  6810. double discount = sqlite3_column_double(stmt, 2);
  6811. int item_count = sqlite3_column_int(stmt, 3);
  6812. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6813. NSString *nsline_note=nil;
  6814. if(line_note!=nil)
  6815. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6816. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6817. // NSString *nsname = nil;
  6818. // if(name!=nil)
  6819. // nsname= [[NSString alloc]initWithUTF8String:name];
  6820. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6821. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6822. // NSString *nsdescription=nil;
  6823. // if(description!=nil)
  6824. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6825. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6826. // int stockUom = sqlite3_column_int(stmt, 8);
  6827. // int _id = sqlite3_column_int(stmt, 9);
  6828. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6829. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6830. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6831. int carton=[bsubtotaljson[@"carton"] intValue];
  6832. TotalCuft += cuft;
  6833. TotalWeight += weight;
  6834. TotalCarton += carton;
  6835. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6836. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6837. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6838. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6839. itemjson[@"note"]=nsline_note;
  6840. itemjson[@"origin_price"] = Price;
  6841. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6842. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6843. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6844. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6845. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6846. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6847. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6848. if(itemjson[@"combine"] != nil)
  6849. {
  6850. // int citem=0;
  6851. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6852. for(int bc=0;bc<bcount;bc++)
  6853. {
  6854. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6855. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6856. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6857. subTotal += uprice * modulus * item_count;
  6858. }
  6859. }
  6860. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6861. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6862. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6863. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6864. itemjson[@"type"] = @"order_item";
  6865. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6866. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6867. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6868. allItemPrice += subTotal;
  6869. }];
  6870. section_3[@"data"] = model_data;
  6871. section_3[@"type"] = @"sub_order";
  6872. section_3[@"title"] = @"Models";
  6873. section_3[@"switch"] = @(false);
  6874. ret[@"section_3"] = section_3;
  6875. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6876. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6877. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6878. // payments/Credits
  6879. // payments_and_credist = sqlite3_column_double(statement, 34);
  6880. payments_and_credist = allItemPrice;
  6881. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6882. NSDictionary *sub_total_item = @{
  6883. @"title":@"Sub-Total",
  6884. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6885. };
  6886. [price_data setObject:sub_total_item forKey:@"item_0"];
  6887. // // total
  6888. // totalPrice = sqlite3_column_double(statement, 35);
  6889. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6890. } else {
  6891. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6892. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6893. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6894. // payments/Credits
  6895. payments_and_credist = 0;
  6896. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6897. NSDictionary *sub_total_item = @{
  6898. @"title":@"Sub-Total",
  6899. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6900. };
  6901. [price_data setObject:sub_total_item forKey:@"item_0"];
  6902. }
  6903. // total
  6904. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6905. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6906. NSDictionary *total_item = @{
  6907. @"title":@"Total",
  6908. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6909. };
  6910. [price_data setObject:total_item forKey:@"item_4"];
  6911. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6912. section_4 = @{
  6913. @"data":price_data,
  6914. @"title":@"Price Info",
  6915. @"type":@"price_info"
  6916. }.mutableCopy;
  6917. ret[@"section_4"] = section_4;
  6918. // ret[@"order_info"]= orderinfo;
  6919. section_0 = @{
  6920. @"data":orderinfo,
  6921. @"title":@"Order Info",
  6922. @"type":@"order_info"
  6923. }.mutableCopy;
  6924. ret[@"section_0"] = section_0;
  6925. // ret[@"more_order_info"] = moreInfo;
  6926. section_2 = @{
  6927. @"data":moreInfo,
  6928. @"title":@"More Info",
  6929. @"type":@"more_order_info"
  6930. }.mutableCopy;
  6931. ret[@"section_2"] = section_2;
  6932. ret[@"count"] = @(5);
  6933. return [RAConvertor dict2data:ret];
  6934. }
  6935. #pragma mark order list
  6936. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6937. NSString *nsstatus = @"";
  6938. switch (status) {
  6939. case 0:
  6940. {
  6941. nsstatus=@"Temp Order";
  6942. break;
  6943. }
  6944. case 1:
  6945. {
  6946. nsstatus=@"Saved Order";
  6947. break;
  6948. }
  6949. case 2: {
  6950. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6951. break;
  6952. }
  6953. case 3:
  6954. case 15:
  6955. {
  6956. nsstatus=@"Cancelled";
  6957. break;
  6958. }
  6959. case 10:
  6960. {
  6961. nsstatus=@"Quote Submitted";
  6962. break;
  6963. }
  6964. case 11:
  6965. {
  6966. nsstatus=@"Sales Order Submitted";
  6967. break;
  6968. }
  6969. case 12:
  6970. {
  6971. nsstatus=@"Processing";
  6972. break;
  6973. }
  6974. case 13:
  6975. {
  6976. nsstatus=@"Shipped";
  6977. break;
  6978. }
  6979. case 14:
  6980. {
  6981. nsstatus=@"Closed";
  6982. break;
  6983. }
  6984. case -11:
  6985. {
  6986. nsstatus = @"Ready For Submit";
  6987. break;
  6988. }
  6989. default:
  6990. break;
  6991. }
  6992. return nsstatus;
  6993. }
  6994. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  6995. {
  6996. double total = 0;
  6997. __block double payments_and_credist = 0;
  6998. __block double allItemPrice = 0;
  6999. // sqlite3 *db1 = [iSalesDB get_db];
  7000. if (so_id) {
  7001. // 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];
  7002. 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];
  7003. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7004. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7005. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7006. int product_id = sqlite3_column_int(stmt, 0);
  7007. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7008. int discount = sqlite3_column_int(stmt, 2);
  7009. int item_count = sqlite3_column_int(stmt, 3);
  7010. // int item_id = sqlite3_column_int(stmt, 7);
  7011. int item_id = sqlite3_column_int(stmt, 4);
  7012. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7013. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7014. NSString* Price=nil;
  7015. if(str_price==nil)
  7016. {
  7017. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7018. if(price==nil)
  7019. Price=@"No Price.";
  7020. else
  7021. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7022. }
  7023. else
  7024. {
  7025. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7026. }
  7027. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7028. itemjson[@"The unit price"]=Price;
  7029. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7030. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7031. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7032. if(itemjson[@"combine"] != nil)
  7033. {
  7034. // int citem=0;
  7035. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7036. for(int bc=0;bc<bcount;bc++)
  7037. {
  7038. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7039. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7040. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7041. subTotal += uprice * modulus * item_count;
  7042. }
  7043. }
  7044. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7045. allItemPrice += subTotal;
  7046. }];
  7047. payments_and_credist = allItemPrice;
  7048. } else {
  7049. // payments/Credits
  7050. payments_and_credist = 0;
  7051. }
  7052. // lift_gate handlingFee shippingFee
  7053. __block double lift_gate = 0;
  7054. __block double handlingFee = 0;
  7055. __block double shippingFee = 0;
  7056. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7057. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7058. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7059. if (have_lift_gate) {
  7060. lift_gate = sqlite3_column_double(stmt, 1);
  7061. }
  7062. handlingFee = sqlite3_column_double(stmt, 2);
  7063. shippingFee = sqlite3_column_double(stmt, 3);
  7064. }];
  7065. // total
  7066. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7067. if (close) {
  7068. [iSalesDB close_db:db1];
  7069. }
  7070. return total;
  7071. }
  7072. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7073. {
  7074. assert(params[@"user"]!=nil);
  7075. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7076. int limit = [[params valueForKey:@"limit"] intValue];
  7077. int offset = [[params valueForKey:@"offset"] intValue];
  7078. NSString* keyword = [params valueForKey:@"keyWord"];
  7079. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7080. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7081. NSString* where=@"1 = 1";
  7082. if(keyword.length>0)
  7083. 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]];
  7084. if (orderStatus.length > 0) {
  7085. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7086. if (order_status_array.count == 1) {
  7087. int status_value = [[order_status_array firstObject] intValue];
  7088. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7089. if (status_value == 15 || status_value == 3) {
  7090. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7091. } else {
  7092. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7093. }
  7094. } else {
  7095. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7096. }
  7097. } else if (order_status_array.count > 1) {
  7098. for (int i = 0; i < order_status_array.count;i++) {
  7099. NSString *status = order_status_array[i];
  7100. NSString *condition = @" or";
  7101. if (i == 0) {
  7102. condition = @" and (";
  7103. }
  7104. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7105. int status_value = [status intValue];
  7106. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7107. if (status_value == 15 || status_value == 3) {
  7108. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7109. } else {
  7110. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7111. }
  7112. } else {
  7113. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7114. }
  7115. }
  7116. where = [where stringByAppendingString:@" )"];
  7117. }
  7118. }
  7119. 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];
  7120. // DebugLog(@"order list sql: %@",sqlQuery);
  7121. sqlite3 *db = [iSalesDB get_db];
  7122. sqlite3_stmt * statement;
  7123. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7124. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7125. {
  7126. int count=0;
  7127. while (sqlite3_step(statement) == SQLITE_ROW)
  7128. {
  7129. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7130. int order_id = sqlite3_column_double(statement, 0);
  7131. char *soid = (char*)sqlite3_column_text(statement, 1);
  7132. if(soid==nil)
  7133. soid= "";
  7134. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7135. int status = sqlite3_column_double(statement, 2);
  7136. int erpStatus = sqlite3_column_double(statement, 9);
  7137. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7138. if(sales_rep==nil)
  7139. sales_rep= "";
  7140. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7141. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7142. if(create_by==nil)
  7143. create_by= "";
  7144. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7145. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7146. if(company_name==nil)
  7147. company_name= "";
  7148. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7149. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7150. if(create_time==nil)
  7151. create_time= "";
  7152. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7153. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7154. // double total_price = sqlite3_column_double(statement, 7);
  7155. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7156. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7157. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7158. int offline_edit = sqlite3_column_int(statement, 10);
  7159. // ": "JH",
  7160. // "": "$8307.00",
  7161. // "": "MOB1608050001",
  7162. // "": "ArpithaT",
  7163. // "": "1st Stage Property Transformations",
  7164. // "": "JANICE SUTTON",
  7165. // "": 2255,
  7166. // "": "08/02/2016 09:49:18",
  7167. // "": 1,
  7168. // "": "Saved Order"
  7169. // "": "1470384050483",
  7170. // "model_count": "6 / 28"
  7171. item[@"sales_rep"]= nssales_rep;
  7172. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7173. item[@"so#"]= nssoid;
  7174. item[@"create_by"]= nscreate_by;
  7175. item[@"customer_name"]= nscompany_name;
  7176. item[@"customer_contact"] = customer_contact;
  7177. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7178. item[@"purchase_time"]= nscreate_time;
  7179. int statusCode = status;
  7180. if (statusCode == 2) {
  7181. statusCode = erpStatus;
  7182. } else if (statusCode == 3) {
  7183. statusCode = 15;
  7184. }
  7185. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7186. item[@"order_status"]= nsstatus;
  7187. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7188. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7189. // item[@"model_count"]
  7190. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7191. count++;
  7192. }
  7193. ret[@"count"]= [NSNumber numberWithInt:count];
  7194. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7195. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7196. ret[@"result"]= [NSNumber numberWithInt:2];
  7197. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7198. ret[@"time_zone"] = @"PST";
  7199. sqlite3_finalize(statement);
  7200. }
  7201. 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];
  7202. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7203. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7204. [iSalesDB close_db:db];
  7205. return [RAConvertor dict2data:ret];
  7206. }
  7207. #pragma mark update gnotes
  7208. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7209. {
  7210. DebugLog(@"params: %@",params);
  7211. // comments = Meyoyoyoyoyoyoy;
  7212. // orderCode = MOB1608110001;
  7213. // password = 123456;
  7214. // user = EvanK;
  7215. 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]];
  7216. int ret = [iSalesDB execSql:sql];
  7217. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7218. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7219. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7220. // [dic setValue:@"160409" forKey:@"min_ver"];
  7221. return [RAConvertor dict2data:dic];
  7222. }
  7223. #pragma mark move to wishlist
  7224. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7225. {
  7226. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7227. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7228. _id = [NSString stringWithFormat:@"(%@)",_id];
  7229. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7230. sqlite3 *db = [iSalesDB get_db];
  7231. __block NSString *product_id = @"";
  7232. __block NSString *item_count_str = @"";
  7233. // __block NSString *item_id = nil;
  7234. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7235. // product_id = [self textAtColumn:0 statement:stmt];
  7236. int item_count = sqlite3_column_int(stmt, 1);
  7237. // item_id = [self textAtColumn:2 statement:stmt];
  7238. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7239. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7240. if (p_id.length) {
  7241. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7242. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7243. }
  7244. }];
  7245. [iSalesDB close_db:db];
  7246. // 去除第一个,
  7247. if (product_id.length > 1) {
  7248. product_id = [product_id substringFromIndex:1];
  7249. }
  7250. if (item_count_str.length > 1) {
  7251. item_count_str = [item_count_str substringFromIndex:1];
  7252. }
  7253. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7254. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7255. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7256. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7257. sqlite3 *db1 = [iSalesDB get_db];
  7258. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7259. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7260. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7261. // 删除
  7262. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7263. int ret = [iSalesDB execSql:deleteSql db:db1];
  7264. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7265. [iSalesDB close_db:db1];
  7266. return [RAConvertor dict2data:dic];
  7267. }
  7268. #pragma mark cart delete
  7269. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7270. {
  7271. // cartItemId = 548;
  7272. // orderCode = MOB1608110001;
  7273. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7274. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7275. _id = [NSString stringWithFormat:@"(%@)",_id];
  7276. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7277. int ret = [iSalesDB execSql:sql];
  7278. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7279. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7280. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7281. // [dic setValue:@"160409" forKey:@"min_ver"];
  7282. return [RAConvertor dict2data:dic];
  7283. }
  7284. #pragma mark set price
  7285. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7286. {
  7287. DebugLog(@"cart set price params: %@",params);
  7288. // "cartitem_id" = 1;
  7289. // discount = "0.000000";
  7290. // "item_note" = "";
  7291. // price = "269.000000";
  7292. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7293. NSString *notes = [self valueInParams:params key:@"item_note"];
  7294. NSString *discount = [self valueInParams:params key:@"discount"];
  7295. NSString *price = [self valueInParams:params key:@"price"];
  7296. // bool badd_price_changed=false;
  7297. // sqlite3* db=[iSalesDB get_db];
  7298. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7299. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7300. // NSRange range;
  7301. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7302. //
  7303. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7304. // badd_price_changed=true;
  7305. // [iSalesDB close_db:db];
  7306. //
  7307. // if(badd_price_changed)
  7308. // {
  7309. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7310. // }
  7311. //
  7312. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7313. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7314. int ret = [iSalesDB execSql:sql];
  7315. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7316. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7317. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7318. // [dic setValue:@"160409" forKey:@"min_ver"];
  7319. return [RAConvertor dict2data:dic];
  7320. }
  7321. #pragma mark set line notes
  7322. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7323. {
  7324. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7325. NSString *notes = [self valueInParams:params key:@"notes"];
  7326. notes = [self translateSingleQuote:notes];
  7327. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7328. int ret = [iSalesDB execSql:sql];
  7329. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7330. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7331. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7332. // [dic setValue:@"160409" forKey:@"min_ver"];
  7333. return [RAConvertor dict2data:dic];
  7334. }
  7335. #pragma mark set qty
  7336. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7337. {
  7338. assert(params[@"can_create_backorder"]!=nil);
  7339. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7340. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7341. int item_count = [params[@"inputInt"] intValue];
  7342. // 购买检查数量大于库存
  7343. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7344. if (!params[@"can_create_backorder"]) {
  7345. 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];
  7346. __block BOOL out_of_stock = NO;
  7347. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7348. int availability = sqlite3_column_int(stmt, 0);
  7349. if (availability < item_count) {
  7350. out_of_stock = YES;
  7351. }
  7352. }];
  7353. if (out_of_stock) { // 缺货
  7354. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7355. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7356. return [RAConvertor dict2data:dic];
  7357. }
  7358. }
  7359. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7360. int ret = [iSalesDB execSql:sql];
  7361. __block int item_id = 0;
  7362. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7363. item_id = sqlite3_column_int(stmt, 0);
  7364. }];
  7365. sqlite3 *db = [iSalesDB get_db];
  7366. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7367. [iSalesDB close_db:db];
  7368. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7369. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7370. // [dic setValue:@"160409" forKey:@"min_ver"];
  7371. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7372. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7373. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7374. return [RAConvertor dict2data:dic];
  7375. }
  7376. #pragma mark place order
  7377. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7378. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7379. if (aname) {
  7380. [dic setValue:aname forKey:@"aname"];
  7381. }
  7382. if (control) {
  7383. [dic setValue:control forKey:@"control"];
  7384. }
  7385. if (keyboard) {
  7386. [dic setValue:keyboard forKey:@"keyboard"];
  7387. }
  7388. if (name) {
  7389. [dic setValue:name forKey:@"name"];
  7390. }
  7391. if (value) {
  7392. [dic setValue:value forKey:@"value"];
  7393. }
  7394. return dic;
  7395. }
  7396. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7397. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7398. orderCode = [self translateSingleQuote:orderCode];
  7399. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7400. 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];
  7401. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7402. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7403. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7404. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7405. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7406. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7407. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7408. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7409. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7410. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7411. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7412. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7413. NSString *email = [self textAtColumn:11 statement:stmt];
  7414. NSString *phone = [self textAtColumn:12 statement:stmt];
  7415. NSString *fax = [self textAtColumn:13 statement:stmt];
  7416. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7417. NSString *city = [self textAtColumn:15 statement:stmt];
  7418. NSString *state = [self textAtColumn:16 statement:stmt];
  7419. NSString *country = [self textAtColumn:17 statement:stmt];
  7420. NSString *name = [self textAtColumn:18 statement:stmt];
  7421. // contact id
  7422. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7423. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7424. [contact_id_dic setValue:@"text" forKey:@"control"];
  7425. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7426. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7427. [contact_id_dic setValue:@"true" forKey:@"required"];
  7428. [contact_id_dic setValue:contact_id forKey:@"value"];
  7429. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7430. // business card
  7431. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7432. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7433. [business_card_dic setValue:@"img" forKey:@"control"];
  7434. [business_card_dic setValue:@"1" forKey:@"disable"];
  7435. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7436. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7437. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7438. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7439. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7440. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7441. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7442. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7443. // fax
  7444. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7445. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7446. // zipcode
  7447. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7448. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7449. // city
  7450. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7451. [customer_dic setValue:city_dic forKey:@"item_12"];
  7452. // state
  7453. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7454. [customer_dic setValue:state_dic forKey:@"item_13"];
  7455. // country
  7456. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7457. [customer_dic setValue:country_dic forKey:@"item_14"];
  7458. // company name
  7459. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7460. [customer_dic setValue:company_dic forKey:@"item_2"];
  7461. // addr_1
  7462. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7463. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7464. // addr_2
  7465. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7466. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7467. // addr_3
  7468. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7469. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7470. // addr_4
  7471. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7472. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7473. // Contact
  7474. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7475. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7476. // email
  7477. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7478. [customer_dic setValue:email_dic forKey:@"item_8"];
  7479. // phone
  7480. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7481. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7482. // title
  7483. [customer_dic setValue:@"Customer" forKey:@"title"];
  7484. // count
  7485. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7486. }];
  7487. // setting
  7488. NSDictionary *setting = params[@"setting"];
  7489. NSNumber *hide = setting[@"CustomerHide"];
  7490. [customer_dic setValue:hide forKey:@"hide"];
  7491. return customer_dic;
  7492. }
  7493. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7494. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7495. // setting
  7496. NSDictionary *setting = params[@"setting"];
  7497. NSNumber *hide = setting[@"ShipToHide"];
  7498. [dic setValue:hide forKey:@"hide"];
  7499. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7500. orderCode = [self translateSingleQuote:orderCode];
  7501. 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];
  7502. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7503. NSString *cid = [self textAtColumn:0 statement:stmt];
  7504. NSString *name = [self textAtColumn:1 statement:stmt];
  7505. NSString *ext = [self textAtColumn:2 statement:stmt];
  7506. NSString *contact = [self textAtColumn:3 statement:stmt];
  7507. NSString *email = [self textAtColumn:4 statement:stmt];
  7508. NSString *fax = [self textAtColumn:5 statement:stmt];
  7509. NSString *phone = [self textAtColumn:6 statement:stmt];
  7510. // count
  7511. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7512. // title
  7513. [dic setValue:@"Ship To" forKey:@"title"];
  7514. // choose
  7515. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7516. [choose_dic setValue:@"choose" forKey:@"aname"];
  7517. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7518. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7519. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7520. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7521. @"receive_contact" : @"customer_contact",
  7522. @"receive_email" : @"customer_email",
  7523. @"receive_ext" : @"customer_contact_ext",
  7524. @"receive_fax" : @"customer_fax",
  7525. @"receive_name" : @"customer_name",
  7526. @"receive_phone" : @"customer_phone"},
  7527. @"refresh" : [NSNumber numberWithInteger:1],
  7528. @"type" : @"pull"};
  7529. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7530. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7531. @"name" : @"Add new address",
  7532. @"refresh" : [NSNumber numberWithInteger:1],
  7533. @"value" : @"new_addr"
  7534. };
  7535. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7536. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7537. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7538. @"receive_contact" : @"customer_contact",
  7539. @"receive_email" : @"customer_email",
  7540. @"receive_ext" : @"customer_contact_ext",
  7541. @"receive_fax" : @"customer_fax",
  7542. @"receive_name" : @"customer_name",
  7543. @"receive_phone" : @"customer_phone"},
  7544. @"name" : @"select_ship_to",
  7545. @"refresh" : [NSNumber numberWithInteger:1],
  7546. @"value" : @"Sales_Order_Ship_To"};
  7547. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7548. [dic setValue:choose_dic forKey:@"item_0"];
  7549. // contact id
  7550. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7551. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7552. [contact_id_dic setValue:@"true" forKey:@"required"];
  7553. [dic setValue:contact_id_dic forKey:@"item_1"];
  7554. // company name
  7555. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7556. [dic setValue:company_name_dic forKey:@"item_2"];
  7557. // address
  7558. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7559. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7560. if ([required integerValue]) {
  7561. [ext_dic setValue:@"true" forKey:@"required"];
  7562. }
  7563. [dic setValue:ext_dic forKey:@"item_3"];
  7564. // contact
  7565. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7566. [dic setValue:contact_dic forKey:@"item_4"];
  7567. // phone
  7568. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7569. [dic setValue:phone_dic forKey:@"item_5"];
  7570. // fax
  7571. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7572. [dic setValue:fax_dic forKey:@"item_6"];
  7573. // email
  7574. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7575. [dic setValue:email_dic forKey:@"item_7"];
  7576. }];
  7577. return dic;
  7578. }
  7579. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7580. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7581. // setting
  7582. NSDictionary *setting = params[@"setting"];
  7583. NSNumber *hide = setting[@"ShipFromHide"];
  7584. [dic setValue:hide forKey:@"hide"];
  7585. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7586. orderCode = [self translateSingleQuote:orderCode];
  7587. 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];
  7588. 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';";
  7589. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7590. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7591. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7592. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7593. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7594. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7595. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7596. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7597. if (!cid.length) {
  7598. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7599. cid = [self textAtColumn:0 statement:statment];
  7600. name = [self textAtColumn:1 statement:statment];
  7601. ext = [self textAtColumn:2 statement:statment];
  7602. contact = [self textAtColumn:3 statement:statment];
  7603. email = [self textAtColumn:4 statement:statment];
  7604. fax = [self textAtColumn:5 statement:statment];
  7605. phone = [self textAtColumn:6 statement:statment];
  7606. }];
  7607. }
  7608. // count
  7609. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7610. // title
  7611. [dic setValue:@"Ship From" forKey:@"title"];
  7612. // hide
  7613. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7614. // choose
  7615. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7616. [choose_dic setValue:@"choose" forKey:@"aname"];
  7617. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7618. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7619. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7620. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7621. @"sender_contact" : @"customer_contact",
  7622. @"sender_email" : @"customer_email",
  7623. @"sender_ext" : @"customer_contact_ext",
  7624. @"sender_fax" : @"customer_fax",
  7625. @"sender_name" : @"customer_name",
  7626. @"sender_phone" : @"customer_phone"},
  7627. @"name" : @"select_cid",
  7628. @"refresh" : [NSNumber numberWithInteger:0],
  7629. @"value" : @"Sales_Order_Ship_From"};
  7630. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7631. [dic setValue:choose_dic forKey:@"item_0"];
  7632. // contact id
  7633. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7634. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7635. [contact_id_dic setValue:@"true" forKey:@"required"];
  7636. [dic setValue:contact_id_dic forKey:@"item_1"];
  7637. // company name
  7638. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7639. [dic setValue:company_name_dic forKey:@"item_2"];
  7640. // address
  7641. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7642. [dic setValue:ext_dic forKey:@"item_3"];
  7643. // contact
  7644. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7645. [dic setValue:contact_dic forKey:@"item_4"];
  7646. // phone
  7647. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7648. [dic setValue:phone_dic forKey:@"item_5"];
  7649. // fax
  7650. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7651. [dic setValue:fax_dic forKey:@"item_6"];
  7652. // email
  7653. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7654. [dic setValue:email_dic forKey:@"item_7"];
  7655. }];
  7656. return dic;
  7657. }
  7658. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7659. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7660. // setting
  7661. NSDictionary *setting = params[@"setting"];
  7662. NSNumber *hide = setting[@"FreightBillToHide"];
  7663. [dic setValue:hide forKey:@"hide"];
  7664. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7665. orderCode = [self translateSingleQuote:orderCode];
  7666. 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];
  7667. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7668. NSString *cid = [self textAtColumn:0 statement:stmt];
  7669. NSString *name = [self textAtColumn:1 statement:stmt];
  7670. NSString *ext = [self textAtColumn:2 statement:stmt];
  7671. NSString *contact = [self textAtColumn:3 statement:stmt];
  7672. NSString *email = [self textAtColumn:4 statement:stmt];
  7673. NSString *fax = [self textAtColumn:5 statement:stmt];
  7674. NSString *phone = [self textAtColumn:6 statement:stmt];
  7675. // count
  7676. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7677. // title
  7678. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7679. // hide
  7680. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7681. // choose
  7682. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7683. [choose_dic setValue:@"choose" forKey:@"aname"];
  7684. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7685. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7686. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7687. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7688. @"shipping_billto_contact" : @"receive_contact",
  7689. @"shipping_billto_email" : @"receive_email",
  7690. @"shipping_billto_ext" : @"receive_ext",
  7691. @"shipping_billto_fax" : @"receive_fax",
  7692. @"shipping_billto_name" : @"receive_name",
  7693. @"shipping_billto_phone" : @"receive_phone"},
  7694. @"type" : @"pull"};
  7695. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7696. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7697. @"type" : @"pull",
  7698. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7699. @"shipping_billto_contact" : @"customer_contact",
  7700. @"shipping_billto_email" : @"customer_email",
  7701. @"shipping_billto_ext" : @"customer_contact_ext",
  7702. @"shipping_billto_fax" : @"customer_fax",
  7703. @"shipping_billto_name" : @"customer_name",
  7704. @"shipping_billto_phone" : @"customer_phone"}
  7705. };
  7706. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7707. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7708. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7709. @"shipping_billto_contact" : @"sender_contact",
  7710. @"shipping_billto_email" : @"sender_email",
  7711. @"shipping_billto_ext" : @"sender_ext",
  7712. @"shipping_billto_fax" : @"sender_fax",
  7713. @"shipping_billto_name" : @"sender_name",
  7714. @"shipping_billto_phone" : @"sender_phone"},
  7715. @"type" : @"pull"
  7716. };
  7717. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7718. NSDictionary *select_freight_bill_to_dic = @{
  7719. @"aname" : @"Select freight bill to",
  7720. @"name" : @"select_cid",
  7721. @"refresh" : [NSNumber numberWithInteger:0],
  7722. @"value" : @"Sales_Order_Freight_Bill_To",
  7723. @"key_map" : @{
  7724. @"shipping_billto_cid" : @"customer_cid",
  7725. @"shipping_billto_contact" : @"customer_contact",
  7726. @"shipping_billto_email" : @"customer_email",
  7727. @"shipping_billto_ext" : @"customer_contact_ext",
  7728. @"shipping_billto_fax" : @"customer_fax",
  7729. @"shipping_billto_name" : @"customer_name",
  7730. @"shipping_billto_phone" : @"customer_phone"
  7731. }
  7732. };
  7733. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7734. [dic setValue:choose_dic forKey:@"item_0"];
  7735. // contact id
  7736. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7737. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7738. [contact_id_dic setValue:@"true" forKey:@"required"];
  7739. [dic setValue:contact_id_dic forKey:@"item_1"];
  7740. // company name
  7741. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7742. [dic setValue:company_name_dic forKey:@"item_2"];
  7743. // address
  7744. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7745. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7746. if ([ext_required integerValue]) {
  7747. [ext_dic setValue:@"true" forKey:@"required"];
  7748. }
  7749. [dic setValue:ext_dic forKey:@"item_3"];
  7750. // contact
  7751. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7752. [dic setValue:contact_dic forKey:@"item_4"];
  7753. // phone
  7754. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7755. [dic setValue:phone_dic forKey:@"item_5"];
  7756. // fax
  7757. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7758. [dic setValue:fax_dic forKey:@"item_6"];
  7759. // email
  7760. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7761. [dic setValue:email_dic forKey:@"item_7"];
  7762. }];
  7763. return dic;
  7764. }
  7765. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7766. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7767. // setting
  7768. NSDictionary *setting = params[@"setting"];
  7769. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7770. [dic setValue:hide forKey:@"hide"];
  7771. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7772. orderCode = [self translateSingleQuote:orderCode];
  7773. 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];
  7774. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7775. NSString *cid = [self textAtColumn:0 statement:stmt];
  7776. NSString *name = [self textAtColumn:1 statement:stmt];
  7777. NSString *ext = [self textAtColumn:2 statement:stmt];
  7778. NSString *contact = [self textAtColumn:3 statement:stmt];
  7779. NSString *email = [self textAtColumn:4 statement:stmt];
  7780. NSString *fax = [self textAtColumn:5 statement:stmt];
  7781. NSString *phone = [self textAtColumn:6 statement:stmt];
  7782. // count
  7783. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7784. // title
  7785. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7786. // hide
  7787. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7788. // choose
  7789. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7790. [choose_dic setValue:@"choose" forKey:@"aname"];
  7791. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7792. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7793. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7794. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7795. @"billing_contact" : @"receive_contact",
  7796. @"billing_email" : @"receive_email",
  7797. @"billing_ext" : @"receive_ext",
  7798. @"billing_fax" : @"receive_fax",
  7799. @"billing_name" : @"receive_name",
  7800. @"billing_phone" : @"receive_phone"},
  7801. @"type" : @"pull"};
  7802. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7803. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7804. @"type" : @"pull",
  7805. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7806. @"billing_contact" : @"customer_contact",
  7807. @"billing_email" : @"customer_email",
  7808. @"billing_ext" : @"customer_contact_ext",
  7809. @"billing_fax" : @"customer_fax",
  7810. @"billing_name" : @"customer_name",
  7811. @"billing_phone" : @"customer_phone"}
  7812. };
  7813. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7814. NSDictionary *select_bill_to_dic = @{
  7815. @"aname" : @"Select bill to",
  7816. @"name" : @"select_cid",
  7817. @"refresh" : [NSNumber numberWithInteger:0],
  7818. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7819. @"key_map" : @{
  7820. @"billing_cid" : @"customer_cid",
  7821. @"billing_contact" : @"customer_contact",
  7822. @"billing_email" : @"customer_email",
  7823. @"billing_ext" : @"customer_contact_ext",
  7824. @"billing_fax" : @"customer_fax",
  7825. @"billing_name" : @"customer_name",
  7826. @"billing_phone" : @"customer_phone"
  7827. }
  7828. };
  7829. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7830. [dic setValue:choose_dic forKey:@"item_0"];
  7831. // contact id
  7832. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7833. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7834. [contact_id_dic setValue:@"true" forKey:@"required"];
  7835. [dic setValue:contact_id_dic forKey:@"item_1"];
  7836. // company name
  7837. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7838. [dic setValue:company_name_dic forKey:@"item_2"];
  7839. // address
  7840. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7841. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7842. if ([ext_required integerValue]) {
  7843. [ext_dic setValue:@"true" forKey:@"required"];
  7844. }
  7845. [dic setValue:ext_dic forKey:@"item_3"];
  7846. // contact
  7847. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7848. [dic setValue:contact_dic forKey:@"item_4"];
  7849. // phone
  7850. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7851. [dic setValue:phone_dic forKey:@"item_5"];
  7852. // fax
  7853. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7854. [dic setValue:fax_dic forKey:@"item_6"];
  7855. // email
  7856. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7857. [dic setValue:email_dic forKey:@"item_7"];
  7858. }];
  7859. return dic;
  7860. }
  7861. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7862. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7863. // setting
  7864. NSDictionary *setting = params[@"setting"];
  7865. NSNumber *hide = setting[@"ReturnToHide"];
  7866. [dic setValue:hide forKey:@"hide"];
  7867. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7868. orderCode = [self translateSingleQuote:orderCode];
  7869. 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];
  7870. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7871. NSString *cid = [self textAtColumn:0 statement:stmt];
  7872. NSString *name = [self textAtColumn:1 statement:stmt];
  7873. NSString *ext = [self textAtColumn:2 statement:stmt];
  7874. NSString *contact = [self textAtColumn:3 statement:stmt];
  7875. NSString *email = [self textAtColumn:4 statement:stmt];
  7876. NSString *fax = [self textAtColumn:5 statement:stmt];
  7877. NSString *phone = [self textAtColumn:6 statement:stmt];
  7878. // count
  7879. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7880. // title
  7881. [dic setValue:@"Return To" forKey:@"title"];
  7882. // hide
  7883. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7884. // choose
  7885. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7886. [choose_dic setValue:@"choose" forKey:@"aname"];
  7887. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7888. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7889. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7890. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7891. @"returnto_contact" : @"sender_contact",
  7892. @"returnto_email" : @"sender_email",
  7893. @"returnto_ext" : @"sender_ext",
  7894. @"returnto_fax" : @"sender_fax",
  7895. @"returnto_name" : @"sender_name",
  7896. @"returnto_phone" : @"sender_phone"},
  7897. @"type" : @"pull"};
  7898. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7899. NSDictionary *select_return_to_dic = @{
  7900. @"aname" : @"Select return to",
  7901. @"name" : @"select_cid",
  7902. @"refresh" : [NSNumber numberWithInteger:0],
  7903. @"value" : @"Contact_Return_To",
  7904. @"key_map" : @{
  7905. @"returnto_cid" : @"customer_cid",
  7906. @"returnto_contact" : @"customer_contact",
  7907. @"returnto_email" : @"customer_email",
  7908. @"returnto_ext" : @"customer_contact_ext",
  7909. @"returnto_fax" : @"customer_fax",
  7910. @"returnto_name" : @"customer_name",
  7911. @"returnto_phone" : @"customer_phone"
  7912. }
  7913. };
  7914. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7915. [dic setValue:choose_dic forKey:@"item_0"];
  7916. // contact id
  7917. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7918. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7919. [contact_id_dic setValue:@"true" forKey:@"required"];
  7920. [dic setValue:contact_id_dic forKey:@"item_1"];
  7921. // company name
  7922. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7923. [dic setValue:company_name_dic forKey:@"item_2"];
  7924. // address
  7925. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7926. [dic setValue:ext_dic forKey:@"item_3"];
  7927. // contact
  7928. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7929. [dic setValue:contact_dic forKey:@"item_4"];
  7930. // phone
  7931. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7932. [dic setValue:phone_dic forKey:@"item_5"];
  7933. // fax
  7934. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7935. [dic setValue:fax_dic forKey:@"item_6"];
  7936. // email
  7937. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7938. [dic setValue:email_dic forKey:@"item_7"];
  7939. }];
  7940. return dic;
  7941. }
  7942. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7943. {
  7944. assert(params[@"user"]!=nil);
  7945. assert(params[@"contact_id"]!=nil);
  7946. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7947. orderCode = [self translateSingleQuote:orderCode];
  7948. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7949. __block double TotalCuft = 0;
  7950. __block double TotalWeight = 0;
  7951. __block int TotalCarton = 0;
  7952. __block double payments = 0;
  7953. // setting
  7954. NSDictionary *setting = params[@"setting"];
  7955. NSNumber *hide = setting[@"ModelInformationHide"];
  7956. [dic setValue:hide forKey:@"hide"];
  7957. 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];
  7958. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7959. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7960. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7961. // item id
  7962. int item_id = sqlite3_column_int(stmt, 0);
  7963. // count
  7964. int item_count = sqlite3_column_int(stmt, 1);
  7965. // stockUom
  7966. int stockUom = sqlite3_column_int(stmt, 2);
  7967. // unit price
  7968. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7969. NSString* Price=nil;
  7970. if([str_price isEqualToString:@""])
  7971. {
  7972. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  7973. if(price==nil)
  7974. Price=@"No Price.";
  7975. else
  7976. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7977. }
  7978. else
  7979. {
  7980. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7981. }
  7982. // discount
  7983. double discount = sqlite3_column_double(stmt, 4);
  7984. // name
  7985. NSString *name = [self textAtColumn:5 statement:stmt];
  7986. // description
  7987. NSString *description = [self textAtColumn:6 statement:stmt];
  7988. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7989. // line note
  7990. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7991. int avaulability = sqlite3_column_int(stmt, 8);
  7992. // img
  7993. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7994. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  7995. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7996. if(combine != nil)
  7997. {
  7998. // int citem=0;
  7999. int bcount=[[combine valueForKey:@"count"] intValue];
  8000. for(int bc=0;bc<bcount;bc++)
  8001. {
  8002. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8003. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8004. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8005. subTotal += uprice * modulus * item_count;
  8006. }
  8007. }
  8008. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8009. [model_dic setValue:@"model" forKey:@"control"];
  8010. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8011. [model_dic setValue:description forKey:@"description"];
  8012. [model_dic setValue:line_note forKey:@"note"];
  8013. [model_dic setValue:img forKey:@"img_url"];
  8014. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8015. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8016. [model_dic setValue:Price forKey:@"unit_price"];
  8017. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8018. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8019. if (combine) {
  8020. [model_dic setValue:combine forKey:@"combine"];
  8021. }
  8022. // well,what under the row is the info for total
  8023. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8024. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8025. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8026. int carton=[bsubtotaljson[@"carton"] intValue];
  8027. TotalCuft += cuft;
  8028. TotalWeight += weight;
  8029. TotalCarton += carton;
  8030. payments += subTotal;
  8031. //---------------------------
  8032. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8033. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8034. }];
  8035. [dic setValue:@"Model Information" forKey:@"title"];
  8036. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8037. return dic;
  8038. }
  8039. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8040. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8041. [dic setValue:@"Remarks Content" forKey:@"title"];
  8042. // setting
  8043. NSDictionary *setting = params[@"setting"];
  8044. NSNumber *hide = setting[@"RemarksContentHide"];
  8045. [dic setValue:hide forKey:@"hide"];
  8046. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8047. orderCode = [self translateSingleQuote:orderCode];
  8048. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  8049. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8050. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8051. int mustCall = sqlite3_column_int(stmt, 1);
  8052. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8053. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8054. NSDictionary *po_dic = @{
  8055. @"aname" : @"PO#",
  8056. @"control" : @"edit",
  8057. @"keyboard" : @"text",
  8058. @"name" : @"poNumber",
  8059. @"value" : poNumber
  8060. };
  8061. NSDictionary *must_call_dic = @{
  8062. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8063. @"control" : @"switch",
  8064. @"name" : @"must_call",
  8065. @"value" : mustCall ? @"true" : @"false"
  8066. };
  8067. NSDictionary *general_notes_dic = @{
  8068. @"aname" : @"General notes",
  8069. @"control" : @"text_view",
  8070. @"keyboard" : @"text",
  8071. @"name" : @"comments",
  8072. @"value" : generalNotes
  8073. };
  8074. // NSDictionary *internal_notes_dic = @{
  8075. // @"aname" : @"Internal notes",
  8076. // @"control" : @"text_view",
  8077. // @"keyboard" : @"text",
  8078. // @"name" : @"internal_notes",
  8079. // @"value" : internalNotes
  8080. // };
  8081. [dic setValue:po_dic forKey:@"item_0"];
  8082. [dic setValue:must_call_dic forKey:@"item_1"];
  8083. [dic setValue:general_notes_dic forKey:@"item_2"];
  8084. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8085. }];
  8086. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8087. return dic;
  8088. }
  8089. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8090. // params
  8091. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8092. orderCode = [self translateSingleQuote:orderCode];
  8093. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8094. // setting
  8095. NSDictionary *setting = params[@"setting"];
  8096. NSNumber *hide = setting[@"OrderTotalHide"];
  8097. [dic setValue:hide forKey:@"hide"];
  8098. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8099. __block double lift_gate_value = 0;
  8100. __block double handling_fee = 0;
  8101. __block double shipping = 0;
  8102. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8103. int lift_gate = sqlite3_column_int(stmt, 0);
  8104. lift_gate_value = sqlite3_column_double(stmt, 1);
  8105. shipping = sqlite3_column_double(stmt, 2);
  8106. handling_fee = sqlite3_column_double(stmt, 3);
  8107. if (!lift_gate) {
  8108. lift_gate_value = 0;
  8109. }
  8110. }];
  8111. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8112. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8113. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8114. double payments = [[total valueForKey:@"payments"] doubleValue];
  8115. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8116. double totalPrice = payments;
  8117. [dic setValue:@"Order Total" forKey:@"title"];
  8118. NSDictionary *payments_dic = @{
  8119. @"align" : @"right",
  8120. @"aname" : @"Payments/Credits",
  8121. @"control" : @"text",
  8122. @"name" : @"paymentsAndCredits",
  8123. @"type" : @"price",
  8124. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8125. };
  8126. [dic setValue:payments_dic forKey:@"item_0"];
  8127. // version 1.71 remove
  8128. // NSDictionary *handling_fee_dic = @{
  8129. // @"align" : @"right",
  8130. // @"aname" : @"Handling Fee",
  8131. // @"control" : @"text",
  8132. // @"name" : @"handling_fee_value",
  8133. // @"required" : @"true",
  8134. // @"type" : @"price",
  8135. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8136. // };
  8137. NSDictionary *shipping_dic = @{
  8138. @"align" : @"right",
  8139. @"aname" : @"Shipping*",
  8140. @"control" : @"text",
  8141. @"name" : @"shipping",
  8142. @"required" : @"true",
  8143. @"type" : @"price",
  8144. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8145. };
  8146. NSDictionary *lift_gate_dic = @{
  8147. @"align" : @"right",
  8148. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8149. @"control" : @"text",
  8150. @"name" : @"lift_gate_value",
  8151. @"required" : @"true",
  8152. @"type" : @"price",
  8153. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8154. };
  8155. int item_count = 1;
  8156. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8157. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8158. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8159. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8160. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8161. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8162. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8163. } else {
  8164. }
  8165. }
  8166. // version 1.71 remove
  8167. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8168. NSDictionary *total_price_dic = @{
  8169. @"align" : @"right",
  8170. @"aname" : @"Total",
  8171. @"control" : @"text",
  8172. @"name" : @"totalPrice",
  8173. @"type" : @"price",
  8174. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8175. };
  8176. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8177. NSDictionary *total_cuft_dic = @{
  8178. @"align" : @"right",
  8179. @"aname" : @"Total Cuft",
  8180. @"control" : @"text",
  8181. @"name" : @"",
  8182. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8183. };
  8184. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8185. NSDictionary *total_weight_dic = @{
  8186. @"align" : @"right",
  8187. @"aname" : @"Total Weight",
  8188. @"control" : @"text",
  8189. @"name" : @"",
  8190. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8191. };
  8192. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8193. NSDictionary *total_carton_dic = @{
  8194. @"align" : @"right",
  8195. @"aname" : @"Total Carton",
  8196. @"control" : @"text",
  8197. @"name" : @"",
  8198. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8199. };
  8200. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8201. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8202. return dic;
  8203. }
  8204. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8205. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8206. orderCode = [self translateSingleQuote:orderCode];
  8207. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8208. // setting
  8209. NSDictionary *setting = params[@"setting"];
  8210. NSNumber *hide = setting[@"SignatureHide"];
  8211. [dic setValue:hide forKey:@"hide"];
  8212. [dic setValue:@"Signature" forKey:@"title"];
  8213. __block NSString *pic_path = @"";
  8214. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8215. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8216. pic_path = [self textAtColumn:0 statement:stmt];
  8217. }];
  8218. NSDictionary *pic_dic = @{
  8219. @"aname" : @"Signature",
  8220. @"avalue" :pic_path,
  8221. @"control" : @"signature",
  8222. @"name" : @"sign_picpath",
  8223. @"value" : pic_path
  8224. };
  8225. [dic setValue:pic_dic forKey:@"item_0"];
  8226. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8227. return dic;
  8228. }
  8229. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8230. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8231. // setting
  8232. NSDictionary *setting = params[@"setting"];
  8233. NSNumber *hide = setting[@"ShippingMethodHide"];
  8234. [dic setValue:hide forKey:@"hide"];
  8235. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8236. orderCode = [self translateSingleQuote:orderCode];
  8237. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8238. __block NSString *logist = @"";
  8239. __block NSString *lift_gate = @"";
  8240. __block int lift_gate_integer = 0;
  8241. __block NSString *logistic_note = @"";
  8242. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8243. logist = [self textAtColumn:0 statement:stmt];
  8244. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8245. logistic_note = [self textAtColumn:2 statement:stmt];
  8246. }];
  8247. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8248. [dic setValue:@"Shipping Method" forKey:@"title"];
  8249. // val_0
  8250. int PERSONAL_PICK_UP_check = 0;
  8251. int USE_MY_CARRIER_check = 0;
  8252. NSString *logistic_note_text = @"";
  8253. int will_call_check = 0;
  8254. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8255. will_call_check = 1;
  8256. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8257. PERSONAL_PICK_UP_check = 1;
  8258. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8259. USE_MY_CARRIER_check = 1;
  8260. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8261. logistic_note = [logistic_note_array firstObject];
  8262. if (logistic_note_array.count > 1) {
  8263. logistic_note_text = [logistic_note_array lastObject];
  8264. }
  8265. }
  8266. }
  8267. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8268. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8269. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8270. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8271. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8272. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8273. NSDictionary *val0_subItem_item0 = @{
  8274. @"aname" : @"Option",
  8275. @"cadedate" : @{
  8276. @"count" : [NSNumber numberWithInteger:2],
  8277. @"val_0" : @{
  8278. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8279. @"refresh" : [NSNumber numberWithInteger:0],
  8280. @"value" : @"PERSONAL PICK UP",
  8281. @"value_id" : @"PERSONAL PICK UP"
  8282. },
  8283. @"val_1" : @{
  8284. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8285. @"refresh" : [NSNumber numberWithInteger:0],
  8286. @"sub_item" : @{
  8287. @"count" : [NSNumber numberWithInteger:1],
  8288. @"item_0" : @{
  8289. @"aname" : @"BOL",
  8290. @"control" : @"edit",
  8291. @"keyboard" : @"text",
  8292. @"name" : @"logist_note_text",
  8293. @"refresh" : [NSNumber numberWithInteger:0],
  8294. @"required" : @"false",
  8295. @"value" : logistic_note_text
  8296. }
  8297. },
  8298. @"value" : @"USE MY CARRIER",
  8299. @"value_id" : @"USE MY CARRIER"
  8300. }
  8301. },
  8302. @"control" : @"enum",
  8303. @"name" : @"logistic_note",
  8304. @"required" : @"true",
  8305. @"single_select" : @"true"
  8306. };
  8307. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8308. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8309. // val_1
  8310. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8311. if([lift_gate isEqualToString:@""]) {
  8312. if (lift_gate_integer == 1) {
  8313. lift_gate = @"true";
  8314. } else {
  8315. lift_gate = @"false";
  8316. }
  8317. }
  8318. int common_carrier_check = 0;
  8319. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8320. common_carrier_check = 1;
  8321. [params setValue:lift_gate forKey:@"lift_gate"];
  8322. }
  8323. NSDictionary *val_1 = @{
  8324. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8325. @"sub_item" : @{
  8326. @"count" : [NSNumber numberWithInteger:1],
  8327. @"item_0" : @{
  8328. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8329. @"control" : @"switch",
  8330. @"name" : @"lift_gate",
  8331. @"refresh" : [NSNumber numberWithInteger:1],
  8332. @"required" : @"true",
  8333. @"value" : lift_gate
  8334. }
  8335. },
  8336. @"value" : @"COMMON CARRIER",
  8337. @"value_id" : @"COMMON CARRIER"
  8338. };
  8339. // cadedate
  8340. NSDictionary *cadedate = @{
  8341. @"count" : [NSNumber numberWithInteger:2],
  8342. @"val_0" : val_0,
  8343. @"val_1" : val_1
  8344. };
  8345. // item_0
  8346. NSMutableDictionary *item_0 = @{
  8347. @"aname" : @"Shipping",
  8348. @"cadedate" : cadedate,
  8349. @"control" : @"enum",
  8350. @"name" : @"logist",
  8351. @"refresh" : [NSNumber numberWithInteger:1],
  8352. @"single_select" : @"true",
  8353. }.mutableCopy;
  8354. NSNumber *required = setting[@"ShippingMethodRequire"];
  8355. if ([required integerValue]) {
  8356. [item_0 setValue:@"true" forKey:@"required"];
  8357. }
  8358. [dic setValue:item_0 forKey:@"item_0"];
  8359. if ([logist isEqualToString:@"WILL CALL"]) {
  8360. [dic removeObjectForKey:@"lift_gate"];
  8361. }
  8362. return dic;
  8363. }
  8364. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8365. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8366. orderCode = [self translateSingleQuote:orderCode];
  8367. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8368. __block int submit_as_integer = 0;
  8369. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8370. submit_as_integer = sqlite3_column_int(stmt, 0);
  8371. }];
  8372. int check11 = 0;
  8373. int check10 = 0;
  8374. if (submit_as_integer == 11) {
  8375. check11 = 1;
  8376. }
  8377. if (submit_as_integer == 10) {
  8378. check10 = 1;
  8379. }
  8380. // section_0
  8381. NSMutableDictionary *dic = @{
  8382. @"count" : @(1),
  8383. @"item_0" : @{
  8384. @"aname" : @"Submit Order As",
  8385. @"cadedate" : @{
  8386. @"count" : @(2),
  8387. @"val_0" : @{
  8388. @"check" : [NSNumber numberWithInteger:check11],
  8389. @"value" : @"Sales Order",
  8390. @"value_id" : @(11)
  8391. },
  8392. @"val_1" : @{
  8393. @"check" : [NSNumber numberWithInteger:check10],
  8394. @"value" : @"Quote",
  8395. @"value_id" : @(10)
  8396. }
  8397. },
  8398. @"control" : @"enum",
  8399. @"name" : @"erpOrderStatus",
  8400. @"required" : @"true",
  8401. @"single_select" : @"true"
  8402. },
  8403. @"title" : @"Order Type"
  8404. }.mutableCopy;
  8405. // setting
  8406. NSDictionary *setting = params[@"setting"];
  8407. NSNumber *hide = setting[@"OrderTypeHide"];
  8408. [dic setValue:hide forKey:@"hide"];
  8409. return dic;
  8410. }
  8411. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8412. countryCode = [self translateSingleQuote:countryCode];
  8413. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8414. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8415. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8416. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8417. if (name == NULL) {
  8418. name = "";
  8419. }
  8420. if (code == NULL) {
  8421. code = "";
  8422. }
  8423. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8424. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8425. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8426. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8427. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8428. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8429. }
  8430. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8431. [container setValue:stateDic forKey:key];
  8432. }] mutableCopy];
  8433. [ret removeObjectForKey:@"result"];
  8434. // failure 可以不用了,一样的
  8435. if (ret.allKeys.count == 0) {
  8436. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8437. [stateDic setValue:@"Other" forKey:@"value"];
  8438. [stateDic setValue:@"" forKey:@"value_id"];
  8439. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8440. if (state_code && [@"" isEqualToString:state_code]) {
  8441. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8442. }
  8443. NSString *key = [NSString stringWithFormat:@"val_0"];
  8444. [ret setValue:stateDic forKey:key];
  8445. }
  8446. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8447. return ret;
  8448. }
  8449. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8450. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8451. orderCode = [self translateSingleQuote:orderCode];
  8452. 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];
  8453. __block NSString *payType = @"";
  8454. __block NSString *firstName = @"";
  8455. __block NSString *lastName = @"";
  8456. __block NSString *addr1 = @"";
  8457. __block NSString *addr2 = @"";
  8458. __block NSString *zipcode = @"";
  8459. __block NSString *cardType = @"";
  8460. __block NSString *cardNumber = @"";
  8461. __block NSString *securityCode = @"";
  8462. __block NSString *month = @"";
  8463. __block NSString *year = @"";
  8464. __block NSString *city = @"";
  8465. __block NSString *state = @"";
  8466. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8467. payType = [self textAtColumn:0 statement:stmt];
  8468. firstName = [self textAtColumn:1 statement:stmt];
  8469. lastName = [self textAtColumn:2 statement:stmt];
  8470. addr1 = [self textAtColumn:3 statement:stmt];
  8471. addr2 = [self textAtColumn:4 statement:stmt];
  8472. zipcode = [self textAtColumn:5 statement:stmt];
  8473. cardType = [self textAtColumn:6 statement:stmt];
  8474. cardNumber = [self textAtColumn:7 statement:stmt];
  8475. securityCode = [self textAtColumn:8 statement:stmt];
  8476. month = [self textAtColumn:9 statement:stmt];
  8477. year = [self textAtColumn:10 statement:stmt];
  8478. city = [self textAtColumn:11 statement:stmt];
  8479. state = [self textAtColumn:12 statement:stmt];
  8480. }];
  8481. NSString *required = @"true";
  8482. // setting
  8483. NSDictionary *setting = params[@"setting"];
  8484. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8485. if ([requiredNumber integerValue]) {
  8486. required = @"true";
  8487. } else {
  8488. required = @"false";
  8489. }
  8490. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8491. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8492. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8493. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8494. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8495. NSString *type = [self textAtColumn:1 statement:stmt];
  8496. NSMutableDictionary *val = @{
  8497. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8498. @"value" : type,
  8499. @"value_id" : type_id
  8500. }.mutableCopy;
  8501. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8502. NSDictionary *sub_item = @{
  8503. @"count" : @(3),
  8504. @"item_0" : @{
  8505. @"aname" : @"choose",
  8506. @"control" : @"multi_action",
  8507. @"count" : @(1),
  8508. @"item_0" : @{
  8509. @"aname" : @"Same as customer",
  8510. @"key_map" : @{
  8511. @"credit_card_address1" : @"customer_address1",
  8512. @"credit_card_address2" : @"customer_address2",
  8513. @"credit_card_city" : @"customer_city",
  8514. @"credit_card_first_name" : @"customer_first_name",
  8515. @"credit_card_last_name" : @"customer_last_name",
  8516. @"credit_card_state" : @"customer_state",
  8517. @"credit_card_zipcode" : @"customer_zipcode"
  8518. },
  8519. @"type" : @"pull"
  8520. }
  8521. },
  8522. @"item_1" : @{
  8523. @"aname" : @"",
  8524. @"color" : @"red",
  8525. @"control" : @"text",
  8526. @"name" : @"",
  8527. @"value" : @"USA Credit cards only"
  8528. },
  8529. @"item_2" : @{
  8530. @"aname" : @"Fill",
  8531. @"cadedate" : @{
  8532. @"count" : @(2),
  8533. @"val_0" : @{
  8534. @"check" : @(1),
  8535. @"sub_item" : @{
  8536. @"count" : @(11),
  8537. @"item_0" : @{
  8538. @"aname" : @"Type",
  8539. @"cadedate" : @{
  8540. @"count" : @(2),
  8541. @"val_0" : @{
  8542. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8543. @"value" : @"VISA",
  8544. @"value_id" : @"VISA"/*@(0)*/
  8545. },
  8546. @"val_1" : @{
  8547. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8548. @"value" : @"MASTER CARD",
  8549. @"value_id" : @"MASTER CARD"/*@(1)*/
  8550. }
  8551. },
  8552. @"control" : @"enum",
  8553. @"name" : @"credit_card_type",
  8554. @"required" : @"true",
  8555. @"single_select" : @"true"
  8556. },
  8557. @"item_1" : @{
  8558. @"aname" : @"Number",
  8559. @"control" : @"edit",
  8560. @"keyboard" : @"int",
  8561. @"length" : @"16",
  8562. @"name" : @"credit_card_number",
  8563. @"required" : @"true",
  8564. @"value" : cardNumber
  8565. },
  8566. @"item_10" : @{
  8567. @"aname" : @"State",
  8568. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8569. @"control" : @"enum",
  8570. @"enum" : @"true",
  8571. @"name" : @"credit_card_state",
  8572. @"required" : @"true",
  8573. @"single_select" : @"true"
  8574. },
  8575. @"item_2" : @{
  8576. @"aname" : @"Expiration Date",
  8577. @"control" : @"monthpicker",
  8578. @"name" : @"credit_card_expiration",
  8579. @"required" : @"true",
  8580. @"type" : @"date",
  8581. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8582. },
  8583. @"item_3" : @{
  8584. @"aname" : @"Security Code",
  8585. @"control" : @"edit",
  8586. @"keyboard" : @"int",
  8587. @"length" : @"3",
  8588. @"name" : @"credit_card_security_code",
  8589. @"required" : @"true",
  8590. @"value" : securityCode
  8591. },
  8592. @"item_4" : @{
  8593. @"aname" : @"First Name",
  8594. @"control" : @"edit",
  8595. @"keyboard" : @"text",
  8596. @"name" : @"credit_card_first_name",
  8597. @"required" : @"true",
  8598. @"value" : firstName
  8599. },
  8600. @"item_5" : @{
  8601. @"aname" : @"Last Name",
  8602. @"control" : @"edit",
  8603. @"keyboard" : @"text",
  8604. @"name" : @"credit_card_last_name",
  8605. @"required" : @"true",
  8606. @"value" : lastName
  8607. },
  8608. @"item_6" : @{
  8609. @"aname" : @"Address 1",
  8610. @"control" : @"edit",
  8611. @"keyboard" : @"text",
  8612. @"name" : @"credit_card_address1",
  8613. @"required" : @"true",
  8614. @"value" : addr1
  8615. },
  8616. @"item_7" : @{
  8617. @"aname" : @"Address 2",
  8618. @"control" : @"edit",
  8619. @"keyboard" : @"text",
  8620. @"name" : @"credit_card_address2",
  8621. @"value" : addr2
  8622. },
  8623. @"item_8" : @{
  8624. @"aname" : @"zip code",
  8625. @"control" : @"edit",
  8626. @"keyboard" : @"text",
  8627. @"name" : @"credit_card_zipcode",
  8628. @"required" : @"true",
  8629. @"value" : zipcode
  8630. },
  8631. @"item_9" : @{
  8632. @"aname" : @"City",
  8633. @"control" : @"edit",
  8634. @"keyboard" : @"text",
  8635. @"name" : @"credit_card_city",
  8636. @"required" : @"true",
  8637. @"value" : city
  8638. }
  8639. },
  8640. @"value" : @"Fill Now",
  8641. @"value_id" : @""
  8642. },
  8643. @"val_1" : @{
  8644. @"check" : @(0),
  8645. @"value" : @"Fill Later",
  8646. @"value_id" : @""
  8647. }
  8648. },
  8649. @"control" : @"enum",
  8650. @"name" : @"",
  8651. @"single_select" : @"true"
  8652. }
  8653. };
  8654. [val setObject:sub_item forKey:@"sub_item"];
  8655. }
  8656. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8657. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8658. }];
  8659. // "section_2"
  8660. NSMutableDictionary *dic = @{
  8661. @"count" : @(1),
  8662. @"item_0" : @{
  8663. @"aname" : @"Payment",
  8664. @"required" : required,
  8665. @"cadedate" : cadedate,
  8666. // @{
  8667. // @"count" : @(6),
  8668. // @"val_3" : @{
  8669. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8670. // @"value" : @"Check",
  8671. // @"value_id" : @"Check"
  8672. // },
  8673. // @"val_2" : @{
  8674. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8675. // @"value" : @"Cash",
  8676. // @"value_id" : @"Cash"
  8677. // },
  8678. // @"val_1" : @{
  8679. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8680. // @"value" : @"NET 60",
  8681. // @"value_id" : @"NET 30"
  8682. // },
  8683. // @"val_4" : @{
  8684. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8685. // @"value" : @"Wire Transfer",
  8686. // @"value_id" : @"Wire Transfer"
  8687. // },
  8688. // @"val_0" : @{
  8689. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8690. // @"sub_item" : @{
  8691. // @"count" : @(3),
  8692. // @"item_0" : @{
  8693. // @"aname" : @"choose",
  8694. // @"control" : @"multi_action",
  8695. // @"count" : @(1),
  8696. // @"item_0" : @{
  8697. // @"aname" : @"Same as customer",
  8698. // @"key_map" : @{
  8699. // @"credit_card_address1" : @"customer_address1",
  8700. // @"credit_card_address2" : @"customer_address2",
  8701. // @"credit_card_city" : @"customer_city",
  8702. // @"credit_card_first_name" : @"customer_first_name",
  8703. // @"credit_card_last_name" : @"customer_last_name",
  8704. // @"credit_card_state" : @"customer_state",
  8705. // @"credit_card_zipcode" : @"customer_zipcode"
  8706. // },
  8707. // @"type" : @"pull"
  8708. // }
  8709. // },
  8710. // @"item_1" : @{
  8711. // @"aname" : @"",
  8712. // @"color" : @"red",
  8713. // @"control" : @"text",
  8714. // @"name" : @"",
  8715. // @"value" : @"USA Credit cards only"
  8716. // },
  8717. // @"item_2" : @{
  8718. // @"aname" : @"Fill",
  8719. // @"cadedate" : @{
  8720. // @"count" : @(2),
  8721. // @"val_0" : @{
  8722. // @"check" : @(1),
  8723. // @"sub_item" : @{
  8724. // @"count" : @(11),
  8725. // @"item_0" : @{
  8726. // @"aname" : @"Type",
  8727. // @"cadedate" : @{
  8728. // @"count" : @(2),
  8729. // @"val_0" : @{
  8730. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8731. // @"value" : @"VISA",
  8732. // @"value_id" : @(0)
  8733. // },
  8734. // @"val_1" : @{
  8735. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8736. // @"value" : @"MASTER CARD",
  8737. // @"value_id" : @(1)
  8738. // }
  8739. // },
  8740. // @"control" : @"enum",
  8741. // @"name" : @"credit_card_type",
  8742. // @"required" : @"true",
  8743. // @"single_select" : @"true"
  8744. // },
  8745. // @"item_1" : @{
  8746. // @"aname" : @"Number",
  8747. // @"control" : @"edit",
  8748. // @"keyboard" : @"int",
  8749. // @"length" : @"16",
  8750. // @"name" : @"credit_card_number",
  8751. // @"required" : @"true",
  8752. // @"value" : cardNumber
  8753. // },
  8754. // @"item_10" : @{
  8755. // @"aname" : @"State",
  8756. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8757. // @"control" : @"enum",
  8758. // @"enum" : @"true",
  8759. // @"name" : @"credit_card_state",
  8760. // @"required" : @"true",
  8761. // @"single_select" : @"true"
  8762. // },
  8763. // @"item_2" : @{
  8764. // @"aname" : @"Expiration Date",
  8765. // @"control" : @"monthpicker",
  8766. // @"name" : @"credit_card_expiration",
  8767. // @"required" : @"true",
  8768. // @"type" : @"date",
  8769. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8770. // },
  8771. // @"item_3" : @{
  8772. // @"aname" : @"Security Code",
  8773. // @"control" : @"edit",
  8774. // @"keyboard" : @"int",
  8775. // @"length" : @"3",
  8776. // @"name" : @"credit_card_security_code",
  8777. // @"required" : @"true",
  8778. // @"value" : securityCode
  8779. // },
  8780. // @"item_4" : @{
  8781. // @"aname" : @"First Name",
  8782. // @"control" : @"edit",
  8783. // @"keyboard" : @"text",
  8784. // @"name" : @"credit_card_first_name",
  8785. // @"required" : @"true",
  8786. // @"value" : firstName
  8787. // },
  8788. // @"item_5" : @{
  8789. // @"aname" : @"Last Name",
  8790. // @"control" : @"edit",
  8791. // @"keyboard" : @"text",
  8792. // @"name" : @"credit_card_last_name",
  8793. // @"required" : @"true",
  8794. // @"value" : lastName
  8795. // },
  8796. // @"item_6" : @{
  8797. // @"aname" : @"Address 1",
  8798. // @"control" : @"edit",
  8799. // @"keyboard" : @"text",
  8800. // @"name" : @"credit_card_address1",
  8801. // @"required" : @"true",
  8802. // @"value" : addr1
  8803. // },
  8804. // @"item_7" : @{
  8805. // @"aname" : @"Address 2",
  8806. // @"control" : @"edit",
  8807. // @"keyboard" : @"text",
  8808. // @"name" : @"credit_card_address2",
  8809. // @"value" : addr2
  8810. // },
  8811. // @"item_8" : @{
  8812. // @"aname" : @"zip code",
  8813. // @"control" : @"edit",
  8814. // @"keyboard" : @"text",
  8815. // @"name" : @"credit_card_zipcode",
  8816. // @"required" : @"true",
  8817. // @"value" : zipcode
  8818. // },
  8819. // @"item_9" : @{
  8820. // @"aname" : @"City",
  8821. // @"control" : @"edit",
  8822. // @"keyboard" : @"text",
  8823. // @"name" : @"credit_card_city",
  8824. // @"required" : @"true",
  8825. // @"value" : city
  8826. // }
  8827. // },
  8828. // @"value" : @"Fill Now",
  8829. // @"value_id" : @""
  8830. // },
  8831. // @"val_1" : @{
  8832. // @"check" : @(0),
  8833. // @"value" : @"Fill Later",
  8834. // @"value_id" : @""
  8835. // }
  8836. // },
  8837. // @"control" : @"enum",
  8838. // @"name" : @"",
  8839. // @"single_select" : @"true"
  8840. // }
  8841. // },
  8842. // @"value" : @"Visa/Master",
  8843. // @"value_id" : @"Credit Card"
  8844. // },
  8845. // @"val_5" : @{
  8846. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8847. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8848. // @"value_id" : @"FOLLOW EXISTING"
  8849. // }
  8850. // },
  8851. @"control" : @"enum",
  8852. @"name" : @"paymentType",
  8853. @"single_select" : @"true"
  8854. },
  8855. @"title" : @"Payment Information"
  8856. }.mutableCopy;
  8857. NSNumber *hide = setting[@"PaymentInformationHide"];
  8858. [dic setValue:hide forKey:@"hide"];
  8859. return dic;
  8860. }
  8861. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8862. // params
  8863. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8864. orderCode = [self translateSingleQuote:orderCode];
  8865. // 缺货检查
  8866. 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];
  8867. __block BOOL outOfStock = NO;
  8868. sqlite3 *database = db;
  8869. if (!db) {
  8870. database = [iSalesDB get_db];
  8871. }
  8872. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8873. int availability = sqlite3_column_int(stmt, 0);
  8874. int item_qty = sqlite3_column_int(stmt, 1);
  8875. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8876. }];
  8877. if (!db) {
  8878. [iSalesDB close_db:database];
  8879. }
  8880. return outOfStock;
  8881. }
  8882. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8883. {
  8884. assert(params[@"user"]!=nil);
  8885. assert(params[@"contact_id"]!=nil);
  8886. assert(params[@"can_create_backorder"]!=nil);
  8887. sqlite3 *db = [iSalesDB get_db];
  8888. // params
  8889. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8890. orderCode = [self translateSingleQuote:orderCode];
  8891. // 缺货检查
  8892. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8893. if (![params[@"can_create_backorder"] boolValue]) {
  8894. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8895. if (out_of_stock) {
  8896. [iSalesDB close_db:db];
  8897. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8898. [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"];
  8899. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8900. return [RAConvertor dict2data:resultDic];
  8901. }
  8902. }
  8903. // UISetting
  8904. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8905. NSString *cachefolder = [paths objectAtIndex:0];
  8906. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8907. NSData* json =nil;
  8908. json=[NSData dataWithContentsOfFile:img_cache];
  8909. NSError *error=nil;
  8910. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8911. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8912. [params setObject:setting forKey:@"setting"];
  8913. int section_count = 0;
  8914. // 0 Order Type 1 Shipping Method 2 Payment Information
  8915. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8916. // 0 Order Type
  8917. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8918. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8919. [ret setValue:order_type_dic forKey:key0];
  8920. // 1 Shipping Method
  8921. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8922. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8923. [ret setValue:shipping_method_dic forKey:key1];
  8924. // 2 Payment Information
  8925. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8926. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8927. [ret setValue:payment_info_dic forKey:key2];
  8928. // 3 Customer
  8929. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8930. NSDictionary *customer_dic = [self customerDic:params db:db];
  8931. [ret setValue:customer_dic forKey:key3];
  8932. // 4 Ship To
  8933. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8934. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8935. [ret setValue:ship_to_dic forKey:key4];
  8936. // 5 Ship From
  8937. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8938. if (![shipFromDisable integerValue]) {
  8939. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8940. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8941. [ret setValue:ship_from_dic forKey:key5];
  8942. }
  8943. // 6 Freight Bill To
  8944. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8945. if (![freightBillToDisable integerValue]) {
  8946. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8947. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8948. [ret setValue:freight_bill_to forKey:key6];
  8949. }
  8950. // 7 Merchandise Bill To
  8951. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8952. if (![merchandiseBillToDisable integerValue]) {
  8953. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8954. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8955. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8956. }
  8957. // 8 Return To
  8958. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8959. if (![returnToDisable integerValue]) {
  8960. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8961. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8962. [ret setValue:return_to_dic forKey:key8];
  8963. }
  8964. // 9 Model Information
  8965. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8966. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8967. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8968. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8969. [ret setValue:model_info_dic forKey:key9];
  8970. // 10 Remarks Content
  8971. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8972. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8973. [ret setValue:remarks_content_dic forKey:key10];
  8974. // 11 Order Total
  8975. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8976. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8977. [ret setValue:order_total_dic forKey:key11];
  8978. // 12 Signature
  8979. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8980. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8981. [ret setValue:sign_dic forKey:key12];
  8982. [ret setValue:@(section_count) forKey:@"section_count"];
  8983. [iSalesDB close_db:db];
  8984. return [RAConvertor dict2data:ret];
  8985. // return nil;
  8986. }
  8987. #pragma mark addr editor
  8988. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8989. NSMutableDictionary *new_item = [item mutableCopy];
  8990. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8991. return new_item;
  8992. }
  8993. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8994. {
  8995. // "is_subaction" = true;
  8996. // orderCode = MOB1608240002;
  8997. // password = 123456;
  8998. // "subaction_tag" = 1;
  8999. // user = EvanK;
  9000. // {
  9001. // "is_subaction" = true;
  9002. // orderCode = MOB1608240002;
  9003. // password = 123456;
  9004. // "refresh_trigger" = zipcode;
  9005. // "subaction_tag" = 1;
  9006. // user = EvanK;
  9007. // }
  9008. NSString *country_code = nil;
  9009. NSString *zipCode = nil;
  9010. NSString *stateCode = nil;
  9011. NSString *city = nil;
  9012. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9013. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9014. NSString *code_id = params[@"country"];
  9015. country_code = [self countryCodeByid:code_id];
  9016. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9017. NSString *zip_code = params[@"zipcode"];
  9018. // 剔除全部为空格
  9019. int spaceCount = 0;
  9020. for (int i = 0; i < zip_code.length; i++) {
  9021. if ([zip_code characterAtIndex:i] == ' ') {
  9022. spaceCount++;
  9023. }
  9024. }
  9025. if (spaceCount == zip_code.length) {
  9026. zip_code = @"";
  9027. }
  9028. zipCode = zip_code;
  9029. if (zipCode.length > 0) {
  9030. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9031. country_code = [dic valueForKey:@"country_code"];
  9032. if (!country_code) {
  9033. // country_code = @"US";
  9034. NSString *code_id = params[@"country"];
  9035. country_code = [self countryCodeByid:code_id];
  9036. }
  9037. stateCode = [dic valueForKey:@"state_code"];
  9038. if(!stateCode.length) {
  9039. stateCode = params[@"state"];
  9040. }
  9041. city = [dic valueForKey:@"city"];
  9042. if (!city.length) {
  9043. city = params[@"city"];
  9044. }
  9045. // zip code
  9046. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9047. // [zipDic setValue:zipCode forKey:@"value"];
  9048. // [section_0 setValue:zipDic forKey:@"item_11"];
  9049. } else {
  9050. NSString *code_id = params[@"country"];
  9051. country_code = [self countryCodeByid:code_id];
  9052. stateCode = params[@"state"];
  9053. city = params[@"city"];
  9054. }
  9055. }
  9056. }
  9057. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9058. // [ret removeObjectForKey:@"up_params"];
  9059. [ret setObject:@"New Address" forKey:@"title"];
  9060. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9061. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9062. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9063. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9064. // [country_dic removeObjectForKey:@"refresh"];
  9065. // [country_dic removeObjectForKey:@"restore"];
  9066. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9067. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9068. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9069. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9070. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9071. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9072. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9073. // [zip_code_dic removeObjectForKey:@"refresh"];
  9074. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9075. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9076. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9077. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9078. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9079. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9080. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9081. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9082. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9083. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9084. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9085. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9086. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9087. // country
  9088. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9089. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9090. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9091. // state
  9092. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9093. // NSDictionary *tmpDic = @{
  9094. // @"value" : @"Other",
  9095. // @"value_id" : @"",
  9096. // @"check" : [NSNumber numberWithInteger:0]
  9097. // };
  9098. //
  9099. // for (int i = 0; i < allState.allKeys.count; i++) {
  9100. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9101. //
  9102. // NSDictionary *tmp = allState[key];
  9103. // [allState setValue:tmpDic forKey:key];
  9104. // tmpDic = tmp;
  9105. // }
  9106. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9107. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9108. [ret setValue:new_section_0 forKey:@"section_0"];
  9109. return [RAConvertor dict2data:ret];
  9110. }
  9111. #pragma mark save addr
  9112. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9113. {
  9114. // NSString *companyName = [self valueInParams:params key:@"company"];
  9115. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9116. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9117. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9118. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9119. // NSString *countryId = [self valueInParams:params key:@"country"];
  9120. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9121. // NSString *city = [self valueInParams:params key:@"city"];
  9122. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9123. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9124. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9125. // NSString *phone = [self valueInParams:params key:@"phone"];
  9126. // NSString *fax = [self valueInParams:params key:@"fax"];
  9127. // NSString *email = [self valueInParams:params key:@"email"];
  9128. return [self offline_saveContact:params update:NO isCustomer:NO];
  9129. }
  9130. #pragma mark cancel order
  9131. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9132. {
  9133. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9134. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9135. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9136. if (order_id.length) {
  9137. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9138. }
  9139. int ret = [iSalesDB execSql:sql];
  9140. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9141. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9142. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9143. // [dic setValue:@"160409" forKey:@"min_ver"];
  9144. return [RAConvertor dict2data:dic];
  9145. }
  9146. #pragma mark sign order
  9147. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9148. {
  9149. //参考 offline_saveBusinesscard
  9150. DebugLog(@"sign order params: %@",params);
  9151. // orderCode = MOB1608240002;
  9152. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9153. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9154. orderCode = [self translateSingleQuote:orderCode];
  9155. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9156. picPath = [self translateSingleQuote:picPath];
  9157. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9158. int ret = [iSalesDB execSql:sql];
  9159. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9160. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9161. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9162. // [dic setValue:@"160409" forKey:@"min_ver"];
  9163. return [RAConvertor dict2data:dic];
  9164. }
  9165. #pragma mark save order
  9166. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9167. NSString *ret = [self valueInParams:params key:key];
  9168. if (translate) {
  9169. ret = [self translateSingleQuote:ret];
  9170. }
  9171. return ret;
  9172. }
  9173. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9174. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9175. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9176. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9177. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9178. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9179. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9180. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9181. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9182. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9183. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9184. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9185. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9186. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9187. if (![total_price isEqualToString:@""]) {
  9188. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9189. } else {
  9190. total_price = @"";
  9191. }
  9192. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9193. if ([paymentsAndCredits isEqualToString:@""]) {
  9194. paymentsAndCredits = @"";
  9195. } else {
  9196. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9197. }
  9198. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9199. if ([handling_fee_value isEqualToString:@""]) {
  9200. handling_fee_value = @"";
  9201. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9202. handling_fee_value = @"";
  9203. } else {
  9204. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9205. }
  9206. NSString *handling_fee_placeholder = handling_fee_value;
  9207. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9208. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9209. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9210. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9211. NSString *lift_gate_placeholder = @"";
  9212. if ([lift_gate_value isEqualToString:@""]) {
  9213. lift_gate_placeholder = @"";
  9214. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9215. lift_gate_placeholder = @"";
  9216. } else {
  9217. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9218. }
  9219. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9220. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9221. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9222. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9223. 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];
  9224. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9225. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9226. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9227. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9228. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9229. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9230. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9231. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9232. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9233. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9234. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9235. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9236. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9237. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9238. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9239. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9240. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9241. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9242. if (!number_nil) {
  9243. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9244. }
  9245. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9246. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9247. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9248. if (!security_code_nil) {
  9249. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9250. }
  9251. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9252. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9253. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9254. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9255. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9256. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9257. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9258. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9259. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9260. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9261. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9262. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9263. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9264. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9265. NSString *contact_id = customer_cid;
  9266. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9267. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9268. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9269. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9270. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9271. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9272. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9273. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9274. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9275. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9276. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9277. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9278. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9279. //
  9280. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9281. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9282. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9283. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9284. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9285. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9286. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9287. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9288. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9289. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9290. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9291. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9292. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9293. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9294. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9295. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9296. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9297. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9298. 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];
  9299. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9300. if (receive_cid.length) {
  9301. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9302. }
  9303. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9304. if (receive_name.length) {
  9305. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9306. }
  9307. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9308. if (receive_ext.length) {
  9309. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9310. }
  9311. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9312. if (receive_contact.length) {
  9313. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9314. }
  9315. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9316. if (receive_phone.length) {
  9317. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9318. }
  9319. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9320. if (receive_email.length) {
  9321. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9322. }
  9323. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9324. if (receive_fax.length) {
  9325. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9326. }
  9327. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9328. if (sender_cid.length) {
  9329. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9330. }
  9331. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9332. if (sender_name.length) {
  9333. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9334. }
  9335. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9336. if (sender_ext.length) {
  9337. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9338. }
  9339. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9340. if(sender_contact.length) {
  9341. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9342. }
  9343. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9344. if (sender_phone.length) {
  9345. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9346. }
  9347. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9348. if (sender_fax.length) {
  9349. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9350. }
  9351. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9352. if (sender_email.length) {
  9353. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9354. }
  9355. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9356. if (shipping_billto_cid.length) {
  9357. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9358. }
  9359. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9360. if (shipping_billto_name.length) {
  9361. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9362. }
  9363. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9364. if (shipping_billto_ext.length) {
  9365. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9366. }
  9367. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9368. if (shipping_billto_contact.length) {
  9369. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9370. }
  9371. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9372. if (shipping_billto_phone.length) {
  9373. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9374. }
  9375. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9376. if (shipping_billto_fax.length) {
  9377. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9378. }
  9379. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9380. if (shipping_billto_email.length) {
  9381. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9382. }
  9383. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9384. if (billing_cid.length) {
  9385. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9386. }
  9387. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9388. if (billing_name.length) {
  9389. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9390. }
  9391. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9392. if (billing_ext.length) {
  9393. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9394. }
  9395. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9396. if (billing_contact.length) {
  9397. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9398. }
  9399. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9400. if (billing_phone.length) {
  9401. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9402. }
  9403. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9404. if (billing_fax.length) {
  9405. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9406. }
  9407. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9408. if (billing_email.length) {
  9409. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9410. }
  9411. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9412. if (returnto_cid.length) {
  9413. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9414. }
  9415. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9416. if (returnto_name.length) {
  9417. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9418. }
  9419. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9420. if (returnto_ext.length) {
  9421. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9422. }
  9423. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9424. if (returnto_contact.length) {
  9425. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9426. }
  9427. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9428. if (returnto_phone.length) {
  9429. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9430. }
  9431. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9432. if (returnto_fax.length) {
  9433. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9434. }
  9435. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9436. if (returnto_email.length) {
  9437. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9438. }
  9439. NSString *order_status = @"status = 1,";
  9440. if (submit) {
  9441. order_status = @"status = -11,";
  9442. }
  9443. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9444. NSString *sync_sql = @"";
  9445. if (submit) {
  9446. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9447. [param setValue:sales_rep forKey:@"sales_rep"];
  9448. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9449. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9450. }
  9451. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  9452. DebugLog(@"save order contactSql: %@",contactSql);
  9453. DebugLog(@"save order orderSql: %@",orderSql);
  9454. // int contact_ret = [iSalesDB execSql:contactSql];
  9455. int order_ret = [iSalesDB execSql:orderSql];
  9456. int ret = order_ret;
  9457. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9458. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9459. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9460. // [dic setValue:@"160409" forKey:@"min_ver"];
  9461. [dic setObject:so_id forKey:@"so#"];
  9462. return [RAConvertor dict2data:dic];
  9463. }
  9464. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9465. {
  9466. // id foo = nil;
  9467. // NSMutableArray *a = @[].mutableCopy;
  9468. // [a addObject:foo];
  9469. return [self saveorder:param submit:NO];
  9470. }
  9471. #pragma mark add to cart by name
  9472. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9473. {
  9474. NSString *orderCode = [params objectForKey:@"orderCode"];
  9475. NSDictionary *newParams = @{
  9476. @"product_id" : params[@"product_id_string"],
  9477. @"orderCode" : orderCode,
  9478. @"can_create_backorder":params[@"can_create_backorder"]
  9479. };
  9480. return [self offline_add2cart:[newParams mutableCopy]];
  9481. }
  9482. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9483. {
  9484. NSString *orderCode = [params objectForKey:@"orderCode"];
  9485. NSString *upccode = [params objectForKey:@"upc_code"];
  9486. // product_name = [self translateSingleQuote:product_name];
  9487. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9488. // bool search_upc = [params[@"search_upc"] boolValue];
  9489. sqlite3 *db = [iSalesDB get_db];
  9490. __block NSMutableString *product_id_string = [NSMutableString string];
  9491. // NSString *name = [product_name_array objectAtIndex:i];
  9492. NSString *sql =nil;
  9493. 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];
  9494. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9495. int product_id = sqlite3_column_int(stmt, 0);
  9496. [product_id_string appendFormat:@"%d",product_id];
  9497. }];
  9498. if (!orderCode) {
  9499. orderCode = @"";
  9500. }
  9501. NSDictionary *newParams = @{
  9502. @"product_id" : product_id_string,
  9503. @"orderCode" : orderCode,
  9504. @"can_create_backorder":params[@"can_create_backorder"]
  9505. };
  9506. [iSalesDB close_db:db];
  9507. return [self offline_add2cart:[newParams mutableCopy]];
  9508. }
  9509. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9510. {
  9511. NSString *orderCode = [params objectForKey:@"orderCode"];
  9512. NSString *product_name = [params objectForKey:@"product_name"];
  9513. product_name = [self translateSingleQuote:product_name];
  9514. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9515. bool search_upc = [params[@"search_upc"] boolValue];
  9516. sqlite3 *db = [iSalesDB get_db];
  9517. __block NSMutableString *product_id_string = [NSMutableString string];
  9518. for (int i = 0; i < product_name_array.count; i++) {
  9519. NSString *name = [product_name_array objectAtIndex:i];
  9520. NSString *sql =nil;
  9521. if(search_upc)
  9522. 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];
  9523. else
  9524. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9525. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9526. int product_id = sqlite3_column_int(stmt, 0);
  9527. if (i == product_name_array.count - 1) {
  9528. [product_id_string appendFormat:@"%d",product_id];
  9529. } else {
  9530. [product_id_string appendFormat:@"%d,",product_id];
  9531. }
  9532. }];
  9533. }
  9534. if (!orderCode) {
  9535. orderCode = @"";
  9536. }
  9537. NSDictionary *newParams = @{
  9538. @"product_id" : product_id_string,
  9539. @"orderCode" : orderCode,
  9540. @"can_create_backorder":params[@"can_create_backorder"]
  9541. };
  9542. [iSalesDB close_db:db];
  9543. return [self offline_add2cart:[newParams mutableCopy]];
  9544. }
  9545. #pragma mark reset order
  9546. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9547. {
  9548. // UIApplication * app = [UIApplication sharedApplication];
  9549. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9550. [iSalesDB disable_trigger];
  9551. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9552. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9553. [iSalesDB enable_trigger];
  9554. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9555. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9556. return [RAConvertor dict2data:dic];
  9557. }
  9558. #pragma mark submit order
  9559. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9560. {
  9561. return [self saveorder:params submit:YES];
  9562. }
  9563. #pragma mark copy order
  9564. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9565. {
  9566. NSMutableDictionary *ret = @{}.mutableCopy;
  9567. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9568. sqlite3 *db = [iSalesDB get_db];
  9569. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9570. // 首先查看联系人是否active
  9571. 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];
  9572. __block int customer_is_active = 1;
  9573. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9574. customer_is_active = sqlite3_column_int(stmt, 0);
  9575. }];
  9576. if (!customer_is_active) {
  9577. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9578. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9579. [iSalesDB close_db:db];
  9580. return [RAConvertor dict2data:ret];
  9581. }
  9582. // new order
  9583. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9584. NSString *new_order_code = [self get_offline_soid:db];
  9585. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9586. user = [self translateSingleQuote:user];
  9587. 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
  9588. __block int result = 1;
  9589. result = [iSalesDB execSql:insert_order_sql db:db];
  9590. if (!result) {
  9591. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9592. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9593. [iSalesDB close_db:db];
  9594. return [RAConvertor dict2data:ret];
  9595. }
  9596. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9597. // __block NSString *product_id = @"";
  9598. __weak typeof(self) weakSelf = self;
  9599. 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];
  9600. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9601. int is_active = sqlite3_column_int(stmt, 1);
  9602. if (is_active) {
  9603. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9604. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9605. 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];
  9606. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9607. }
  9608. }];
  9609. // product_id = [product_id substringFromIndex:1];
  9610. //
  9611. // [self offline_add2cart:@{}.mutableCopy];
  9612. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9613. [iSalesDB close_db:db];
  9614. if (result) {
  9615. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9616. } else {
  9617. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9618. }
  9619. [ret setObject:new_order_code forKey:@"so_id"];
  9620. return [RAConvertor dict2data:ret];
  9621. }
  9622. #pragma mark move wish list to cart
  9623. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9624. assert(params[@"can_create_backorder"]!=nil);
  9625. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9626. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9627. NSString *collectId = params[@"collectId"];
  9628. 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];
  9629. __block NSString *product_id = @"";
  9630. __block NSString *qty = @"";
  9631. __block int number_of_outOfStock = 0;
  9632. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9633. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9634. int productId = sqlite3_column_int(stmt, 0);
  9635. int item_qty = sqlite3_column_int(stmt, 1);
  9636. int availability = sqlite3_column_int(stmt, 2);
  9637. int _id = sqlite3_column_int(stmt, 3);
  9638. if (![params[@"can_create_backorder"] boolValue]) {
  9639. // 库存小于购买量为缺货
  9640. if (availability >= item_qty) {
  9641. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9642. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9643. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9644. } else {
  9645. number_of_outOfStock++;
  9646. }
  9647. } else {
  9648. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9649. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9650. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9651. }
  9652. }];
  9653. NSMutableDictionary *retDic = nil;
  9654. if (![params[@"can_create_backorder"] boolValue]) {
  9655. if (delete_collectId.count == 0) {
  9656. retDic = [NSMutableDictionary dictionary];
  9657. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9658. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9659. return [RAConvertor dict2data:retDic];
  9660. }
  9661. }
  9662. if (product_id.length > 1) {
  9663. product_id = [product_id substringFromIndex:1];
  9664. }
  9665. if (qty.length > 1) {
  9666. qty = [qty substringFromIndex:1];
  9667. }
  9668. NSString *orderCode = params[@"orderCode"];
  9669. if (!orderCode) {
  9670. orderCode = @"";
  9671. }
  9672. NSDictionary *newParams = @{
  9673. @"product_id" : product_id,
  9674. @"orderCode" : orderCode,
  9675. @"qty" : qty,
  9676. @"can_create_backorder":params[@"can_create_backorder"]
  9677. };
  9678. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9679. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9680. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9681. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9682. NSInteger ret = [wish_return[@"result"] intValue];
  9683. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9684. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9685. }
  9686. if (![params[@"can_create_backorder"] boolValue]) {
  9687. if (number_of_outOfStock > 0) {
  9688. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9689. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9690. }
  9691. }
  9692. return [RAConvertor dict2data:retDic];
  9693. }
  9694. #pragma mark - portfolio
  9695. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9696. // assert(params[@"contact_id"]!=nil);
  9697. assert(params[@"user"]!=nil);
  9698. assert(params[@"can_see_price"]!=nil);
  9699. int sort = [[params valueForKey:@"sort"] intValue];
  9700. int offset = [[params valueForKey:@"offset"] intValue];
  9701. int limit = [[params valueForKey:@"limit"] intValue];
  9702. NSString *orderBy = @"";
  9703. switch (sort) {
  9704. case 0:{
  9705. orderBy = @"p.modify_time desc";
  9706. }
  9707. break;
  9708. case 1:{
  9709. orderBy = @"modify_time asc";
  9710. }
  9711. break;
  9712. case 2:{
  9713. orderBy = @"p.name asc";
  9714. }
  9715. break;
  9716. case 3:{
  9717. orderBy = @"p.name desc";
  9718. }
  9719. break;
  9720. case 4:{
  9721. orderBy = @"p.description asc";
  9722. }
  9723. break;
  9724. case 5: {
  9725. orderBy = @"m.default_category asc";
  9726. }
  9727. default:
  9728. break;
  9729. }
  9730. // 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];
  9731. 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];
  9732. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9733. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9734. sqlite3 *db = [iSalesDB get_db];
  9735. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9736. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9737. int product_id = sqlite3_column_int(stmt, 0);
  9738. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9739. NSString *name = [self textAtColumn:1 statement:stmt];
  9740. NSString *description = [self textAtColumn:2 statement:stmt];
  9741. double price = sqlite3_column_double(stmt, 3);
  9742. double discount = sqlite3_column_double(stmt,4);
  9743. int qty = sqlite3_column_int(stmt, 5);
  9744. int percentage = sqlite3_column_int(stmt, 6);
  9745. int item_id = sqlite3_column_int(stmt, 7);
  9746. // int fashion_id = sqlite3_column_int(stmt, 8);
  9747. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9748. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9749. double percent = sqlite3_column_double(stmt, 11);
  9750. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9751. if ([price_null isEqualToString:@"null"]) {
  9752. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9753. }
  9754. NSMutableDictionary *item = @{
  9755. @"linenotes": line_note,
  9756. @"check": @(1),
  9757. @"product_id": product_id_string,
  9758. @"available_qty": @(qty),
  9759. @"available_percent" : @(percent),
  9760. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9761. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9762. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9763. @"img": img_path,
  9764. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9765. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9766. }.mutableCopy;
  9767. if (percentage) {
  9768. [item removeObjectForKey:@"available_qty"];
  9769. } else {
  9770. [item removeObjectForKey:@"available_percent"];
  9771. }
  9772. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9773. if ([qty_null isEqualToString:@"null"]) {
  9774. [item removeObjectForKey:@"available_qty"];
  9775. }
  9776. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9777. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9778. }];
  9779. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9780. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9781. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9782. }
  9783. [iSalesDB close_db:db];
  9784. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9785. [dic setValue:@"" forKey:@"email_content"];
  9786. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9787. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9788. return [RAConvertor dict2data:dic];
  9789. }
  9790. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9791. __block int qty = 0;
  9792. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9793. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9794. qty = sqlite3_column_int(stmt, 0);
  9795. }];
  9796. return qty;
  9797. }
  9798. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9799. {
  9800. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9801. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9802. // NSMutableDictionary * values = params[@"replaceValue"];
  9803. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9804. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9805. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9806. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9807. NSString *pdf_path = @"";
  9808. if (direct) {
  9809. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9810. } else {
  9811. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9812. }
  9813. NSString *create_user = [self valueInParams:params key:@"user"];
  9814. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9815. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9816. // model info
  9817. // 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];
  9818. // V1.90 more color
  9819. 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];
  9820. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9821. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9822. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9823. sqlite3 *db = [iSalesDB get_db];
  9824. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9825. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9826. int product_id = sqlite3_column_int(stmt, 0);
  9827. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9828. double price = sqlite3_column_double(stmt, 1);
  9829. double discount = sqlite3_column_double(stmt,2);
  9830. int qty = sqlite3_column_int(stmt, 3);
  9831. int percentage = sqlite3_column_int(stmt, 4);
  9832. int item_id = sqlite3_column_int(stmt, 5);
  9833. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9834. double percent = sqlite3_column_double(stmt, 7);
  9835. int more_color = sqlite3_column_int(stmt, 8);
  9836. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9837. /* if ([price_null isEqualToString:@"null"]) {
  9838. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9839. }
  9840. */
  9841. [product_ids_string appendFormat:@"%@,",product_id_string];
  9842. // Regular Price
  9843. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9844. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9845. // QTY
  9846. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9847. if ([qty_null isEqualToString:@"null"]) {
  9848. // 查available
  9849. qty = [self model_QTY:product_id_string db:db];
  9850. }
  9851. if (percentage) {
  9852. qty = qty * percent / 100;
  9853. }
  9854. // Special Price
  9855. if ([price_null isEqualToString:@"null"]) {
  9856. // price = regular price
  9857. price = [regular_price_str doubleValue];
  9858. }
  9859. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9860. if (![discount_null isEqualToString:@"null"]) {
  9861. price = price * (1 - discount / 100.0);
  9862. }
  9863. NSMutableDictionary *item = @{
  9864. @"line_note": line_note,
  9865. @"product_id": product_id_string,
  9866. @"available_qty": @(qty),
  9867. @"more_color":@(more_color),
  9868. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9869. @"regular_price" : regular_price_str,
  9870. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9871. }.mutableCopy;
  9872. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9873. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9874. }];
  9875. [iSalesDB close_db:db];
  9876. if (product_ids_string.length > 0) {
  9877. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9878. }
  9879. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9880. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9881. return [RAConvertor dict2data:resultDictionary];
  9882. }
  9883. NSString *model_info = [RAConvertor dict2string:dic];
  9884. // 创建PDF
  9885. // 在preview情况下保存,则不需要新建了
  9886. if (direct) {
  9887. NSMutableDictionary *tear_sheet_params = params;
  9888. // if (tear_sheet_id) {
  9889. // tear_sheet_params = values;
  9890. // }
  9891. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9892. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9893. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9894. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9895. resultDictionary = pdfInfo.mutableCopy;
  9896. } else { // 创建PDF失败
  9897. return pdfData;
  9898. }
  9899. } else {
  9900. // pdf_path 就是本地路径
  9901. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9902. }
  9903. // 将文件移动到PDF Cache文件夹
  9904. NSString *newPath = [pdf_path lastPathComponent];
  9905. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9906. NSString *cachefolder = [paths objectAtIndex:0];
  9907. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9908. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9909. NSFileManager *fileManager = [NSFileManager defaultManager];
  9910. NSError *error = nil;
  9911. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9912. if (error) { // 移动文件失败
  9913. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9914. return [RAConvertor dict2data:resultDictionary];
  9915. }
  9916. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9917. pdf_path = [newPath lastPathComponent];
  9918. // 保存信息
  9919. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9920. NSString *off_params = [RAConvertor dict2string:params];
  9921. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9922. pdf_path = [self translateSingleQuote:pdf_path];
  9923. create_user = [self translateSingleQuote:create_user];
  9924. tear_note = [self translateSingleQuote:tear_note];
  9925. tear_name = [self translateSingleQuote:tear_name];
  9926. model_info = [self translateSingleQuote:model_info];
  9927. configureParams = [self translateSingleQuote:configureParams];
  9928. off_params = [self translateSingleQuote:off_params];
  9929. 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];
  9930. if (tear_sheet_id) {
  9931. int _id = [tear_sheet_id intValue];
  9932. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9933. }
  9934. int result = [iSalesDB execSql:save_pdf_sql];
  9935. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9936. //
  9937. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9938. if (remove_Item) {
  9939. // portfolioId
  9940. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9941. }
  9942. return [RAConvertor dict2data:resultDictionary];
  9943. }
  9944. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9945. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9946. }
  9947. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9948. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9949. }
  9950. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9951. int offset = [[params valueForKey:@"offset"] intValue];
  9952. int limit = [[params valueForKey:@"limit"] intValue];
  9953. NSString *keyword = [params valueForKey:@"keyWord"];
  9954. NSString *where = @"where is_delete is null or is_delete = 0";
  9955. if (keyword.length) {
  9956. keyword = [self translateSingleQuote:keyword];
  9957. 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];
  9958. }
  9959. 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
  9960. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9961. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9962. NSString *cachefolder = [paths objectAtIndex:0];
  9963. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9964. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9965. NSString *name = [self textAtColumn:0 statement:stmt];
  9966. NSString *note = [self textAtColumn:1 statement:stmt];
  9967. NSString *time = [self textAtColumn:2 statement:stmt];
  9968. NSString *user = [self textAtColumn:3 statement:stmt];
  9969. NSString *path = [self textAtColumn:4 statement:stmt];
  9970. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9971. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9972. path = [pdfFolder stringByAppendingPathComponent:path];
  9973. BOOL bdir=NO;
  9974. NSFileManager* fileManager = [NSFileManager defaultManager];
  9975. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9976. {
  9977. //pdf文件不存在
  9978. path=nil;
  9979. }
  9980. time = [self changeDateTimeFormate:time];
  9981. time = [time stringByAppendingString:@" PST"];
  9982. int sheet_id = sqlite3_column_int(stmt, 5);
  9983. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9984. item[@"tearsheetsId"]=@(sheet_id);
  9985. item[@"pdf_path"]=path;
  9986. item[@"create_time"]=time;
  9987. item[@"create_user"]=user;
  9988. item[@"tear_note"]=note;
  9989. item[@"tear_name"]=name;
  9990. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9991. item[@"model_info"]=model_info;
  9992. item[@"off_params"]=off_params;
  9993. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9994. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9995. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9996. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9997. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9998. }];
  9999. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10000. return [RAConvertor dict2data:dic];
  10001. }
  10002. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10003. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10004. // NSString *user = [params objectForKey:@"user"];
  10005. // 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];
  10006. 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];
  10007. int result = [iSalesDB execSql:sql];
  10008. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10009. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10010. if (result == RESULT_TRUE) {
  10011. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10012. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10013. sqlite3 *db = [iSalesDB get_db];
  10014. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10015. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10016. [iSalesDB close_db:db];
  10017. }
  10018. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10019. return [RAConvertor dict2data:dic];
  10020. }
  10021. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10022. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10023. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10024. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10025. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10026. NSDictionary *company_item = @{
  10027. @"control": @"text",
  10028. @"keyboard": @"text",
  10029. @"name": @"company_name",
  10030. @"value": COMPANY_FULL_NAME,
  10031. @"aname": @"Company Name"
  10032. };
  10033. [section_0 setObject:company_item forKey:@"item_0"];
  10034. [dic setObject:section_0 forKey:@"section_0"];
  10035. // Regurlar Price
  10036. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10037. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10038. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10039. __block long val_count = 0;
  10040. NSString *sql = @"select name,type,order_by from price order by order_by";
  10041. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10042. NSString *name = [self textAtColumn:0 statement:stmt];
  10043. int type = sqlite3_column_int(stmt, 1);
  10044. int order_by = sqlite3_column_int(stmt, 2);
  10045. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10046. NSDictionary *price_dic = @{
  10047. @"value" : name,
  10048. @"value_id" : [NSNumber numberWithInteger:type],
  10049. @"check" : order_by == 0 ? @(1) : @(0)
  10050. };
  10051. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10052. val_count = ++(*count);
  10053. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10054. val_count = 0;
  10055. }];
  10056. [cadedate setObject:@{@"value" : @"None",
  10057. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10058. val_count++;
  10059. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10060. [price setObject:cadedate forKey:@"cadedate"];
  10061. [section1 setObject:price forKey:@"item_2"];
  10062. [dic setObject:section1 forKey:@"section_1"];
  10063. return [RAConvertor dict2data:dic];
  10064. }
  10065. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10066. NSString *product_id = [params objectForKey:@"fashionId"];
  10067. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10068. __block int result = RESULT_TRUE;
  10069. __block int qty = 0;
  10070. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10071. qty = sqlite3_column_int(stmt, 0);
  10072. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10073. result = RESULT_FALSE;
  10074. }];
  10075. NSMutableDictionary *dic = @{
  10076. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10077. @"mode" : @"Regular Mode",
  10078. @"quantity_available" : @(qty),
  10079. @"result" : @(result),
  10080. }.mutableCopy;
  10081. return [RAConvertor dict2data:dic];
  10082. }
  10083. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10084. NSString *item_ids = [params objectForKey:@"item_id"];
  10085. NSString *line_notes = [params objectForKey:@"notes"];
  10086. NSString *price_str = [params objectForKey:@"price"];
  10087. NSString *discount_str = [params objectForKey:@"discount"];
  10088. NSString *percent = [params objectForKey:@"available_percent"];
  10089. NSString *qty = [params objectForKey:@"available_qty"];
  10090. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10091. int dot = 0;
  10092. if (line_notes) {
  10093. line_notes = [self translateSingleQuote:line_notes];
  10094. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10095. sql = [sql stringByAppendingString:line_notes];
  10096. dot = 1;
  10097. } else {
  10098. line_notes = @"";
  10099. }
  10100. if (price_str) {
  10101. if (dot) {
  10102. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10103. } else {
  10104. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10105. dot = 1;
  10106. }
  10107. sql = [sql stringByAppendingString:price_str];
  10108. } else {
  10109. price_str = @"";
  10110. }
  10111. if (discount_str) {
  10112. if (dot) {
  10113. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10114. } else {
  10115. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10116. dot = 1;
  10117. }
  10118. sql = [sql stringByAppendingString:discount_str];
  10119. } else {
  10120. discount_str = @"";
  10121. }
  10122. if (percent) {
  10123. if (dot) {
  10124. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10125. } else {
  10126. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10127. dot = 1;
  10128. }
  10129. sql = [sql stringByAppendingString:percent];
  10130. } else {
  10131. percent = @"";
  10132. }
  10133. if (qty) {
  10134. if (dot) {
  10135. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10136. } else {
  10137. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10138. dot = 1;
  10139. }
  10140. sql = [sql stringByAppendingString:qty];
  10141. } else {
  10142. qty = @"";
  10143. }
  10144. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10145. sql = [sql stringByAppendingString:where];
  10146. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10147. int result = [iSalesDB execSql:sql];
  10148. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10149. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10150. return [RAConvertor dict2data:dic];
  10151. }
  10152. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10153. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10154. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10155. sqlite3 *db = [iSalesDB get_db];
  10156. 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];
  10157. int result = [iSalesDB execSql:sql db:db];
  10158. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10159. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10160. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10161. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10162. [iSalesDB close_db:db];
  10163. return [RAConvertor dict2data:dic];
  10164. }
  10165. + (void)offline_removePDFWithName:(NSString *)name {
  10166. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10167. NSString *cachefolder = [paths objectAtIndex:0];
  10168. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10169. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10170. NSFileManager *fileManager = [NSFileManager defaultManager];
  10171. [fileManager removeItemAtPath:path error:nil];
  10172. }
  10173. + (void)offline_clear_PDFCache {
  10174. NSFileManager *fileManager = [NSFileManager defaultManager];
  10175. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10176. NSString *cachefolder = [paths objectAtIndex:0];
  10177. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10178. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10179. for (NSString *path in pdf_files) {
  10180. [self offline_removePDFWithName:[path lastPathComponent]];
  10181. }
  10182. }
  10183. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10184. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10185. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10186. NSString *user = [params objectForKey:@"user"];
  10187. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10188. if (![create_user isEqualToString:user]) {
  10189. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10190. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10191. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10192. return [RAConvertor dict2data:dic];
  10193. }
  10194. 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]];
  10195. __block int is_local = 0;
  10196. __block NSString *path = @"";
  10197. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10198. is_local = sqlite3_column_int(stmt, 0);
  10199. path = [self textAtColumn:1 statement:stmt];
  10200. }];
  10201. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10202. if (is_local == 1) {
  10203. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10204. // 删除文件
  10205. [self offline_removePDFWithName:path];
  10206. }
  10207. int result = [iSalesDB execSql:sql];
  10208. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10209. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10210. return [RAConvertor dict2data:dic];
  10211. }
  10212. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10213. {
  10214. // assert(add_params[@"contact_id"]!=nil);
  10215. assert(add_params[@"user"]!=nil);
  10216. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10217. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10218. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10219. NSString* where=@"1=1";
  10220. if (ver!=nil) {
  10221. where=@"is_dirty=1";
  10222. }
  10223. 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];
  10224. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10225. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10226. sqlite3 *db = [iSalesDB get_db];
  10227. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10228. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10229. NSInteger _id = sqlite3_column_int(stmt, 0);
  10230. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10231. NSString *name = [self textAtColumn:2 statement:stmt];
  10232. NSString *desc = [self textAtColumn:3 statement:stmt];
  10233. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10234. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10235. NSInteger qty = sqlite3_column_int(stmt, 6);
  10236. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10237. double percent = sqlite3_column_double(stmt, 8);
  10238. double price = sqlite3_column_double(stmt, 9);
  10239. double discount = sqlite3_column_double(stmt, 10);
  10240. NSString *img = [self textAtColumn:11 statement:stmt];
  10241. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10242. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10243. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10244. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10245. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10246. if ([price_null isEqualToString:@"null"]) {
  10247. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10248. }
  10249. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10250. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10251. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10252. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10253. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10254. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10255. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10256. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10257. if ([qty_null isEqualToString:@"null"]) {
  10258. // [item setValue:@"null" forKey:@"available_qty"];
  10259. } else {
  10260. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10261. }
  10262. if ([is_percent_null isEqualToString:@"null"]) {
  10263. // [item setValue:@"null" forKey:@"percentage"];
  10264. } else {
  10265. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10266. }
  10267. if ([percent_null isEqualToString:@"null"]) {
  10268. // [item setValue:@"null" forKey:@"percent"];
  10269. } else {
  10270. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10271. }
  10272. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10273. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10274. [item setValue:name forKey:@"name"];
  10275. [item setValue:desc forKey:@"description"];
  10276. [item setValue:img forKey:@"img"];
  10277. [item setValue:line_note forKey:@"line_note"];
  10278. [item setValue:create_time forKey:@"createtime"];
  10279. [item setValue:modify_time forKey:@"modifytime"];
  10280. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10281. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10282. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10283. }];
  10284. [iSalesDB close_db:db];
  10285. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10286. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10287. }
  10288. return ret;
  10289. }
  10290. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10291. {
  10292. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10293. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10294. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10295. NSString* where=@"1=1";
  10296. if (ver!=nil) {
  10297. where=@"is_dirty=1";
  10298. }
  10299. 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];
  10300. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10301. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10302. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10303. NSInteger _id = sqlite3_column_int(stmt, 0);
  10304. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10305. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10306. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10307. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10308. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10309. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10310. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10311. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10312. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10313. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10314. NSString *uuid = [NSUUID UUID].UUIDString;
  10315. int is_delete = sqlite3_column_int(stmt, 11);
  10316. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10317. int is_local = sqlite3_column_int(stmt, 12);
  10318. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10319. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10320. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10321. }
  10322. [item setObject:pdf_path forKey:@"pdf_path"];
  10323. [item setObject:create_user forKey:@"create_user"];
  10324. [item setObject:tear_note forKey:@"tear_note"];
  10325. [item setObject:tear_name forKey:@"tear_name"];
  10326. [item setObject:model_info forKey:@"model_info"];
  10327. [item setObject:createtime forKey:@"createtime"];
  10328. [item setObject:modifytime forKey:@"modifytime"];
  10329. [item setObject:urlParams forKey:@"urlParams"];
  10330. [item setObject:off_params forKey:@"off_params"];
  10331. [item setObject:uuid forKey:@"pdf_token"];
  10332. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10333. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10334. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10335. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10336. } else {
  10337. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10338. }
  10339. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10340. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10341. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10342. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10343. }];
  10344. return ret;
  10345. }
  10346. #pragma mark 2020
  10347. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10348. {
  10349. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10350. sqlite3 *db = [iSalesDB get_db];
  10351. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10352. if (contactType) {
  10353. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10354. } else {
  10355. contactType = @"1 = 1";
  10356. }
  10357. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10358. DebugLog(@"offline contact list keyword: %@",keyword);
  10359. // advanced search
  10360. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10361. if (contact_name) {
  10362. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10363. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10364. } else {
  10365. contact_name = @"";
  10366. }
  10367. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10368. if (customer_phone) {
  10369. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10370. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10371. } else {
  10372. customer_phone = @"";
  10373. }
  10374. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10375. if (customer_fax) {
  10376. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10377. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10378. } else {
  10379. customer_fax = @"";
  10380. }
  10381. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10382. if (customer_zipcode) {
  10383. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10384. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10385. } else {
  10386. customer_zipcode = @"";
  10387. }
  10388. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10389. if (customer_sales_rep) {
  10390. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10391. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10392. } else {
  10393. customer_sales_rep = @"";
  10394. }
  10395. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10396. if (customer_state) {
  10397. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10398. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10399. } else {
  10400. customer_state = @"";
  10401. }
  10402. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10403. if (customer_name) {
  10404. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10405. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10406. } else {
  10407. customer_name = @"";
  10408. }
  10409. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10410. if (customer_country) {
  10411. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10412. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10413. } else {
  10414. customer_country = @"";
  10415. }
  10416. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10417. if (customer_cid) {
  10418. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10419. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10420. } else {
  10421. customer_cid = @"";
  10422. }
  10423. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10424. if (customer_city) {
  10425. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10426. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10427. } else {
  10428. customer_city = @"";
  10429. }
  10430. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10431. if (customer_address) {
  10432. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10433. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10434. } else {
  10435. customer_address = @"";
  10436. }
  10437. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10438. if (customer_email) {
  10439. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10440. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10441. } else {
  10442. customer_email = @"";
  10443. }
  10444. NSString *price_name = [params valueForKey:@"price_name"];
  10445. if (price_name) {
  10446. if ([price_name containsString:@","]) {
  10447. // 首先从 price表中查处name
  10448. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10449. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10450. for (int i = 1;i < pArray.count;i++) {
  10451. NSString *p = pArray[i];
  10452. [mutablePStr appendFormat:@" or type = %@ ",p];
  10453. }
  10454. [mutablePStr appendString:@";"];
  10455. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10456. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10457. char *name = (char *)sqlite3_column_text(stmt, 0);
  10458. if (!name)
  10459. name = "";
  10460. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10461. }];
  10462. // 再根据name 拼sql
  10463. NSMutableString *mutable_price_name = [NSMutableString string];
  10464. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10465. for (int i = 1; i < price_name_array.count; i++) {
  10466. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10467. }
  10468. [mutable_price_name appendString:@")"];
  10469. price_name = mutable_price_name;
  10470. } else {
  10471. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10472. if ([price_name isEqualToString:@""]) {
  10473. price_name = @"";
  10474. } else {
  10475. __block NSString *price;
  10476. price_name = [self translateSingleQuote:price_name];
  10477. [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) {
  10478. char *p = (char *)sqlite3_column_text(stmt, 0);
  10479. if (p == NULL) {
  10480. p = "";
  10481. }
  10482. price = [NSString stringWithUTF8String:p];
  10483. }];
  10484. if ([price isEqualToString:@""]) {
  10485. price_name = @"";
  10486. } else {
  10487. price = [self translateSingleQuote:price];
  10488. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10489. }
  10490. }
  10491. }
  10492. } else {
  10493. price_name = @"";
  10494. }
  10495. int limit = [[params valueForKey:@"limit"] intValue];
  10496. int offset = [[params valueForKey:@"offset"] intValue];
  10497. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10498. 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];
  10499. 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];
  10500. // int result= [iSalesDB AddExFunction:db];
  10501. int count =0;
  10502. NSString *sqlQuery = nil;
  10503. if(keyword.length==0)
  10504. {
  10505. // 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];
  10506. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10507. sqlQuery = sql;
  10508. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10509. }
  10510. else
  10511. {
  10512. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10513. keyword = keyword.lowercaseString;
  10514. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10515. 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];
  10516. 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]];
  10517. }
  10518. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10519. sqlite3_stmt * statement;
  10520. [ret setValue:@"2" forKey:@"result"];
  10521. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10522. // 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";
  10523. int i = 0;
  10524. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10525. {
  10526. while (sqlite3_step(statement) == SQLITE_ROW)
  10527. {
  10528. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10529. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10530. int editable = sqlite3_column_int(statement, 0);
  10531. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10532. NSString *nscompany_name =nil;
  10533. if(company_name==nil)
  10534. nscompany_name=@"";
  10535. else
  10536. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10537. char *country = (char*)sqlite3_column_text(statement, 2);
  10538. if(country==nil)
  10539. country="";
  10540. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10541. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10542. // if(addr==nil)
  10543. // addr="";
  10544. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10545. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10546. if(zipcode==nil)
  10547. zipcode="";
  10548. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10549. char *state = (char*)sqlite3_column_text(statement, 5);
  10550. if(state==nil)
  10551. state="";
  10552. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10553. char *city = (char*)sqlite3_column_text(statement, 6);
  10554. if(city==nil)
  10555. city="";
  10556. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10557. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10558. // NSString *nscontact_name = nil;
  10559. // if(contact_name==nil)
  10560. // nscontact_name=@"";
  10561. // else
  10562. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10563. char *phone = (char*)sqlite3_column_text(statement, 8);
  10564. NSString *nsphone = nil;
  10565. if(phone==nil)
  10566. nsphone=@"";
  10567. else
  10568. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10569. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10570. if(contact_id==nil)
  10571. contact_id="";
  10572. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10573. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10574. if(addr_1==nil)
  10575. addr_1="";
  10576. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10577. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10578. if(addr_2==nil)
  10579. addr_2="";
  10580. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10581. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10582. if(addr_3==nil)
  10583. addr_3="";
  10584. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10585. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10586. if(addr_4==nil)
  10587. addr_4="";
  10588. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10589. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10590. if(first_name==nil)
  10591. first_name="";
  10592. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10593. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10594. if(last_name==nil)
  10595. last_name="";
  10596. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10597. char *fax = (char*)sqlite3_column_text(statement, 16);
  10598. NSString *nsfax = nil;
  10599. if(fax==nil)
  10600. nsfax=@"";
  10601. else
  10602. {
  10603. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10604. if(nsfax.length>0)
  10605. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10606. }
  10607. char *email = (char*)sqlite3_column_text(statement, 17);
  10608. NSString *nsemail = nil;
  10609. if(email==nil)
  10610. nsemail=@"";
  10611. else
  10612. {
  10613. nsemail= [[NSString alloc]initWithUTF8String:email];
  10614. if(nsemail.length>0)
  10615. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10616. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10617. }
  10618. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10619. [arr_name addObject:nsfirst_name];
  10620. [arr_name addObject:nslast_name];
  10621. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10622. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10623. {
  10624. // decrypt
  10625. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10626. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10627. nsphone=[AESCrypt fastdecrypt:nsphone];
  10628. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10629. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10630. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10631. }
  10632. [arr_addr addObject:nscompany_name];
  10633. [arr_addr addObject:nscontact_name];
  10634. [arr_addr addObject:@"<br>"];
  10635. [arr_addr addObject:nsaddr_1];
  10636. [arr_addr addObject:nsaddr_2];
  10637. [arr_addr addObject:nsaddr_3];
  10638. [arr_addr addObject:nsaddr_4];
  10639. //[arr_addr addObject:nsaddr];
  10640. [arr_addr addObject:nszipcode];
  10641. [arr_addr addObject:nscity];
  10642. [arr_addr addObject:nsstate];
  10643. [arr_addr addObject:nscountry];
  10644. [arr_addr addObject:@"<br>"];
  10645. [arr_addr addObject:nsphone];
  10646. [arr_addr addObject:nsfax];
  10647. [arr_addr addObject:nsemail];
  10648. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10649. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10650. [item setValue:name forKey:@"name"];
  10651. [item setValue:nscontact_id forKey:@"contact_id"];
  10652. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10653. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10654. i++;
  10655. }
  10656. sqlite3_finalize(statement);
  10657. }
  10658. [iSalesDB close_db:db];
  10659. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10660. dispatch_async(dispatch_get_main_queue(), ^{
  10661. UIApplication * app = [UIApplication sharedApplication];
  10662. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10663. [ret setValue:appDelegate.mode forKey:@"mode"];
  10664. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10665. result(ret);
  10666. });
  10667. return ;
  10668. });
  10669. }
  10670. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10671. //{
  10672. //
  10673. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10674. //
  10675. // NSString* contactId = [params valueForKey:@"contactId"];
  10676. //
  10677. //
  10678. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10679. //
  10680. //
  10681. //
  10682. //
  10683. //
  10684. //
  10685. // sqlite3 *db = [iSalesDB get_db];
  10686. //
  10687. //
  10688. //
  10689. //
  10690. //
  10691. // NSString *sqlQuery = nil;
  10692. //
  10693. //
  10694. // {
  10695. // 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];
  10696. //
  10697. // }
  10698. //
  10699. //
  10700. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10701. // sqlite3_stmt * statement;
  10702. //
  10703. //
  10704. // [ret setValue:@"2" forKey:@"result"];
  10705. //
  10706. //
  10707. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10708. // {
  10709. //
  10710. // //int i = 0;
  10711. // if (sqlite3_step(statement) == SQLITE_ROW)
  10712. // {
  10713. //
  10714. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10715. //
  10716. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10717. //
  10718. //
  10719. // // int editable = sqlite3_column_int(statement, 0);
  10720. //
  10721. //
  10722. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10723. // NSString *nscompany_name =nil;
  10724. // if(company_name==nil)
  10725. // nscompany_name=@"";
  10726. // else
  10727. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10728. //
  10729. //
  10730. // char *country = (char*)sqlite3_column_text(statement, 2);
  10731. // if(country==nil)
  10732. // country="";
  10733. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10734. //
  10735. //
  10736. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10737. // // if(addr==nil)
  10738. // // addr="";
  10739. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10740. //
  10741. //
  10742. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10743. // if(zipcode==nil)
  10744. // zipcode="";
  10745. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10746. //
  10747. //
  10748. // char *state = (char*)sqlite3_column_text(statement, 5);
  10749. // if(state==nil)
  10750. // state="";
  10751. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10752. //
  10753. // char *city = (char*)sqlite3_column_text(statement, 6);
  10754. // if(city==nil)
  10755. // city="";
  10756. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10757. //
  10758. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10759. // // NSString *nscontact_name = nil;
  10760. // // if(contact_name==nil)
  10761. // // nscontact_name=@"";
  10762. // // else
  10763. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10764. //
  10765. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10766. // NSString *nsphone = nil;
  10767. // if(phone==nil)
  10768. // nsphone=@"";
  10769. // else
  10770. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10771. //
  10772. //
  10773. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10774. // // if(contact_id==nil)
  10775. // // contact_id="";
  10776. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10777. //
  10778. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10779. // if(addr_1==nil)
  10780. // addr_1="";
  10781. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10782. //
  10783. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10784. // if(addr_2==nil)
  10785. // addr_2="";
  10786. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10787. //
  10788. //
  10789. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10790. // if(addr_3==nil)
  10791. // addr_3="";
  10792. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10793. //
  10794. //
  10795. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10796. // if(addr_4==nil)
  10797. // addr_4="";
  10798. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10799. //
  10800. //
  10801. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10802. // if(first_name==nil)
  10803. // first_name="";
  10804. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10805. //
  10806. //
  10807. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10808. // if(last_name==nil)
  10809. // last_name="";
  10810. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10811. //
  10812. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10813. // NSString *nsfax = nil;
  10814. // if(fax==nil)
  10815. // nsfax=@"";
  10816. // else
  10817. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10818. //
  10819. // char *email = (char*)sqlite3_column_text(statement, 17);
  10820. // NSString *nsemail = nil;
  10821. // if(email==nil)
  10822. // nsemail=@"";
  10823. // else
  10824. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10825. //
  10826. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10827. // NSString *nsimg_0 = nil;
  10828. // if(img_0==nil)
  10829. // nsimg_0=@"";
  10830. // else
  10831. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10832. //
  10833. // [self copy_bcardImg:nsimg_0];
  10834. //
  10835. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10836. // NSString *nsimg_1 = nil;
  10837. // if(img_1==nil)
  10838. // nsimg_1=@"";
  10839. // else
  10840. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10841. // [self copy_bcardImg:nsimg_1];
  10842. //
  10843. //
  10844. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10845. // NSString *nsimg_2 = nil;
  10846. // if(img_2==nil)
  10847. // nsimg_2=@"";
  10848. // else
  10849. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10850. // [self copy_bcardImg:nsimg_2];
  10851. //
  10852. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10853. // NSString *nsprice_type = nil;
  10854. // if(price_type==nil)
  10855. // nsprice_type=@"";
  10856. // else
  10857. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10858. //
  10859. //
  10860. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10861. // NSString *nsnotes = nil;
  10862. // if(notes==nil)
  10863. // nsnotes=@"";
  10864. // else
  10865. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10866. //
  10867. //
  10868. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10869. // NSString *nssalesrep = nil;
  10870. // if(salesrep==nil)
  10871. // nssalesrep=@"";
  10872. // else
  10873. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10874. //
  10875. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10876. //
  10877. //
  10878. // {
  10879. // // decrypt
  10880. //
  10881. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10882. //
  10883. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10884. //
  10885. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10886. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10887. //
  10888. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10889. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10890. //
  10891. // }
  10892. //
  10893. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10894. //
  10895. // [arr_name addObject:nsfirst_name];
  10896. // [arr_name addObject:nslast_name];
  10897. //
  10898. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10899. //
  10900. //
  10901. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10902. //
  10903. // [arr_ext addObject:nsaddr_1];
  10904. // [arr_ext addObject:nsaddr_2];
  10905. // [arr_ext addObject:nsaddr_3];
  10906. // [arr_ext addObject:nsaddr_4];
  10907. // [arr_ext addObject:@"\r\n"];
  10908. //
  10909. // [arr_ext addObject:nscity];
  10910. // [arr_ext addObject:nsstate];
  10911. // [arr_ext addObject:nszipcode];
  10912. // [arr_ext addObject:nscountry];
  10913. //
  10914. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10915. //
  10916. //
  10917. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10918. //
  10919. //
  10920. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10921. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10922. // [item setValue:nscountry forKey:@"customer_country"];
  10923. // [item setValue:nsphone forKey:@"customer_phone"];
  10924. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10925. // [item setValue:nscompany_name forKey:@"customer_name"];
  10926. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10927. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10928. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10929. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10930. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10931. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10932. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10933. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10934. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10935. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10936. // [item setValue:nscity forKey:@"customer_city"];
  10937. // [item setValue:nsstate forKey:@"customer_state"];
  10938. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10939. // [item setValue:contactId forKey:@"customer_cid"];
  10940. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10941. // [item setValue:nsfax forKey:@"customer_fax"];
  10942. // [item setValue:nsemail forKey:@"customer_email"];
  10943. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10944. //
  10945. //
  10946. // [ret setObject:item forKey:@"customerInfo"];
  10947. // // i++;
  10948. //
  10949. //
  10950. //
  10951. // }
  10952. //
  10953. //
  10954. //
  10955. //
  10956. // sqlite3_finalize(statement);
  10957. // }
  10958. //
  10959. //
  10960. //
  10961. // [iSalesDB close_db:db];
  10962. //
  10963. //
  10964. //
  10965. //
  10966. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10967. // dispatch_async(dispatch_get_main_queue(), ^{
  10968. // UIApplication * app = [UIApplication sharedApplication];
  10969. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10970. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10971. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10972. // result(ret);
  10973. // });
  10974. //
  10975. // return ;
  10976. // });
  10977. //}
  10978. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10979. {
  10980. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10981. NSString* order_code= appDelegate.order_code;
  10982. NSString* user = appDelegate.user;
  10983. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10984. NSString* category = [params valueForKey:@"category"];
  10985. if (!category || [category isEqualToString:@""]) {
  10986. category = @"%";
  10987. }
  10988. category = [self translateSingleQuote:category];
  10989. int limit = [[params valueForKey:@"limit"] intValue];
  10990. int offset = [[params valueForKey:@"offset"] intValue];
  10991. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10992. NSString *limit_str = @"";
  10993. if (limited) {
  10994. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10995. }
  10996. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10997. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10998. sqlite3 *db = [iSalesDB get_db];
  10999. // [iSalesDB AddExFunction:db];
  11000. int count;
  11001. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11002. 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];
  11003. double price_min = 0;
  11004. double price_max = 0;
  11005. if ([params.allKeys containsObject:@"alert"]) {
  11006. // alert
  11007. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11008. NSString *alert = params[@"alert"];
  11009. if ([alert isEqualToString:@"Display All"]) {
  11010. alert = [NSString stringWithFormat:@""];
  11011. } else {
  11012. alert = [self translateSingleQuote:alert];
  11013. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11014. }
  11015. // available
  11016. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11017. NSString *available = params[@"available"];
  11018. NSString *available_condition;
  11019. if ([available isEqualToString:@"Display All"]) {
  11020. available_condition = @"";
  11021. } else if ([available isEqualToString:@"Available Now"]) {
  11022. available_condition = @"and availability > 0";
  11023. } else {
  11024. available_condition = @"and availability == 0";
  11025. }
  11026. // best seller
  11027. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11028. NSString *best_seller = @"";
  11029. NSString *order_best_seller = @"m.name asc";
  11030. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11031. best_seller = @"and best_seller > 0";
  11032. order_best_seller = @"m.best_seller desc,m.name asc";
  11033. }
  11034. // price
  11035. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11036. NSString *price = params[@"price"];
  11037. price_min = 0;
  11038. price_max = MAXFLOAT;
  11039. if (user && price != nil) {
  11040. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11041. NSMutableString *priceName = [NSMutableString string];
  11042. for (int i = 0; i < priceTypeArray.count; i++) {
  11043. NSString *pricetype = priceTypeArray[i];
  11044. pricetype = [self translateSingleQuote:pricetype];
  11045. if (i == 0) {
  11046. [priceName appendFormat:@"'%@'",pricetype];
  11047. } else {
  11048. [priceName appendFormat:@",'%@'",pricetype];
  11049. }
  11050. }
  11051. if ([price isEqualToString:@"Display All"]) {
  11052. price = [NSString stringWithFormat:@""];
  11053. } else if([price containsString:@"+"]){
  11054. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11055. price_min = [price doubleValue];
  11056. 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];
  11057. } else {
  11058. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11059. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11060. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11061. 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];
  11062. }
  11063. } else {
  11064. price = @"";
  11065. }
  11066. // sold_by_qty : Sold in quantities of %@
  11067. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11068. NSString *qty = params[@"sold_by_qty"];
  11069. if ([qty isEqualToString:@"Display All"]) {
  11070. qty = @"";
  11071. } else {
  11072. qty = [self translateSingleQuote:qty];
  11073. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11074. }
  11075. // cate
  11076. // category = [self translateSingleQuote:category];
  11077. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11078. // cate mutiple selection
  11079. NSString *category_id = params[@"category"];
  11080. NSMutableArray *cate_id_array = nil;
  11081. NSMutableString *cateWhere = [NSMutableString string];
  11082. if ([category_id isEqualToString:@""] || !category_id) {
  11083. [cateWhere appendString:@"1 = 1"];
  11084. } else {
  11085. if ([category_id containsString:@","]) {
  11086. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11087. } else {
  11088. cate_id_array = [@[category_id] mutableCopy];
  11089. }
  11090. [cateWhere appendString:@"("];
  11091. for (int i = 0; i < cate_id_array.count; i++) {
  11092. if (i == 0) {
  11093. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11094. } else {
  11095. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11096. }
  11097. }
  11098. [cateWhere appendString:@")"];
  11099. }
  11100. // where bestseller > 0 order by bestseller desc
  11101. // sql query: alert availability(int) best_seller(int) price qty
  11102. 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];
  11103. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11104. }
  11105. DebugLog(@"offline category where: %@",where);
  11106. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11107. if (!user) {
  11108. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11109. }
  11110. [ret setValue:filter forKey:@"filter"];
  11111. DebugLog(@"offline_category sql:%@",sqlQuery);
  11112. sqlite3_stmt * statement;
  11113. [ret setValue:@"2" forKey:@"result"];
  11114. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11115. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11116. // int count=0;
  11117. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11118. {
  11119. int i=0;
  11120. while (sqlite3_step(statement) == SQLITE_ROW)
  11121. {
  11122. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11123. char *name = (char*)sqlite3_column_text(statement, 0);
  11124. if(name==nil)
  11125. name="";
  11126. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11127. char *description = (char*)sqlite3_column_text(statement, 1);
  11128. if(description==nil)
  11129. description="";
  11130. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11131. int product_id = sqlite3_column_int(statement, 2);
  11132. int wid = sqlite3_column_int(statement, 3);
  11133. int closeout = sqlite3_column_int(statement, 4);
  11134. int cid = sqlite3_column_int(statement, 5);
  11135. int wisdelete = sqlite3_column_int(statement, 6);
  11136. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11137. int more_color = sqlite3_column_int(statement, 8);
  11138. // Defaul Category ID
  11139. __block NSString *categoryID = nil;
  11140. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11141. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11142. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11143. if(default_category==nil)
  11144. default_category="";
  11145. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11146. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11147. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11148. categoryID = nsdefault_category;
  11149. }];
  11150. if (!categoryID.length) {
  11151. NSString *cateIDs = params[@"category"];
  11152. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11153. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11154. for (NSString *cateID in requestCategoryArr) {
  11155. BOOL needBreak = NO;
  11156. for (NSString *itemCateIDBox in itemCategoryArr) {
  11157. if (itemCateIDBox.length > 4) {
  11158. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11159. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11160. if ([itemCategoryID isEqualToString:cateID]) {
  11161. needBreak = YES;
  11162. categoryID = itemCategoryID;
  11163. break;
  11164. }
  11165. }
  11166. }
  11167. if (needBreak) {
  11168. break;
  11169. }
  11170. }
  11171. }
  11172. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11173. if(wid !=0 && wisdelete != 1)
  11174. [item setValue:@"true" forKey:@"wish_exists"];
  11175. else
  11176. [item setValue:@"false" forKey:@"wish_exists"];
  11177. if(closeout==0)
  11178. [item setValue:@"false" forKey:@"is_closeout"];
  11179. else
  11180. [item setValue:@"true" forKey:@"is_closeout"];
  11181. if(cid==0)
  11182. [item setValue:@"false" forKey:@"cart_exists"];
  11183. else
  11184. [item setValue:@"true" forKey:@"cart_exists"];
  11185. if (more_color == 0) {
  11186. [item setObject:@(false) forKey:@"more_color"];
  11187. } else if (more_color == 1) {
  11188. [item setObject:@(true) forKey:@"more_color"];
  11189. }
  11190. [item addEntriesFromDictionary:imgjson];
  11191. // [item setValue:nsurl forKey:@"img"];
  11192. [item setValue:nsname forKey:@"name"];
  11193. [item setValue:nsdescription forKey:@"description"];
  11194. if (categoryID) {
  11195. [item setValue:categoryID forKey:@"item_category_id"];
  11196. }
  11197. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11198. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11199. i++;
  11200. }
  11201. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11202. [ret setObject:items forKey:@"items"];
  11203. sqlite3_finalize(statement);
  11204. } else {
  11205. DebugLog(@"nothing...");
  11206. }
  11207. DebugLog(@"count:%d",count);
  11208. [iSalesDB close_db:db];
  11209. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11210. dispatch_async(dispatch_get_main_queue(), ^{
  11211. // UIApplication * app = [UIApplication sharedApplication];
  11212. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11213. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11214. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11215. result(ret);
  11216. });
  11217. });
  11218. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11219. }
  11220. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11221. {
  11222. [self categoryList:params limited:YES completionHandler:result];
  11223. }
  11224. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11225. {
  11226. UIApplication * app = [UIApplication sharedApplication];
  11227. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11228. NSString* order_code = appDelegate.order_code;
  11229. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11230. NSString* keyword = [params valueForKey:@"keyword"];
  11231. keyword=keyword.lowercaseString;
  11232. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11233. int limit = [[params valueForKey:@"limit"] intValue];
  11234. int offset = [[params valueForKey:@"offset"] intValue];
  11235. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11236. NSString *limit_str = @"";
  11237. if (limited) {
  11238. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11239. }
  11240. sqlite3 *db = [iSalesDB get_db];
  11241. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11242. NSString *sqlQuery = nil;
  11243. if(exactMatch )
  11244. 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 ;
  11245. else
  11246. 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
  11247. DebugLog(@"offline_search sql:%@",sqlQuery);
  11248. sqlite3_stmt * statement;
  11249. [ret setValue:@"2" forKey:@"result"];
  11250. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11251. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11252. // int count=0;
  11253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11254. {
  11255. int i=0;
  11256. while (sqlite3_step(statement) == SQLITE_ROW)
  11257. {
  11258. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11259. // char *name = (char*)sqlite3_column_text(statement, 1);
  11260. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11261. char *name = (char*)sqlite3_column_text(statement, 0);
  11262. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11263. char *description = (char*)sqlite3_column_text(statement, 1);
  11264. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11265. int product_id = sqlite3_column_int(statement, 2);
  11266. // char *url = (char*)sqlite3_column_text(statement, 3);
  11267. // if(url==nil)
  11268. // url="";
  11269. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11270. int wid = sqlite3_column_int(statement, 3);
  11271. int closeout = sqlite3_column_int(statement, 4);
  11272. int cid = sqlite3_column_int(statement, 5);
  11273. int wisdelete = sqlite3_column_int(statement, 6);
  11274. int more_color = sqlite3_column_int(statement, 7);
  11275. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11276. if(wid !=0 && wisdelete != 1)
  11277. [item setValue:@"true" forKey:@"wish_exists"];
  11278. else
  11279. [item setValue:@"false" forKey:@"wish_exists"];
  11280. if(closeout==0)
  11281. [item setValue:@"false" forKey:@"is_closeout"];
  11282. else
  11283. [item setValue:@"true" forKey:@"is_closeout"];
  11284. if(cid==0)
  11285. [item setValue:@"false" forKey:@"cart_exists"];
  11286. else
  11287. [item setValue:@"true" forKey:@"cart_exists"];
  11288. if (more_color == 0) {
  11289. [item setObject:@(false) forKey:@"more_color"];
  11290. } else if (more_color == 1) {
  11291. [item setObject:@(true) forKey:@"more_color"];
  11292. }
  11293. [item addEntriesFromDictionary:imgjson];
  11294. // [item setValue:nsurl forKey:@"img"];
  11295. [item setValue:nsname forKey:@"fash_name"];
  11296. [item setValue:nsdescription forKey:@"description"];
  11297. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11298. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11299. i++;
  11300. }
  11301. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11302. [ret setObject:items forKey:@"items"];
  11303. sqlite3_finalize(statement);
  11304. }
  11305. DebugLog(@"count:%d",count);
  11306. [iSalesDB close_db:db];
  11307. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11308. dispatch_async(dispatch_get_main_queue(), ^{
  11309. result(ret);
  11310. });
  11311. });
  11312. }
  11313. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11314. {
  11315. [self search:params limited:YES completionHandler:result];
  11316. }
  11317. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11318. //{
  11319. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11320. // NSString* order_code = appDelegate.order_code;
  11321. // NSString* user = appDelegate.user;
  11322. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11323. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11324. //
  11325. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11326. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11327. //
  11328. // // category
  11329. // NSDictionary *category_menu = [self offline_category_menu];
  11330. // [filter setValue:category_menu forKey:@"category"];
  11331. //
  11332. // NSString* where= nil;
  11333. // NSString* orderby= @"m.name";
  11334. // if (!filterSearch) {
  11335. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11336. //
  11337. // switch (covertype) {
  11338. // case 0:
  11339. // {
  11340. // where=@"m.category like'%%#005#%%'";
  11341. // break;
  11342. // }
  11343. // case 1:
  11344. // {
  11345. // where=@"m.alert like '%QS%'";
  11346. // break;
  11347. // }
  11348. // case 2:
  11349. // {
  11350. // where=@"m.availability>0";
  11351. // break;
  11352. // }
  11353. // case 3:
  11354. // {
  11355. // where=@"m.best_seller>0";
  11356. // orderby=@"m.best_seller desc,m.name asc";
  11357. // break;
  11358. // }
  11359. // default:
  11360. // where=@"1=1";
  11361. // break;
  11362. // }
  11363. //
  11364. // }
  11365. //
  11366. //
  11367. // int limit = [[params valueForKey:@"limit"] intValue];
  11368. // int offset = [[params valueForKey:@"offset"] intValue];
  11369. //
  11370. // NSString *limit_str = @"";
  11371. // if (limited) {
  11372. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11373. // }
  11374. //
  11375. //
  11376. // sqlite3 *db = [iSalesDB get_db];
  11377. // // [iSalesDB AddExFunction:db];
  11378. //
  11379. // int count;
  11380. //
  11381. // NSString *sqlQuery = nil;
  11382. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11383. // 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];
  11384. //
  11385. //
  11386. // double price_min = 0;
  11387. // double price_max = 0;
  11388. // if (filterSearch) {
  11389. // // alert
  11390. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11391. // NSString *alert = params[@"alert"];
  11392. // if ([alert isEqualToString:@"Display All"]) {
  11393. // alert = [NSString stringWithFormat:@""];
  11394. // } else {
  11395. // alert = [self translateSingleQuote:alert];
  11396. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11397. // }
  11398. //
  11399. // // available
  11400. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11401. // NSString *available = params[@"available"];
  11402. // NSString *available_condition;
  11403. // if ([available isEqualToString:@"Display All"]) {
  11404. // available_condition = @"";
  11405. // } else if ([available isEqualToString:@"Available Now"]) {
  11406. // available_condition = @"and availability > 0";
  11407. // } else {
  11408. // available_condition = @"and availability == 0";
  11409. // }
  11410. //
  11411. // // best seller
  11412. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11413. // NSString *best_seller = @"";
  11414. // NSString *order_best_seller = @"m.name asc";
  11415. //
  11416. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11417. // best_seller = @"and best_seller > 0";
  11418. // order_best_seller = @"m.best_seller desc,m.name asc";
  11419. // }
  11420. //
  11421. // // price
  11422. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11423. // NSString *price = params[@"price"];
  11424. // price_min = 0;
  11425. // price_max = MAXFLOAT;
  11426. // if (user) {
  11427. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11428. // NSMutableString *priceName = [NSMutableString string];
  11429. // for (int i = 0; i < priceTypeArray.count; i++) {
  11430. // NSString *pricetype = priceTypeArray[i];
  11431. // pricetype = [self translateSingleQuote:pricetype];
  11432. // if (i == 0) {
  11433. // [priceName appendFormat:@"'%@'",pricetype];
  11434. // } else {
  11435. // [priceName appendFormat:@",'%@'",pricetype];
  11436. // }
  11437. // }
  11438. //
  11439. // if ([price isEqualToString:@"Display All"]) {
  11440. // price = [NSString stringWithFormat:@""];
  11441. // } else if([price containsString:@"+"]){
  11442. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11443. // price_min = [price doubleValue];
  11444. // 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];
  11445. // } else {
  11446. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11447. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11448. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11449. // 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];
  11450. // }
  11451. // } else {
  11452. // price = @"";
  11453. // }
  11454. //
  11455. // // sold_by_qty : Sold in quantities of %@
  11456. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11457. // NSString *qty = params[@"sold_by_qty"];
  11458. // if ([qty isEqualToString:@"Display All"]) {
  11459. // qty = @"";
  11460. // } else {
  11461. // qty = [self translateSingleQuote:qty];
  11462. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11463. // }
  11464. //
  11465. // // category
  11466. // NSString *category_id = params[@"ctgId"];
  11467. // NSMutableArray *cate_id_array = nil;
  11468. // NSMutableString *cateWhere = [NSMutableString string];
  11469. //
  11470. // if ([category_id isEqualToString:@""] || !category_id) {
  11471. // [cateWhere appendString:@"1 = 1"];
  11472. // } else {
  11473. //
  11474. // if ([category_id containsString:@","]) {
  11475. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11476. // } else {
  11477. // cate_id_array = [@[category_id] mutableCopy];
  11478. // }
  11479. // /*-----------*/
  11480. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11481. //
  11482. // [cateWhere appendString:@"("];
  11483. // for (int i = 0; i < cate_id_array.count; i++) {
  11484. //
  11485. // for (NSString *key0 in cateDic.allKeys) {
  11486. //
  11487. // if ([key0 containsString:@"category_"]) {
  11488. //
  11489. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11490. //
  11491. // for (NSString *key1 in category0.allKeys) {
  11492. //
  11493. // if ([key1 containsString:@"category_"]) {
  11494. //
  11495. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11496. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11497. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11498. //
  11499. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11500. // if (i == 0) {
  11501. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11502. // } else {
  11503. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11504. // }
  11505. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11506. // [category0 setValue:category1 forKey:key1];
  11507. // [cateDic setValue:category0 forKey:key0];
  11508. //
  11509. // }
  11510. //
  11511. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11512. //
  11513. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11514. //
  11515. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11516. // if (i == 0) {
  11517. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11518. // } else {
  11519. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11520. // }
  11521. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11522. // [cateDic setValue:category0 forKey:key0];
  11523. //
  11524. // }
  11525. //
  11526. // }
  11527. //
  11528. // }
  11529. //
  11530. // }
  11531. //
  11532. // }
  11533. //
  11534. // }
  11535. // [cateWhere appendString:@")"];
  11536. //
  11537. // [filter setValue:cateDic forKey:@"category"];
  11538. // }
  11539. //
  11540. // // where bestseller > 0 order by bestseller desc
  11541. // // sql query: alert availability(int) best_seller(int) price qty
  11542. //
  11543. // 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];
  11544. //
  11545. //
  11546. // // count
  11547. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11548. //
  11549. // }
  11550. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11551. //
  11552. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11553. //
  11554. //
  11555. //
  11556. //
  11557. // if (!user) {
  11558. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11559. // }
  11560. //
  11561. // [ret setValue:filter forKey:@"filter"];
  11562. //
  11563. //
  11564. //
  11565. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11566. //
  11567. // sqlite3_stmt * statement;
  11568. // [ret setValue:@"2" forKey:@"result"];
  11569. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11570. //
  11571. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11572. // // int count=0;
  11573. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11574. // {
  11575. //
  11576. // int i=0;
  11577. // while (sqlite3_step(statement) == SQLITE_ROW)
  11578. // {
  11579. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11580. //
  11581. //
  11582. // char *name = (char*)sqlite3_column_text(statement, 0);
  11583. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11584. //
  11585. // char *description = (char*)sqlite3_column_text(statement, 1);
  11586. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11587. //
  11588. //
  11589. //
  11590. // int product_id = sqlite3_column_int(statement, 2);
  11591. //
  11592. //
  11593. // int wid = sqlite3_column_int(statement, 3);
  11594. // int closeout = sqlite3_column_int(statement, 4);
  11595. // int cid = sqlite3_column_int(statement, 5);
  11596. // int wisdelete = sqlite3_column_int(statement, 6);
  11597. // int more_color = sqlite3_column_int(statement, 7);
  11598. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11599. //
  11600. // if(wid !=0 && wisdelete != 1)
  11601. // [item setValue:@"true" forKey:@"wish_exists"];
  11602. // else
  11603. // [item setValue:@"false" forKey:@"wish_exists"];
  11604. //
  11605. // if(closeout==0)
  11606. // [item setValue:@"false" forKey:@"is_closeout"];
  11607. // else
  11608. // [item setValue:@"true" forKey:@"is_closeout"];
  11609. //
  11610. // if(cid==0)
  11611. // [item setValue:@"false" forKey:@"cart_exists"];
  11612. // else
  11613. // [item setValue:@"true" forKey:@"cart_exists"];
  11614. //
  11615. // if (more_color == 0) {
  11616. // [item setObject:@(false) forKey:@"more_color"];
  11617. // } else if (more_color == 1) {
  11618. // [item setObject:@(true) forKey:@"more_color"];
  11619. // }
  11620. //
  11621. // [item addEntriesFromDictionary:imgjson];
  11622. //
  11623. //
  11624. // // [item setValue:nsurl forKey:@"img"];
  11625. // [item setValue:nsname forKey:@"fash_name"];
  11626. // [item setValue:nsdescription forKey:@"description"];
  11627. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11628. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11629. // i++;
  11630. // }
  11631. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11632. // [ret setObject:items forKey:@"items"];
  11633. // sqlite3_finalize(statement);
  11634. // }
  11635. //
  11636. // [iSalesDB close_db:db];
  11637. //
  11638. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11639. //
  11640. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11641. // dispatch_async(dispatch_get_main_queue(), ^{
  11642. //
  11643. // result(ret);
  11644. // });
  11645. //
  11646. // });
  11647. //
  11648. //
  11649. //
  11650. //}
  11651. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11652. //{
  11653. // [self itemsearch:params limited:YES completionHandler:result];
  11654. //}
  11655. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11656. {
  11657. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11658. NSString* offline_command=params[@"offline_Command"];
  11659. NSMutableDictionary* ret=nil;
  11660. if([offline_command isEqualToString:@"model_NIYMAL"])
  11661. {
  11662. NSString* category = params[@"category"];
  11663. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11664. }
  11665. dispatch_async(dispatch_get_main_queue(), ^{
  11666. result(ret);
  11667. });
  11668. });
  11669. }
  11670. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11671. {
  11672. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11673. int sort = [[params objectForKey:@"sort"] intValue];
  11674. NSString *sort_str = @"";
  11675. switch (sort) {
  11676. case 0:{
  11677. sort_str = @"order by w.modify_time desc";
  11678. }
  11679. break;
  11680. case 1:{
  11681. sort_str = @"order by w.modify_time asc";
  11682. }
  11683. break;
  11684. case 2:{
  11685. sort_str = @"order by m.name asc";
  11686. }
  11687. break;
  11688. case 3:{
  11689. sort_str = @"order by m.name desc";
  11690. }
  11691. break;
  11692. case 4:{
  11693. sort_str = @"order by m.description asc";
  11694. }
  11695. break;
  11696. default:
  11697. break;
  11698. }
  11699. // NSString* user = appDelegate.user;
  11700. sqlite3 *db = [iSalesDB get_db];
  11701. // order by w.create_time
  11702. 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];
  11703. // 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];
  11704. sqlite3_stmt * statement;
  11705. NSDate *date1 = [NSDate date];
  11706. // NSDate *date2 = nil;
  11707. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11708. int count=0;
  11709. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11710. {
  11711. while (sqlite3_step(statement) == SQLITE_ROW)
  11712. {
  11713. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11714. int product_id = sqlite3_column_double(statement, 0);
  11715. char *description = (char*)sqlite3_column_text(statement, 1);
  11716. if(description==nil)
  11717. description= "";
  11718. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11719. int item_id = sqlite3_column_double(statement, 2);
  11720. NSDate *date_image = [NSDate date];
  11721. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11722. printf("image : ");
  11723. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11724. // char *url = (char*)sqlite3_column_text(statement, 3);
  11725. // if(url==nil)
  11726. // url="";
  11727. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11728. int qty = sqlite3_column_int(statement, 3);
  11729. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11730. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11731. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11732. item[@"description"]= nsdescription;
  11733. item[@"img"]= nsurl;
  11734. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11735. count++;
  11736. }
  11737. printf("total time:");
  11738. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11739. ret[@"count"]= [NSNumber numberWithInt:count];
  11740. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11741. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11742. ret[@"result"]= [NSNumber numberWithInt:2];
  11743. sqlite3_finalize(statement);
  11744. }
  11745. [iSalesDB close_db:db];
  11746. dispatch_async(dispatch_get_main_queue(), ^{
  11747. UIApplication * app = [UIApplication sharedApplication];
  11748. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11749. appDelegate.wish_count =count;
  11750. [appDelegate update_count_mark];
  11751. result(ret);
  11752. });
  11753. });
  11754. }
  11755. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11756. {
  11757. // UIApplication * app = [UIApplication sharedApplication];
  11758. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11759. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11760. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11761. sqlite3 *db = [iSalesDB get_db];
  11762. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11763. NSString* product_id=params[@"product_id"];
  11764. NSString *item_count_str = params[@"item_count"];
  11765. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11766. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11767. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11768. // NSString *sql = @"";
  11769. for(int i=0;i<arr.count;i++)
  11770. {
  11771. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11772. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11773. __block int cart_count = 0;
  11774. if (!item_count_str) {
  11775. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11776. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11777. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11778. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11779. cart_count = [[model_set_components lastObject] intValue];
  11780. }];
  11781. }
  11782. if(count==0)
  11783. {
  11784. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11785. sqlite3_stmt *stmt;
  11786. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11787. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11788. if (item_count_arr) {
  11789. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11790. } else {
  11791. sqlite3_bind_int(stmt,2,cart_count);
  11792. }
  11793. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11794. [iSalesDB execSql:@"ROLLBACK" db:db];
  11795. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11796. [iSalesDB close_db:db];
  11797. DebugLog(@"add to wishlist error");
  11798. dispatch_async(dispatch_get_main_queue(), ^{
  11799. result(ret);
  11800. });
  11801. }
  11802. } else {
  11803. int qty = 0;
  11804. if (item_count_arr) {
  11805. qty = [item_count_arr[i] intValue];
  11806. } else {
  11807. qty = cart_count;
  11808. }
  11809. 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]];
  11810. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11811. [iSalesDB execSql:@"ROLLBACK" db:db];
  11812. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11813. [iSalesDB close_db:db];
  11814. DebugLog(@"add to wishlist error");
  11815. dispatch_async(dispatch_get_main_queue(), ^{
  11816. result(ret);
  11817. });
  11818. }
  11819. }
  11820. }
  11821. // [iSalesDB execSql:sql db:db];
  11822. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11823. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11824. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11825. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11826. [iSalesDB close_db:db];
  11827. ret[@"result"]= [NSNumber numberWithInt:2];
  11828. dispatch_async(dispatch_get_main_queue(), ^{
  11829. UIApplication * app = [UIApplication sharedApplication];
  11830. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11831. appDelegate.wish_count =count;
  11832. [appDelegate update_count_mark];
  11833. result(ret);
  11834. });
  11835. });
  11836. }
  11837. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11838. {
  11839. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11840. bool bLogin = appDelegate.bLogin;
  11841. __block NSString* contact_id = appDelegate.contact_id;
  11842. __block NSString* user = appDelegate.user;
  11843. __block NSString* order_code = appDelegate.order_code;
  11844. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11845. NSString* model_name = [params valueForKey:@"product_name"];
  11846. NSString* product_id = [params valueForKey:@"product_id"];
  11847. NSString* upc_code = [params valueForKey:@"upc_code"];
  11848. NSString* category = [params valueForKey:@"category"];
  11849. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11850. if(category==nil) {
  11851. category = default_category_id;
  11852. } else {
  11853. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11854. // 参数重有多个category id
  11855. if (arr_0.count > 1) {
  11856. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11857. // 取交集
  11858. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11859. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11860. [set_0 intersectSet:set_1];
  11861. if (set_0.count == 1) {
  11862. category = (NSString *)[set_0 anyObject];
  11863. } else {
  11864. if ([set_0 containsObject:default_category_id]) {
  11865. category = default_category_id;
  11866. } else {
  11867. category = (NSString *)[set_0 anyObject];
  11868. }
  11869. }
  11870. }
  11871. }
  11872. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11873. sqlite3 *db = [iSalesDB get_db];
  11874. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11875. NSString *sqlQuery = nil;
  11876. if(product_id==nil)
  11877. if(model_name==nil)
  11878. {
  11879. 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='%@';
  11880. }
  11881. else
  11882. {
  11883. 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='%@';
  11884. }
  11885. else
  11886. 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=%@;
  11887. sqlite3_stmt * statement;
  11888. [ret setValue:@"2" forKey:@"result"];
  11889. [ret setValue:@"3" forKey:@"detail_section_count"];
  11890. // int count=0;
  11891. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11892. {
  11893. if (sqlite3_step(statement) == SQLITE_ROW)
  11894. {
  11895. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11896. char *name = (char*)sqlite3_column_text(statement, 0);
  11897. if(name==nil)
  11898. name="";
  11899. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11900. char *description = (char*)sqlite3_column_text(statement, 1);
  11901. if(description==nil)
  11902. description="";
  11903. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11904. int product_id = sqlite3_column_int(statement, 2);
  11905. char *color = (char*)sqlite3_column_text(statement, 3);
  11906. if(color==nil)
  11907. color="";
  11908. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11909. //
  11910. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11911. // if(legcolor==nil)
  11912. // legcolor="";
  11913. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11914. //
  11915. //
  11916. int availability = sqlite3_column_int(statement, 5);
  11917. //
  11918. int incoming_stock = sqlite3_column_int(statement, 6);
  11919. char *demension = (char*)sqlite3_column_text(statement, 7);
  11920. if(demension==nil)
  11921. demension="";
  11922. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11923. // ,,,,,,,,,
  11924. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11925. if(seat_height==nil)
  11926. seat_height="";
  11927. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11928. char *material = (char*)sqlite3_column_text(statement, 9);
  11929. if(material==nil)
  11930. material="";
  11931. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11932. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11933. if(box_dim==nil)
  11934. box_dim="";
  11935. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11936. char *volume = (char*)sqlite3_column_text(statement, 11);
  11937. if(volume==nil)
  11938. volume="";
  11939. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11940. double weight = sqlite3_column_double(statement, 12);
  11941. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11942. if(model_set==nil)
  11943. model_set="";
  11944. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11945. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11946. if(load_ability==nil)
  11947. load_ability="";
  11948. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11949. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11950. // if(default_category==nil)
  11951. // default_category="";
  11952. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11953. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11954. if(fabric_content==nil)
  11955. fabric_content="";
  11956. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11957. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11958. if(assembling==nil)
  11959. assembling="";
  11960. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11961. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11962. if(made_in==nil)
  11963. made_in="";
  11964. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11965. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11966. if(special_remarks==nil)
  11967. special_remarks="";
  11968. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11969. int stockUcom = sqlite3_column_double(statement, 20);
  11970. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11971. if(product_group==nil)
  11972. product_group="";
  11973. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11974. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11975. // if(fashion_selector==nil)
  11976. // fashion_selector="";
  11977. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11978. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11979. if(selector_field==nil)
  11980. selector_field="";
  11981. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11982. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11983. if(property_field==nil)
  11984. property_field="";
  11985. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11986. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11987. if(packaging==nil)
  11988. packaging="";
  11989. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11990. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11991. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11992. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11993. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11994. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11995. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11996. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11997. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11998. NSString* Availability=nil;
  11999. if(availability>0)
  12000. Availability=[NSString stringWithFormat:@"%d",availability];
  12001. else
  12002. Availability = @"Out of Stock";
  12003. [img_section setValue:Availability forKey:@"Availability"];
  12004. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12005. char *eta = (char*)sqlite3_column_text(statement, 25);
  12006. if(eta==nil)
  12007. eta="";
  12008. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12009. if ([nseta isEqualToString:@"null"]) {
  12010. nseta = @"";
  12011. }
  12012. if (availability <= 0) {
  12013. [img_section setValue:nseta forKey:@"ETA"];
  12014. }
  12015. int item_id = sqlite3_column_int(statement, 26);
  12016. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12017. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12018. NSString* Price=nil;
  12019. if(bLogin==false)
  12020. Price=@"Must Sign in.";
  12021. else
  12022. {
  12023. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12024. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12025. if(price==nil)
  12026. Price=@"No Price.";
  12027. else
  12028. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12029. }
  12030. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12031. [img_section setValue:Price forKey:@"price"];
  12032. [img_section setValue:nsname forKey:@"model_name"];
  12033. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12034. if (order_code) { // 离线order code即so#
  12035. 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];
  12036. __block int cartQTY = 0;
  12037. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12038. cartQTY = sqlite3_column_int(stmt, 0);
  12039. }];
  12040. if (cartQTY > 0) {
  12041. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12042. }
  12043. }
  12044. [ret setObject:img_section forKey:@"img_section"];
  12045. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12046. int detail0_item_count=0;
  12047. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12048. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12049. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12050. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12051. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12052. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12053. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12054. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12055. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12056. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12057. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12058. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12059. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12060. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12061. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12062. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12063. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12064. {
  12065. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12066. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12067. }
  12068. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12069. [detail0_section setValue:@"kv" forKey:@"type"];
  12070. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12071. [ret setObject:detail0_section forKey:@"detail_0"];
  12072. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12073. // [detail1_section setValue:@"detail" forKey:@"target"];
  12074. // [detail1_section setValue:@"popup" forKey:@"action"];
  12075. // [detail1_section setValue:@"content" forKey:@"type"];
  12076. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12077. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12078. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12079. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12080. [ret setObject:detail1_section forKey:@"detail_1"];
  12081. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12082. [detail2_section setValue:@"detail" forKey:@"target"];
  12083. [detail2_section setValue:@"popup" forKey:@"action"];
  12084. [detail2_section setValue:@"content" forKey:@"type"];
  12085. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12086. [detail2_section setValue:@"true" forKey:@"single_row"];
  12087. [detail2_section setValue:@"local" forKey:@"data"];
  12088. [ret setObject:detail2_section forKey:@"detail_2"];
  12089. }
  12090. sqlite3_finalize(statement);
  12091. }
  12092. else
  12093. {
  12094. [ret setValue:@"8" forKey:@"result"];
  12095. }
  12096. // DebugLog(@"count:%d",count);
  12097. [iSalesDB close_db:db];
  12098. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12099. dispatch_async(dispatch_get_main_queue(), ^{
  12100. result(ret);
  12101. });
  12102. });
  12103. }
  12104. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12105. //{
  12106. //
  12107. // UIApplication * app = [UIApplication sharedApplication];
  12108. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12109. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12110. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12111. // sqlite3 *db = [iSalesDB get_db];
  12112. // NSString* collectId=params[@"collectId"];
  12113. //
  12114. //
  12115. //
  12116. //
  12117. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12118. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12119. // [iSalesDB execSql:sqlQuery db:db];
  12120. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12121. // [iSalesDB close_db:db];
  12122. //
  12123. //
  12124. // ret[@"result"]= [NSNumber numberWithInt:2];
  12125. // dispatch_async(dispatch_get_main_queue(), ^{
  12126. //
  12127. // appDelegate.wish_count =count;
  12128. //
  12129. // [appDelegate update_count_mark];
  12130. // result(ret);
  12131. // });
  12132. //
  12133. // });
  12134. //}
  12135. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12136. {
  12137. UIApplication * app = [UIApplication sharedApplication];
  12138. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12139. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12140. [iSalesDB disable_trigger];
  12141. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12142. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12143. [iSalesDB enable_trigger];
  12144. dispatch_async(dispatch_get_main_queue(), ^{
  12145. //
  12146. // NSString* user = [params valueForKey:@"user"];
  12147. //
  12148. // NSString* password = [params valueForKey:@"password"];
  12149. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12150. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12151. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12152. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12153. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12154. [appDelegate update_count_mark];
  12155. appDelegate.can_show_price =false;
  12156. appDelegate.can_see_price =false;
  12157. appDelegate.can_create_portfolio =false;
  12158. appDelegate.can_create_order =false;
  12159. appDelegate.can_cancel_order =false;
  12160. appDelegate.can_set_cart_price =false;
  12161. appDelegate.can_delete_order =false;
  12162. appDelegate.can_submit_order =false;
  12163. appDelegate.can_set_tearsheet_price =false;
  12164. appDelegate.can_update_contact_info = false;
  12165. appDelegate.save_order_logout = false;
  12166. appDelegate.submit_order_logout = false;
  12167. appDelegate.alert_sold_in_quantities = false;
  12168. appDelegate.ipad_perm =nil ;
  12169. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12170. appDelegate.OrderFilter= nil;
  12171. [appDelegate SetSo:nil];
  12172. [appDelegate set_main_button_panel];
  12173. result(ret);
  12174. });
  12175. });
  12176. }
  12177. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12178. {
  12179. // iSalesDB.
  12180. // UIApplication * app = [UIApplication sharedApplication];
  12181. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12182. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12183. [iSalesDB disable_trigger];
  12184. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12185. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12186. [iSalesDB enable_trigger];
  12187. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12188. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12189. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12190. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12191. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12192. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12193. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12194. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12195. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12196. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12197. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12198. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12199. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12200. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12201. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12202. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12203. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12204. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12205. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12206. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12207. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12208. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12209. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12210. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12211. [arr_name addObject:customer_first_name];
  12212. [arr_name addObject:customer_last_name];
  12213. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12214. // default ship from
  12215. 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';";
  12216. __block NSString *cid = @"";
  12217. __block NSString *name = @"";
  12218. __block NSString *ext = @"";
  12219. __block NSString *contact = @"";
  12220. __block NSString *email = @"";
  12221. __block NSString *fax = @"";
  12222. __block NSString *phone = @"";
  12223. sqlite3 *db = [iSalesDB get_db];
  12224. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12225. cid = [self textAtColumn:0 statement:statment];
  12226. name = [self textAtColumn:1 statement:statment];
  12227. ext = [self textAtColumn:2 statement:statment];
  12228. contact = [self textAtColumn:3 statement:statment];
  12229. email = [self textAtColumn:4 statement:statment];
  12230. fax = [self textAtColumn:5 statement:statment];
  12231. phone = [self textAtColumn:6 statement:statment];
  12232. }];
  12233. NSString* so_id = [self get_offline_soid:db];
  12234. if(so_id==nil)
  12235. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12236. 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];
  12237. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12238. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12239. //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'
  12240. //soId
  12241. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12242. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12243. sqlite3_stmt * statement;
  12244. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12245. {
  12246. if (sqlite3_step(statement) == SQLITE_ROW)
  12247. {
  12248. // char *name = (char*)sqlite3_column_text(statement, 1);
  12249. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12250. //ret = sqlite3_column_int(statement, 0);
  12251. char *soId = (char*)sqlite3_column_text(statement, 0);
  12252. if(soId==nil)
  12253. soId="";
  12254. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12255. [ret setValue:nssoId forKey:@"soId"];
  12256. [ret setValue:nssoId forKey:@"orderCode"];
  12257. }
  12258. sqlite3_finalize(statement);
  12259. }
  12260. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12261. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12262. [iSalesDB close_db:db];
  12263. dispatch_async(dispatch_get_main_queue(), ^{
  12264. result(ret);
  12265. });
  12266. });
  12267. }
  12268. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12269. {
  12270. UIApplication * app = [UIApplication sharedApplication];
  12271. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12272. bool can_create_backorder= appDelegate.can_create_backorder;
  12273. NSString* user = appDelegate.user;
  12274. NSString* contact_id = appDelegate.contact_id;
  12275. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12276. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12277. sqlite3 *db = [iSalesDB get_db];
  12278. NSString* orderCode=params[@"orderCode"];
  12279. int sort = [[params objectForKey:@"sort"] intValue];
  12280. NSString *sort_str = @"";
  12281. switch (sort) {
  12282. case 0:{
  12283. sort_str = @"order by c.modify_time desc";
  12284. }
  12285. break;
  12286. case 1:{
  12287. sort_str = @"order by c.modify_time asc";
  12288. }
  12289. break;
  12290. case 2:{
  12291. sort_str = @"order by m.name asc";
  12292. }
  12293. break;
  12294. case 3:{
  12295. sort_str = @"order by m.name desc";
  12296. }
  12297. break;
  12298. case 4:{
  12299. sort_str = @"order by m.description asc";
  12300. }
  12301. break;
  12302. default:
  12303. break;
  12304. }
  12305. 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 ];
  12306. // 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 ];
  12307. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12308. DebugLog(@"offline_login sql:%@",sqlQuery);
  12309. sqlite3_stmt * statement;
  12310. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12311. NSDate *date1 = [NSDate date];
  12312. int count=0;
  12313. int cart_count=0;
  12314. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12315. if ( dbresult== SQLITE_OK)
  12316. {
  12317. while (sqlite3_step(statement) == SQLITE_ROW)
  12318. {
  12319. // NSDate *row_date = [NSDate date];
  12320. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12321. int product_id = sqlite3_column_int(statement, 0);
  12322. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12323. int item_id = sqlite3_column_int(statement, 7);
  12324. NSString* Price=nil;
  12325. if(str_price==nil)
  12326. {
  12327. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12328. // NSDate *price_date = [NSDate date];
  12329. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12330. // DebugLog(@"price time interval");
  12331. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12332. if(price==nil)
  12333. Price=@"No Price.";
  12334. else
  12335. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12336. }
  12337. else
  12338. {
  12339. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12340. }
  12341. double discount = sqlite3_column_double(statement, 2);
  12342. int item_count = sqlite3_column_int(statement, 3);
  12343. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12344. NSString *nsline_note=nil;
  12345. if(line_note!=nil)
  12346. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12347. char *name = (char*)sqlite3_column_text(statement, 5);
  12348. NSString *nsname=nil;
  12349. if(name!=nil)
  12350. nsname= [[NSString alloc]initWithUTF8String:name];
  12351. char *description = (char*)sqlite3_column_text(statement, 6);
  12352. NSString *nsdescription=nil;
  12353. if(description!=nil)
  12354. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12355. int stockUom = sqlite3_column_int(statement, 8);
  12356. int _id = sqlite3_column_int(statement, 9);
  12357. int availability = sqlite3_column_int(statement, 10);
  12358. // NSDate *subtotal_date = [NSDate date];
  12359. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12360. // DebugLog(@"subtotal_date time interval");
  12361. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12362. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12363. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12364. int carton=[bsubtotaljson[@"carton"] intValue];
  12365. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12366. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12367. // NSDate *img_date = [NSDate date];
  12368. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12369. // DebugLog(@"img_date time interval");
  12370. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12371. itemjson[@"model"]=nsname;
  12372. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12373. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12374. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12375. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12376. itemjson[@"check"]=@"true";
  12377. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12378. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12379. itemjson[@"unit_price"]=Price;
  12380. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12381. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12382. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12383. itemjson[@"note"]=nsline_note;
  12384. if (!can_create_backorder) {
  12385. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12386. }
  12387. // NSDate *date2 = [NSDate date];
  12388. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12389. // DebugLog(@"model_bundle time interval");
  12390. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12391. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12392. count++;
  12393. // DebugLog(@"row time interval");
  12394. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12395. }
  12396. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12397. sqlite3_finalize(statement);
  12398. }
  12399. DebugLog(@"request cart total time interval");
  12400. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12401. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12402. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12403. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12404. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12405. ret[@"count"]=[NSNumber numberWithInt:count ];
  12406. ret[@"mode"]=@"Regular Mode";
  12407. [iSalesDB close_db:db];
  12408. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12409. DebugLog(@"general notes :%@",general_note);
  12410. ret[@"general_note"]= general_note;
  12411. dispatch_async(dispatch_get_main_queue(), ^{
  12412. result(ret);
  12413. });
  12414. });
  12415. }
  12416. @end