OLDataProvider.m 437 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626
  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 "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  24. {
  25. NSString* ret= nil;
  26. NSString *sqlQuery = nil;
  27. // 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
  28. 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];
  29. sqlite3_stmt * statement;
  30. // int count=0;
  31. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  32. {
  33. if (sqlite3_step(statement) == SQLITE_ROW)
  34. {
  35. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  36. if(imgurl==nil)
  37. imgurl="";
  38. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  39. ret=nsimgurl;
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. else
  44. {
  45. ret=@"";
  46. }
  47. // [iSalesDB close_db:db];
  48. // DebugLog(@"data string: %@",ret );
  49. return ret;
  50. }
  51. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  52. {
  53. //生成portfolio pdf需要的数据
  54. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  55. data[@"npd_url"]=@"www.newpacificdirect.com";
  56. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  57. [formatter setDateFormat:@"MMMM yyyy"];
  58. NSString* date = [formatter stringFromDate:[NSDate date]];
  59. data[@"create_month"]=date;
  60. data[@"company_name"]=@"New Pacific Direct,Inc.";
  61. data[@"catalog_name"]=params[@"catalog_name"];
  62. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  63. data[@"TOTAL_PAGE"]=0;
  64. data[@"CURRENT_PAGE"]=0;
  65. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  66. bool show_stockout=[params[@"show_stockout"] boolValue];
  67. NSString *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 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",params[@"product_ids"],params[@"product_ids"]];
  68. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  69. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  70. sqlite3 *db = [iSalesDB get_db];
  71. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  72. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  73. int product_id = sqlite3_column_int(stmt, 0);
  74. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  75. NSString *name = [self textAtColumn:1 statement:stmt];
  76. NSString *description = [self textAtColumn:2 statement:stmt];
  77. double price = sqlite3_column_double(stmt, 3);
  78. double discount = sqlite3_column_double(stmt,4);
  79. int qty = sqlite3_column_int(stmt, 5);
  80. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  81. int is_percentage = sqlite3_column_int(stmt, 6);
  82. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  83. int item_id = sqlite3_column_int(stmt, 7);
  84. // int fashion_id = sqlite3_column_int(stmt, 8);
  85. NSString *line_note = [self textAtColumn:9 statement:stmt];
  86. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  87. double percent = sqlite3_column_double(stmt, 10);
  88. // int portfolio_id = sqlite3_column_int(stmt, 11);
  89. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  90. int availability = sqlite3_column_int(stmt, 12);
  91. NSString *color = [self textAtColumn:13 statement:stmt];
  92. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  93. NSString *demension = [self textAtColumn:15 statement:stmt];
  94. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  95. NSString *material = [self textAtColumn:17 statement:stmt];
  96. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  97. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  98. double volume = sqlite3_column_double(stmt, 19);
  99. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  100. double weight = sqlite3_column_double(stmt, 20);
  101. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  102. int model_set = sqlite3_column_int(stmt, 21);
  103. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  104. double load_ability = sqlite3_column_double(stmt, 22);
  105. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  106. NSString *assembling = [self textAtColumn:24 statement:stmt];
  107. NSString *made_in = [self textAtColumn:25 statement:stmt];
  108. if(is_percentage==0&&qty==0&&!show_stockout)
  109. {
  110. return;
  111. }
  112. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  113. {
  114. return;
  115. }
  116. if(is_percentage)
  117. {
  118. qty=availability;
  119. }
  120. else
  121. {
  122. percent=1;
  123. }
  124. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  125. // if ([qty_null isEqualToString:@"null"]) {
  126. // qty=availability;
  127. // }
  128. //
  129. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  130. // if ([qty_percent_null isEqualToString:@"null"]) {
  131. // percentage=1;
  132. // }
  133. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)(qty*percent+0.5)];
  134. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  135. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  136. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  137. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  138. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  139. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  140. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  141. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  142. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  143. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  144. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  145. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  146. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  147. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  148. if([params[@"availability"] boolValue]==false || (s_qty.length==0 && s_availability.length==0))
  149. str_availability=@"";
  150. if([params[@"color"] boolValue]==false || color.length==0 )
  151. str_color=@"";
  152. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  153. str_model_set=@"";
  154. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  155. str_legcolor=@"";
  156. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  157. str_demension=@"";
  158. if([params[@"seat_height"] boolValue]==false && seat_height.length==0 )
  159. str_seat_height=@"";
  160. if([params[@"material"] boolValue]==false && material.length==0 )
  161. str_meterial=@"";
  162. if([params[@"box_dimension"] boolValue]==false && box_dim.length==0 )
  163. str_box_dim=@"";
  164. if([params[@"volume"] boolValue]==false && s_volume.length==0 )
  165. str_volume=@"";
  166. if([params[@"weight"] boolValue]==false && s_weight.length==0 )
  167. str_weight=@"";
  168. if([params[@"load_ability"] boolValue]==false && s_load_ability.length==0 )
  169. str_load_ability=@"";
  170. if([params[@"fabric_content"] boolValue]==false && fabric_content.length==0 )
  171. str_fabric_content=@"";
  172. if([params[@"assembling"] boolValue]==false && assembling.length==0 )
  173. str_assembling=@"";
  174. if([params[@"made"] boolValue]==false && made_in.length==0 )
  175. str_made_in=@"";
  176. if([params[@"show_line_note"] boolValue]==false && line_note.length==0 )
  177. str_line_note=@"";
  178. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  179. [arr_detail addObject:str_color];
  180. [arr_detail addObject:str_model_set];
  181. [arr_detail addObject:str_legcolor];
  182. [arr_detail addObject:str_demension];
  183. [arr_detail addObject:str_seat_height];
  184. [arr_detail addObject:str_meterial];
  185. [arr_detail addObject:str_box_dim];
  186. [arr_detail addObject:str_volume];
  187. [arr_detail addObject:str_weight];
  188. [arr_detail addObject:str_load_ability];
  189. [arr_detail addObject:str_fabric_content];
  190. [arr_detail addObject:str_assembling];
  191. [arr_detail addObject:str_made_in];
  192. [arr_detail addObject:str_line_note];
  193. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  194. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  195. //model image;
  196. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  197. // qr image
  198. NSString* temp = NSTemporaryDirectory();
  199. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  200. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  201. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  202. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  203. NSString* set_price=@"";
  204. NSString *price_null = [self textAtColumn:3 statement:stmt];
  205. if ([price_null isEqualToString:@"null"]) {
  206. }
  207. else
  208. {
  209. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount)];
  210. }
  211. NSString* get_price=@"";
  212. {
  213. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  214. // DebugLog(@"price time interval");
  215. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  216. if(price!=nil)
  217. get_price=[NSString stringWithFormat:@"Price: $ %.2f",price.floatValue];
  218. }
  219. NSMutableDictionary *item = @{
  220. //@"linenotes": line_note,
  221. // @"product_id": product_id_string,
  222. // @"available_qty": @(qty),
  223. // @"available_percent" : @(percent),
  224. // @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  225. // @"item_id": [NSString stringWithFormat:@"%d",item_id],
  226. // @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  227. @"img": img_path,
  228. @"img_qr": qrpath,
  229. @"special_price": set_price,
  230. @"price": get_price,
  231. @"name": name,
  232. @"description": description,
  233. @"detail": detail
  234. }.mutableCopy;
  235. // if (percentage) {
  236. // [item removeObjectForKey:@"available_qty"];
  237. // } else {
  238. // [item removeObjectForKey:@"available_percent"];
  239. // }
  240. //
  241. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  242. // if ([qty_null isEqualToString:@"null"]) {
  243. // [item removeObjectForKey:@"available_qty"];
  244. // }
  245. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  246. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  247. }];
  248. [iSalesDB close_db:db];
  249. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  250. // [dic setValue:@"" forKey:@"email_content"];
  251. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  252. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  253. //
  254. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  255. // cell[@"count"]=[NSNumber numberWithInt:10];
  256. grid[@"cell0"]=dic;
  257. data[@"grid0"]=grid;
  258. return data;
  259. }
  260. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  261. {
  262. NSString *path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  263. NSData *data = [NSData dataWithContentsOfFile:path];
  264. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  265. return ret;
  266. }
  267. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  268. {
  269. NSString* template_file=@"portfolio_3x2.json";
  270. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  271. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  272. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  273. DebugLog(@"%@",file);
  274. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  275. [dic setObject:@"Regular Mode" forKey:@"mode"];
  276. if (file) {
  277. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  278. [dic setObject:file forKey:@"pdf_path"];
  279. dic[@"isLocalFile"]=@"true";
  280. return [RAUtils dict2data:dic];
  281. }
  282. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  283. dic[@"isLocalFile"]=@"true";
  284. return [RAUtils dict2data:dic];
  285. }
  286. +(NSString*) get_offline_soid:(sqlite3*)db
  287. {
  288. NSString* soid=nil;
  289. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  290. [formatter setDateFormat:@"yyMMdd"];
  291. NSString* date = [formatter stringFromDate:[NSDate date]];
  292. for(int i=1;i<999;i++)
  293. {
  294. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  295. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  296. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  297. if(count==0)
  298. return soid;
  299. }
  300. return nil;
  301. }
  302. +(NSArray*) enumOfflineOrder
  303. {
  304. NSMutableArray* ret = [[NSMutableArray alloc] init];
  305. sqlite3 *db = [iSalesDB get_db];
  306. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  307. sqlite3_stmt * statement;
  308. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  309. if ( dbresult== SQLITE_OK)
  310. {
  311. while (sqlite3_step(statement) == SQLITE_ROW)
  312. {
  313. char *so_id = (char*)sqlite3_column_text(statement, 0);
  314. NSString* nsso_id=nil;
  315. if(so_id!=nil)
  316. {
  317. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  318. }
  319. [ret addObject:nsso_id];
  320. }
  321. sqlite3_finalize(statement);
  322. }
  323. [iSalesDB close_db:db];
  324. return ret;
  325. }
  326. +(void) uploadFile:(NSString*) file
  327. {
  328. NSData* data = [NSData dataWithContentsOfFile: file];
  329. UIApplication * app = [UIApplication sharedApplication];
  330. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  331. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  332. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  333. if(appDelegate.user!=nil)
  334. [params setValue:appDelegate.user forKey:@"user"];
  335. // if(appDelegate.contact_id!=nil)
  336. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  337. if(appDelegate.password!=nil)
  338. [params setValue:appDelegate.password forKey:@"password"];
  339. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  340. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  341. } error:nil];
  342. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  343. NSProgress *progress = nil;
  344. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  345. if (error) {
  346. NSString* err_msg = [error localizedDescription];
  347. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  348. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  349. NSLog(@"data string: %@",str);
  350. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  351. } else {
  352. NSLog(@"response ");
  353. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  354. // 再将NSData转为字符串
  355. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  356. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  357. NSLog(@"data string: %@",jsonStr);
  358. NSDictionary* json = responseObject;
  359. if([[json valueForKey:@"result"] intValue]==2)
  360. {
  361. // NSString* img_url_down = json[@"img_url_aname"];
  362. // NSString* img_url_up = json[@"img_url"];
  363. }
  364. else
  365. {
  366. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  367. }
  368. }
  369. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  370. }];
  371. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  372. //
  373. [uploadTask resume];
  374. }
  375. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  376. {
  377. NSString* serial= [[NSUUID UUID] UUIDString];
  378. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  379. NSString *cachefolder = [paths objectAtIndex:0];
  380. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  381. upfolder = [upfolder stringByAppendingPathComponent:serial];
  382. NSFileManager* fileManager = [NSFileManager defaultManager];
  383. BOOL bdir=YES;
  384. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  385. {
  386. NSError *error = nil;
  387. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  388. if(!bsuccess)
  389. DebugLog(@"Create UPLOAD folder failed");
  390. }
  391. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  392. ret[@"contact"]=[self prepareContact:serial];
  393. ret[@"wishlist"]=[self prepareWishlist:serial];
  394. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  395. ret[@"portfolio"] = [self preparePortfolio:serial];
  396. ret[@"view_portfolio"] = [self preparePDF:serial];
  397. NSString* str= [RAUtils dict2string:ret];
  398. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  399. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  400. NSError *error=nil;
  401. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  402. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  403. ZipArchive* zip = [[ZipArchive alloc] init];
  404. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  405. for(int i=0;i<arr_files.count;i++)
  406. {
  407. NSString* file=arr_files[i];
  408. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  409. }
  410. if( ![zip CloseZipFile2] )
  411. {
  412. zippath = @"";
  413. }
  414. ret[@"file"]=zippath;
  415. return ret;
  416. }
  417. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  418. {
  419. if(filename.length==0)
  420. return false;
  421. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  422. bool ret=false;
  423. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  424. NSString *cachefolder = [paths objectAtIndex:0];
  425. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  426. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  427. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  428. upfolder = [upfolder stringByAppendingPathComponent:serial];
  429. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  430. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  431. NSFileManager* fileManager = [NSFileManager defaultManager];
  432. BOOL bdir=NO;
  433. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  434. {
  435. NSError *error = nil;
  436. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  437. {
  438. ret=false;
  439. }
  440. else
  441. {
  442. ret=true;
  443. }
  444. }
  445. return ret;
  446. }
  447. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  448. {
  449. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  450. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  451. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  452. NSString* where=@" 1=1";
  453. if (ver!=nil) {
  454. where=@"is_dirty=1";
  455. }
  456. sqlite3 *db = [iSalesDB get_db];
  457. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  458. sqlite3_stmt * statement;
  459. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  460. int count=0;
  461. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  462. if ( dbresult== SQLITE_OK)
  463. {
  464. while (sqlite3_step(statement) == SQLITE_ROW)
  465. {
  466. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  467. int _id = sqlite3_column_int(statement, 0);
  468. int product_id = sqlite3_column_int(statement, 1);
  469. int item_id = sqlite3_column_int(statement, 2);
  470. int qty = sqlite3_column_int(statement, 3);
  471. char *create_time = (char*)sqlite3_column_text(statement, 4);
  472. NSString* nscreate_time=nil;
  473. if(create_time!=nil)
  474. {
  475. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  476. }
  477. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  478. NSString* nsmodify_time=nil;
  479. if(modify_time!=nil)
  480. {
  481. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  482. }
  483. int is_delete = sqlite3_column_int(statement, 6);
  484. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  485. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  486. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  487. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  488. itemjson[@"create_time"]=nscreate_time;
  489. itemjson[@"modify_time"]=nsmodify_time;
  490. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  491. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  492. count++;
  493. }
  494. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  495. sqlite3_finalize(statement);
  496. }
  497. ret[@"count"]=[NSNumber numberWithInt:count ];
  498. [iSalesDB close_db:db];
  499. return ret;
  500. }
  501. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  502. {
  503. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  504. UIApplication * app = [UIApplication sharedApplication];
  505. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  506. 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 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 ];
  507. // 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 ];
  508. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  509. DebugLog(@"offline_login sql:%@",sqlQuery);
  510. sqlite3_stmt * statement;
  511. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  512. int count=0;
  513. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  514. if ( dbresult== SQLITE_OK)
  515. {
  516. while (sqlite3_step(statement) == SQLITE_ROW)
  517. {
  518. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  519. int product_id = sqlite3_column_int(statement, 0);
  520. char *str_price = (char*)sqlite3_column_text(statement, 1);
  521. int item_id = sqlite3_column_int(statement, 7);
  522. NSString* Price=nil;
  523. if(str_price==nil)
  524. {
  525. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  526. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  527. if(price==nil)
  528. Price=@"No Price.";
  529. else
  530. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  531. }
  532. else
  533. {
  534. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  535. }
  536. double discount = sqlite3_column_double(statement, 2);
  537. int item_count = sqlite3_column_int(statement, 3);
  538. char *line_note = (char*)sqlite3_column_text(statement, 4);
  539. NSString *nsline_note=nil;
  540. if(line_note!=nil)
  541. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  542. char *name = (char*)sqlite3_column_text(statement, 5);
  543. NSString *nsname=nil;
  544. if(name!=nil)
  545. nsname= [[NSString alloc]initWithUTF8String:name];
  546. char *description = (char*)sqlite3_column_text(statement, 6);
  547. NSString *nsdescription=nil;
  548. if(description!=nil)
  549. nsdescription= [[NSString alloc]initWithUTF8String:description];
  550. // int stockUom = sqlite3_column_int(statement, 8);
  551. // int _id = sqlite3_column_int(statement, 9);
  552. //
  553. //
  554. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  555. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  556. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  557. // int carton=[bsubtotaljson[@"carton"] intValue];
  558. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  559. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  560. itemjson[@"model"]=nsname;
  561. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  562. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  563. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  564. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  565. // itemjson[@"check"]=@"true";
  566. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  567. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  568. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  569. itemjson[@"unit_price"]=Price;
  570. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  571. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  572. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  573. itemjson[@"note"]=nsline_note;
  574. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  575. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  576. count++;
  577. }
  578. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  579. sqlite3_finalize(statement);
  580. }
  581. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  582. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  583. return ret;
  584. }
  585. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  586. {
  587. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  588. sqlite3 *db = [iSalesDB get_db];
  589. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  590. // for(int i=0;i<arr_soid.count;i++)
  591. sqlite3_stmt * statement;
  592. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  593. //int count=0;
  594. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  595. if ( dbresult== SQLITE_OK)
  596. {
  597. int count=0;
  598. while (sqlite3_step(statement) == SQLITE_ROW)
  599. {
  600. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  601. int _id = sqlite3_column_int(statement, 0);
  602. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  603. NSString* nssync_data=nil;
  604. if(sync_data!=nil)
  605. {
  606. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  607. }
  608. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  609. NSString* nsimg_1=nil;
  610. if(img_1!=nil)
  611. {
  612. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  613. [self copy_upImg:serial file:nsimg_1];
  614. }
  615. char *so_no = (char*)sqlite3_column_text(statement, 3);
  616. NSString* nsso_no=nil;
  617. if(so_no!=nil)
  618. {
  619. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  620. }
  621. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  622. itemjson[@"order_type"]=@"submit order";
  623. else
  624. itemjson[@"order_type"]=@"archive order";
  625. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  626. itemjson[@"json_data"]= nssync_data;
  627. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  628. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  629. count++;
  630. }
  631. ret[@"count"]=[NSNumber numberWithInt:count ];
  632. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  633. }
  634. sqlite3_finalize(statement);
  635. [iSalesDB close_db:db];
  636. return ret;
  637. }
  638. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  639. {
  640. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  641. sqlite3 *db = [iSalesDB get_db];
  642. // UIApplication * app = [UIApplication sharedApplication];
  643. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  644. 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";
  645. sqlite3_stmt * statement;
  646. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  647. int count=0;
  648. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  649. if ( dbresult== SQLITE_OK)
  650. {
  651. while (sqlite3_step(statement) == SQLITE_ROW)
  652. {
  653. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  654. int _id = sqlite3_column_int(statement, 0);
  655. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  656. NSString* nsimg_0=nil;
  657. if(img_0!=nil)
  658. {
  659. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  660. [self copy_upImg:serial file:nsimg_0];
  661. }
  662. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  663. NSString* nsimg_1=nil;
  664. if(img_1!=nil)
  665. {
  666. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  667. [self copy_upImg:serial file:nsimg_1];
  668. }
  669. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  670. NSString* nsimg_2=nil;
  671. if(img_2!=nil)
  672. {
  673. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  674. [self copy_upImg:serial file:nsimg_2];
  675. }
  676. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  677. NSString* nssync_data=nil;
  678. if(sync_data!=nil)
  679. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  680. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  681. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  682. itemjson[@"json_data"]= nssync_data;
  683. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  684. count++;
  685. }
  686. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  687. sqlite3_finalize(statement);
  688. }
  689. ret[@"count"]=[NSNumber numberWithInt:count ];
  690. [iSalesDB close_db:db];
  691. return ret;
  692. }
  693. +(bool) check_offlinedata
  694. {
  695. UIApplication * app = [UIApplication sharedApplication];
  696. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  697. sqlite3 *db = [iSalesDB get_db];
  698. NSString * where=@"1=1";
  699. // if(appDelegate.user!=nil)
  700. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  701. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  702. [iSalesDB close_db:db];
  703. if(count==0)
  704. {
  705. return false;
  706. }
  707. return true;
  708. //
  709. //[iSalesDB close_db:db];
  710. }
  711. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  712. {
  713. UIApplication * app = [UIApplication sharedApplication];
  714. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  715. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  716. sqlite3 *db = [iSalesDB get_db];
  717. NSString* collectId=params[@"collectId"];
  718. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  719. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  720. [iSalesDB execSql:sqlQuery db:db];
  721. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  722. [iSalesDB close_db:db];
  723. appDelegate.wish_count =count;
  724. [appDelegate update_count_mark];
  725. ret[@"result"]= [NSNumber numberWithInt:2];
  726. return ret;
  727. }
  728. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  729. //{
  730. //
  731. // UIApplication * app = [UIApplication sharedApplication];
  732. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  733. //
  734. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  735. // sqlite3 *db = [iSalesDB get_db];
  736. // NSString* product_id=params[@"product_id"];
  737. //
  738. //
  739. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  740. //
  741. //
  742. // for(int i=0;i<arr.count;i++)
  743. // {
  744. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  745. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  746. // if(count==0)
  747. // {
  748. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  749. // [iSalesDB execSql:sqlQuery db:db];
  750. // }
  751. // }
  752. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  753. // [iSalesDB close_db:db];
  754. //
  755. // appDelegate.wish_count =count;
  756. //
  757. // [appDelegate update_count_mark];
  758. // ret[@"result"]= [NSNumber numberWithInt:2];
  759. // return ret;
  760. // //
  761. // //return ret;
  762. //}
  763. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  764. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  765. DebugLog(@"time interval: %lf",interval);
  766. }
  767. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  768. {
  769. UIApplication * app = [UIApplication sharedApplication];
  770. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  771. int sort = [[params objectForKey:@"sort"] intValue];
  772. NSString *sort_str = @"";
  773. switch (sort) {
  774. case 0:{
  775. sort_str = @"order by w.modify_time desc";
  776. }
  777. break;
  778. case 1:{
  779. sort_str = @"order by w.modify_time asc";
  780. }
  781. break;
  782. case 2:{
  783. sort_str = @"order by m.name asc";
  784. }
  785. break;
  786. case 3:{
  787. sort_str = @"order by m.name desc";
  788. }
  789. break;
  790. case 4:{
  791. sort_str = @"order by m.description asc";
  792. }
  793. break;
  794. default:
  795. break;
  796. }
  797. NSString* user = appDelegate.user;
  798. sqlite3 *db = [iSalesDB get_db];
  799. // order by w.create_time
  800. 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];
  801. // 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];
  802. sqlite3_stmt * statement;
  803. NSDate *date1 = [NSDate date];
  804. NSDate *date2 = nil;
  805. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  806. int count=0;
  807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  808. {
  809. while (sqlite3_step(statement) == SQLITE_ROW)
  810. {
  811. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  812. int product_id = sqlite3_column_double(statement, 0);
  813. char *description = (char*)sqlite3_column_text(statement, 1);
  814. if(description==nil)
  815. description= "";
  816. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  817. int item_id = sqlite3_column_double(statement, 2);
  818. NSDate *date_image = [NSDate date];
  819. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  820. printf("image : ");
  821. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  822. // char *url = (char*)sqlite3_column_text(statement, 3);
  823. // if(url==nil)
  824. // url="";
  825. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  826. int qty = sqlite3_column_int(statement, 3);
  827. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  828. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  829. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  830. item[@"description"]= nsdescription;
  831. item[@"img"]= nsurl;
  832. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  833. count++;
  834. }
  835. printf("total time:");
  836. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  837. ret[@"count"]= [NSNumber numberWithInt:count];
  838. ret[@"total_count"]= [NSNumber numberWithInt:count];
  839. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  840. ret[@"result"]= [NSNumber numberWithInt:2];
  841. appDelegate.wish_count =count;
  842. [appDelegate update_count_mark];
  843. sqlite3_finalize(statement);
  844. }
  845. [iSalesDB close_db:db];
  846. return ret;
  847. }
  848. +(NSDictionary*) offline_notimpl
  849. {
  850. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  851. ret[@"result"]=@"8";
  852. ret[@"err_msg"]=@"offline mode does not support this function.";
  853. return ret;
  854. }
  855. +(NSDictionary*) offline_home
  856. {
  857. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  858. NSString *cachefolder = [paths objectAtIndex:0];
  859. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  860. NSData* json =nil;
  861. json=[NSData dataWithContentsOfFile:img_cache];
  862. NSError *error=nil;
  863. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  864. return menu;
  865. }
  866. +(NSDictionary*) offline_category_menu
  867. {
  868. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  869. NSString *cachefolder = [paths objectAtIndex:0];
  870. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  871. NSData* json =nil;
  872. json=[NSData dataWithContentsOfFile:img_cache];
  873. NSError *error=nil;
  874. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  875. return menu;
  876. }
  877. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  878. {
  879. NSString* offline_command=params[@"offline_Command"];
  880. NSDictionary* ret=nil;
  881. if([offline_command isEqualToString:@"model_NIYMAL"])
  882. {
  883. NSString* category = params[@"category"];
  884. ret = [self refresh_model_NIYMAL:category];
  885. }
  886. return ret;
  887. }
  888. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  889. {
  890. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  891. [ret setValue:@"2" forKey:@"result"];
  892. [ret setValue:@"Regular Mode" forKey:@"mode"];
  893. sqlite3* db= [iSalesDB get_db];
  894. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  895. [iSalesDB close_db:db];
  896. [ret setObject:detail1_section forKey:@"detail_1"];
  897. return ret;
  898. }
  899. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  900. {
  901. UIApplication * app = [UIApplication sharedApplication];
  902. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  903. // NSArray* arr1 = [self get_user_all_price_type];
  904. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  905. // NSSet *set1 = [NSSet setWithArray:arr1];
  906. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  907. // if(appDelegate.contact_id==nil)
  908. // set2=[set1 mutableCopy];
  909. // else
  910. // [set2 intersectsSet:set1];
  911. // NSArray *retarr = [set2 allObjects];
  912. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  913. // sqlite3 *db = [iSalesDB get_db];
  914. NSString* sqlQuery = nil;
  915. if(appDelegate.contact_id==nil)
  916. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price 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];
  917. else
  918. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price 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];
  919. sqlite3_stmt * statement;
  920. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  921. int count=0;
  922. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  923. {
  924. while (sqlite3_step(statement) == SQLITE_ROW)
  925. {
  926. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  927. char *name = (char*)sqlite3_column_text(statement, 0);
  928. if(name==nil)
  929. name="";
  930. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  931. // double price = sqlite3_column_double(statement, 1);
  932. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  933. // if(isnull==nil)
  934. // item[nsname]= @"No Price";
  935. // else
  936. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  937. char *price = (char*)sqlite3_column_text(statement, 1);
  938. if(price!=nil)
  939. {
  940. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  941. nsprice=[AESCrypt fastdecrypt:nsprice];
  942. if(nsprice.length>0)
  943. {
  944. double dp= [nsprice doubleValue];
  945. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  946. }
  947. }
  948. else
  949. {
  950. item[nsname]= @"No Price";
  951. }
  952. // item[nsname]= nsprice;
  953. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  954. count++;
  955. }
  956. ret[@"count"]= [NSNumber numberWithInt:count];
  957. sqlite3_finalize(statement);
  958. }
  959. // [iSalesDB close_db:db];
  960. return ret;
  961. }
  962. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  963. //{
  964. // NSArray* arr1 = [self get_user_all_price_type:db];
  965. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  966. //
  967. // // NSSet *set1 = [NSSet setWithArray:arr1];
  968. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  969. // // [set2 intersectsSet:set1];
  970. // //
  971. // //
  972. // // NSArray *retarr = [set2 allObjects];
  973. //
  974. // NSString* whereprice=nil;
  975. // if(contact_id==nil)
  976. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  977. // else
  978. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  979. //
  980. //
  981. // // sqlite3 *db = [iSalesDB get_db];
  982. //
  983. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  984. // sqlite3_stmt * statement;
  985. //
  986. //
  987. // NSNumber* ret = nil;
  988. // double dprice=DBL_MAX;
  989. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  990. // {
  991. //
  992. //
  993. // while (sqlite3_step(statement) == SQLITE_ROW)
  994. // {
  995. //
  996. // // double val = sqlite3_column_double(statement, 0);
  997. // char *price = (char*)sqlite3_column_text(statement, 0);
  998. // if(price!=nil)
  999. // {
  1000. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1001. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1002. // if(nsprice.length>0)
  1003. // {
  1004. // double dp= [nsprice doubleValue];
  1005. // if(dp<dprice)
  1006. // dprice=dp;
  1007. // }
  1008. // }
  1009. // }
  1010. //
  1011. //
  1012. //
  1013. //
  1014. // sqlite3_finalize(statement);
  1015. //
  1016. //
  1017. //
  1018. //
  1019. // }
  1020. //
  1021. // // [iSalesDB close_db:db];
  1022. //
  1023. // if(dprice==DBL_MAX)
  1024. // ret= nil;
  1025. // else
  1026. // ret= [NSNumber numberWithDouble:dprice];
  1027. // return ret;
  1028. //}
  1029. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1030. {
  1031. NSArray* arr1 = [self get_user_all_price_type:db];
  1032. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1033. NSString* whereprice=nil;
  1034. if(contact_id==nil)
  1035. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1036. else
  1037. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1038. // sqlite3 *db = [iSalesDB get_db];
  1039. NSString *productIdCondition = @"1 = 1";
  1040. if (product_id) {
  1041. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1042. }
  1043. NSString *itemIdCondition = @"";
  1044. if (item_id) {
  1045. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1046. }
  1047. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1048. sqlite3_stmt * statement;
  1049. NSNumber* ret = nil;
  1050. double dprice=DBL_MAX;
  1051. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1052. {
  1053. while (sqlite3_step(statement) == SQLITE_ROW)
  1054. {
  1055. // double val = sqlite3_column_double(statement, 0);
  1056. char *price = (char*)sqlite3_column_text(statement, 0);
  1057. if(price!=nil)
  1058. {
  1059. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1060. nsprice=[AESCrypt fastdecrypt:nsprice];
  1061. if(nsprice.length>0)
  1062. {
  1063. double dp= [nsprice doubleValue];
  1064. if(dp<dprice)
  1065. dprice=dp;
  1066. }
  1067. }
  1068. }
  1069. sqlite3_finalize(statement);
  1070. }
  1071. // [iSalesDB close_db:db];
  1072. if(dprice==DBL_MAX)
  1073. ret= nil;
  1074. else
  1075. ret= [NSNumber numberWithDouble:dprice];
  1076. return ret;
  1077. }
  1078. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1079. {
  1080. NSArray* ret=nil;
  1081. // sqlite3 *db = [iSalesDB get_db];
  1082. // no customer assigned , use login user contact_id
  1083. UIApplication * app = [UIApplication sharedApplication];
  1084. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1085. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1086. sqlite3_stmt * statement;
  1087. // int count=0;
  1088. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1089. {
  1090. if (sqlite3_step(statement) == SQLITE_ROW)
  1091. {
  1092. char *val = (char*)sqlite3_column_text(statement, 0);
  1093. if(val==nil)
  1094. val="";
  1095. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1096. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1097. }
  1098. sqlite3_finalize(statement);
  1099. }
  1100. // [iSalesDB close_db:db];
  1101. return ret;
  1102. }
  1103. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1104. {
  1105. // sqlite3 *db = [iSalesDB get_db];
  1106. if(contact_id==nil)
  1107. {
  1108. // no customer assigned , use login user contact_id
  1109. UIApplication * app = [UIApplication sharedApplication];
  1110. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1111. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1112. sqlite3_stmt * statement;
  1113. // int count=0;
  1114. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1115. {
  1116. if (sqlite3_step(statement) == SQLITE_ROW)
  1117. {
  1118. char *val = (char*)sqlite3_column_text(statement, 0);
  1119. if(val==nil)
  1120. val="";
  1121. contact_id = [[NSString alloc]initWithUTF8String:val];
  1122. }
  1123. sqlite3_finalize(statement);
  1124. }
  1125. if(contact_id.length<=0)
  1126. {
  1127. // [iSalesDB close_db:db];
  1128. return nil;
  1129. }
  1130. }
  1131. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1132. sqlite3_stmt * statement;
  1133. NSArray* ret=nil;
  1134. // int count=0;
  1135. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1136. {
  1137. if (sqlite3_step(statement) == SQLITE_ROW)
  1138. {
  1139. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1140. if(price_type==nil)
  1141. price_type="";
  1142. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1143. if(nsprice_type.length>0)
  1144. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1145. else
  1146. ret=nil;
  1147. }
  1148. sqlite3_finalize(statement);
  1149. }
  1150. // [iSalesDB close_db:db];
  1151. return ret;
  1152. }
  1153. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1154. {
  1155. NSString* ret= nil;
  1156. // sqlite3 *db = [iSalesDB get_db];
  1157. NSString *sqlQuery = nil;
  1158. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1159. // 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;
  1160. // 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
  1161. if(product_id==nil && model_name)
  1162. 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;
  1163. else if (product_id)
  1164. 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
  1165. sqlite3_stmt * statement;
  1166. // int count=0;
  1167. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1168. {
  1169. if (sqlite3_step(statement) == SQLITE_ROW)
  1170. {
  1171. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1172. if(imgurl==nil)
  1173. imgurl="";
  1174. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1175. ret=nsimgurl;
  1176. }
  1177. sqlite3_finalize(statement);
  1178. }
  1179. else
  1180. {
  1181. [ret setValue:@"8" forKey:@"result"];
  1182. }
  1183. // [iSalesDB close_db:db];
  1184. DebugLog(@"data string: %@",ret );
  1185. return ret;
  1186. }
  1187. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1188. {
  1189. NSString* ret= nil;
  1190. sqlite3 *db = [iSalesDB get_db];
  1191. NSString *sqlQuery = nil;
  1192. if(product_id==nil)
  1193. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1194. else
  1195. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1196. sqlite3_stmt * statement;
  1197. // int count=0;
  1198. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1199. {
  1200. if (sqlite3_step(statement) == SQLITE_ROW)
  1201. {
  1202. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1203. if(default_category==nil)
  1204. default_category="";
  1205. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1206. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1207. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1208. ret=nsdefault_category;
  1209. }
  1210. sqlite3_finalize(statement);
  1211. }
  1212. else
  1213. {
  1214. [ret setValue:@"8" forKey:@"result"];
  1215. }
  1216. [iSalesDB close_db:db];
  1217. DebugLog(@"data string: %@",ret );
  1218. return ret;
  1219. }
  1220. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1221. {
  1222. NSString* model_name = [params valueForKey:@"product_name"];
  1223. NSString* product_id = [params valueForKey:@"product_id"];
  1224. NSString* category = [params valueForKey:@"category"];
  1225. if(category==nil)
  1226. category = [self model_default_category:product_id model_name:model_name];
  1227. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1228. sqlite3 *db = [iSalesDB get_db];
  1229. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1230. NSString *sqlQuery = nil;
  1231. if(product_id==nil)
  1232. 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 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='%@';
  1233. else
  1234. 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 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=%@;
  1235. sqlite3_stmt * statement;
  1236. [ret setValue:@"2" forKey:@"result"];
  1237. [ret setValue:@"3" forKey:@"detail_section_count"];
  1238. // int count=0;
  1239. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1240. {
  1241. if (sqlite3_step(statement) == SQLITE_ROW)
  1242. {
  1243. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1244. char *name = (char*)sqlite3_column_text(statement, 0);
  1245. if(name==nil)
  1246. name="";
  1247. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1248. char *description = (char*)sqlite3_column_text(statement, 1);
  1249. if(description==nil)
  1250. description="";
  1251. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1252. int product_id = sqlite3_column_int(statement, 2);
  1253. char *color = (char*)sqlite3_column_text(statement, 3);
  1254. if(color==nil)
  1255. color="";
  1256. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1257. //
  1258. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1259. // if(legcolor==nil)
  1260. // legcolor="";
  1261. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1262. //
  1263. //
  1264. int availability = sqlite3_column_int(statement, 5);
  1265. //
  1266. int incoming_stock = sqlite3_column_int(statement, 6);
  1267. char *demension = (char*)sqlite3_column_text(statement, 7);
  1268. if(demension==nil)
  1269. demension="";
  1270. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1271. // ,,,,,,,,,
  1272. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1273. if(seat_height==nil)
  1274. seat_height="";
  1275. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1276. char *material = (char*)sqlite3_column_text(statement, 9);
  1277. if(material==nil)
  1278. material="";
  1279. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1280. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1281. if(box_dim==nil)
  1282. box_dim="";
  1283. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1284. char *volume = (char*)sqlite3_column_text(statement, 11);
  1285. if(volume==nil)
  1286. volume="";
  1287. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1288. double weight = sqlite3_column_double(statement, 12);
  1289. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1290. if(model_set==nil)
  1291. model_set="";
  1292. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1293. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1294. if(load_ability==nil)
  1295. load_ability="";
  1296. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1297. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1298. if(default_category==nil)
  1299. default_category="";
  1300. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1301. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1302. if(fabric_content==nil)
  1303. fabric_content="";
  1304. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1305. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1306. if(assembling==nil)
  1307. assembling="";
  1308. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1309. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1310. if(made_in==nil)
  1311. made_in="";
  1312. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1313. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1314. if(special_remarks==nil)
  1315. special_remarks="";
  1316. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1317. int stockUcom = sqlite3_column_double(statement, 20);
  1318. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1319. if(product_group==nil)
  1320. product_group="";
  1321. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1322. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1323. // if(fashion_selector==nil)
  1324. // fashion_selector="";
  1325. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1326. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1327. if(selector_field==nil)
  1328. selector_field="";
  1329. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1330. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1331. if(property_field==nil)
  1332. property_field="";
  1333. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1334. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1335. if(packaging==nil)
  1336. packaging="";
  1337. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1338. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1339. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1340. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1341. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1342. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1343. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1344. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1345. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1346. NSString* Availability=nil;
  1347. if(availability>0)
  1348. Availability=[NSString stringWithFormat:@"%d",availability];
  1349. else
  1350. Availability = @"In Production";
  1351. [img_section setValue:Availability forKey:@"Availability"];
  1352. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1353. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1354. char *eta = (char*)sqlite3_column_text(statement, 25);
  1355. if(eta==nil)
  1356. eta="";
  1357. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1358. [img_section setValue:nseta forKey:@"ETA"];
  1359. int item_id = sqlite3_column_int(statement, 26);
  1360. NSString* Price=nil;
  1361. if(appDelegate.bLogin==false)
  1362. Price=@"Must Sign in.";
  1363. else
  1364. {
  1365. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1366. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1367. if(price==nil)
  1368. Price=@"No Price.";
  1369. else
  1370. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1371. }
  1372. [img_section setValue:Price forKey:@"price"];
  1373. [img_section setValue:nsname forKey:@"model_name"];
  1374. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1375. [ret setObject:img_section forKey:@"img_section"];
  1376. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1377. int detail0_item_count=0;
  1378. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1379. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1380. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1381. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1382. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1383. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1384. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1385. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1386. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1387. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1388. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1389. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1390. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1391. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1392. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1393. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1394. {
  1395. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1396. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1397. }
  1398. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1399. [detail0_section setValue:@"kv" forKey:@"type"];
  1400. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1401. [ret setObject:detail0_section forKey:@"detail_0"];
  1402. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1403. // [detail1_section setValue:@"detail" forKey:@"target"];
  1404. // [detail1_section setValue:@"popup" forKey:@"action"];
  1405. // [detail1_section setValue:@"content" forKey:@"type"];
  1406. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1407. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1408. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1409. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1410. [ret setObject:detail1_section forKey:@"detail_1"];
  1411. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1412. [detail2_section setValue:@"detail" forKey:@"target"];
  1413. [detail2_section setValue:@"popup" forKey:@"action"];
  1414. [detail2_section setValue:@"content" forKey:@"type"];
  1415. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1416. [detail2_section setValue:@"true" forKey:@"single_row"];
  1417. [detail2_section setValue:@"local" forKey:@"data"];
  1418. [ret setObject:detail2_section forKey:@"detail_2"];
  1419. }
  1420. sqlite3_finalize(statement);
  1421. }
  1422. else
  1423. {
  1424. [ret setValue:@"8" forKey:@"result"];
  1425. }
  1426. NSLog(@"count:%d",count);
  1427. [iSalesDB close_db:db];
  1428. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1429. return ret;
  1430. }
  1431. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1432. NSString* orderCode = [params valueForKey:@"orderCode"];
  1433. NSString* keyword = [params valueForKey:@"keyword"];
  1434. keyword=keyword.lowercaseString;
  1435. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1436. int limit = [[params valueForKey:@"limit"] intValue];
  1437. int offset = [[params valueForKey:@"offset"] intValue];
  1438. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1439. NSString *limit_str = @"";
  1440. if (limited) {
  1441. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1442. }
  1443. sqlite3 *db = [iSalesDB get_db];
  1444. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1445. UIApplication * app = [UIApplication sharedApplication];
  1446. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1447. NSString *sqlQuery = nil;
  1448. if(exactMatch )
  1449. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 %@;",appDelegate.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 ;
  1450. else
  1451. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, 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
  1452. DebugLog(@"offline_search sql:%@",sqlQuery);
  1453. sqlite3_stmt * statement;
  1454. [ret setValue:@"2" forKey:@"result"];
  1455. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1456. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1457. // int count=0;
  1458. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1459. {
  1460. int i=0;
  1461. while (sqlite3_step(statement) == SQLITE_ROW)
  1462. {
  1463. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1464. // char *name = (char*)sqlite3_column_text(statement, 1);
  1465. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1466. char *name = (char*)sqlite3_column_text(statement, 0);
  1467. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1468. char *description = (char*)sqlite3_column_text(statement, 1);
  1469. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1470. int product_id = sqlite3_column_int(statement, 2);
  1471. // char *url = (char*)sqlite3_column_text(statement, 3);
  1472. // if(url==nil)
  1473. // url="";
  1474. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1475. int wid = sqlite3_column_int(statement, 3);
  1476. int closeout = sqlite3_column_int(statement, 4);
  1477. int cid = sqlite3_column_int(statement, 5);
  1478. int wisdelete = sqlite3_column_int(statement, 6);
  1479. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1480. if(wid !=0 && wisdelete != 1)
  1481. [item setValue:@"true" forKey:@"wish_exists"];
  1482. else
  1483. [item setValue:@"false" forKey:@"wish_exists"];
  1484. if(closeout==0)
  1485. [item setValue:@"false" forKey:@"is_closeout"];
  1486. else
  1487. [item setValue:@"true" forKey:@"is_closeout"];
  1488. if(cid==0)
  1489. [item setValue:@"false" forKey:@"cart_exists"];
  1490. else
  1491. [item setValue:@"true" forKey:@"cart_exists"];
  1492. [item addEntriesFromDictionary:imgjson];
  1493. // [item setValue:nsurl forKey:@"img"];
  1494. [item setValue:nsname forKey:@"fash_name"];
  1495. [item setValue:nsdescription forKey:@"description"];
  1496. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1497. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1498. i++;
  1499. }
  1500. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1501. [ret setObject:items forKey:@"items"];
  1502. sqlite3_finalize(statement);
  1503. }
  1504. NSLog(@"count:%d",count);
  1505. [iSalesDB close_db:db];
  1506. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1507. return ret;
  1508. }
  1509. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1510. {
  1511. return [self search:params limited:YES];
  1512. }
  1513. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1514. {
  1515. UIApplication * app = [UIApplication sharedApplication];
  1516. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1517. appDelegate.disable_trigger=true;
  1518. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1519. appDelegate.disable_trigger=false;
  1520. //
  1521. // NSString* user = [params valueForKey:@"user"];
  1522. //
  1523. // NSString* password = [params valueForKey:@"password"];
  1524. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1525. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1526. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1527. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1528. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1529. [appDelegate update_count_mark];
  1530. appDelegate.can_show_price =false;
  1531. appDelegate.can_see_price =false;
  1532. appDelegate.can_create_portfolio =false;
  1533. appDelegate.can_create_order =false;
  1534. appDelegate.can_cancel_order =false;
  1535. appDelegate.can_set_cart_price =false;
  1536. appDelegate.can_delete_order =false;
  1537. appDelegate.can_submit_order =false;
  1538. appDelegate.can_set_tearsheet_price =false;
  1539. appDelegate.can_update_contact_info = false;
  1540. appDelegate.save_order_logout = false;
  1541. appDelegate.submit_order_logout = false;
  1542. appDelegate.alert_sold_in_quantities = false;
  1543. appDelegate.ipad_perm =nil ;
  1544. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1545. appDelegate.OrderFilter= nil;
  1546. [appDelegate SetSo:nil];
  1547. [appDelegate set_main_button_panel];
  1548. // sqlite3 *db = [iSalesDB get_db];
  1549. //
  1550. //
  1551. //
  1552. //
  1553. //
  1554. // 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"]];
  1555. //
  1556. //
  1557. //
  1558. //
  1559. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1560. // sqlite3_stmt * statement;
  1561. //
  1562. //
  1563. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1564. //
  1565. //
  1566. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1567. // {
  1568. //
  1569. //
  1570. // if (sqlite3_step(statement) == SQLITE_ROW)
  1571. // {
  1572. //
  1573. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1574. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1575. //
  1576. //
  1577. //
  1578. // int can_show_price = sqlite3_column_int(statement, 0);
  1579. // int can_see_price = sqlite3_column_int(statement, 1);
  1580. //
  1581. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1582. // if(contact_id==nil)
  1583. // contact_id="";
  1584. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1585. //
  1586. // int user_type = sqlite3_column_int(statement, 3);
  1587. //
  1588. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1589. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1590. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1591. // int can_delete_order = sqlite3_column_int(statement, 7);
  1592. // int can_submit_order = sqlite3_column_int(statement, 8);
  1593. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1594. // int can_create_order = sqlite3_column_int(statement, 10);
  1595. //
  1596. //
  1597. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1598. // if(mode==nil)
  1599. // mode="";
  1600. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1601. //
  1602. //
  1603. // char *username = (char*)sqlite3_column_text(statement, 12);
  1604. // if(username==nil)
  1605. // username="";
  1606. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1607. //
  1608. //
  1609. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1610. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1611. // [header setValue:nscontact_id forKey:@"contact_id"];
  1612. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1613. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1614. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1615. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1616. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1617. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1618. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1619. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1620. //
  1621. // [header setValue:nsusername forKey:@"username"];
  1622. //
  1623. //
  1624. // [ret setObject:header forKey:@"header"];
  1625. // [ret setValue:nsmode forKey:@"mode"];
  1626. //
  1627. //
  1628. // }
  1629. //
  1630. //
  1631. //
  1632. // sqlite3_finalize(statement);
  1633. // }
  1634. //
  1635. //
  1636. //
  1637. // [iSalesDB close_db:db];
  1638. //
  1639. //
  1640. //
  1641. //
  1642. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1643. return ret;
  1644. }
  1645. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1646. {
  1647. UIApplication * app = [UIApplication sharedApplication];
  1648. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1649. appDelegate.disable_trigger=true;
  1650. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1651. appDelegate.disable_trigger=false;
  1652. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1653. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1654. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1655. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1656. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1657. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1658. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1659. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1660. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1661. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1662. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1663. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1664. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1665. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1666. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1667. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1668. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1669. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1670. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1671. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1672. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1673. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1674. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1675. [arr_name addObject:customer_first_name];
  1676. [arr_name addObject:customer_last_name];
  1677. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1678. // default ship from
  1679. 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';";
  1680. __block NSString *cid = @"";
  1681. __block NSString *name = @"";
  1682. __block NSString *ext = @"";
  1683. __block NSString *contact = @"";
  1684. __block NSString *email = @"";
  1685. __block NSString *fax = @"";
  1686. __block NSString *phone = @"";
  1687. sqlite3 *db = [iSalesDB get_db];
  1688. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1689. cid = [self textAtColumn:0 statement:statment];
  1690. name = [self textAtColumn:1 statement:statment];
  1691. ext = [self textAtColumn:2 statement:statment];
  1692. contact = [self textAtColumn:3 statement:statment];
  1693. email = [self textAtColumn:4 statement:statment];
  1694. fax = [self textAtColumn:5 statement:statment];
  1695. phone = [self textAtColumn:6 statement:statment];
  1696. }];
  1697. NSString* so_id = [self get_offline_soid:db];
  1698. if(so_id==nil)
  1699. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1700. 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) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",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];
  1701. int result =[iSalesDB execSql:sql_neworder db:db];
  1702. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1703. //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'
  1704. //soId
  1705. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1706. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1707. sqlite3_stmt * statement;
  1708. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1709. {
  1710. if (sqlite3_step(statement) == SQLITE_ROW)
  1711. {
  1712. // char *name = (char*)sqlite3_column_text(statement, 1);
  1713. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1714. //ret = sqlite3_column_int(statement, 0);
  1715. char *soId = (char*)sqlite3_column_text(statement, 0);
  1716. if(soId==nil)
  1717. soId="";
  1718. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1719. [ret setValue:nssoId forKey:@"soId"];
  1720. [ret setValue:nssoId forKey:@"orderCode"];
  1721. }
  1722. sqlite3_finalize(statement);
  1723. }
  1724. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1725. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1726. [iSalesDB close_db:db];
  1727. return [RAUtils dict2data:ret];
  1728. }
  1729. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1730. {
  1731. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1732. NSString* orderCode = [params valueForKey:@"orderCode"];
  1733. UIApplication * app = [UIApplication sharedApplication];
  1734. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1735. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1736. {
  1737. appDelegate.disable_trigger=true;
  1738. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1739. appDelegate.disable_trigger=false;
  1740. }
  1741. sqlite3 *db = [iSalesDB get_db];
  1742. int cart_count=[self query_ordercartcount:orderCode db:db];
  1743. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1744. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1745. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1746. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1747. [iSalesDB close_db:db];
  1748. return [RAUtils dict2data:ret];
  1749. }
  1750. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1751. {
  1752. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1753. sqlite3 *db = [iSalesDB get_db];
  1754. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1755. int count =0;
  1756. if(params[@"count"]!=nil)
  1757. {
  1758. count = [params[@"count"] intValue];
  1759. }
  1760. NSString* product_id=params[@"product_id"];
  1761. NSString* orderCode=params[@"orderCode"];
  1762. NSString *qty = params[@"qty"];
  1763. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1764. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1765. for(int i=0;i<arr_id.count;i++)
  1766. {
  1767. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1768. int item_qty= count;
  1769. if (qty) {
  1770. item_qty = [qty_arr[i] integerValue];
  1771. }
  1772. if(item_qty==0)
  1773. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1774. NSString* sql=nil;
  1775. sqlite3_stmt *stmt;
  1776. if(_id<0)
  1777. {
  1778. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1779. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1780. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1781. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1782. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1783. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1784. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1785. }
  1786. else
  1787. {
  1788. if (qty) {
  1789. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1790. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1791. sqlite3_bind_int(stmt, 1, _id);
  1792. } else {
  1793. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1794. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1795. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1796. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1797. }
  1798. }
  1799. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1800. [iSalesDB execSql:@"ROLLBACK" db:db];
  1801. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1802. [iSalesDB close_db:db];
  1803. DebugLog(@"add to cart error");
  1804. return [RAUtils dict2data:ret];
  1805. }
  1806. }
  1807. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1808. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1809. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1810. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1811. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1812. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1813. [iSalesDB close_db:db];
  1814. return [RAUtils dict2data:ret];
  1815. }
  1816. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1817. {
  1818. UIApplication * app = [UIApplication sharedApplication];
  1819. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1820. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1821. 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];
  1822. sqlite3_stmt * statement;
  1823. int count=0;
  1824. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1825. {
  1826. while (sqlite3_step(statement) == SQLITE_ROW)
  1827. {
  1828. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1829. int bitem_id = sqlite3_column_int(statement, 0);
  1830. int bitem_qty = sqlite3_column_int(statement, 1);
  1831. char *name = (char*)sqlite3_column_text(statement, 2);
  1832. if(name==nil)
  1833. name="";
  1834. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1835. char *description = (char*)sqlite3_column_text(statement, 3);
  1836. if(description==nil)
  1837. description="";
  1838. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1839. double unit_price = sqlite3_column_double(statement, 4);
  1840. int use_unitprice = sqlite3_column_int(statement, 5);
  1841. if(use_unitprice!=1)
  1842. {
  1843. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1844. }
  1845. itemjson[@"model"]=nsname;
  1846. itemjson[@"description"]=nsdescription;
  1847. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1848. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1849. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1850. if(compute)
  1851. {
  1852. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1853. }
  1854. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1855. count++;
  1856. }
  1857. sqlite3_finalize(statement);
  1858. }
  1859. ret[@"count"]=@(count);
  1860. if(count==0)
  1861. return nil;
  1862. else
  1863. return ret;
  1864. }
  1865. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1866. {
  1867. //compute: add part to subtotal;
  1868. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1869. dict_item[@(item_id)]=@"1";
  1870. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1871. double cuft=0;
  1872. double weight=0;
  1873. int carton=0;
  1874. 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];
  1875. sqlite3_stmt * statement;
  1876. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1877. {
  1878. if (sqlite3_step(statement) == SQLITE_ROW)
  1879. {
  1880. double ulength = sqlite3_column_double(statement, 0);
  1881. double uwidth = sqlite3_column_double(statement, 1);
  1882. double uheight = sqlite3_column_double(statement, 2);
  1883. double uweight = sqlite3_column_double(statement, 3);
  1884. double mlength = sqlite3_column_double(statement, 4);
  1885. double mwidth = sqlite3_column_double(statement, 5);
  1886. double mheight = sqlite3_column_double(statement, 6);
  1887. double mweight = sqlite3_column_double(statement, 7);
  1888. double ilength = sqlite3_column_double(statement, 8);
  1889. double iwidth = sqlite3_column_double(statement, 9);
  1890. double iheight = sqlite3_column_double(statement, 10);
  1891. double iweight = sqlite3_column_double(statement, 11);
  1892. int pcs = sqlite3_column_int(statement,12);
  1893. int mpack = sqlite3_column_int(statement, 13);
  1894. int ipack = sqlite3_column_int(statement, 14);
  1895. double ucbf = sqlite3_column_double(statement, 15);
  1896. double icbf = sqlite3_column_double(statement, 16);
  1897. double mcbf = sqlite3_column_double(statement, 17);
  1898. if(ipack==0)
  1899. {
  1900. carton= count/mpack ;
  1901. weight = mweight*carton;
  1902. cuft= carton*(mlength*mwidth*mheight);
  1903. int remain=count%mpack;
  1904. if(remain==0)
  1905. {
  1906. //do nothing;
  1907. }
  1908. else
  1909. {
  1910. carton++;
  1911. weight += uweight*remain;
  1912. cuft += (ulength*uwidth*uheight)*remain;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. carton = count/(mpack*ipack);
  1918. weight = mweight*carton;
  1919. cuft= carton*(mlength*mwidth*mheight);
  1920. int remain=count%(mpack*ipack);
  1921. if(remain==0)
  1922. {
  1923. // do nothing;
  1924. }
  1925. else
  1926. {
  1927. carton++;
  1928. int icarton =remain/ipack;
  1929. int iremain=remain%ipack;
  1930. weight += iweight*icarton;
  1931. cuft += (ilength*iwidth*iheight)*icarton;
  1932. if(iremain==0)
  1933. {
  1934. //do nothing;
  1935. }
  1936. else
  1937. {
  1938. weight += uweight*iremain;
  1939. cuft += (ulength*uwidth*uheight)*iremain;
  1940. }
  1941. }
  1942. }
  1943. #ifdef BUILD_NPD
  1944. cuft=ucbf*count;
  1945. weight= uweight*count;
  1946. #endif
  1947. }
  1948. sqlite3_finalize(statement);
  1949. }
  1950. if(compute)
  1951. {
  1952. NSArray * arr_count=nil;
  1953. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1954. for(int i=0;i<arr_bundle.count;i++)
  1955. {
  1956. dict_item[arr_bundle[i]]=@"1";
  1957. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1958. cuft+=[bundlejson[@"cuft"] doubleValue];
  1959. weight+=[bundlejson[@"weight"] doubleValue];
  1960. carton+=[bundlejson[@"carton"] intValue];
  1961. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  1962. }
  1963. }
  1964. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1965. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1966. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1967. ret[@"items"]=dict_item;
  1968. return ret;
  1969. }
  1970. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  1971. {
  1972. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  1973. // 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 ];
  1974. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1975. DebugLog(@"offline_login sql:%@",sqlQuery);
  1976. sqlite3_stmt * statement;
  1977. int cart_count=0;
  1978. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1979. if ( dbresult== SQLITE_OK)
  1980. {
  1981. while (sqlite3_step(statement) == SQLITE_ROW)
  1982. {
  1983. int item_id = sqlite3_column_int(statement, 0);
  1984. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  1985. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1986. }
  1987. sqlite3_finalize(statement);
  1988. }
  1989. return cart_count;
  1990. }
  1991. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1992. {
  1993. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1994. sqlite3 *db = [iSalesDB get_db];
  1995. UIApplication * app = [UIApplication sharedApplication];
  1996. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1997. NSString* orderCode=params[@"orderCode"];
  1998. int sort = [[params objectForKey:@"sort"] intValue];
  1999. NSString *sort_str = @"";
  2000. switch (sort) {
  2001. case 0:{
  2002. sort_str = @"order by c.modify_time desc";
  2003. }
  2004. break;
  2005. case 1:{
  2006. sort_str = @"order by c.modify_time asc";
  2007. }
  2008. break;
  2009. case 2:{
  2010. sort_str = @"order by m.name asc";
  2011. }
  2012. break;
  2013. case 3:{
  2014. sort_str = @"order by m.name desc";
  2015. }
  2016. break;
  2017. case 4:{
  2018. sort_str = @"order by m.description asc";
  2019. }
  2020. break;
  2021. default:
  2022. break;
  2023. }
  2024. 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 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 ];
  2025. // 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 ];
  2026. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2027. DebugLog(@"offline_login sql:%@",sqlQuery);
  2028. sqlite3_stmt * statement;
  2029. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2030. NSDate *date1 = [NSDate date];
  2031. int count=0;
  2032. int cart_count=0;
  2033. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2034. if ( dbresult== SQLITE_OK)
  2035. {
  2036. while (sqlite3_step(statement) == SQLITE_ROW)
  2037. {
  2038. NSDate *row_date = [NSDate date];
  2039. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2040. int product_id = sqlite3_column_int(statement, 0);
  2041. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2042. int item_id = sqlite3_column_int(statement, 7);
  2043. NSString* Price=nil;
  2044. if(str_price==nil)
  2045. {
  2046. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2047. // NSDate *price_date = [NSDate date];
  2048. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2049. // DebugLog(@"price time interval");
  2050. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2051. if(price==nil)
  2052. Price=@"No Price.";
  2053. else
  2054. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2055. }
  2056. else
  2057. {
  2058. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2059. }
  2060. double discount = sqlite3_column_double(statement, 2);
  2061. int item_count = sqlite3_column_int(statement, 3);
  2062. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2063. NSString *nsline_note=nil;
  2064. if(line_note!=nil)
  2065. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2066. char *name = (char*)sqlite3_column_text(statement, 5);
  2067. NSString *nsname=nil;
  2068. if(name!=nil)
  2069. nsname= [[NSString alloc]initWithUTF8String:name];
  2070. char *description = (char*)sqlite3_column_text(statement, 6);
  2071. NSString *nsdescription=nil;
  2072. if(description!=nil)
  2073. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2074. int stockUom = sqlite3_column_int(statement, 8);
  2075. int _id = sqlite3_column_int(statement, 9);
  2076. // NSDate *subtotal_date = [NSDate date];
  2077. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2078. // DebugLog(@"subtotal_date time interval");
  2079. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2080. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2081. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2082. int carton=[bsubtotaljson[@"carton"] intValue];
  2083. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2084. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2085. // NSDate *img_date = [NSDate date];
  2086. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2087. // DebugLog(@"img_date time interval");
  2088. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2089. itemjson[@"model"]=nsname;
  2090. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2091. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2092. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2093. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2094. itemjson[@"check"]=@"true";
  2095. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2096. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2097. itemjson[@"unit_price"]=Price;
  2098. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2099. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2100. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2101. itemjson[@"note"]=nsline_note;
  2102. // NSDate *date2 = [NSDate date];
  2103. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2104. // DebugLog(@"model_bundle time interval");
  2105. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2106. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2107. count++;
  2108. // DebugLog(@"row time interval");
  2109. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2110. }
  2111. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2112. sqlite3_finalize(statement);
  2113. }
  2114. DebugLog(@"request cart total time interval");
  2115. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2116. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2117. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2118. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2119. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2120. ret[@"count"]=[NSNumber numberWithInt:count ];
  2121. ret[@"mode"]=@"Regular Mode";
  2122. [iSalesDB close_db:db];
  2123. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2124. DebugLog(@"general notes :%@",general_note);
  2125. ret[@"general_note"]= general_note;
  2126. return [RAUtils dict2data:ret];
  2127. }
  2128. +(NSData*) offline_login :(NSMutableDictionary *) params
  2129. {
  2130. NSString* user = [params valueForKey:@"user"];
  2131. NSString* password = [params valueForKey:@"password"];
  2132. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2133. sqlite3 *db = [iSalesDB get_db];
  2134. 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"]];
  2135. DebugLog(@"offline_login sql:%@",sqlQuery);
  2136. sqlite3_stmt * statement;
  2137. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2138. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2139. {
  2140. if (sqlite3_step(statement) == SQLITE_ROW)
  2141. {
  2142. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2143. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2144. int can_show_price = sqlite3_column_int(statement, 0);
  2145. int can_see_price = sqlite3_column_int(statement, 1);
  2146. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2147. if(contact_id==nil)
  2148. contact_id="";
  2149. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2150. int user_type = sqlite3_column_int(statement, 3);
  2151. int can_cancel_order = sqlite3_column_int(statement, 4);
  2152. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2153. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2154. int can_delete_order = sqlite3_column_int(statement, 7);
  2155. int can_submit_order = sqlite3_column_int(statement, 8);
  2156. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2157. int can_create_order = sqlite3_column_int(statement, 10);
  2158. char *mode = (char*)sqlite3_column_text(statement, 11);
  2159. if(mode==nil)
  2160. mode="";
  2161. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2162. char *username = (char*)sqlite3_column_text(statement, 12);
  2163. if(username==nil)
  2164. username="";
  2165. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2166. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2167. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2168. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2169. [header setValue:nscontact_id forKey:@"contact_id"];
  2170. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2171. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2172. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2173. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2174. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2175. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2176. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2177. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2178. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2179. [header setValue:nsusername forKey:@"username"];
  2180. NSError* error=nil;
  2181. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2182. [header setValue:statusFilter forKey:@"statusFilter"];
  2183. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2184. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2185. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2186. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2187. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"1=1"];
  2188. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2189. [ret setObject:header forKey:@"header"];
  2190. [ret setValue:nsmode forKey:@"mode"];
  2191. }
  2192. sqlite3_finalize(statement);
  2193. }
  2194. [iSalesDB close_db:db];
  2195. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2196. return [RAUtils dict2data:ret];
  2197. }
  2198. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2199. {
  2200. NSString* contactId = [params valueForKey:@"contactId"];
  2201. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2202. sqlite3 *db = [iSalesDB get_db];
  2203. NSString *sqlQuery = nil;
  2204. {
  2205. 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 from offline_contact where contact_id='%@'",contactId];
  2206. }
  2207. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2208. sqlite3_stmt * statement;
  2209. [ret setValue:@"2" forKey:@"result"];
  2210. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2211. {
  2212. //int i = 0;
  2213. if (sqlite3_step(statement) == SQLITE_ROW)
  2214. {
  2215. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2216. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2217. // int editable = sqlite3_column_int(statement, 0);
  2218. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2219. NSString *nscompany_name =nil;
  2220. if(company_name==nil)
  2221. nscompany_name=@"";
  2222. else
  2223. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2224. char *country = (char*)sqlite3_column_text(statement, 2);
  2225. if(country==nil)
  2226. country="";
  2227. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2228. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2229. // if(addr==nil)
  2230. // addr="";
  2231. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2232. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2233. if(zipcode==nil)
  2234. zipcode="";
  2235. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2236. char *state = (char*)sqlite3_column_text(statement, 5);
  2237. if(state==nil)
  2238. state="";
  2239. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2240. char *city = (char*)sqlite3_column_text(statement, 6);
  2241. if(city==nil)
  2242. city="";
  2243. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2244. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2245. // NSString *nscontact_name = nil;
  2246. // if(contact_name==nil)
  2247. // nscontact_name=@"";
  2248. // else
  2249. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2250. char *phone = (char*)sqlite3_column_text(statement, 8);
  2251. NSString *nsphone = nil;
  2252. if(phone==nil)
  2253. nsphone=@"";
  2254. else
  2255. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2256. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2257. if(contact_id==nil)
  2258. contact_id="";
  2259. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2260. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2261. if(addr_1==nil)
  2262. addr_1="";
  2263. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2264. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2265. if(addr_2==nil)
  2266. addr_2="";
  2267. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2268. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2269. if(addr_3==nil)
  2270. addr_3="";
  2271. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2272. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2273. if(addr_4==nil)
  2274. addr_4="";
  2275. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2276. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2277. if(first_name==nil)
  2278. first_name="";
  2279. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2280. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2281. if(last_name==nil)
  2282. last_name="";
  2283. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2284. char *fax = (char*)sqlite3_column_text(statement, 16);
  2285. NSString *nsfax = nil;
  2286. if(fax==nil)
  2287. nsfax=@"";
  2288. else
  2289. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2290. char *email = (char*)sqlite3_column_text(statement, 17);
  2291. NSString *nsemail = nil;
  2292. if(email==nil)
  2293. nsemail=@"";
  2294. else
  2295. nsemail= [[NSString alloc]initWithUTF8String:email];
  2296. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2297. NSString *nsimg_0 = nil;
  2298. if(img_0==nil)
  2299. nsimg_0=@"";
  2300. else
  2301. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2302. [self copy_bcardImg:nsimg_0];
  2303. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2304. NSString *nsimg_1 = nil;
  2305. if(img_1==nil)
  2306. nsimg_1=@"";
  2307. else
  2308. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2309. [self copy_bcardImg:nsimg_1];
  2310. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2311. NSString *nsimg_2 = nil;
  2312. if(img_2==nil)
  2313. nsimg_2=@"";
  2314. else
  2315. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2316. [self copy_bcardImg:nsimg_2];
  2317. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2318. NSString *nsprice_type = nil;
  2319. if(price_type==nil)
  2320. nsprice_type=@"";
  2321. else
  2322. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2323. char *notes = (char*)sqlite3_column_text(statement, 22);
  2324. NSString *nsnotes = nil;
  2325. if(notes==nil)
  2326. nsnotes=@"";
  2327. else
  2328. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2329. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2330. NSString *nssalesrep = nil;
  2331. if(salesrep==nil)
  2332. nssalesrep=@"";
  2333. else
  2334. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2335. {
  2336. // decrypt
  2337. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2338. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2339. nsphone=[AESCrypt fastdecrypt:nsphone];
  2340. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2341. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2342. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2343. }
  2344. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2345. [arr_name addObject:nsfirst_name];
  2346. [arr_name addObject:nslast_name];
  2347. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2348. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2349. [arr_ext addObject:nsaddr_1];
  2350. [arr_ext addObject:nsaddr_2];
  2351. [arr_ext addObject:nsaddr_3];
  2352. [arr_ext addObject:nsaddr_4];
  2353. [arr_ext addObject:@"\r\n"];
  2354. [arr_ext addObject:nscity];
  2355. [arr_ext addObject:nsstate];
  2356. [arr_ext addObject:nszipcode];
  2357. [arr_ext addObject:nscountry];
  2358. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2359. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2360. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2361. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2362. [item setValue:nscountry forKey:@"customer_country"];
  2363. [item setValue:nsphone forKey:@"customer_phone"];
  2364. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2365. [item setValue:nscompany_name forKey:@"customer_name"];
  2366. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2367. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2368. [item setValue:nsext forKey:@"customer_contact_ext"];
  2369. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2370. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2371. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2372. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2373. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2374. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2375. [item setValue:nslast_name forKey:@"customer_last_name"];
  2376. [item setValue:nscity forKey:@"customer_city"];
  2377. [item setValue:nsstate forKey:@"customer_state"];
  2378. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2379. [item setValue:contactId forKey:@"customer_cid"];
  2380. [item setValue:nscontact_name forKey:@"customer_contact"];
  2381. [item setValue:nsfax forKey:@"customer_fax"];
  2382. [item setValue:nsemail forKey:@"customer_email"];
  2383. [ret setObject:item forKey:@"customerInfo"];
  2384. // i++;
  2385. }
  2386. UIApplication * app = [UIApplication sharedApplication];
  2387. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2388. [ret setValue:appDelegate.mode forKey:@"mode"];
  2389. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2390. sqlite3_finalize(statement);
  2391. }
  2392. [iSalesDB close_db:db];
  2393. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2394. return ret;
  2395. }
  2396. + (bool) copy_bcardImg:(NSString*) filename
  2397. {
  2398. if(filename.length==0)
  2399. return false;
  2400. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2401. bool ret=false;
  2402. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2403. NSString *cachefolder = [paths objectAtIndex:0];
  2404. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2405. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2406. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2407. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2408. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2409. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2410. //
  2411. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2412. NSFileManager* fileManager = [NSFileManager defaultManager];
  2413. BOOL bdir=NO;
  2414. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2415. {
  2416. NSError *error = nil;
  2417. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2418. {
  2419. ret=false;
  2420. }
  2421. else
  2422. {
  2423. ret=true;
  2424. }
  2425. // NSError *error = nil;
  2426. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2427. //
  2428. // if(!bsuccess)
  2429. // {
  2430. // DebugLog(@"Create offline_createimg folder failed");
  2431. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2432. // return [RAUtils dict2data:ret];
  2433. // }
  2434. // if(bsuccess)
  2435. // {
  2436. // sqlite3 *db = [self get_db];
  2437. //
  2438. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2439. // [iSalesDB close_db:db];
  2440. // }
  2441. }
  2442. return ret;
  2443. //
  2444. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2445. // if(bsuccess)
  2446. // {
  2447. // NSError *error = nil;
  2448. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2449. // {
  2450. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2451. // }
  2452. // else
  2453. // {
  2454. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2455. //
  2456. // ret[@"img_url_aname"]=filename;
  2457. // ret[@"img_url"]=savedImagePath;
  2458. // }
  2459. // }
  2460. }
  2461. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2462. {
  2463. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2464. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2465. NSString *cachefolder = [paths objectAtIndex:0];
  2466. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2467. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2468. NSFileManager* fileManager = [NSFileManager defaultManager];
  2469. BOOL bdir=YES;
  2470. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2471. {
  2472. NSError *error = nil;
  2473. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2474. if(!bsuccess)
  2475. {
  2476. DebugLog(@"Create offline_createimg folder failed");
  2477. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2478. return [RAUtils dict2data:ret];
  2479. }
  2480. // if(bsuccess)
  2481. // {
  2482. // sqlite3 *db = [self get_db];
  2483. //
  2484. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2485. // [iSalesDB close_db:db];
  2486. // }
  2487. }
  2488. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2489. //JEPG格式
  2490. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2491. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2492. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2493. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2494. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2495. if(bsuccess)
  2496. {
  2497. NSError *error = nil;
  2498. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2499. {
  2500. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2501. }
  2502. else
  2503. {
  2504. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2505. ret[@"img_url_aname"]=filename;
  2506. ret[@"img_url"]=filename;
  2507. }
  2508. }
  2509. else
  2510. {
  2511. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2512. }
  2513. return [RAUtils dict2data:ret];
  2514. }
  2515. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2516. {
  2517. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2518. category = [category substringToIndex:3];
  2519. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2520. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2521. params[@"category"]= category;
  2522. ret[@"params"]= params;
  2523. [ret setValue:@"detail" forKey:@"target"];
  2524. [ret setValue:@"popup" forKey:@"action"];
  2525. [ret setValue:@"content" forKey:@"type"];
  2526. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2527. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2528. [ret setValue:@"true" forKey:@"single_row"];
  2529. [ret setValue:@"true" forKey:@"partial_refresh"];
  2530. // sqlite3 *db = [iSalesDB get_db];
  2531. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2532. NSString *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 ;
  2533. sqlite3_stmt * statement;
  2534. int count = 0;
  2535. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2536. // int count=0;
  2537. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2538. {
  2539. int i=0;
  2540. while (sqlite3_step(statement) == SQLITE_ROW)
  2541. {
  2542. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2543. // char *name = (char*)sqlite3_column_text(statement, 1);
  2544. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2545. char *name = (char*)sqlite3_column_text(statement, 0);
  2546. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2547. int product_id = sqlite3_column_int(statement, 1);
  2548. char *url = (char*)sqlite3_column_text(statement, 2);
  2549. if(url==nil)
  2550. url="";
  2551. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2552. [item setValue:nsurl forKey:@"picture_path"];
  2553. [item setValue:nsname forKey:@"fash_name"];
  2554. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2555. [item setValue:category forKey:@"category"];
  2556. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2557. i++;
  2558. }
  2559. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2560. sqlite3_finalize(statement);
  2561. }
  2562. NSLog(@"count:%d",count);
  2563. // [iSalesDB close_db:db];
  2564. return ret;
  2565. }
  2566. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2567. {
  2568. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2569. [ret setValue:key forKey:@"key"];
  2570. [ret setValue:value forKey:@"val"];
  2571. return ret;
  2572. }
  2573. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2574. {
  2575. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2576. [ret setValue:@"0" forKey:@"img_count"];
  2577. // sqlite3 *db = [iSalesDB get_db];
  2578. 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 ;
  2579. sqlite3_stmt * statement;
  2580. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2581. {
  2582. int i=0;
  2583. if (sqlite3_step(statement) == SQLITE_ROW)
  2584. {
  2585. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2586. // char *name = (char*)sqlite3_column_text(statement, 1);
  2587. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2588. char *value = (char*)sqlite3_column_text(statement, 0);
  2589. if(value==nil)
  2590. value="";
  2591. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2592. char *key = (char*)sqlite3_column_text(statement, 1);
  2593. if(key==nil)
  2594. key="";
  2595. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2596. [item setValue:nsvalue forKey:@"val"];
  2597. [item setValue:nskey forKey:@"key"];
  2598. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2599. [ret setValue:@"1" forKey:@"count"];
  2600. i++;
  2601. }
  2602. sqlite3_finalize(statement);
  2603. }
  2604. // [iSalesDB close_db:db];
  2605. return ret;
  2606. }
  2607. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2608. {
  2609. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2610. [ret setValue:@"0" forKey:@"count"];
  2611. // sqlite3 *db = [iSalesDB get_db];
  2612. 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;
  2613. sqlite3_stmt * statement;
  2614. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2615. {
  2616. int i=0;
  2617. while (sqlite3_step(statement) == SQLITE_ROW)
  2618. {
  2619. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2620. // char *name = (char*)sqlite3_column_text(statement, 1);
  2621. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2622. char *value = (char*)sqlite3_column_text(statement, 0);
  2623. if(value==nil)
  2624. value="";
  2625. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2626. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2627. if(selector_display==nil)
  2628. selector_display="";
  2629. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2630. int product_id = sqlite3_column_int(statement, 2);
  2631. char *category = (char*)sqlite3_column_text(statement, 3);
  2632. if(category==nil)
  2633. category="";
  2634. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2635. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2636. [item setValue:nsvalue forKey:@"title"];
  2637. [item setValue:url forKey:@"pic_url"];
  2638. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2639. [params setValue:@"2" forKey:@"count"];
  2640. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2641. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2642. [param0 setValue:@"product_id" forKey:@"name"];
  2643. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2644. [param1 setValue:nscategory forKey:@"val"];
  2645. [param1 setValue:@"category" forKey:@"name"];
  2646. [params setObject:param0 forKey:@"param_0"];
  2647. [params setObject:param1 forKey:@"param_1"];
  2648. [item setObject:params forKey:@"params"];
  2649. [ret setValue:nsselector_display forKey:@"name"];
  2650. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2651. i++;
  2652. }
  2653. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2654. [ret setValue:@"switch" forKey:@"action"];
  2655. sqlite3_finalize(statement);
  2656. }
  2657. // [iSalesDB close_db:db];
  2658. return ret;
  2659. }
  2660. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2661. {
  2662. // model 在 category search 显示的图片。
  2663. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2664. // sqlite3 *db = [iSalesDB get_db];
  2665. 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];
  2666. sqlite3_stmt * statement;
  2667. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2668. {
  2669. while (sqlite3_step(statement) == SQLITE_ROW)
  2670. {
  2671. char *url = (char*)sqlite3_column_text(statement, 0);
  2672. if(url==nil)
  2673. url="";
  2674. int type = sqlite3_column_int(statement, 1);
  2675. if(type==0)
  2676. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2677. else
  2678. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2679. }
  2680. sqlite3_finalize(statement);
  2681. }
  2682. // [iSalesDB close_db:db];
  2683. return ret;
  2684. }
  2685. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2686. {
  2687. int item_id=-1;
  2688. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2689. sqlite3_stmt * statement;
  2690. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2691. {
  2692. if (sqlite3_step(statement) == SQLITE_ROW)
  2693. {
  2694. item_id = sqlite3_column_int(statement, 0);
  2695. }
  2696. sqlite3_finalize(statement);
  2697. }
  2698. return item_id;
  2699. }
  2700. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2701. {
  2702. // NSString* ret = @"";
  2703. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2704. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2705. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2706. sqlite3_stmt * statement;
  2707. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2708. {
  2709. while (sqlite3_step(statement) == SQLITE_ROW)
  2710. {
  2711. int bitem_id = sqlite3_column_int(statement, 0);
  2712. int bitem_qty = sqlite3_column_int(statement, 1);
  2713. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2714. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2715. }
  2716. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2717. sqlite3_finalize(statement);
  2718. }
  2719. // if(ret==nil)
  2720. // ret=@"";
  2721. *count=arr_count;
  2722. return arr_bundle;
  2723. }
  2724. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2725. {
  2726. // get default sold qty, return -1 if model not found;
  2727. int ret = -1;
  2728. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2729. sqlite3_stmt * statement;
  2730. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2731. {
  2732. if (sqlite3_step(statement) == SQLITE_ROW)
  2733. {
  2734. ret = sqlite3_column_int(statement, 0);
  2735. }
  2736. sqlite3_finalize(statement);
  2737. }
  2738. return ret;
  2739. }
  2740. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2741. {
  2742. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2743. [ret setValue:@"0" forKey:@"img_count"];
  2744. // sqlite3 *db = [iSalesDB get_db];
  2745. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2746. sqlite3_stmt * statement;
  2747. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2748. {
  2749. int i=0;
  2750. while (sqlite3_step(statement) == SQLITE_ROW)
  2751. {
  2752. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2753. // char *name = (char*)sqlite3_column_text(statement, 1);
  2754. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2755. char *url = (char*)sqlite3_column_text(statement, 0);
  2756. if(url==nil)
  2757. url="";
  2758. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2759. [item setValue:nsurl forKey:@"s"];
  2760. [item setValue:nsurl forKey:@"l"];
  2761. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2762. i++;
  2763. }
  2764. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2765. sqlite3_finalize(statement);
  2766. }
  2767. // [iSalesDB close_db:db];
  2768. return ret;
  2769. }
  2770. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2771. UIApplication * app = [UIApplication sharedApplication];
  2772. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2773. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2774. sqlite3 *db = [iSalesDB get_db];
  2775. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2776. NSString* product_id=params[@"product_id"];
  2777. NSString *item_count_str = params[@"item_count"];
  2778. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2779. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2780. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2781. // NSString *sql = @"";
  2782. for(int i=0;i<arr.count;i++)
  2783. {
  2784. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2785. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2786. __block int cart_count = 0;
  2787. if (!item_count_str) {
  2788. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2789. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2790. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2791. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2792. cart_count = [[model_set_components lastObject] integerValue];
  2793. }];
  2794. }
  2795. if(count==0)
  2796. {
  2797. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2798. sqlite3_stmt *stmt;
  2799. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2800. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2801. if (item_count_arr) {
  2802. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2803. } else {
  2804. sqlite3_bind_int(stmt,2,cart_count);
  2805. }
  2806. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2807. [iSalesDB execSql:@"ROLLBACK" db:db];
  2808. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2809. [iSalesDB close_db:db];
  2810. DebugLog(@"add to wishlist error");
  2811. return ret;
  2812. }
  2813. } else {
  2814. int qty = 0;
  2815. if (item_count_arr) {
  2816. qty = [item_count_arr[i] integerValue];
  2817. } else {
  2818. qty = cart_count;
  2819. }
  2820. 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]];
  2821. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2822. [iSalesDB execSql:@"ROLLBACK" db:db];
  2823. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2824. [iSalesDB close_db:db];
  2825. DebugLog(@"add to wishlist error");
  2826. return ret;
  2827. }
  2828. }
  2829. }
  2830. // [iSalesDB execSql:sql db:db];
  2831. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2832. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2833. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2834. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2835. [iSalesDB close_db:db];
  2836. appDelegate.wish_count =count;
  2837. [appDelegate update_count_mark];
  2838. ret[@"result"]= [NSNumber numberWithInt:2];
  2839. return ret;
  2840. }
  2841. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2842. // 0 category
  2843. // 1 search
  2844. // 2 itemsearch
  2845. NSData *ret = nil;
  2846. NSDictionary *items = nil;
  2847. switch (from) {
  2848. case 0:{
  2849. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2850. }
  2851. break;
  2852. case 1:{
  2853. items = [[self search:params limited:NO] objectForKey:@"items"];
  2854. }
  2855. break;
  2856. case 2:{
  2857. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2858. }
  2859. break;
  2860. default:
  2861. break;
  2862. }
  2863. if (!items) {
  2864. return ret;
  2865. }
  2866. int count = [[items objectForKey:@"count"] intValue];
  2867. NSMutableString *product_id_str = [@"" mutableCopy];
  2868. for (int i = 0; i < count; i++) {
  2869. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2870. NSDictionary *item = [items objectForKey:key];
  2871. NSString *product_id = [item objectForKey:@"product_id"];
  2872. if (i == 0) {
  2873. [product_id_str appendString:product_id];
  2874. } else {
  2875. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2876. }
  2877. }
  2878. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2879. if ([add_to isEqualToString:@"cart"]) {
  2880. NSString *order_code = [params objectForKey:@"orderCode"];
  2881. if (order_code.length) {
  2882. NSDictionary *newParams = @{
  2883. @"product_id" : product_id_str,
  2884. @"orderCode" : order_code
  2885. };
  2886. ret = [self offline_add2cart:newParams.mutableCopy];
  2887. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2888. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2889. ret = [RAUtils dict2data:retDic];
  2890. }
  2891. } else if([add_to isEqualToString:@"wishlist"]) {
  2892. NSDictionary *newParams = @{
  2893. @"product_id" : product_id_str
  2894. };
  2895. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2896. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2897. ret = [RAUtils dict2data:retDic];
  2898. } else if([add_to isEqualToString:@"portfolio"]) {
  2899. }
  2900. return ret;
  2901. }
  2902. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2903. {
  2904. return [self addAll:params from:0];
  2905. }
  2906. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2907. {
  2908. return [self addAll:params from:1];
  2909. }
  2910. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2911. {
  2912. return [self addAll:params from:2];
  2913. }
  2914. #pragma mark - Jack
  2915. #warning 做SQL操作时转义!!
  2916. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2917. // "val_227" : {
  2918. // "check" : 1,
  2919. // "value" : "US United States",
  2920. // "value_id" : "228"
  2921. // },
  2922. if (!countryCode) {
  2923. countryCode = @"US";
  2924. }
  2925. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2926. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2927. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2928. int code_id = sqlite3_column_int(stmt, 3); // id
  2929. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2930. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2931. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2932. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2933. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2934. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2935. }
  2936. long n = *count;
  2937. *count = n + 1;
  2938. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2939. [container setValue:countryDic forKey:key];
  2940. }] mutableCopy];
  2941. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2942. return ret;
  2943. }
  2944. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2945. countryCode = [self translateSingleQuote:countryCode];
  2946. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2947. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2948. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2949. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2950. if (name == NULL) {
  2951. name = "";
  2952. }
  2953. if (code == NULL) {
  2954. code = "";
  2955. }
  2956. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2957. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2958. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2959. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2960. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2961. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2962. }
  2963. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2964. [container setValue:stateDic forKey:key];
  2965. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2966. DebugLog(@"query all state error: %@",err_msg);
  2967. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2968. // [stateDic setValue:@"Other" forKey:@"value"];
  2969. // [stateDic setValue:@"" forKey:@"value_id"];
  2970. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2971. //
  2972. // if (state_code && [@"" isEqualToString:state_code]) {
  2973. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2974. // }
  2975. //
  2976. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2977. // [container setValue:stateDic forKey:key];
  2978. }] mutableCopy];
  2979. // failure 可以不用了,一样的
  2980. if (ret.allKeys.count == 0) {
  2981. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2982. [stateDic setValue:@"Other" forKey:@"value"];
  2983. [stateDic setValue:@"" forKey:@"value_id"];
  2984. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2985. if (state_code && [@"" isEqualToString:state_code]) {
  2986. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2987. }
  2988. NSString *key = [NSString stringWithFormat:@"val_0"];
  2989. [ret setValue:stateDic forKey:key];
  2990. }
  2991. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2992. return ret;
  2993. }
  2994. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2995. codeId = [self translateSingleQuote:codeId];
  2996. 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];
  2997. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2998. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2999. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3000. if (name == NULL) {
  3001. name = "";
  3002. }
  3003. if (code == NULL) {
  3004. code = "";
  3005. }
  3006. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3007. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3008. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3009. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3010. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3011. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3012. }
  3013. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3014. [container setValue:stateDic forKey:key];
  3015. }] mutableCopy];
  3016. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3017. return ret;
  3018. }
  3019. + (NSDictionary *)offline_getPrice {
  3020. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3021. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3022. char *name = (char *) sqlite3_column_text(stmt, 1);
  3023. int type = sqlite3_column_int(stmt, 2);
  3024. int orderBy = sqlite3_column_int(stmt, 3);
  3025. if (name == NULL) {
  3026. name = "";
  3027. }
  3028. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3029. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3030. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3031. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3032. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3033. if (orderBy == 0) {
  3034. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3035. }
  3036. [container setValue:priceDic forKey:key];
  3037. }] mutableCopy];
  3038. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3039. return ret;
  3040. }
  3041. + (NSDictionary *)offline_getSalesRep {
  3042. // 首先从offline_login表中取出sales_code
  3043. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3044. NSString *user = app.user;
  3045. user = [self translateSingleQuote:user];
  3046. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3047. __block NSString *user_code = @"";
  3048. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3049. char *code = (char *)sqlite3_column_text(stmt, 0);
  3050. if (code == NULL) {
  3051. code = "";
  3052. }
  3053. user_code = [NSString stringWithUTF8String:code];
  3054. }];
  3055. // 再取所有salesRep
  3056. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3057. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3058. // 1 name 2 code 3 salesrep_id
  3059. char *name = (char *)sqlite3_column_text(stmt, 1);
  3060. char *code = (char *)sqlite3_column_text(stmt, 2);
  3061. int salesrep_id = sqlite3_column_int(stmt, 3);
  3062. if (name == NULL) {
  3063. name = "";
  3064. }
  3065. if (code == NULL) {
  3066. code = "";
  3067. }
  3068. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3069. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3070. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3071. // 比较code 相等则check
  3072. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3073. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3074. }
  3075. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3076. }] mutableCopy];
  3077. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3078. return ret;
  3079. }
  3080. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3081. zipcode = [self translateSingleQuote:zipcode];
  3082. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3083. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3084. char *country = (char *)sqlite3_column_text(stmt, 0);
  3085. char *state = (char *)sqlite3_column_text(stmt, 1);
  3086. char *city = (char *)sqlite3_column_text(stmt, 2);
  3087. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3088. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3089. if (country == NULL) {
  3090. country = "";
  3091. }
  3092. if (state == NULL) {
  3093. state = "";
  3094. }
  3095. if (city == NULL) {
  3096. city = "";
  3097. }
  3098. if (country_code == NULL) {
  3099. country_code = "";
  3100. }
  3101. if (state_code == NULL) {
  3102. state_code = "";
  3103. }
  3104. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3105. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3106. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3107. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3108. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3109. }] mutableCopy];
  3110. return ret;
  3111. }
  3112. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3113. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3114. [item setValue:value forKey:valueKey];
  3115. [dic setValue:item forKey:itemKey];
  3116. }
  3117. + (NSString *)countryCodeByid:(NSString *)code_id {
  3118. NSString *ret = nil;
  3119. code_id = [self translateSingleQuote:code_id];
  3120. sqlite3 *db = [iSalesDB get_db];
  3121. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3122. sqlite3_stmt * statement;
  3123. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3124. while (sqlite3_step(statement) == SQLITE_ROW) {
  3125. char *code = (char *)sqlite3_column_text(statement, 0);
  3126. if (code == NULL) {
  3127. code = "";
  3128. }
  3129. ret = [NSString stringWithUTF8String:code];
  3130. }
  3131. sqlite3_finalize(statement);
  3132. }
  3133. [iSalesDB close_db:db];
  3134. return ret;
  3135. }
  3136. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3137. NSString *ret = nil;
  3138. code = [self translateSingleQuote:code];
  3139. sqlite3 *db = [iSalesDB get_db];
  3140. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3141. sqlite3_stmt * statement;
  3142. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3143. while (sqlite3_step(statement) == SQLITE_ROW) {
  3144. char *_id = (char *)sqlite3_column_text(statement, 0);
  3145. if (_id == NULL) {
  3146. _id = "";
  3147. }
  3148. ret = [NSString stringWithFormat:@"%s",_id];
  3149. }
  3150. sqlite3_finalize(statement);
  3151. }
  3152. [iSalesDB close_db:db];
  3153. return ret;
  3154. }
  3155. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3156. NSString *name = nil;
  3157. codeId = [self translateSingleQuote:codeId];
  3158. sqlite3 *db = [iSalesDB get_db];
  3159. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3160. sqlite3_stmt * statement;
  3161. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3162. while (sqlite3_step(statement) == SQLITE_ROW) {
  3163. char *value = (char *)sqlite3_column_text(statement, 0);
  3164. if (value == NULL) {
  3165. value = "";
  3166. }
  3167. name = [NSString stringWithUTF8String:value];
  3168. }
  3169. sqlite3_finalize(statement);
  3170. }
  3171. [iSalesDB close_db:db];
  3172. return name;
  3173. }
  3174. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3175. NSString *ret = nil;
  3176. sqlite3 *db = [iSalesDB get_db];
  3177. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3178. sqlite3_stmt * statement;
  3179. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3180. while (sqlite3_step(statement) == SQLITE_ROW) {
  3181. char *name = (char *)sqlite3_column_text(statement, 0);
  3182. if (name == NULL) {
  3183. name = "";
  3184. }
  3185. ret = [NSString stringWithUTF8String:name];
  3186. }
  3187. sqlite3_finalize(statement);
  3188. }
  3189. [iSalesDB close_db:db];
  3190. return ret;
  3191. }
  3192. + (NSString *)salesRepCodeById:(NSString *)_id {
  3193. NSString *ret = nil;
  3194. _id = [self translateSingleQuote:_id];
  3195. sqlite3 *db = [iSalesDB get_db];
  3196. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3197. sqlite3_stmt * statement;
  3198. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3199. while (sqlite3_step(statement) == SQLITE_ROW) {
  3200. char *rep = (char *)sqlite3_column_text(statement, 0);
  3201. if (rep == NULL) {
  3202. rep = "";
  3203. }
  3204. ret = [NSString stringWithUTF8String:rep];
  3205. }
  3206. sqlite3_finalize(statement);
  3207. }
  3208. [iSalesDB close_db:db];
  3209. return ret;
  3210. }
  3211. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3212. char *tx = (char *)sqlite3_column_text(stmt, col);
  3213. if (tx == NULL) {
  3214. tx = "";
  3215. }
  3216. NSString *text = [NSString stringWithFormat:@"%s",tx];
  3217. if (!text) {
  3218. text = @"";
  3219. }
  3220. // 将字符全部为' '的字符串干掉
  3221. int spaceCount = 0;
  3222. for (int i = 0; i < text.length; i++) {
  3223. if ([text characterAtIndex:i] == ' ') {
  3224. spaceCount++;
  3225. }
  3226. }
  3227. if (spaceCount == text.length) {
  3228. text = @"";
  3229. }
  3230. return text;
  3231. }
  3232. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3233. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3234. NSData *data = [NSData dataWithContentsOfFile:path];
  3235. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3236. return ret;
  3237. }
  3238. + (NSString *)textFileName:(NSString *)name {
  3239. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3240. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3241. if (!text) {
  3242. text = @"";
  3243. }
  3244. return text;
  3245. }
  3246. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3247. return [[dic objectForKey:key] mutableCopy];
  3248. }
  3249. + (id)translateSingleQuote:(NSString *)string {
  3250. if ([string isKindOfClass:[NSString class]])
  3251. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3252. return string;
  3253. }
  3254. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3255. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3256. }
  3257. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3258. NSString* ret= nil;
  3259. NSString *sqlQuery = nil;
  3260. // 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
  3261. 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];
  3262. sqlite3_stmt * statement;
  3263. // int count=0;
  3264. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3265. {
  3266. if (sqlite3_step(statement) == SQLITE_ROW)
  3267. {
  3268. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3269. if(imgurl==nil)
  3270. imgurl="";
  3271. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3272. ret=nsimgurl;
  3273. }
  3274. sqlite3_finalize(statement);
  3275. }
  3276. else
  3277. {
  3278. [ret setValue:@"8" forKey:@"result"];
  3279. }
  3280. // [iSalesDB close_db:db];
  3281. // DebugLog(@"data string: %@",ret );
  3282. return ret;
  3283. }
  3284. #pragma mark contact list
  3285. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3286. {
  3287. // contactType = "Sales_Order_Customer";
  3288. // limit = 25;
  3289. // offset = 0;
  3290. // password = 123456;
  3291. // "price_name" = 16;
  3292. // user = EvanK;
  3293. sqlite3 *db = [iSalesDB get_db];
  3294. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3295. if (contactType) {
  3296. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3297. } else {
  3298. contactType = @"1 = 1";
  3299. }
  3300. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3301. DebugLog(@"offline contact list keyword: %@",keyword);
  3302. // advanced search
  3303. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3304. if (contact_name) {
  3305. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3306. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3307. } else {
  3308. contact_name = @"";
  3309. }
  3310. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3311. if (customer_phone) {
  3312. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3313. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3314. } else {
  3315. customer_phone = @"";
  3316. }
  3317. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3318. if (customer_fax) {
  3319. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3320. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3321. } else {
  3322. customer_fax = @"";
  3323. }
  3324. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3325. if (customer_zipcode) {
  3326. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3327. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3328. } else {
  3329. customer_zipcode = @"";
  3330. }
  3331. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3332. if (customer_sales_rep) {
  3333. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3334. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3335. } else {
  3336. customer_sales_rep = @"";
  3337. }
  3338. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3339. if (customer_state) {
  3340. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3341. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3342. } else {
  3343. customer_state = @"";
  3344. }
  3345. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3346. if (customer_name) {
  3347. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3348. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3349. } else {
  3350. customer_name = @"";
  3351. }
  3352. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3353. if (customer_country) {
  3354. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3355. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3356. } else {
  3357. customer_country = @"";
  3358. }
  3359. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3360. if (customer_cid) {
  3361. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3362. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3363. } else {
  3364. customer_cid = @"";
  3365. }
  3366. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3367. if (customer_city) {
  3368. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3369. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3370. } else {
  3371. customer_city = @"";
  3372. }
  3373. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3374. if (customer_address) {
  3375. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3376. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3377. } else {
  3378. customer_address = @"";
  3379. }
  3380. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3381. if (customer_email) {
  3382. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3383. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3384. } else {
  3385. customer_email = @"";
  3386. }
  3387. NSString *price_name = [params valueForKey:@"price_name"];
  3388. if (price_name) {
  3389. if ([price_name containsString:@","]) {
  3390. // 首先从 price表中查处name
  3391. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3392. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3393. for (int i = 1;i < pArray.count;i++) {
  3394. NSString *p = pArray[i];
  3395. [mutablePStr appendFormat:@" or type = %@ ",p];
  3396. }
  3397. [mutablePStr appendString:@";"];
  3398. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3399. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3400. char *name = (char *)sqlite3_column_text(stmt, 0);
  3401. if (!name)
  3402. name = "";
  3403. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3404. }];
  3405. // 再根据name 拼sql
  3406. NSMutableString *mutable_price_name = [NSMutableString string];
  3407. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3408. for (int i = 1; i < price_name_array.count; i++) {
  3409. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3410. }
  3411. [mutable_price_name appendString:@")"];
  3412. price_name = mutable_price_name;
  3413. } else {
  3414. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3415. if ([price_name isEqualToString:@""]) {
  3416. price_name = @"";
  3417. } else {
  3418. __block NSString *price;
  3419. price_name = [self translateSingleQuote:price_name];
  3420. [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) {
  3421. char *p = (char *)sqlite3_column_text(stmt, 0);
  3422. if (p == NULL) {
  3423. p = "";
  3424. }
  3425. price = [NSString stringWithUTF8String:p];
  3426. }];
  3427. if ([price isEqualToString:@""]) {
  3428. price_name = @"";
  3429. } else {
  3430. price = [self translateSingleQuote:price];
  3431. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3432. }
  3433. }
  3434. }
  3435. } else {
  3436. price_name = @"";
  3437. }
  3438. int limit = [[params valueForKey:@"limit"] intValue];
  3439. int offset = [[params valueForKey:@"offset"] intValue];
  3440. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3441. 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];
  3442. 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];
  3443. // int result= [iSalesDB AddExFunction:db];
  3444. int count =0;
  3445. NSString *sqlQuery = nil;
  3446. if(keyword.length==0)
  3447. {
  3448. // 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];
  3449. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3450. sqlQuery = sql;
  3451. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3452. }
  3453. else
  3454. {
  3455. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3456. keyword = keyword.lowercaseString;
  3457. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3458. 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];
  3459. 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]];
  3460. }
  3461. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3462. sqlite3_stmt * statement;
  3463. [ret setValue:@"2" forKey:@"result"];
  3464. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3465. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3466. {
  3467. int i = 0;
  3468. while (sqlite3_step(statement) == SQLITE_ROW)
  3469. {
  3470. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3471. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3472. int editable = sqlite3_column_int(statement, 0);
  3473. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3474. NSString *nscompany_name =nil;
  3475. if(company_name==nil)
  3476. nscompany_name=@"";
  3477. else
  3478. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3479. char *country = (char*)sqlite3_column_text(statement, 2);
  3480. if(country==nil)
  3481. country="";
  3482. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3483. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3484. // if(addr==nil)
  3485. // addr="";
  3486. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3487. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3488. if(zipcode==nil)
  3489. zipcode="";
  3490. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3491. char *state = (char*)sqlite3_column_text(statement, 5);
  3492. if(state==nil)
  3493. state="";
  3494. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3495. char *city = (char*)sqlite3_column_text(statement, 6);
  3496. if(city==nil)
  3497. city="";
  3498. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3499. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3500. // NSString *nscontact_name = nil;
  3501. // if(contact_name==nil)
  3502. // nscontact_name=@"";
  3503. // else
  3504. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3505. char *phone = (char*)sqlite3_column_text(statement, 8);
  3506. NSString *nsphone = nil;
  3507. if(phone==nil)
  3508. nsphone=@"";
  3509. else
  3510. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3511. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3512. if(contact_id==nil)
  3513. contact_id="";
  3514. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3515. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3516. if(addr_1==nil)
  3517. addr_1="";
  3518. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3519. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3520. if(addr_2==nil)
  3521. addr_2="";
  3522. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3523. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3524. if(addr_3==nil)
  3525. addr_3="";
  3526. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3527. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3528. if(addr_4==nil)
  3529. addr_4="";
  3530. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3531. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3532. if(first_name==nil)
  3533. first_name="";
  3534. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3535. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3536. if(last_name==nil)
  3537. last_name="";
  3538. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3539. char *fax = (char*)sqlite3_column_text(statement, 16);
  3540. NSString *nsfax = nil;
  3541. if(fax==nil)
  3542. nsfax=@"";
  3543. else
  3544. {
  3545. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3546. if(nsfax.length>0)
  3547. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3548. }
  3549. char *email = (char*)sqlite3_column_text(statement, 17);
  3550. NSString *nsemail = nil;
  3551. if(email==nil)
  3552. nsemail=@"";
  3553. else
  3554. {
  3555. nsemail= [[NSString alloc]initWithUTF8String:email];
  3556. if(nsemail.length>0)
  3557. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3558. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3559. }
  3560. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3561. [arr_name addObject:nsfirst_name];
  3562. [arr_name addObject:nslast_name];
  3563. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3564. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3565. {
  3566. // decrypt
  3567. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3568. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3569. nsphone=[AESCrypt fastdecrypt:nsphone];
  3570. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3571. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3572. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3573. }
  3574. [arr_addr addObject:nscompany_name];
  3575. [arr_addr addObject:nscontact_name];
  3576. [arr_addr addObject:@"<br>"];
  3577. [arr_addr addObject:nsaddr_1];
  3578. [arr_addr addObject:nsaddr_2];
  3579. [arr_addr addObject:nsaddr_3];
  3580. [arr_addr addObject:nsaddr_4];
  3581. //[arr_addr addObject:nsaddr];
  3582. [arr_addr addObject:nszipcode];
  3583. [arr_addr addObject:nscity];
  3584. [arr_addr addObject:nsstate];
  3585. [arr_addr addObject:nscountry];
  3586. [arr_addr addObject:@"<br>"];
  3587. [arr_addr addObject:nsphone];
  3588. [arr_addr addObject:nsfax];
  3589. [arr_addr addObject:nsemail];
  3590. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3591. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3592. [item setValue:name forKey:@"name"];
  3593. [item setValue:nscontact_id forKey:@"contact_id"];
  3594. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3595. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3596. i++;
  3597. }
  3598. UIApplication * app = [UIApplication sharedApplication];
  3599. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3600. [ret setValue:appDelegate.mode forKey:@"mode"];
  3601. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3602. sqlite3_finalize(statement);
  3603. }
  3604. [iSalesDB close_db:db];
  3605. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3606. return ret;
  3607. }
  3608. #pragma mark contact Advanced search
  3609. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3610. {
  3611. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3612. return [RAUtils dict2data:contactAdvanceDic];
  3613. }
  3614. #pragma mark create new contact
  3615. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3616. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3617. NSData *data = [NSData dataWithContentsOfFile:path];
  3618. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3619. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3620. NSString *countryCode = nil;
  3621. NSString *countryCode_id = nil;
  3622. NSString *stateCode = nil;
  3623. NSString *city = nil;
  3624. NSString *zipCode = nil;
  3625. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3626. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3627. NSString *code_id = params[@"country"];
  3628. countryCode_id = code_id;
  3629. countryCode = [self countryCodeByid:code_id];
  3630. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3631. NSString *zip_code = params[@"zipcode"];
  3632. // 剔除全部为空格
  3633. int spaceCount = 0;
  3634. for (int i = 0; i < zip_code.length; i++) {
  3635. if ([zip_code characterAtIndex:i] == ' ') {
  3636. spaceCount++;
  3637. }
  3638. }
  3639. if (spaceCount == zip_code.length) {
  3640. zip_code = @"";
  3641. }
  3642. zipCode = zip_code;
  3643. if (zipCode.length > 0) {
  3644. countryCode_id = params[@"country"];
  3645. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3646. countryCode = [dic valueForKey:@"country_code"];
  3647. if (!countryCode) {
  3648. countryCode = @"US";
  3649. }
  3650. stateCode = [dic valueForKey:@"state_code"];
  3651. city = [dic valueForKey:@"city"];
  3652. // zip code
  3653. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3654. [zipDic setValue:zipCode forKey:@"value"];
  3655. [section_0 setValue:zipDic forKey:@"item_10"];
  3656. }
  3657. }
  3658. } else {
  3659. // default: US United States
  3660. countryCode = @"US";
  3661. countryCode_id = @"228";
  3662. }
  3663. // country
  3664. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3665. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3666. // state
  3667. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3668. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3669. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3670. // city
  3671. if (city) {
  3672. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3673. [cityDic setValue:city forKey:@"value"];
  3674. [section_0 setValue:cityDic forKey:@"item_12"];
  3675. }
  3676. // price type
  3677. NSDictionary *priceDic = [self offline_getPrice];
  3678. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3679. // Sales Rep
  3680. NSDictionary *repDic = [self offline_getSalesRep];
  3681. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3682. [ret setValue:section_0 forKey:@"section_0"];
  3683. return [RAUtils dict2data:ret];
  3684. }
  3685. #pragma mark save
  3686. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3687. NSString *addr = nil;
  3688. NSString *contact_name = nil;
  3689. NSString *companyName = [params objectForKey:@"company"];
  3690. if (companyName) {
  3691. companyName = [AESCrypt fastencrypt:companyName];
  3692. } else {
  3693. companyName = @"";
  3694. }
  3695. DebugLog(@"company");
  3696. companyName = [self translateSingleQuote:companyName];
  3697. NSString *addr1 = [params objectForKey:@"address"];
  3698. NSString *addr2 = [params objectForKey:@"address2"];
  3699. NSString *addr3 = [params objectForKey:@"address_3"];
  3700. NSString *addr4 = [params objectForKey:@"address_4"];
  3701. if (!addr2) {
  3702. addr2 = @"";
  3703. }
  3704. if (!addr3) {
  3705. addr3 = @"";
  3706. }
  3707. if (!addr4) {
  3708. addr4 = @"";
  3709. }
  3710. if (!addr1) {
  3711. addr1 = @"";
  3712. }
  3713. DebugLog(@"addr");
  3714. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3715. addr = [AESCrypt fastencrypt:addr];
  3716. addr = [self translateSingleQuote:addr];
  3717. if (addr1 && ![addr1 isEqualToString:@""]) {
  3718. addr1 = [AESCrypt fastencrypt:addr1];
  3719. }
  3720. addr1 = [self translateSingleQuote:addr1];
  3721. addr2 = [self translateSingleQuote:addr2];
  3722. addr3 = [self translateSingleQuote:addr3];
  3723. addr4 = [self translateSingleQuote:addr4];
  3724. NSString *country = [params objectForKey:@"country"];
  3725. if (country) {
  3726. country = [self countryNameByCountryCodeId:country];
  3727. } else {
  3728. country = @"";
  3729. }
  3730. DebugLog(@"country");
  3731. country = [self translateSingleQuote:country];
  3732. NSString *state = [params objectForKey:@"state"];
  3733. if (!state) {
  3734. state = @"";
  3735. }
  3736. DebugLog(@"state");
  3737. state = [self translateSingleQuote:state];
  3738. NSString *city = [params objectForKey:@"city"];
  3739. if (!city) {
  3740. city = @"";
  3741. }
  3742. city = [self translateSingleQuote:city];
  3743. NSString *zipcode = [params objectForKey:@"zipcode"];
  3744. if (!zipcode) {
  3745. zipcode = @"";
  3746. }
  3747. DebugLog(@"zip");
  3748. zipcode = [self translateSingleQuote:zipcode];
  3749. NSString *fistName = [params objectForKey:@"firstname"];
  3750. if (!fistName) {
  3751. fistName = @"";
  3752. }
  3753. NSString *lastName = [params objectForKey:@"lastname"];
  3754. if (!lastName) {
  3755. lastName = @"";
  3756. }
  3757. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3758. DebugLog(@"contact_name");
  3759. contact_name = [self translateSingleQuote:contact_name];
  3760. fistName = [self translateSingleQuote:fistName];
  3761. lastName = [self translateSingleQuote:lastName];
  3762. NSString *phone = [params objectForKey:@"phone"];
  3763. if (phone) {
  3764. phone = [AESCrypt fastencrypt:phone];
  3765. } else {
  3766. phone = @"";
  3767. }
  3768. DebugLog(@"PHONE");
  3769. phone = [self translateSingleQuote:phone];
  3770. NSString *fax = [params objectForKey:@"fax"];
  3771. if (!fax) {
  3772. fax = @"";
  3773. }
  3774. DebugLog(@"FAX");
  3775. fax = [self translateSingleQuote:fax];
  3776. NSString *email = [params objectForKey:@"email"];
  3777. if (!email) {
  3778. email = @"";
  3779. }
  3780. DebugLog(@"EMAIL:%@",email);
  3781. email = [self translateSingleQuote:email];
  3782. NSString *notes = [params objectForKey:@"contact_notes"];
  3783. if (!notes) {
  3784. notes = @"";
  3785. }
  3786. DebugLog(@"NOTE:%@",notes);
  3787. notes = [self translateSingleQuote:notes];
  3788. NSString *price = [params objectForKey:@"price_name"];
  3789. if (price) {
  3790. price = [self priceNameByPriceId:price];
  3791. } else {
  3792. price = @"";
  3793. }
  3794. DebugLog(@"PRICE");
  3795. price = [self translateSingleQuote:price];
  3796. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3797. if (salesRep) {
  3798. salesRep = [self salesRepCodeById:salesRep];
  3799. } else {
  3800. salesRep = @"";
  3801. }
  3802. salesRep = [self translateSingleQuote:salesRep];
  3803. NSString *img = [params objectForKey:@"business_card"];
  3804. NSArray *array = [img componentsSeparatedByString:@","];
  3805. NSString *img_0 = array[0];
  3806. if (!img_0) {
  3807. img_0 = @"";
  3808. }
  3809. img_0 = [self translateSingleQuote:img_0];
  3810. NSString *img_1 = array[1];
  3811. if (!img_1) {
  3812. img_1 = @"";
  3813. }
  3814. img_1 = [self translateSingleQuote:img_1];
  3815. NSString *img_2 = array[2];
  3816. if (!img_2) {
  3817. img_2 = @"";
  3818. }
  3819. img_2 = [self translateSingleQuote:img_2];
  3820. NSString *contact_id = [NSUUID UUID].UUIDString;
  3821. // 判断更新时是否为customer
  3822. if (update) {
  3823. contact_id = [params objectForKey:@"contact_id"];
  3824. if (!contact_id) {
  3825. contact_id = @"";
  3826. }
  3827. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3828. __block int customer = 0;
  3829. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3830. customer = sqlite3_column_int(stmt, 0);
  3831. }];
  3832. isCustomer = customer ? YES : NO;
  3833. }
  3834. NSMutableDictionary *sync_dic = [params mutableCopy];
  3835. if (isCustomer) {
  3836. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3837. } else {
  3838. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3839. }
  3840. NSString *sync_data = nil;
  3841. NSString *sql = nil;
  3842. if (update){
  3843. contact_id = [params objectForKey:@"contact_id"];
  3844. if (!contact_id) {
  3845. contact_id = @"";
  3846. }
  3847. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3848. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3849. sync_data = [RAUtils dict2string:sync_dic];
  3850. sync_data = [self translateSingleQuote:sync_data];
  3851. 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 = '%@' 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_id];
  3852. } else {
  3853. contact_id = [self translateSingleQuote:contact_id];
  3854. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3855. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3856. sync_data = [RAUtils dict2string:sync_dic];
  3857. sync_data = [self translateSingleQuote:sync_data];
  3858. 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) 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];
  3859. }
  3860. int result = [iSalesDB execSql:sql];
  3861. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3862. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3863. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3864. }
  3865. #pragma mark save new contact
  3866. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3867. {
  3868. return [self offline_saveContact:params update:NO isCustomer:YES];
  3869. }
  3870. #pragma mark edit contact
  3871. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3872. {
  3873. // {
  3874. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3875. // password = 123456;
  3876. // user = EvanK;
  3877. // }
  3878. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3879. NSData *data = [NSData dataWithContentsOfFile:path];
  3880. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3881. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3882. NSString *countryCode = nil;
  3883. NSString *countryCode_id = nil;
  3884. NSString *stateCode = nil;
  3885. /*------contact infor------*/
  3886. __block NSString *country = nil;
  3887. __block NSString *company_name = nil;
  3888. __block NSString *contact_id = params[@"contact_id"];
  3889. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3890. __block NSString *zipcode = nil;
  3891. __block NSString *state = nil; // state_code
  3892. __block NSString *city = nil; //
  3893. __block NSString *firt_name,*last_name;
  3894. __block NSString *phone,*fax,*email;
  3895. __block NSString *notes,*price_type,*sales_rep;
  3896. __block NSString *img_0,*img_1,*img_2;
  3897. contact_id = [self translateSingleQuote:contact_id];
  3898. 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 from offline_contact where contact_id = '%@';",contact_id];
  3899. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3900. country = [self textAtColumn:0 statement:stmt]; // country name
  3901. company_name = [self textAtColumn:1 statement:stmt];
  3902. addr_1 = [self textAtColumn:2 statement:stmt];
  3903. addr_2 = [self textAtColumn:3 statement:stmt];
  3904. addr_3 = [self textAtColumn:4 statement:stmt];
  3905. addr_4 = [self textAtColumn:5 statement:stmt];
  3906. zipcode = [self textAtColumn:6 statement:stmt];
  3907. state = [self textAtColumn:7 statement:stmt]; // state code
  3908. city = [self textAtColumn:8 statement:stmt];
  3909. firt_name = [self textAtColumn:9 statement:stmt];
  3910. last_name = [self textAtColumn:10 statement:stmt];
  3911. phone = [self textAtColumn:11 statement:stmt];
  3912. fax = [self textAtColumn:12 statement:stmt];
  3913. email = [self textAtColumn:13 statement:stmt];
  3914. notes = [self textAtColumn:14 statement:stmt];
  3915. price_type = [self textAtColumn:15 statement:stmt]; // name
  3916. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3917. img_0 = [self textAtColumn:17 statement:stmt];
  3918. img_1 = [self textAtColumn:18 statement:stmt];
  3919. img_2 = [self textAtColumn:19 statement:stmt];
  3920. }];
  3921. // decrypt
  3922. if (company_name) {
  3923. company_name = [AESCrypt fastdecrypt:company_name];
  3924. }
  3925. if (addr_1) {
  3926. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3927. }
  3928. if (phone) {
  3929. phone = [AESCrypt fastdecrypt:phone];
  3930. }
  3931. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3932. countryCode = [iSalesDB jk_queryText:countrySql];
  3933. stateCode = state;
  3934. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3935. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3936. NSString *code_id = params[@"country"];
  3937. countryCode_id = code_id;
  3938. countryCode = [self countryCodeByid:code_id];
  3939. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3940. NSString *zip_code = params[@"zipcode"];
  3941. // 剔除全部为空格
  3942. int spaceCount = 0;
  3943. for (int i = 0; i < zip_code.length; i++) {
  3944. if ([zip_code characterAtIndex:i] == ' ') {
  3945. spaceCount++;
  3946. }
  3947. }
  3948. if (spaceCount == zip_code.length) {
  3949. zip_code = @"";
  3950. }
  3951. if (zipcode.length > 0) {
  3952. zipcode = zip_code;
  3953. countryCode_id = params[@"country"];
  3954. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3955. countryCode = [dic valueForKey:@"country_code"];
  3956. stateCode = [dic valueForKey:@"state_code"];
  3957. city = [dic valueForKey:@"city"];
  3958. // zip code
  3959. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3960. [zipDic setValue:zipcode forKey:@"value"];
  3961. [section_0 setValue:zipDic forKey:@"item_8"];
  3962. }
  3963. }
  3964. }
  3965. // 0 Country
  3966. // 1 Company Name
  3967. // 2 Contact ID
  3968. // 3 Picture
  3969. // 4 Address 1
  3970. // 5 Address 2
  3971. // 6 Address 3
  3972. // 7 Address 4
  3973. // 8 Zip Code
  3974. // 9 State/Province
  3975. // 10 City
  3976. // 11 Contact First Name
  3977. // 12 Contact Last Name
  3978. // 13 Phone
  3979. // 14 Fax
  3980. // 15 Email
  3981. // 16 Contact Notes
  3982. // 17 Price Type
  3983. // 18 Sales Rep
  3984. // country
  3985. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3986. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3987. // company
  3988. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3989. // contact_id
  3990. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3991. // picture
  3992. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3993. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3994. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3995. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3996. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3997. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3998. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3999. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4000. // addr 1 2 3 4
  4001. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4002. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4003. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4004. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4005. // zip code
  4006. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4007. // state
  4008. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4009. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4010. // city
  4011. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4012. // first last
  4013. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4014. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4015. // phone fax email
  4016. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4017. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4018. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4019. // notes
  4020. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4021. // price
  4022. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4023. for (NSString *key in priceDic.allKeys) {
  4024. if ([key containsString:@"val_"]) {
  4025. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4026. if ([dic[@"value"] isEqualToString:price_type]) {
  4027. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4028. [priceDic setValue:dic forKey:key];
  4029. }
  4030. }
  4031. }
  4032. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4033. // Sales Rep
  4034. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4035. for (NSString *key in repDic.allKeys) {
  4036. if ([key containsString:@"val_"]) {
  4037. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4038. NSString *value = dic[@"value"];
  4039. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4040. if (code && [code isEqualToString:sales_rep]) {
  4041. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4042. [repDic setValue:dic forKey:key];
  4043. }
  4044. }
  4045. }
  4046. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4047. [ret setValue:section_0 forKey:@"section_0"];
  4048. return [RAUtils dict2data:ret];
  4049. }
  4050. #pragma mark save contact
  4051. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4052. {
  4053. return [self offline_saveContact:params update:YES isCustomer:YES];
  4054. }
  4055. #pragma mark category
  4056. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4057. if (ck) {
  4058. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4059. for (NSString *key in res.allKeys) {
  4060. if (![key isEqualToString:@"count"]) {
  4061. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4062. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4063. if ([val[@"value"] isEqualToString:ck]) {
  4064. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4065. }
  4066. [res setValue:val forKey:key];
  4067. }
  4068. }
  4069. [dic setValue:res forKey:valueKey];
  4070. }
  4071. }
  4072. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4073. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4074. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4075. NSString* category = [params valueForKey:@"category"];
  4076. if (!category || [category isEqualToString:@""]) {
  4077. category = @"%";
  4078. }
  4079. category = [self translateSingleQuote:category];
  4080. int limit = [[params valueForKey:@"limit"] intValue];
  4081. int offset = [[params valueForKey:@"offset"] intValue];
  4082. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4083. NSString *limit_str = @"";
  4084. if (limited) {
  4085. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4086. }
  4087. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4088. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4089. sqlite3 *db = [iSalesDB get_db];
  4090. // [iSalesDB AddExFunction:db];
  4091. int count;
  4092. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4093. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 %@ ;",appDelegate.order_code, category,limit_str];
  4094. double price_min = 0;
  4095. double price_max = 0;
  4096. if ([params.allKeys containsObject:@"alert"]) {
  4097. // alert
  4098. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4099. NSString *alert = params[@"alert"];
  4100. if ([alert isEqualToString:@"Display All"]) {
  4101. alert = [NSString stringWithFormat:@""];
  4102. } else {
  4103. alert = [self translateSingleQuote:alert];
  4104. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4105. }
  4106. // available
  4107. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4108. NSString *available = params[@"available"];
  4109. NSString *available_condition;
  4110. if ([available isEqualToString:@"Display All"]) {
  4111. available_condition = @"";
  4112. } else if ([available isEqualToString:@"Available Now"]) {
  4113. available_condition = @"and availability > 0";
  4114. } else {
  4115. available_condition = @"and availability == 0";
  4116. }
  4117. // best seller
  4118. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4119. NSString *best_seller = @"";
  4120. NSString *order_best_seller = @"m.name asc";
  4121. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4122. best_seller = @"and best_seller > 0";
  4123. order_best_seller = @"m.best_seller desc,m.name asc";
  4124. }
  4125. // price
  4126. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4127. NSString *price = params[@"price"];
  4128. price_min = 0;
  4129. price_max = MAXFLOAT;
  4130. if (appDelegate.user) {
  4131. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4132. NSMutableString *priceName = [NSMutableString string];
  4133. for (int i = 0; i < priceTypeArray.count; i++) {
  4134. NSString *pricetype = priceTypeArray[i];
  4135. pricetype = [self translateSingleQuote:pricetype];
  4136. if (i == 0) {
  4137. [priceName appendFormat:@"'%@'",pricetype];
  4138. } else {
  4139. [priceName appendFormat:@",'%@'",pricetype];
  4140. }
  4141. }
  4142. if ([price isEqualToString:@"Display All"]) {
  4143. price = [NSString stringWithFormat:@""];
  4144. } else if([price containsString:@"+"]){
  4145. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4146. price_min = [price doubleValue];
  4147. 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];
  4148. } else {
  4149. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4150. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4151. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4152. 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];
  4153. }
  4154. } else {
  4155. price = @"";
  4156. }
  4157. // sold_by_qty : Sold in quantities of %@
  4158. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4159. NSString *qty = params[@"sold_by_qty"];
  4160. if ([qty isEqualToString:@"Display All"]) {
  4161. qty = @"";
  4162. } else {
  4163. qty = [self translateSingleQuote:qty];
  4164. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4165. }
  4166. // cate
  4167. category = [self translateSingleQuote:category];
  4168. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4169. // where bestseller > 0 order by bestseller desc
  4170. // sql query: alert availability(int) best_seller(int) price qty
  4171. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller 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,appDelegate.order_code,order_best_seller,limit_str];
  4172. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4173. }
  4174. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4175. if (!appDelegate.user) {
  4176. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4177. }
  4178. [ret setValue:filter forKey:@"filter"];
  4179. DebugLog(@"offline_category sql:%@",sqlQuery);
  4180. sqlite3_stmt * statement;
  4181. [ret setValue:@"2" forKey:@"result"];
  4182. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4183. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4184. // int count=0;
  4185. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4186. {
  4187. int i=0;
  4188. while (sqlite3_step(statement) == SQLITE_ROW)
  4189. {
  4190. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4191. char *name = (char*)sqlite3_column_text(statement, 0);
  4192. if(name==nil)
  4193. name="";
  4194. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4195. char *description = (char*)sqlite3_column_text(statement, 1);
  4196. if(description==nil)
  4197. description="";
  4198. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4199. int product_id = sqlite3_column_int(statement, 2);
  4200. int wid = sqlite3_column_int(statement, 3);
  4201. int closeout = sqlite3_column_int(statement, 4);
  4202. int cid = sqlite3_column_int(statement, 5);
  4203. int wisdelete = sqlite3_column_int(statement, 6);
  4204. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4205. if(wid !=0 && wisdelete != 1)
  4206. [item setValue:@"true" forKey:@"wish_exists"];
  4207. else
  4208. [item setValue:@"false" forKey:@"wish_exists"];
  4209. if(closeout==0)
  4210. [item setValue:@"false" forKey:@"is_closeout"];
  4211. else
  4212. [item setValue:@"true" forKey:@"is_closeout"];
  4213. if(cid==0)
  4214. [item setValue:@"false" forKey:@"cart_exists"];
  4215. else
  4216. [item setValue:@"true" forKey:@"cart_exists"];
  4217. [item addEntriesFromDictionary:imgjson];
  4218. // [item setValue:nsurl forKey:@"img"];
  4219. [item setValue:nsname forKey:@"name"];
  4220. [item setValue:nsdescription forKey:@"description"];
  4221. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4222. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4223. i++;
  4224. }
  4225. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4226. [ret setObject:items forKey:@"items"];
  4227. sqlite3_finalize(statement);
  4228. } else {
  4229. DebugLog(@"nothing...");
  4230. }
  4231. DebugLog(@"count:%d",count);
  4232. [iSalesDB close_db:db];
  4233. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4234. return ret;
  4235. }
  4236. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4237. {
  4238. return [self categoryList:params limited:YES];
  4239. }
  4240. # pragma mark item search
  4241. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4242. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4243. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4244. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4245. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4246. // category
  4247. NSDictionary *category_menu = [self offline_category_menu];
  4248. [filter setValue:category_menu forKey:@"category"];
  4249. NSString* where= nil;
  4250. NSString* orderby= @"m.name";
  4251. if (!filterSearch) {
  4252. int covertype = [[params valueForKey:@"covertype"] intValue];
  4253. switch (covertype) {
  4254. case 0:
  4255. {
  4256. where=@"m.category like'%%#005#%%'";
  4257. break;
  4258. }
  4259. case 1:
  4260. {
  4261. where=@"m.alert like '%QS%'";
  4262. break;
  4263. }
  4264. case 2:
  4265. {
  4266. where=@"m.availability>0";
  4267. break;
  4268. }
  4269. case 3:
  4270. {
  4271. where=@"m.best_seller>0";
  4272. orderby=@"m.best_seller desc,m.name asc";
  4273. break;
  4274. }
  4275. default:
  4276. where=@"1=1";
  4277. break;
  4278. }
  4279. }
  4280. int limit = [[params valueForKey:@"limit"] intValue];
  4281. int offset = [[params valueForKey:@"offset"] intValue];
  4282. NSString *limit_str = @"";
  4283. if (limited) {
  4284. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4285. }
  4286. sqlite3 *db = [iSalesDB get_db];
  4287. // [iSalesDB AddExFunction:db];
  4288. int count;
  4289. NSString *sqlQuery = nil;
  4290. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4291. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  4292. double price_min = 0;
  4293. double price_max = 0;
  4294. if (filterSearch) {
  4295. // alert
  4296. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4297. NSString *alert = params[@"alert"];
  4298. if ([alert isEqualToString:@"Display All"]) {
  4299. alert = [NSString stringWithFormat:@""];
  4300. } else {
  4301. alert = [self translateSingleQuote:alert];
  4302. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4303. }
  4304. // available
  4305. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4306. NSString *available = params[@"available"];
  4307. NSString *available_condition;
  4308. if ([available isEqualToString:@"Display All"]) {
  4309. available_condition = @"";
  4310. } else if ([available isEqualToString:@"Available Now"]) {
  4311. available_condition = @"and availability > 0";
  4312. } else {
  4313. available_condition = @"and availability == 0";
  4314. }
  4315. // best seller
  4316. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4317. NSString *best_seller = @"";
  4318. NSString *order_best_seller = @"m.name asc";
  4319. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4320. best_seller = @"and best_seller > 0";
  4321. order_best_seller = @"m.best_seller desc,m.name asc";
  4322. }
  4323. // price
  4324. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4325. NSString *price = params[@"price"];
  4326. price_min = 0;
  4327. price_max = MAXFLOAT;
  4328. if (appDelegate.user) {
  4329. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4330. NSMutableString *priceName = [NSMutableString string];
  4331. for (int i = 0; i < priceTypeArray.count; i++) {
  4332. NSString *pricetype = priceTypeArray[i];
  4333. pricetype = [self translateSingleQuote:pricetype];
  4334. if (i == 0) {
  4335. [priceName appendFormat:@"'%@'",pricetype];
  4336. } else {
  4337. [priceName appendFormat:@",'%@'",pricetype];
  4338. }
  4339. }
  4340. if ([price isEqualToString:@"Display All"]) {
  4341. price = [NSString stringWithFormat:@""];
  4342. } else if([price containsString:@"+"]){
  4343. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4344. price_min = [price doubleValue];
  4345. 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];
  4346. } else {
  4347. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4348. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4349. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4350. 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];
  4351. }
  4352. } else {
  4353. price = @"";
  4354. }
  4355. // sold_by_qty : Sold in quantities of %@
  4356. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4357. NSString *qty = params[@"sold_by_qty"];
  4358. if ([qty isEqualToString:@"Display All"]) {
  4359. qty = @"";
  4360. } else {
  4361. qty = [self translateSingleQuote:qty];
  4362. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4363. }
  4364. // category
  4365. NSString *category_id = params[@"ctgId"];
  4366. NSMutableArray *cate_id_array = nil;
  4367. NSMutableString *cateWhere = [NSMutableString string];
  4368. if ([category_id isEqualToString:@""] || !category_id) {
  4369. [cateWhere appendString:@"1 = 1"];
  4370. } else {
  4371. if ([category_id containsString:@","]) {
  4372. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4373. } else {
  4374. cate_id_array = [@[category_id] mutableCopy];
  4375. }
  4376. /*-----------*/
  4377. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4378. for (int i = 0; i < cate_id_array.count; i++) {
  4379. for (NSString *key0 in cateDic.allKeys) {
  4380. if ([key0 containsString:@"category_"]) {
  4381. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4382. for (NSString *key1 in category0.allKeys) {
  4383. if ([key1 containsString:@"category_"]) {
  4384. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4385. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4386. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4387. cate_id_array[i] = [category1 objectForKey:@"id"];
  4388. if (i == 0) {
  4389. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4390. } else {
  4391. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4392. }
  4393. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4394. [category0 setValue:category1 forKey:key1];
  4395. [cateDic setValue:category0 forKey:key0];
  4396. }
  4397. }
  4398. }
  4399. }
  4400. }
  4401. }
  4402. [filter setValue:cateDic forKey:@"category"];
  4403. }
  4404. // where bestseller > 0 order by bestseller desc
  4405. // sql query: alert availability(int) best_seller(int) price qty
  4406. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller 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,appDelegate.order_code,order_best_seller,limit_str];
  4407. // count
  4408. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4409. }
  4410. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4411. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4412. if (!appDelegate.user) {
  4413. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4414. }
  4415. [ret setValue:filter forKey:@"filter"];
  4416. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4417. sqlite3_stmt * statement;
  4418. [ret setValue:@"2" forKey:@"result"];
  4419. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4420. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4421. // int count=0;
  4422. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4423. {
  4424. int i=0;
  4425. while (sqlite3_step(statement) == SQLITE_ROW)
  4426. {
  4427. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4428. char *name = (char*)sqlite3_column_text(statement, 0);
  4429. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4430. char *description = (char*)sqlite3_column_text(statement, 1);
  4431. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4432. int product_id = sqlite3_column_int(statement, 2);
  4433. int wid = sqlite3_column_int(statement, 3);
  4434. int closeout = sqlite3_column_int(statement, 4);
  4435. int cid = sqlite3_column_int(statement, 5);
  4436. int wisdelete = sqlite3_column_int(statement, 6);
  4437. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4438. if(wid !=0 && wisdelete != 1)
  4439. [item setValue:@"true" forKey:@"wish_exists"];
  4440. else
  4441. [item setValue:@"false" forKey:@"wish_exists"];
  4442. if(closeout==0)
  4443. [item setValue:@"false" forKey:@"is_closeout"];
  4444. else
  4445. [item setValue:@"true" forKey:@"is_closeout"];
  4446. if(cid==0)
  4447. [item setValue:@"false" forKey:@"cart_exists"];
  4448. else
  4449. [item setValue:@"true" forKey:@"cart_exists"];
  4450. [item addEntriesFromDictionary:imgjson];
  4451. // [item setValue:nsurl forKey:@"img"];
  4452. [item setValue:nsname forKey:@"fash_name"];
  4453. [item setValue:nsdescription forKey:@"description"];
  4454. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4455. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4456. i++;
  4457. }
  4458. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4459. [ret setObject:items forKey:@"items"];
  4460. sqlite3_finalize(statement);
  4461. }
  4462. [iSalesDB close_db:db];
  4463. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4464. return ret;
  4465. }
  4466. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4467. {
  4468. return [self itemsearch:params limited:YES];
  4469. }
  4470. #pragma mark order detail
  4471. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4472. if (str1.length == 0) {
  4473. str1 = @"&nbsp";
  4474. }
  4475. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4476. return str;
  4477. }
  4478. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4479. {
  4480. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4481. [fromformatter setDateFormat:from];
  4482. NSDate *date = [fromformatter dateFromString:datetime];
  4483. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4484. [toformatter setDateFormat:to];
  4485. NSString * ret = [toformatter stringFromDate:date];
  4486. return ret;
  4487. }
  4488. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4489. // 把毫秒去掉
  4490. if ([dateTime containsString:@"."]) {
  4491. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4492. }
  4493. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4494. formatter.dateFormat = formate;
  4495. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4496. NSDate *date = [formatter dateFromString:dateTime];
  4497. formatter.dateFormat = newFormate;
  4498. NSString *result = [formatter stringFromDate:date];
  4499. return result ? result : @"";
  4500. }
  4501. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4502. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4503. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4504. }
  4505. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4506. {
  4507. DebugLog(@"offline oderdetail params: %@",params);
  4508. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4509. int orderId = [params[@"orderId"] intValue];
  4510. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4511. // decrypt card number and card security code
  4512. // 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 ];
  4513. 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 _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4514. sqlite3 *db = [iSalesDB get_db];
  4515. sqlite3_stmt * statement;
  4516. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4517. NSString *nssoid = @"";
  4518. NSString* orderinfo = @"";
  4519. NSString *moreInfo = @"";
  4520. double handlingFee = 0;
  4521. double payments_and_credist = 0;
  4522. double totalPrice = 0;
  4523. double shippingFee = 0;
  4524. double lift_gate = 0;
  4525. NSString *customer_contact = @"";
  4526. NSString *customer_email = @"";
  4527. NSString *customer_fax = @"";
  4528. NSString *customer_phone = @"";
  4529. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4530. {
  4531. if (sqlite3_step(statement) == SQLITE_ROW)
  4532. {
  4533. int order_id = sqlite3_column_int(statement, 0);
  4534. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4535. ret[@"sign_url"] = sign_url;
  4536. customer_contact = [self textAtColumn:52 statement:statement];
  4537. customer_email = [self textAtColumn:53 statement:statement];
  4538. customer_phone = [self textAtColumn:54 statement:statement];
  4539. customer_fax = [self textAtColumn:55 statement:statement];
  4540. int offline_edit=sqlite3_column_int(statement, 56);
  4541. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4542. char *soid = (char*)sqlite3_column_text(statement, 1);
  4543. if(soid==nil)
  4544. soid= "";
  4545. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4546. // so#
  4547. ret[@"so#"] = nssoid;
  4548. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4549. if(poNumber==nil)
  4550. poNumber= "";
  4551. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4552. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4553. if(create_time==nil)
  4554. create_time= "";
  4555. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4556. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4557. int status = sqlite3_column_int(statement, 4);
  4558. int erpStatus = sqlite3_column_int(statement, 49);
  4559. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4560. // status
  4561. if (status > 1 && status != 3) {
  4562. status = erpStatus;
  4563. } else if (status == 3) {
  4564. status = 15;
  4565. }
  4566. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4567. ret[@"order_status"] = nsstatus;
  4568. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4569. if(company_name==nil)
  4570. company_name= "";
  4571. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4572. // company name
  4573. ret[@"company_name"] = nscompany_name;
  4574. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4575. if(customer_contact==nil)
  4576. customer_contact= "";
  4577. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4578. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4579. if(addr_1==nil)
  4580. addr_1="";
  4581. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4582. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4583. if(addr_2==nil)
  4584. addr_2="";
  4585. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4586. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4587. if(addr_3==nil)
  4588. addr_3="";
  4589. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4590. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4591. if(addr_4==nil)
  4592. addr_4="";
  4593. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4594. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4595. [arr_addr addObject:nsaddr_1];
  4596. [arr_addr addObject:nsaddr_2];
  4597. [arr_addr addObject:nsaddr_3];
  4598. [arr_addr addObject:nsaddr_4];
  4599. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4600. char *logist = (char*)sqlite3_column_text(statement, 11);
  4601. if(logist==nil)
  4602. logist= "";
  4603. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4604. if (status == -11 || status == 10 || status == 11) {
  4605. nslogist = [self textAtColumn:59 statement:statement];
  4606. };
  4607. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4608. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4609. shipping = @"Shipping To Be Quoted";
  4610. } else {
  4611. shippingFee = sqlite3_column_double(statement, 12);
  4612. }
  4613. // Shipping
  4614. ret[@"Shipping"] = shipping;
  4615. int have_lift_gate = sqlite3_column_int(statement, 17);
  4616. lift_gate = sqlite3_column_double(statement, 13);
  4617. // Liftgate Fee(No loading dock)
  4618. if (!have_lift_gate) {
  4619. lift_gate = 0;
  4620. }
  4621. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4622. if (sqlite3_column_int(statement, 57)) {
  4623. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4624. }
  4625. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4626. if(general_notes==nil)
  4627. general_notes= "";
  4628. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4629. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4630. if(internal_notes==nil)
  4631. internal_notes= "";
  4632. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4633. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4634. if(payment_type==nil)
  4635. payment_type= "";
  4636. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4637. // order info
  4638. orderinfo = [self textFileName:@"order_info.html"];
  4639. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4640. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4641. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4642. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4643. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4644. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4645. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4646. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4647. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4648. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4649. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4650. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4651. NSString *payment = nil;
  4652. if([nspayment_type isEqualToString:@"Credit Card"])
  4653. {
  4654. payment = [self textFileName:@"creditcardpayment.html"];
  4655. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4656. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4657. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4658. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4659. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4660. NSString *card_type = [self textAtColumn:42 statement:statement];
  4661. if (card_type.length > 0) { // 显示星号
  4662. card_type = @"****";
  4663. }
  4664. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4665. if (card_number.length > 0 && card_number.length > 4) {
  4666. for (int i = 0; i < card_number.length - 4; i++) {
  4667. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4668. }
  4669. } else {
  4670. card_number = @"";
  4671. }
  4672. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4673. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4674. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4675. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4676. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4677. card_expiration = @"****";
  4678. }
  4679. NSString *card_city = [self textAtColumn:46 statement:statement];
  4680. NSString *card_state = [self textAtColumn:47 statement:statement];
  4681. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4682. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4683. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4684. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4685. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4686. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4687. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4688. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4689. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4690. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4691. }
  4692. else
  4693. {
  4694. payment=[self textFileName:@"normalpayment.html"];
  4695. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4696. }
  4697. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4698. ret[@"result"]= [NSNumber numberWithInt:2];
  4699. // more info
  4700. moreInfo = [self textFileName:@"more_info.html"];
  4701. /*****ship to******/
  4702. // ShipToCompany_or_&nbsp
  4703. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4704. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4705. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4706. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4707. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4708. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4709. /*****ship from******/
  4710. // ShipFromCompany_or_&nbsp
  4711. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4712. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4713. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4714. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4715. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4716. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4717. /*****freight to******/
  4718. // FreightBillToCompany_or_&nbsp
  4719. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4720. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4721. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4722. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4723. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4724. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4725. /*****merchandise to******/
  4726. // MerchandiseBillToCompany_or_&nbsp
  4727. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4728. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4729. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4730. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4731. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4732. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4733. /*****return to******/
  4734. // ReturnToCompany_or_&nbsp
  4735. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4736. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4737. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4738. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4739. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4740. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4741. //
  4742. // DebugLog(@"more info : %@",moreInfo);
  4743. // handling fee
  4744. handlingFee = sqlite3_column_double(statement, 33);
  4745. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4746. if (sqlite3_column_int(statement, 58)) {
  4747. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4748. }
  4749. //
  4750. // customer info
  4751. customerID = [self textAtColumn:36 statement:statement];
  4752. // mode
  4753. ret[@"mode"] = appDelegate.mode;
  4754. // model_count
  4755. ret[@"model_count"] = @(0);
  4756. }
  4757. sqlite3_finalize(statement);
  4758. }
  4759. [iSalesDB close_db:db];
  4760. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4761. // "customer_email" = "Shui Hu";
  4762. // "customer_fax" = "";
  4763. // "customer_first_name" = F;
  4764. // "customer_last_name" = L;
  4765. // "customer_name" = ",da He Xiang Dong Liu A";
  4766. // "customer_phone" = "Hey Xuan Feng";
  4767. contactInfo[@"customer_phone"] = customer_phone;
  4768. contactInfo[@"customer_fax"] = customer_fax;
  4769. contactInfo[@"customer_email"] = customer_email;
  4770. NSString *first_name = @"";
  4771. NSString *last_name = @"";
  4772. if ([customer_contact isEqualToString:@""]) {
  4773. } else if ([customer_contact containsString:@" "]) {
  4774. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4775. first_name = [customer_contact substringToIndex:first_space_index];
  4776. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4777. }
  4778. contactInfo[@"customer_first_name"] = first_name;
  4779. contactInfo[@"customer_last_name"] = last_name;
  4780. ret[@"customerInfo"] = contactInfo;
  4781. // models
  4782. if (nssoid) {
  4783. __block double TotalCuft = 0;
  4784. __block double TotalWeight = 0;
  4785. __block int TotalCarton = 0;
  4786. __block double allItemPrice = 0;
  4787. 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];
  4788. sqlite3 *db1 = [iSalesDB get_db];
  4789. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4790. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4791. int product_id = sqlite3_column_int(stmt, 0);
  4792. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4793. int item_id = sqlite3_column_int(stmt, 7);
  4794. NSString* Price=nil;
  4795. if(str_price==nil)
  4796. {
  4797. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4798. if(price==nil)
  4799. Price=@"No Price.";
  4800. else
  4801. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4802. }
  4803. else
  4804. {
  4805. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4806. }
  4807. double discount = sqlite3_column_double(stmt, 2);
  4808. int item_count = sqlite3_column_int(stmt, 3);
  4809. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4810. NSString *nsline_note=nil;
  4811. if(line_note!=nil)
  4812. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4813. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4814. // NSString *nsname = nil;
  4815. // if(name!=nil)
  4816. // nsname= [[NSString alloc]initWithUTF8String:name];
  4817. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4818. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4819. // NSString *nsdescription=nil;
  4820. // if(description!=nil)
  4821. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4822. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4823. // int stockUom = sqlite3_column_int(stmt, 8);
  4824. // int _id = sqlite3_column_int(stmt, 9);
  4825. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4826. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4827. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4828. int carton=[bsubtotaljson[@"carton"] intValue];
  4829. TotalCuft += cuft;
  4830. TotalWeight += weight;
  4831. TotalCarton += carton;
  4832. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4833. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4834. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4835. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4836. itemjson[@"note"]=nsline_note;
  4837. itemjson[@"origin_price"] = Price;
  4838. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4839. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4840. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4841. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4842. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4843. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4844. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4845. if(itemjson[@"combine"] != nil)
  4846. {
  4847. // int citem=0;
  4848. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4849. for(int bc=0;bc<bcount;bc++)
  4850. {
  4851. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4852. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4853. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4854. subTotal += uprice * modulus * item_count;
  4855. }
  4856. }
  4857. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4858. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4859. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4860. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4861. allItemPrice += subTotal;
  4862. }];
  4863. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4864. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4865. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4866. // payments/Credits
  4867. // payments_and_credist = sqlite3_column_double(statement, 34);
  4868. payments_and_credist = allItemPrice;
  4869. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4870. // // total
  4871. // totalPrice = sqlite3_column_double(statement, 35);
  4872. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4873. } else {
  4874. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4875. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4876. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4877. // payments/Credits
  4878. payments_and_credist = 0;
  4879. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4880. }
  4881. // total
  4882. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4883. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4884. ret[@"order_info"]= orderinfo;
  4885. ret[@"more_order_info"] = moreInfo;
  4886. return [RAUtils dict2data:ret];
  4887. }
  4888. #pragma mark order list
  4889. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4890. NSString *nsstatus = @"";
  4891. switch (status) {
  4892. case 0:
  4893. {
  4894. nsstatus=@"Temp Order";
  4895. break;
  4896. }
  4897. case 1:
  4898. {
  4899. nsstatus=@"Saved Order";
  4900. break;
  4901. }
  4902. case 2: {
  4903. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4904. break;
  4905. }
  4906. case 3:
  4907. case 15:
  4908. {
  4909. nsstatus=@"Cancelled";
  4910. break;
  4911. }
  4912. case 10:
  4913. {
  4914. nsstatus=@"Quote Submitted";
  4915. break;
  4916. }
  4917. case 11:
  4918. {
  4919. nsstatus=@"Sales Order Submitted";
  4920. break;
  4921. }
  4922. case 12:
  4923. {
  4924. nsstatus=@"Processing";
  4925. break;
  4926. }
  4927. case 13:
  4928. {
  4929. nsstatus=@"Shipped";
  4930. break;
  4931. }
  4932. case 14:
  4933. {
  4934. nsstatus=@"Closed";
  4935. break;
  4936. }
  4937. case -11:
  4938. {
  4939. nsstatus = @"Ready For Submit";
  4940. break;
  4941. }
  4942. default:
  4943. break;
  4944. }
  4945. return nsstatus;
  4946. }
  4947. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  4948. double total = 0;
  4949. __block double payments_and_credist = 0;
  4950. __block double allItemPrice = 0;
  4951. // sqlite3 *db1 = [iSalesDB get_db];
  4952. if (so_id) {
  4953. // 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];
  4954. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  4955. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4956. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4957. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4958. int product_id = sqlite3_column_int(stmt, 0);
  4959. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4960. int discount = sqlite3_column_int(stmt, 2);
  4961. int item_count = sqlite3_column_int(stmt, 3);
  4962. // int item_id = sqlite3_column_int(stmt, 7);
  4963. int item_id = sqlite3_column_int(stmt, 4);
  4964. NSString* Price=nil;
  4965. if(str_price==nil)
  4966. {
  4967. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4968. if(price==nil)
  4969. Price=@"No Price.";
  4970. else
  4971. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4972. }
  4973. else
  4974. {
  4975. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4976. }
  4977. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4978. itemjson[@"The unit price"]=Price;
  4979. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4980. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4981. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4982. if(itemjson[@"combine"] != nil)
  4983. {
  4984. // int citem=0;
  4985. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4986. for(int bc=0;bc<bcount;bc++)
  4987. {
  4988. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4989. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4990. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4991. subTotal += uprice * modulus * item_count;
  4992. }
  4993. }
  4994. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4995. allItemPrice += subTotal;
  4996. }];
  4997. payments_and_credist = allItemPrice;
  4998. } else {
  4999. // payments/Credits
  5000. payments_and_credist = 0;
  5001. }
  5002. // lift_gate handlingFee shippingFee
  5003. __block double lift_gate = 0;
  5004. __block double handlingFee = 0;
  5005. __block double shippingFee = 0;
  5006. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5007. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5008. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5009. if (have_lift_gate) {
  5010. lift_gate = sqlite3_column_double(stmt, 1);
  5011. }
  5012. handlingFee = sqlite3_column_double(stmt, 2);
  5013. shippingFee = sqlite3_column_double(stmt, 3);
  5014. }];
  5015. // total
  5016. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5017. if (close) {
  5018. [iSalesDB close_db:db1];
  5019. }
  5020. return total;
  5021. }
  5022. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5023. {
  5024. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5025. int limit = [[params valueForKey:@"limit"] intValue];
  5026. int offset = [[params valueForKey:@"offset"] intValue];
  5027. NSString* keyword = [params valueForKey:@"keyWord"];
  5028. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5029. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5030. NSString* where=@"1 = 1";
  5031. if(keyword.length>0)
  5032. 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]];
  5033. if (orderStatus.length > 0) {
  5034. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5035. if (order_status_array.count == 1) {
  5036. int status_value = [[order_status_array firstObject] integerValue];
  5037. if (status_value <= 1 || status_value == 3) {
  5038. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5039. } else {
  5040. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5041. }
  5042. } else if (order_status_array.count > 1) {
  5043. for (int i = 0; i < order_status_array.count;i++) {
  5044. NSString *status = order_status_array[i];
  5045. NSString *condition = @" or";
  5046. if (i == 0) {
  5047. condition = @" and (";
  5048. }
  5049. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5050. int status_value = [status integerValue];
  5051. if (status_value <= 1 || status_value == 3) {
  5052. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5053. } else {
  5054. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5055. }
  5056. }
  5057. where = [where stringByAppendingString:@" )"];
  5058. }
  5059. }
  5060. 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.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5061. // DebugLog(@"order list sql: %@",sqlQuery);
  5062. sqlite3 *db = [iSalesDB get_db];
  5063. sqlite3_stmt * statement;
  5064. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5065. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5066. {
  5067. int count=0;
  5068. while (sqlite3_step(statement) == SQLITE_ROW)
  5069. {
  5070. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5071. int order_id = sqlite3_column_double(statement, 0);
  5072. char *soid = (char*)sqlite3_column_text(statement, 1);
  5073. if(soid==nil)
  5074. soid= "";
  5075. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5076. int status = sqlite3_column_double(statement, 2);
  5077. int erpStatus = sqlite3_column_double(statement, 9);
  5078. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5079. if(sales_rep==nil)
  5080. sales_rep= "";
  5081. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5082. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5083. if(create_by==nil)
  5084. create_by= "";
  5085. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5086. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5087. if(company_name==nil)
  5088. company_name= "";
  5089. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5090. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5091. if(create_time==nil)
  5092. create_time= "";
  5093. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5094. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5095. // double total_price = sqlite3_column_double(statement, 7);
  5096. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5097. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5098. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5099. int offline_edit = sqlite3_column_int(statement, 10);
  5100. // ": "JH",
  5101. // "": "$8307.00",
  5102. // "": "MOB1608050001",
  5103. // "": "ArpithaT",
  5104. // "": "1st Stage Property Transformations",
  5105. // "": "JANICE SUTTON",
  5106. // "": 2255,
  5107. // "": "08/02/2016 09:49:18",
  5108. // "": 1,
  5109. // "": "Saved Order"
  5110. // "": "1470384050483",
  5111. // "model_count": "6 / 28"
  5112. item[@"sales_rep"]= nssales_rep;
  5113. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5114. item[@"so#"]= nssoid;
  5115. item[@"create_by"]= nscreate_by;
  5116. item[@"customer_name"]= nscompany_name;
  5117. item[@"customer_contact"] = customer_contact;
  5118. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5119. item[@"purchase_time"]= nscreate_time;
  5120. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5121. item[@"order_status"]= nsstatus;
  5122. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5123. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5124. // item[@"model_count"]
  5125. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5126. count++;
  5127. }
  5128. ret[@"count"]= [NSNumber numberWithInt:count];
  5129. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5130. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5131. ret[@"result"]= [NSNumber numberWithInt:2];
  5132. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5133. ret[@"time_zone"] = @"PST";
  5134. sqlite3_finalize(statement);
  5135. }
  5136. 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];
  5137. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5138. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5139. [iSalesDB close_db:db];
  5140. return [RAUtils dict2data:ret];
  5141. }
  5142. #pragma mark update gnotes
  5143. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5144. {
  5145. DebugLog(@"params: %@",params);
  5146. // comments = Meyoyoyoyoyoyoy;
  5147. // orderCode = MOB1608110001;
  5148. // password = 123456;
  5149. // user = EvanK;
  5150. 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]];
  5151. int ret = [iSalesDB execSql:sql];
  5152. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5153. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5154. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5155. // [dic setValue:@"160409" forKey:@"min_ver"];
  5156. return [RAUtils dict2data:dic];
  5157. }
  5158. #pragma mark move to wishlist
  5159. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5160. {
  5161. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5162. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5163. _id = [NSString stringWithFormat:@"(%@)",_id];
  5164. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5165. sqlite3 *db = [iSalesDB get_db];
  5166. __block NSString *product_id = @"";
  5167. __block NSString *item_count_str = @"";
  5168. // __block NSString *item_id = nil;
  5169. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5170. // product_id = [self textAtColumn:0 statement:stmt];
  5171. int item_count = sqlite3_column_int(stmt, 1);
  5172. // item_id = [self textAtColumn:2 statement:stmt];
  5173. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5174. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5175. if (p_id.length) {
  5176. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5177. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5178. }
  5179. }];
  5180. [iSalesDB close_db:db];
  5181. // 去除第一个,
  5182. product_id = [product_id substringFromIndex:1];
  5183. item_count_str = [item_count_str substringFromIndex:1];
  5184. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5185. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5186. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5187. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5188. sqlite3 *db1 = [iSalesDB get_db];
  5189. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5190. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5191. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5192. // 删除
  5193. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5194. int ret = [iSalesDB execSql:deleteSql db:db1];
  5195. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5196. [iSalesDB close_db:db1];
  5197. return [RAUtils dict2data:dic];
  5198. }
  5199. #pragma mark cart delete
  5200. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5201. {
  5202. // cartItemId = 548;
  5203. // orderCode = MOB1608110001;
  5204. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5205. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5206. _id = [NSString stringWithFormat:@"(%@)",_id];
  5207. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5208. int ret = [iSalesDB execSql:sql];
  5209. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5210. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5211. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5212. // [dic setValue:@"160409" forKey:@"min_ver"];
  5213. return [RAUtils dict2data:dic];
  5214. }
  5215. #pragma mark set price
  5216. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5217. {
  5218. DebugLog(@"cart set price params: %@",params);
  5219. // "cartitem_id" = 1;
  5220. // discount = "0.000000";
  5221. // "item_note" = "";
  5222. // price = "269.000000";
  5223. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5224. NSString *notes = [self valueInParams:params key:@"item_note"];
  5225. NSString *discount = [self valueInParams:params key:@"discount"];
  5226. NSString *price = [self valueInParams:params key:@"price"];
  5227. // bool badd_price_changed=false;
  5228. // sqlite3* db=[iSalesDB get_db];
  5229. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5230. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5231. // NSRange range;
  5232. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5233. //
  5234. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5235. // badd_price_changed=true;
  5236. // [iSalesDB close_db:db];
  5237. //
  5238. // if(badd_price_changed)
  5239. // {
  5240. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5241. // }
  5242. //
  5243. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5244. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5245. int ret = [iSalesDB execSql:sql];
  5246. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5247. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5248. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5249. // [dic setValue:@"160409" forKey:@"min_ver"];
  5250. return [RAUtils dict2data:dic];
  5251. }
  5252. #pragma mark set line notes
  5253. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5254. {
  5255. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5256. NSString *notes = [self valueInParams:params key:@"notes"];
  5257. notes = [self translateSingleQuote:notes];
  5258. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5259. int ret = [iSalesDB execSql:sql];
  5260. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5261. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5262. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5263. // [dic setValue:@"160409" forKey:@"min_ver"];
  5264. return [RAUtils dict2data:dic];
  5265. }
  5266. #pragma mark set qty
  5267. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5268. {
  5269. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5270. int item_count = [params[@"inputInt"] integerValue];
  5271. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5272. int ret = [iSalesDB execSql:sql];
  5273. __block int item_id = 0;
  5274. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5275. item_id = sqlite3_column_int(stmt, 0);
  5276. }];
  5277. sqlite3 *db = [iSalesDB get_db];
  5278. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5279. [iSalesDB close_db:db];
  5280. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5281. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5282. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5283. // [dic setValue:@"160409" forKey:@"min_ver"];
  5284. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5285. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5286. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5287. return [RAUtils dict2data:dic];
  5288. }
  5289. #pragma mark place order
  5290. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5291. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5292. if (aname) {
  5293. [dic setValue:aname forKey:@"aname"];
  5294. }
  5295. if (control) {
  5296. [dic setValue:control forKey:@"control"];
  5297. }
  5298. if (keyboard) {
  5299. [dic setValue:keyboard forKey:@"keyboard"];
  5300. }
  5301. if (name) {
  5302. [dic setValue:name forKey:@"name"];
  5303. }
  5304. if (value) {
  5305. [dic setValue:value forKey:@"value"];
  5306. }
  5307. return dic;
  5308. }
  5309. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5310. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5311. orderCode = [self translateSingleQuote:orderCode];
  5312. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5313. 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];
  5314. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5315. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5316. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5317. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5318. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5319. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5320. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5321. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5322. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5323. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5324. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5325. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5326. NSString *email = [self textAtColumn:11 statement:stmt];
  5327. NSString *phone = [self textAtColumn:12 statement:stmt];
  5328. NSString *fax = [self textAtColumn:13 statement:stmt];
  5329. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5330. NSString *city = [self textAtColumn:15 statement:stmt];
  5331. NSString *state = [self textAtColumn:16 statement:stmt];
  5332. NSString *country = [self textAtColumn:17 statement:stmt];
  5333. NSString *name = [self textAtColumn:18 statement:stmt];
  5334. // contact id
  5335. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5336. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5337. [contact_id_dic setValue:@"text" forKey:@"control"];
  5338. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5339. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5340. [contact_id_dic setValue:@"true" forKey:@"required"];
  5341. [contact_id_dic setValue:contact_id forKey:@"value"];
  5342. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5343. // business card
  5344. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5345. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5346. [business_card_dic setValue:@"img" forKey:@"control"];
  5347. [business_card_dic setValue:@"1" forKey:@"disable"];
  5348. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5349. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5350. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5351. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5352. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5353. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5354. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5355. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5356. // fax
  5357. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5358. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5359. // zipcode
  5360. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5361. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5362. // city
  5363. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5364. [customer_dic setValue:city_dic forKey:@"item_12"];
  5365. // state
  5366. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5367. [customer_dic setValue:state_dic forKey:@"item_13"];
  5368. // country
  5369. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5370. [customer_dic setValue:country_dic forKey:@"item_14"];
  5371. // company name
  5372. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5373. [customer_dic setValue:company_dic forKey:@"item_2"];
  5374. // addr_1
  5375. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5376. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5377. // addr_2
  5378. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5379. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5380. // addr_3
  5381. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5382. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5383. // addr_4
  5384. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5385. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5386. // Contact
  5387. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5388. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5389. // email
  5390. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5391. [customer_dic setValue:email_dic forKey:@"item_8"];
  5392. // phone
  5393. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5394. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5395. // title
  5396. [customer_dic setValue:@"Customer" forKey:@"title"];
  5397. // count
  5398. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5399. }];
  5400. // setting
  5401. NSDictionary *setting = params[@"setting"];
  5402. NSNumber *hide = setting[@"CustomerHide"];
  5403. [customer_dic setValue:hide forKey:@"hide"];
  5404. return customer_dic;
  5405. }
  5406. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5407. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5408. // setting
  5409. NSDictionary *setting = params[@"setting"];
  5410. NSNumber *hide = setting[@"ShipToHide"];
  5411. [dic setValue:hide forKey:@"hide"];
  5412. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5413. orderCode = [self translateSingleQuote:orderCode];
  5414. 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];
  5415. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5416. NSString *cid = [self textAtColumn:0 statement:stmt];
  5417. NSString *name = [self textAtColumn:1 statement:stmt];
  5418. NSString *ext = [self textAtColumn:2 statement:stmt];
  5419. NSString *contact = [self textAtColumn:3 statement:stmt];
  5420. NSString *email = [self textAtColumn:4 statement:stmt];
  5421. NSString *fax = [self textAtColumn:5 statement:stmt];
  5422. NSString *phone = [self textAtColumn:6 statement:stmt];
  5423. // count
  5424. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5425. // title
  5426. [dic setValue:@"Ship To" forKey:@"title"];
  5427. // choose
  5428. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5429. [choose_dic setValue:@"choose" forKey:@"aname"];
  5430. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5431. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5432. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5433. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5434. @"receive_contact" : @"customer_contact",
  5435. @"receive_email" : @"customer_email",
  5436. @"receive_ext" : @"customer_contact_ext",
  5437. @"receive_fax" : @"customer_fax",
  5438. @"receive_name" : @"customer_name",
  5439. @"receive_phone" : @"customer_phone"},
  5440. @"refresh" : [NSNumber numberWithInteger:1],
  5441. @"type" : @"pull"};
  5442. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5443. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5444. @"name" : @"Add new address",
  5445. @"refresh" : [NSNumber numberWithInteger:1],
  5446. @"value" : @"new_addr"
  5447. };
  5448. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5449. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5450. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5451. @"receive_contact" : @"customer_contact",
  5452. @"receive_email" : @"customer_email",
  5453. @"receive_ext" : @"customer_contact_ext",
  5454. @"receive_fax" : @"customer_fax",
  5455. @"receive_name" : @"customer_name",
  5456. @"receive_phone" : @"customer_phone"},
  5457. @"name" : @"select_ship_to",
  5458. @"refresh" : [NSNumber numberWithInteger:1],
  5459. @"value" : @"Sales_Order_Ship_To"};
  5460. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5461. [dic setValue:choose_dic forKey:@"item_0"];
  5462. // contact id
  5463. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5464. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5465. [contact_id_dic setValue:@"true" forKey:@"required"];
  5466. [dic setValue:contact_id_dic forKey:@"item_1"];
  5467. // company name
  5468. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5469. [dic setValue:company_name_dic forKey:@"item_2"];
  5470. // address
  5471. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5472. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5473. if ([required integerValue]) {
  5474. [ext_dic setValue:@"true" forKey:@"required"];
  5475. }
  5476. [dic setValue:ext_dic forKey:@"item_3"];
  5477. // contact
  5478. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5479. [dic setValue:contact_dic forKey:@"item_4"];
  5480. // phone
  5481. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5482. [dic setValue:phone_dic forKey:@"item_5"];
  5483. // fax
  5484. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5485. [dic setValue:fax_dic forKey:@"item_6"];
  5486. // email
  5487. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5488. [dic setValue:email_dic forKey:@"item_7"];
  5489. }];
  5490. return dic;
  5491. }
  5492. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5493. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5494. // setting
  5495. NSDictionary *setting = params[@"setting"];
  5496. NSNumber *hide = setting[@"ShipFromHide"];
  5497. [dic setValue:hide forKey:@"hide"];
  5498. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5499. orderCode = [self translateSingleQuote:orderCode];
  5500. 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];
  5501. 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';";
  5502. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5503. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5504. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5505. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5506. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5507. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5508. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5509. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5510. if (!cid.length) {
  5511. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5512. cid = [self textAtColumn:0 statement:statment];
  5513. name = [self textAtColumn:1 statement:statment];
  5514. ext = [self textAtColumn:2 statement:statment];
  5515. contact = [self textAtColumn:3 statement:statment];
  5516. email = [self textAtColumn:4 statement:statment];
  5517. fax = [self textAtColumn:5 statement:statment];
  5518. phone = [self textAtColumn:6 statement:statment];
  5519. }];
  5520. }
  5521. // count
  5522. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5523. // title
  5524. [dic setValue:@"Ship From" forKey:@"title"];
  5525. // hide
  5526. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5527. // choose
  5528. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5529. [choose_dic setValue:@"choose" forKey:@"aname"];
  5530. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5531. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5532. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5533. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5534. @"sender_contact" : @"customer_contact",
  5535. @"sender_email" : @"customer_email",
  5536. @"sender_ext" : @"customer_contact_ext",
  5537. @"sender_fax" : @"customer_fax",
  5538. @"sender_name" : @"customer_name",
  5539. @"sender_phone" : @"customer_phone"},
  5540. @"name" : @"select_cid",
  5541. @"refresh" : [NSNumber numberWithInteger:0],
  5542. @"value" : @"Sales_Order_Ship_From"};
  5543. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5544. [dic setValue:choose_dic forKey:@"item_0"];
  5545. // contact id
  5546. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5547. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5548. [contact_id_dic setValue:@"true" forKey:@"required"];
  5549. [dic setValue:contact_id_dic forKey:@"item_1"];
  5550. // company name
  5551. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5552. [dic setValue:company_name_dic forKey:@"item_2"];
  5553. // address
  5554. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5555. [dic setValue:ext_dic forKey:@"item_3"];
  5556. // contact
  5557. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5558. [dic setValue:contact_dic forKey:@"item_4"];
  5559. // phone
  5560. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5561. [dic setValue:phone_dic forKey:@"item_5"];
  5562. // fax
  5563. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5564. [dic setValue:fax_dic forKey:@"item_6"];
  5565. // email
  5566. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5567. [dic setValue:email_dic forKey:@"item_7"];
  5568. }];
  5569. return dic;
  5570. }
  5571. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5572. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5573. // setting
  5574. NSDictionary *setting = params[@"setting"];
  5575. NSNumber *hide = setting[@"FreightBillToHide"];
  5576. [dic setValue:hide forKey:@"hide"];
  5577. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5578. orderCode = [self translateSingleQuote:orderCode];
  5579. 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];
  5580. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5581. NSString *cid = [self textAtColumn:0 statement:stmt];
  5582. NSString *name = [self textAtColumn:1 statement:stmt];
  5583. NSString *ext = [self textAtColumn:2 statement:stmt];
  5584. NSString *contact = [self textAtColumn:3 statement:stmt];
  5585. NSString *email = [self textAtColumn:4 statement:stmt];
  5586. NSString *fax = [self textAtColumn:5 statement:stmt];
  5587. NSString *phone = [self textAtColumn:6 statement:stmt];
  5588. // count
  5589. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5590. // title
  5591. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5592. // hide
  5593. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5594. // choose
  5595. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5596. [choose_dic setValue:@"choose" forKey:@"aname"];
  5597. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5598. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5599. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5600. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5601. @"shipping_billto_contact" : @"receive_contact",
  5602. @"shipping_billto_email" : @"receive_email",
  5603. @"shipping_billto_ext" : @"receive_ext",
  5604. @"shipping_billto_fax" : @"receive_fax",
  5605. @"shipping_billto_name" : @"receive_name",
  5606. @"shipping_billto_phone" : @"receive_phone"},
  5607. @"type" : @"pull"};
  5608. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5609. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5610. @"type" : @"pull",
  5611. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5612. @"shipping_billto_contact" : @"customer_contact",
  5613. @"shipping_billto_email" : @"customer_email",
  5614. @"shipping_billto_ext" : @"customer_contact_ext",
  5615. @"shipping_billto_fax" : @"customer_fax",
  5616. @"shipping_billto_name" : @"customer_name",
  5617. @"shipping_billto_phone" : @"customer_phone"}
  5618. };
  5619. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5620. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5621. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5622. @"shipping_billto_contact" : @"sender_contact",
  5623. @"shipping_billto_email" : @"sender_email",
  5624. @"shipping_billto_ext" : @"sender_ext",
  5625. @"shipping_billto_fax" : @"sender_fax",
  5626. @"shipping_billto_name" : @"sender_name",
  5627. @"shipping_billto_phone" : @"sender_phone"},
  5628. @"type" : @"pull"
  5629. };
  5630. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5631. NSDictionary *select_freight_bill_to_dic = @{
  5632. @"aname" : @"Select freight bill to",
  5633. @"name" : @"select_cid",
  5634. @"refresh" : [NSNumber numberWithInteger:0],
  5635. @"value" : @"Sales_Order_Freight_Bill_To",
  5636. @"key_map" : @{
  5637. @"shipping_billto_cid" : @"customer_cid",
  5638. @"shipping_billto_contact" : @"customer_contact",
  5639. @"shipping_billto_email" : @"customer_email",
  5640. @"shipping_billto_ext" : @"customer_contact_ext",
  5641. @"shipping_billto_fax" : @"customer_fax",
  5642. @"shipping_billto_name" : @"customer_name",
  5643. @"shipping_billto_phone" : @"customer_phone"
  5644. }
  5645. };
  5646. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5647. [dic setValue:choose_dic forKey:@"item_0"];
  5648. // contact id
  5649. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5650. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5651. [contact_id_dic setValue:@"true" forKey:@"required"];
  5652. [dic setValue:contact_id_dic forKey:@"item_1"];
  5653. // company name
  5654. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5655. [dic setValue:company_name_dic forKey:@"item_2"];
  5656. // address
  5657. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5658. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5659. if ([ext_required integerValue]) {
  5660. [ext_dic setValue:@"true" forKey:@"required"];
  5661. }
  5662. [dic setValue:ext_dic forKey:@"item_3"];
  5663. // contact
  5664. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5665. [dic setValue:contact_dic forKey:@"item_4"];
  5666. // phone
  5667. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5668. [dic setValue:phone_dic forKey:@"item_5"];
  5669. // fax
  5670. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5671. [dic setValue:fax_dic forKey:@"item_6"];
  5672. // email
  5673. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5674. [dic setValue:email_dic forKey:@"item_7"];
  5675. }];
  5676. return dic;
  5677. }
  5678. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5679. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5680. // setting
  5681. NSDictionary *setting = params[@"setting"];
  5682. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5683. [dic setValue:hide forKey:@"hide"];
  5684. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5685. orderCode = [self translateSingleQuote:orderCode];
  5686. 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];
  5687. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5688. NSString *cid = [self textAtColumn:0 statement:stmt];
  5689. NSString *name = [self textAtColumn:1 statement:stmt];
  5690. NSString *ext = [self textAtColumn:2 statement:stmt];
  5691. NSString *contact = [self textAtColumn:3 statement:stmt];
  5692. NSString *email = [self textAtColumn:4 statement:stmt];
  5693. NSString *fax = [self textAtColumn:5 statement:stmt];
  5694. NSString *phone = [self textAtColumn:6 statement:stmt];
  5695. // count
  5696. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5697. // title
  5698. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5699. // hide
  5700. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5701. // choose
  5702. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5703. [choose_dic setValue:@"choose" forKey:@"aname"];
  5704. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5705. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5706. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5707. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5708. @"billing_contact" : @"receive_contact",
  5709. @"billing_email" : @"receive_email",
  5710. @"billing_ext" : @"receive_ext",
  5711. @"billing_fax" : @"receive_fax",
  5712. @"billing_name" : @"receive_name",
  5713. @"billing_phone" : @"receive_phone"},
  5714. @"type" : @"pull"};
  5715. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5716. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5717. @"type" : @"pull",
  5718. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5719. @"billing_contact" : @"customer_contact",
  5720. @"billing_email" : @"customer_email",
  5721. @"billing_ext" : @"customer_contact_ext",
  5722. @"billing_fax" : @"customer_fax",
  5723. @"billing_name" : @"customer_name",
  5724. @"billing_phone" : @"customer_phone"}
  5725. };
  5726. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5727. NSDictionary *select_bill_to_dic = @{
  5728. @"aname" : @"Select bill to",
  5729. @"name" : @"select_cid",
  5730. @"refresh" : [NSNumber numberWithInteger:0],
  5731. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5732. @"key_map" : @{
  5733. @"billing_cid" : @"customer_cid",
  5734. @"billing_contact" : @"customer_contact",
  5735. @"billing_email" : @"customer_email",
  5736. @"billing_ext" : @"customer_contact_ext",
  5737. @"billing_fax" : @"customer_fax",
  5738. @"billing_name" : @"customer_name",
  5739. @"billing_phone" : @"customer_phone"
  5740. }
  5741. };
  5742. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5743. [dic setValue:choose_dic forKey:@"item_0"];
  5744. // contact id
  5745. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5746. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5747. [contact_id_dic setValue:@"true" forKey:@"required"];
  5748. [dic setValue:contact_id_dic forKey:@"item_1"];
  5749. // company name
  5750. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5751. [dic setValue:company_name_dic forKey:@"item_2"];
  5752. // address
  5753. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5754. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5755. if ([ext_required integerValue]) {
  5756. [ext_dic setValue:@"true" forKey:@"required"];
  5757. }
  5758. [dic setValue:ext_dic forKey:@"item_3"];
  5759. // contact
  5760. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5761. [dic setValue:contact_dic forKey:@"item_4"];
  5762. // phone
  5763. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5764. [dic setValue:phone_dic forKey:@"item_5"];
  5765. // fax
  5766. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5767. [dic setValue:fax_dic forKey:@"item_6"];
  5768. // email
  5769. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5770. [dic setValue:email_dic forKey:@"item_7"];
  5771. }];
  5772. return dic;
  5773. }
  5774. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5775. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5776. // setting
  5777. NSDictionary *setting = params[@"setting"];
  5778. NSNumber *hide = setting[@"ReturnToHide"];
  5779. [dic setValue:hide forKey:@"hide"];
  5780. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5781. orderCode = [self translateSingleQuote:orderCode];
  5782. 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];
  5783. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5784. NSString *cid = [self textAtColumn:0 statement:stmt];
  5785. NSString *name = [self textAtColumn:1 statement:stmt];
  5786. NSString *ext = [self textAtColumn:2 statement:stmt];
  5787. NSString *contact = [self textAtColumn:3 statement:stmt];
  5788. NSString *email = [self textAtColumn:4 statement:stmt];
  5789. NSString *fax = [self textAtColumn:5 statement:stmt];
  5790. NSString *phone = [self textAtColumn:6 statement:stmt];
  5791. // count
  5792. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5793. // title
  5794. [dic setValue:@"Return To" forKey:@"title"];
  5795. // hide
  5796. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5797. // choose
  5798. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5799. [choose_dic setValue:@"choose" forKey:@"aname"];
  5800. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5801. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5802. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5803. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5804. @"returnto_contact" : @"sender_contact",
  5805. @"returnto_email" : @"sender_email",
  5806. @"returnto_ext" : @"sender_ext",
  5807. @"returnto_fax" : @"sender_fax",
  5808. @"returnto_name" : @"sender_name",
  5809. @"returnto_phone" : @"sender_phone"},
  5810. @"type" : @"pull"};
  5811. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5812. NSDictionary *select_return_to_dic = @{
  5813. @"aname" : @"Select return to",
  5814. @"name" : @"select_cid",
  5815. @"refresh" : [NSNumber numberWithInteger:0],
  5816. @"value" : @"Contact_Return_To",
  5817. @"key_map" : @{
  5818. @"returnto_cid" : @"customer_cid",
  5819. @"returnto_contact" : @"customer_contact",
  5820. @"returnto_email" : @"customer_email",
  5821. @"returnto_ext" : @"customer_contact_ext",
  5822. @"returnto_fax" : @"customer_fax",
  5823. @"returnto_name" : @"customer_name",
  5824. @"returnto_phone" : @"customer_phone"
  5825. }
  5826. };
  5827. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5828. [dic setValue:choose_dic forKey:@"item_0"];
  5829. // contact id
  5830. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5831. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5832. [contact_id_dic setValue:@"true" forKey:@"required"];
  5833. [dic setValue:contact_id_dic forKey:@"item_1"];
  5834. // company name
  5835. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5836. [dic setValue:company_name_dic forKey:@"item_2"];
  5837. // address
  5838. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5839. [dic setValue:ext_dic forKey:@"item_3"];
  5840. // contact
  5841. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5842. [dic setValue:contact_dic forKey:@"item_4"];
  5843. // phone
  5844. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5845. [dic setValue:phone_dic forKey:@"item_5"];
  5846. // fax
  5847. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5848. [dic setValue:fax_dic forKey:@"item_6"];
  5849. // email
  5850. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5851. [dic setValue:email_dic forKey:@"item_7"];
  5852. }];
  5853. return dic;
  5854. }
  5855. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5856. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5857. orderCode = [self translateSingleQuote:orderCode];
  5858. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5859. __block double TotalCuft = 0;
  5860. __block double TotalWeight = 0;
  5861. __block int TotalCarton = 0;
  5862. __block double payments = 0;
  5863. // setting
  5864. NSDictionary *setting = params[@"setting"];
  5865. NSNumber *hide = setting[@"ModelInformationHide"];
  5866. [dic setValue:hide forKey:@"hide"];
  5867. 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];
  5868. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5869. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5870. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5871. // item id
  5872. int item_id = sqlite3_column_int(stmt, 0);
  5873. // count
  5874. int item_count = sqlite3_column_int(stmt, 1);
  5875. // stockUom
  5876. int stockUom = sqlite3_column_int(stmt, 2);
  5877. // unit price
  5878. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5879. NSString* Price=nil;
  5880. if([str_price isEqualToString:@""])
  5881. {
  5882. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5883. if(price==nil)
  5884. Price=@"No Price.";
  5885. else
  5886. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5887. }
  5888. else
  5889. {
  5890. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5891. }
  5892. // discount
  5893. double discount = sqlite3_column_double(stmt, 4);
  5894. // name
  5895. NSString *name = [self textAtColumn:5 statement:stmt];
  5896. // description
  5897. NSString *description = [self textAtColumn:6 statement:stmt];
  5898. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5899. // line note
  5900. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5901. int avaulability = sqlite3_column_int(stmt, 8);
  5902. // img
  5903. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5904. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5905. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5906. if(combine != nil)
  5907. {
  5908. // int citem=0;
  5909. int bcount=[[combine valueForKey:@"count"] intValue];
  5910. for(int bc=0;bc<bcount;bc++)
  5911. {
  5912. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5913. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5914. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5915. subTotal += uprice * modulus * item_count;
  5916. }
  5917. }
  5918. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5919. [model_dic setValue:@"model" forKey:@"control"];
  5920. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5921. [model_dic setValue:description forKey:@"description"];
  5922. [model_dic setValue:line_note forKey:@"note"];
  5923. [model_dic setValue:img forKey:@"img_url"];
  5924. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5925. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5926. [model_dic setValue:Price forKey:@"unit_price"];
  5927. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5928. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  5929. if (combine) {
  5930. [model_dic setValue:combine forKey:@"combine"];
  5931. }
  5932. // well,what under the row is the info for total
  5933. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5934. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5935. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5936. int carton=[bsubtotaljson[@"carton"] intValue];
  5937. TotalCuft += cuft;
  5938. TotalWeight += weight;
  5939. TotalCarton += carton;
  5940. payments += subTotal;
  5941. //---------------------------
  5942. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  5943. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  5944. }];
  5945. [dic setValue:@"Model Information" forKey:@"title"];
  5946. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  5947. return dic;
  5948. }
  5949. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  5950. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5951. [dic setValue:@"Remarks Content" forKey:@"title"];
  5952. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5953. // setting
  5954. NSDictionary *setting = params[@"setting"];
  5955. NSNumber *hide = setting[@"RemarksContentHide"];
  5956. [dic setValue:hide forKey:@"hide"];
  5957. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5958. orderCode = [self translateSingleQuote:orderCode];
  5959. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  5960. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5961. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  5962. int mustCall = sqlite3_column_int(stmt, 1);
  5963. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  5964. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  5965. NSDictionary *po_dic = @{
  5966. @"aname" : @"PO#",
  5967. @"control" : @"edit",
  5968. @"keyboard" : @"text",
  5969. @"name" : @"poNumber",
  5970. @"value" : poNumber
  5971. };
  5972. NSDictionary *must_call_dic = @{
  5973. @"aname" : @"MUST CALL BEFORE DELIVERY",
  5974. @"control" : @"switch",
  5975. @"name" : @"must_call",
  5976. @"value" : mustCall ? @"true" : @"false"
  5977. };
  5978. NSDictionary *general_notes_dic = @{
  5979. @"aname" : @"General notes",
  5980. @"control" : @"text_view",
  5981. @"keyboard" : @"text",
  5982. @"name" : @"comments",
  5983. @"value" : generalNotes
  5984. };
  5985. NSDictionary *internal_notes_dic = @{
  5986. @"aname" : @"Internal notes",
  5987. @"control" : @"text_view",
  5988. @"keyboard" : @"text",
  5989. @"name" : @"internal_notes",
  5990. @"value" : internalNotes
  5991. };
  5992. [dic setValue:po_dic forKey:@"item_0"];
  5993. [dic setValue:must_call_dic forKey:@"item_1"];
  5994. [dic setValue:general_notes_dic forKey:@"item_2"];
  5995. [dic setValue:internal_notes_dic forKey:@"item_3"];
  5996. }];
  5997. return dic;
  5998. }
  5999. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6000. // params
  6001. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6002. orderCode = [self translateSingleQuote:orderCode];
  6003. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6004. // setting
  6005. NSDictionary *setting = params[@"setting"];
  6006. NSNumber *hide = setting[@"OrderTotalHide"];
  6007. [dic setValue:hide forKey:@"hide"];
  6008. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6009. __block double lift_gate_value = 0;
  6010. __block double handling_fee = 0;
  6011. __block double shipping = 0;
  6012. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6013. int lift_gate = sqlite3_column_int(stmt, 0);
  6014. lift_gate_value = sqlite3_column_double(stmt, 1);
  6015. shipping = sqlite3_column_double(stmt, 2);
  6016. handling_fee = sqlite3_column_double(stmt, 3);
  6017. if (!lift_gate) {
  6018. lift_gate_value = 0;
  6019. }
  6020. }];
  6021. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6022. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6023. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6024. double payments = [[total valueForKey:@"payments"] doubleValue];
  6025. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6026. double totalPrice = payments;
  6027. [dic setValue:@"Order Total" forKey:@"title"];
  6028. NSDictionary *payments_dic = @{
  6029. @"align" : @"right",
  6030. @"aname" : @"Payments/Credits",
  6031. @"control" : @"text",
  6032. @"name" : @"paymentsAndCredits",
  6033. @"type" : @"price",
  6034. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6035. };
  6036. [dic setValue:payments_dic forKey:@"item_0"];
  6037. NSDictionary *handling_fee_dic = @{
  6038. @"align" : @"right",
  6039. @"aname" : @"Handling Fee",
  6040. @"control" : @"text",
  6041. @"name" : @"handling_fee_value",
  6042. @"required" : @"true",
  6043. @"type" : @"price",
  6044. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6045. };
  6046. NSDictionary *shipping_dic = @{
  6047. @"align" : @"right",
  6048. @"aname" : @"Shipping*",
  6049. @"control" : @"text",
  6050. @"name" : @"shipping",
  6051. @"required" : @"true",
  6052. @"type" : @"price",
  6053. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6054. };
  6055. NSDictionary *lift_gate_dic = @{
  6056. @"align" : @"right",
  6057. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6058. @"control" : @"text",
  6059. @"name" : @"lift_gate_value",
  6060. @"required" : @"true",
  6061. @"type" : @"price",
  6062. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6063. };
  6064. int item_count = 1;
  6065. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6066. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6067. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6068. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6069. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6070. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6071. } else {
  6072. }
  6073. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6074. NSDictionary *total_price_dic = @{
  6075. @"align" : @"right",
  6076. @"aname" : @"Total",
  6077. @"control" : @"text",
  6078. @"name" : @"totalPrice",
  6079. @"type" : @"price",
  6080. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6081. };
  6082. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6083. NSDictionary *total_cuft_dic = @{
  6084. @"align" : @"right",
  6085. @"aname" : @"Total Cuft",
  6086. @"control" : @"text",
  6087. @"name" : @"",
  6088. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6089. };
  6090. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6091. NSDictionary *total_weight_dic = @{
  6092. @"align" : @"right",
  6093. @"aname" : @"Total Weight",
  6094. @"control" : @"text",
  6095. @"name" : @"",
  6096. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6097. };
  6098. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6099. NSDictionary *total_carton_dic = @{
  6100. @"align" : @"right",
  6101. @"aname" : @"Total Carton",
  6102. @"control" : @"text",
  6103. @"name" : @"",
  6104. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6105. };
  6106. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6107. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6108. return dic;
  6109. }
  6110. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6111. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6112. orderCode = [self translateSingleQuote:orderCode];
  6113. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6114. // setting
  6115. NSDictionary *setting = params[@"setting"];
  6116. NSNumber *hide = setting[@"SignatureHide"];
  6117. [dic setValue:hide forKey:@"hide"];
  6118. [dic setValue:@"Signature" forKey:@"title"];
  6119. __block NSString *pic_path = @"";
  6120. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6121. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6122. pic_path = [self textAtColumn:0 statement:stmt];
  6123. }];
  6124. NSDictionary *pic_dic = @{
  6125. @"aname" : @"Signature",
  6126. @"avalue" :pic_path,
  6127. @"control" : @"signature",
  6128. @"name" : @"sign_picpath",
  6129. @"value" : pic_path
  6130. };
  6131. [dic setValue:pic_dic forKey:@"item_0"];
  6132. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6133. return dic;
  6134. }
  6135. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6136. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6137. // setting
  6138. NSDictionary *setting = params[@"setting"];
  6139. NSNumber *hide = setting[@"ShippingMethodHide"];
  6140. [dic setValue:hide forKey:@"hide"];
  6141. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6142. orderCode = [self translateSingleQuote:orderCode];
  6143. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6144. __block NSString *logist = @"";
  6145. __block NSString *lift_gate = @"";
  6146. __block int lift_gate_integer = 0;
  6147. __block NSString *logistic_note = @"";
  6148. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6149. logist = [self textAtColumn:0 statement:stmt];
  6150. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6151. logistic_note = [self textAtColumn:2 statement:stmt];
  6152. }];
  6153. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6154. [dic setValue:@"Shipping Method" forKey:@"title"];
  6155. // val_0
  6156. int PERSONAL_PICK_UP_check = 0;
  6157. int USE_MY_CARRIER_check = 0;
  6158. NSString *logistic_note_text = @"";
  6159. int will_call_check = 0;
  6160. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6161. will_call_check = 1;
  6162. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6163. PERSONAL_PICK_UP_check = 1;
  6164. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6165. USE_MY_CARRIER_check = 1;
  6166. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6167. logistic_note = [logistic_note_array firstObject];
  6168. if (logistic_note_array.count > 1) {
  6169. logistic_note_text = [logistic_note_array lastObject];
  6170. }
  6171. }
  6172. }
  6173. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6174. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6175. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6176. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6177. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6178. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6179. NSDictionary *val0_subItem_item0 = @{
  6180. @"aname" : @"Option",
  6181. @"cadedate" : @{
  6182. @"count" : [NSNumber numberWithInteger:2],
  6183. @"val_0" : @{
  6184. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6185. @"refresh" : [NSNumber numberWithInteger:0],
  6186. @"value" : @"PERSONAL PICK UP",
  6187. @"value_id" : @"PERSONAL PICK UP"
  6188. },
  6189. @"val_1" : @{
  6190. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6191. @"refresh" : [NSNumber numberWithInteger:0],
  6192. @"sub_item" : @{
  6193. @"count" : [NSNumber numberWithInteger:1],
  6194. @"item_0" : @{
  6195. @"aname" : @"BOL",
  6196. @"control" : @"edit",
  6197. @"keyboard" : @"text",
  6198. @"name" : @"logist_note_text",
  6199. @"refresh" : [NSNumber numberWithInteger:0],
  6200. @"required" : @"false",
  6201. @"value" : logistic_note_text
  6202. }
  6203. },
  6204. @"value" : @"USE MY CARRIER",
  6205. @"value_id" : @"USE MY CARRIER"
  6206. }
  6207. },
  6208. @"control" : @"enum",
  6209. @"name" : @"logistic_note",
  6210. @"required" : @"true",
  6211. @"single_select" : @"true"
  6212. };
  6213. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6214. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6215. // val_1
  6216. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6217. if([lift_gate isEqualToString:@""]) {
  6218. if (lift_gate_integer == 1) {
  6219. lift_gate = @"true";
  6220. } else {
  6221. lift_gate = @"false";
  6222. }
  6223. }
  6224. int common_carrier_check = 0;
  6225. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6226. common_carrier_check = 1;
  6227. [params setValue:lift_gate forKey:@"lift_gate"];
  6228. }
  6229. NSDictionary *val_1 = @{
  6230. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6231. @"sub_item" : @{
  6232. @"count" : [NSNumber numberWithInteger:1],
  6233. @"item_0" : @{
  6234. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6235. @"control" : @"switch",
  6236. @"name" : @"lift_gate",
  6237. @"refresh" : [NSNumber numberWithInteger:1],
  6238. @"required" : @"true",
  6239. @"value" : lift_gate
  6240. }
  6241. },
  6242. @"value" : @"COMMON CARRIER",
  6243. @"value_id" : @"COMMON CARRIER"
  6244. };
  6245. // cadedate
  6246. NSDictionary *cadedate = @{
  6247. @"count" : [NSNumber numberWithInteger:2],
  6248. @"val_0" : val_0,
  6249. @"val_1" : val_1
  6250. };
  6251. // item_0
  6252. NSMutableDictionary *item_0 = @{
  6253. @"aname" : @"Shipping",
  6254. @"cadedate" : cadedate,
  6255. @"control" : @"enum",
  6256. @"name" : @"logist",
  6257. @"refresh" : [NSNumber numberWithInteger:1],
  6258. @"single_select" : @"true",
  6259. }.mutableCopy;
  6260. NSNumber *required = setting[@"ShippingMethodRequire"];
  6261. if ([required integerValue]) {
  6262. [item_0 setValue:@"true" forKey:@"required"];
  6263. }
  6264. [dic setValue:item_0 forKey:@"item_0"];
  6265. return dic;
  6266. }
  6267. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6268. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6269. orderCode = [self translateSingleQuote:orderCode];
  6270. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6271. __block int submit_as_integer = 0;
  6272. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6273. submit_as_integer = sqlite3_column_int(stmt, 0);
  6274. }];
  6275. int check11 = 0;
  6276. int check10 = 0;
  6277. if (submit_as_integer == 11) {
  6278. check11 = 1;
  6279. }
  6280. if (submit_as_integer == 10) {
  6281. check10 = 1;
  6282. }
  6283. // section_0
  6284. NSMutableDictionary *dic = @{
  6285. @"count" : @(1),
  6286. @"item_0" : @{
  6287. @"aname" : @"Submit Order As",
  6288. @"cadedate" : @{
  6289. @"count" : @(2),
  6290. @"val_0" : @{
  6291. @"check" : [NSNumber numberWithInteger:check11],
  6292. @"value" : @"Sales Order",
  6293. @"value_id" : @(11)
  6294. },
  6295. @"val_1" : @{
  6296. @"check" : [NSNumber numberWithInteger:check10],
  6297. @"value" : @"Quote",
  6298. @"value_id" : @(10)
  6299. }
  6300. },
  6301. @"control" : @"enum",
  6302. @"name" : @"erpOrderStatus",
  6303. @"required" : @"true",
  6304. @"single_select" : @"true"
  6305. },
  6306. @"title" : @"Order Type"
  6307. }.mutableCopy;
  6308. // setting
  6309. NSDictionary *setting = params[@"setting"];
  6310. NSNumber *hide = setting[@"OrderTypeHide"];
  6311. [dic setValue:hide forKey:@"hide"];
  6312. return dic;
  6313. }
  6314. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6315. countryCode = [self translateSingleQuote:countryCode];
  6316. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6317. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6318. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6319. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6320. if (name == NULL) {
  6321. name = "";
  6322. }
  6323. if (code == NULL) {
  6324. code = "";
  6325. }
  6326. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6327. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6328. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6329. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6330. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6331. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6332. }
  6333. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6334. [container setValue:stateDic forKey:key];
  6335. }] mutableCopy];
  6336. // failure 可以不用了,一样的
  6337. if (ret.allKeys.count == 0) {
  6338. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6339. [stateDic setValue:@"Other" forKey:@"value"];
  6340. [stateDic setValue:@"" forKey:@"value_id"];
  6341. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6342. if (state_code && [@"" isEqualToString:state_code]) {
  6343. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6344. }
  6345. NSString *key = [NSString stringWithFormat:@"val_0"];
  6346. [ret setValue:stateDic forKey:key];
  6347. }
  6348. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6349. return ret;
  6350. }
  6351. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6352. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6353. orderCode = [self translateSingleQuote:orderCode];
  6354. 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];
  6355. __block NSString *payType = @"";
  6356. __block NSString *firstName = @"";
  6357. __block NSString *lastName = @"";
  6358. __block NSString *addr1 = @"";
  6359. __block NSString *addr2 = @"";
  6360. __block NSString *zipcode = @"";
  6361. __block NSString *cardType = @"";
  6362. __block NSString *cardNumber = @"";
  6363. __block NSString *securityCode = @"";
  6364. __block NSString *month = @"";
  6365. __block NSString *year = @"";
  6366. __block NSString *city = @"";
  6367. __block NSString *state = @"";
  6368. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6369. payType = [self textAtColumn:0 statement:stmt];
  6370. firstName = [self textAtColumn:1 statement:stmt];
  6371. lastName = [self textAtColumn:2 statement:stmt];
  6372. addr1 = [self textAtColumn:3 statement:stmt];
  6373. addr2 = [self textAtColumn:4 statement:stmt];
  6374. zipcode = [self textAtColumn:5 statement:stmt];
  6375. cardType = [self textAtColumn:6 statement:stmt];
  6376. cardNumber = [self textAtColumn:7 statement:stmt];
  6377. securityCode = [self textAtColumn:8 statement:stmt];
  6378. month = [self textAtColumn:9 statement:stmt];
  6379. year = [self textAtColumn:10 statement:stmt];
  6380. city = [self textAtColumn:11 statement:stmt];
  6381. state = [self textAtColumn:12 statement:stmt];
  6382. }];
  6383. NSString *required = @"true";
  6384. // setting
  6385. NSDictionary *setting = params[@"setting"];
  6386. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6387. if ([requiredNumber integerValue]) {
  6388. required = @"true";
  6389. } else {
  6390. required = @"false";
  6391. }
  6392. // "section_2"
  6393. NSMutableDictionary *dic = @{
  6394. @"count" : @(1),
  6395. @"item_0" : @{
  6396. @"aname" : @"Payment",
  6397. @"required" : required,
  6398. @"cadedate" : @{
  6399. @"count" : @(6),
  6400. @"val_0" : @{
  6401. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6402. @"value" : @"Check",
  6403. @"value_id" : @"Check"
  6404. },
  6405. @"val_1" : @{
  6406. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6407. @"value" : @"Cash",
  6408. @"value_id" : @"Cash"
  6409. },
  6410. @"val_2" : @{
  6411. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6412. @"value" : @"NET 30",
  6413. @"value_id" : @"NET 30"
  6414. },
  6415. @"val_3" : @{
  6416. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6417. @"value" : @"Wire Transfer",
  6418. @"value_id" : @"Wire Transfer"
  6419. },
  6420. @"val_4" : @{
  6421. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6422. @"sub_item" : @{
  6423. @"count" : @(3),
  6424. @"item_0" : @{
  6425. @"aname" : @"choose",
  6426. @"control" : @"multi_action",
  6427. @"count" : @(1),
  6428. @"item_0" : @{
  6429. @"aname" : @"Same as customer",
  6430. @"key_map" : @{
  6431. @"credit_card_address1" : @"customer_address1",
  6432. @"credit_card_address2" : @"customer_address2",
  6433. @"credit_card_city" : @"customer_city",
  6434. @"credit_card_first_name" : @"customer_first_name",
  6435. @"credit_card_last_name" : @"customer_last_name",
  6436. @"credit_card_state" : @"customer_state",
  6437. @"credit_card_zipcode" : @"customer_zipcode"
  6438. },
  6439. @"type" : @"pull"
  6440. }
  6441. },
  6442. @"item_1" : @{
  6443. @"aname" : @"",
  6444. @"color" : @"red",
  6445. @"control" : @"text",
  6446. @"name" : @"",
  6447. @"value" : @"USA Credit cards only"
  6448. },
  6449. @"item_2" : @{
  6450. @"aname" : @"Fill",
  6451. @"cadedate" : @{
  6452. @"count" : @(2),
  6453. @"val_0" : @{
  6454. @"check" : @(1),
  6455. @"sub_item" : @{
  6456. @"count" : @(11),
  6457. @"item_0" : @{
  6458. @"aname" : @"Type",
  6459. @"cadedate" : @{
  6460. @"count" : @(2),
  6461. @"val_0" : @{
  6462. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6463. @"value" : @"VISA",
  6464. @"value_id" : @(0)
  6465. },
  6466. @"val_1" : @{
  6467. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6468. @"value" : @"MASTER CARD",
  6469. @"value_id" : @(1)
  6470. }
  6471. },
  6472. @"control" : @"enum",
  6473. @"name" : @"credit_card_type",
  6474. @"required" : @"true",
  6475. @"single_select" : @"true"
  6476. },
  6477. @"item_1" : @{
  6478. @"aname" : @"Number",
  6479. @"control" : @"edit",
  6480. @"keyboard" : @"int",
  6481. @"length" : @"16",
  6482. @"name" : @"credit_card_number",
  6483. @"required" : @"true",
  6484. @"value" : cardNumber
  6485. },
  6486. @"item_10" : @{
  6487. @"aname" : @"State",
  6488. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6489. @"control" : @"enum",
  6490. @"enum" : @"true",
  6491. @"name" : @"credit_card_state",
  6492. @"required" : @"true",
  6493. @"single_select" : @"true"
  6494. },
  6495. @"item_2" : @{
  6496. @"aname" : @"Expiration Date",
  6497. @"control" : @"monthpicker",
  6498. @"name" : @"credit_card_expiration",
  6499. @"required" : @"true",
  6500. @"type" : @"date",
  6501. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6502. },
  6503. @"item_3" : @{
  6504. @"aname" : @"Security Code",
  6505. @"control" : @"edit",
  6506. @"keyboard" : @"int",
  6507. @"length" : @"3",
  6508. @"name" : @"credit_card_security_code",
  6509. @"required" : @"true",
  6510. @"value" : securityCode
  6511. },
  6512. @"item_4" : @{
  6513. @"aname" : @"First Name",
  6514. @"control" : @"edit",
  6515. @"keyboard" : @"text",
  6516. @"name" : @"credit_card_first_name",
  6517. @"required" : @"true",
  6518. @"value" : firstName
  6519. },
  6520. @"item_5" : @{
  6521. @"aname" : @"Last Name",
  6522. @"control" : @"edit",
  6523. @"keyboard" : @"text",
  6524. @"name" : @"credit_card_last_name",
  6525. @"required" : @"true",
  6526. @"value" : lastName
  6527. },
  6528. @"item_6" : @{
  6529. @"aname" : @"Address 1",
  6530. @"control" : @"edit",
  6531. @"keyboard" : @"text",
  6532. @"name" : @"credit_card_address1",
  6533. @"required" : @"true",
  6534. @"value" : addr1
  6535. },
  6536. @"item_7" : @{
  6537. @"aname" : @"Address 2",
  6538. @"control" : @"edit",
  6539. @"keyboard" : @"text",
  6540. @"name" : @"credit_card_address2",
  6541. @"value" : addr2
  6542. },
  6543. @"item_8" : @{
  6544. @"aname" : @"zip code",
  6545. @"control" : @"edit",
  6546. @"keyboard" : @"text",
  6547. @"name" : @"credit_card_zipcode",
  6548. @"required" : @"true",
  6549. @"value" : zipcode
  6550. },
  6551. @"item_9" : @{
  6552. @"aname" : @"City",
  6553. @"control" : @"edit",
  6554. @"keyboard" : @"text",
  6555. @"name" : @"credit_card_city",
  6556. @"required" : @"true",
  6557. @"value" : city
  6558. }
  6559. },
  6560. @"value" : @"Fill Now",
  6561. @"value_id" : @""
  6562. },
  6563. @"val_1" : @{
  6564. @"check" : @(0),
  6565. @"value" : @"Fill Later",
  6566. @"value_id" : @""
  6567. }
  6568. },
  6569. @"control" : @"enum",
  6570. @"name" : @"",
  6571. @"single_select" : @"true"
  6572. }
  6573. },
  6574. @"value" : @"Credit Card",
  6575. @"value_id" : @"Credit Card"
  6576. },
  6577. @"val_5" : @{
  6578. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6579. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6580. @"value_id" : @"FOLLOW EXISTING"
  6581. }
  6582. },
  6583. @"control" : @"enum",
  6584. @"name" : @"paymentType",
  6585. @"single_select" : @"true"
  6586. },
  6587. @"title" : @"Payment Information"
  6588. }.mutableCopy;
  6589. NSNumber *hide = setting[@"PaymentInformationHide"];
  6590. [dic setValue:hide forKey:@"hide"];
  6591. return dic;
  6592. }
  6593. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6594. {
  6595. sqlite3 *db = [iSalesDB get_db];
  6596. // UISetting
  6597. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6598. NSString *cachefolder = [paths objectAtIndex:0];
  6599. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6600. NSData* json =nil;
  6601. json=[NSData dataWithContentsOfFile:img_cache];
  6602. NSError *error=nil;
  6603. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6604. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6605. [params setObject:setting forKey:@"setting"];
  6606. int section_count = 0;
  6607. // params
  6608. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6609. orderCode = [self translateSingleQuote:orderCode];
  6610. // 0 Order Type 1 Shipping Method 2 Payment Information
  6611. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6612. // 0 Order Type
  6613. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6614. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6615. [ret setValue:order_type_dic forKey:key0];
  6616. // 1 Shipping Method
  6617. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6618. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6619. [ret setValue:shipping_method_dic forKey:key1];
  6620. // 2 Payment Information
  6621. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6622. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6623. [ret setValue:payment_info_dic forKey:key2];
  6624. // 3 Customer
  6625. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6626. NSDictionary *customer_dic = [self customerDic:params db:db];
  6627. [ret setValue:customer_dic forKey:key3];
  6628. // 4 Ship To
  6629. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6630. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6631. [ret setValue:ship_to_dic forKey:key4];
  6632. // 5 Ship From
  6633. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6634. if (![shipFromDisable integerValue]) {
  6635. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6636. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6637. [ret setValue:ship_from_dic forKey:key5];
  6638. }
  6639. // 6 Freight Bill To
  6640. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6641. if (![freightBillToDisable integerValue]) {
  6642. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6643. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6644. [ret setValue:freight_bill_to forKey:key6];
  6645. }
  6646. // 7 Merchandise Bill To
  6647. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6648. if (![merchandiseBillToDisable integerValue]) {
  6649. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6650. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6651. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6652. }
  6653. // 8 Return To
  6654. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6655. if (![returnToDisable integerValue]) {
  6656. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6657. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6658. [ret setValue:return_to_dic forKey:key8];
  6659. }
  6660. // 9 Model Information
  6661. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6662. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6663. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6664. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6665. [ret setValue:model_info_dic forKey:key9];
  6666. // 10 Remarks Content
  6667. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6668. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6669. [ret setValue:remarks_content_dic forKey:key10];
  6670. // 11 Order Total
  6671. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6672. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6673. [ret setValue:order_total_dic forKey:key11];
  6674. // 12 Signature
  6675. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6676. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6677. [ret setValue:sign_dic forKey:key12];
  6678. [ret setValue:@(section_count) forKey:@"section_count"];
  6679. [iSalesDB close_db:db];
  6680. return [RAUtils dict2data:ret];
  6681. // return nil;
  6682. }
  6683. #pragma mark addr editor
  6684. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6685. NSMutableDictionary *new_item = [item mutableCopy];
  6686. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6687. return new_item;
  6688. }
  6689. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6690. {
  6691. // "is_subaction" = true;
  6692. // orderCode = MOB1608240002;
  6693. // password = 123456;
  6694. // "subaction_tag" = 1;
  6695. // user = EvanK;
  6696. // {
  6697. // "is_subaction" = true;
  6698. // orderCode = MOB1608240002;
  6699. // password = 123456;
  6700. // "refresh_trigger" = zipcode;
  6701. // "subaction_tag" = 1;
  6702. // user = EvanK;
  6703. // }
  6704. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6705. [ret removeObjectForKey:@"up_params"];
  6706. [ret setObject:@"New Address" forKey:@"title"];
  6707. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6708. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6709. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6710. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6711. [country_dic removeObjectForKey:@"refresh"];
  6712. [country_dic removeObjectForKey:@"restore"];
  6713. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6714. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6715. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6716. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6717. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6718. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6719. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6720. [zip_code_dic removeObjectForKey:@"refresh"];
  6721. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6722. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6723. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6724. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6725. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6726. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6727. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6728. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6729. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6730. // country
  6731. NSString *countryCode = @"US";
  6732. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6733. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6734. // state
  6735. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6736. NSDictionary *tmpDic = @{
  6737. @"value" : @"Other",
  6738. @"value_id" : @"",
  6739. @"check" : [NSNumber numberWithInteger:0]
  6740. };
  6741. for (int i = 0; i < allState.allKeys.count; i++) {
  6742. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6743. NSDictionary *tmp = allState[key];
  6744. [allState setValue:tmpDic forKey:key];
  6745. tmpDic = tmp;
  6746. }
  6747. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6748. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6749. [ret setValue:new_section_0 forKey:@"section_0"];
  6750. return [RAUtils dict2data:ret];
  6751. }
  6752. #pragma mark save addr
  6753. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6754. {
  6755. // NSString *companyName = [self valueInParams:params key:@"company"];
  6756. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6757. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6758. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6759. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6760. // NSString *countryId = [self valueInParams:params key:@"country"];
  6761. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6762. // NSString *city = [self valueInParams:params key:@"city"];
  6763. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6764. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6765. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6766. // NSString *phone = [self valueInParams:params key:@"phone"];
  6767. // NSString *fax = [self valueInParams:params key:@"fax"];
  6768. // NSString *email = [self valueInParams:params key:@"email"];
  6769. return [self offline_saveContact:params update:NO isCustomer:NO];
  6770. }
  6771. #pragma mark cancel order
  6772. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6773. {
  6774. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6775. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6776. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6777. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6778. int ret = [iSalesDB execSql:sql];
  6779. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6780. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6781. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6782. // [dic setValue:@"160409" forKey:@"min_ver"];
  6783. return [RAUtils dict2data:dic];
  6784. }
  6785. #pragma mark sign order
  6786. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6787. {
  6788. //参考 offline_saveBusinesscard
  6789. NSLog(@"sign order params: %@",params);
  6790. // orderCode = MOB1608240002;
  6791. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6792. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6793. orderCode = [self translateSingleQuote:orderCode];
  6794. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6795. picPath = [self translateSingleQuote:picPath];
  6796. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6797. int ret = [iSalesDB execSql:sql];
  6798. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6799. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6800. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6801. // [dic setValue:@"160409" forKey:@"min_ver"];
  6802. return [RAUtils dict2data:dic];
  6803. }
  6804. #pragma mark save order
  6805. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6806. NSString *ret = [self valueInParams:params key:key];
  6807. if (translate) {
  6808. ret = [self translateSingleQuote:ret];
  6809. }
  6810. return ret;
  6811. }
  6812. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6813. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6814. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6815. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6816. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6817. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6818. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6819. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6820. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6821. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6822. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6823. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6824. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6825. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6826. if (![total_price isEqualToString:@""]) {
  6827. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6828. } else {
  6829. total_price = @"";
  6830. }
  6831. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6832. if ([paymentsAndCredits isEqualToString:@""]) {
  6833. paymentsAndCredits = @"";
  6834. } else {
  6835. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6836. }
  6837. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6838. if ([handling_fee_value isEqualToString:@""]) {
  6839. handling_fee_value = @"";
  6840. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6841. handling_fee_value = @"";
  6842. } else {
  6843. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6844. }
  6845. NSString *handling_fee_placeholder = handling_fee_value;
  6846. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6847. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6848. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6849. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6850. NSString *lift_gate_placeholder = @"";
  6851. if ([lift_gate_value isEqualToString:@""]) {
  6852. lift_gate_placeholder = @"";
  6853. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6854. lift_gate_placeholder = @"";
  6855. } else {
  6856. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6857. }
  6858. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6859. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6860. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6861. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6862. 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];
  6863. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6864. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6865. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6866. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6867. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6868. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6869. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6870. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6871. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6872. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6873. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6874. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6875. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6876. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6877. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6878. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6879. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6880. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6881. if (!number_nil) {
  6882. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6883. }
  6884. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6885. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6886. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6887. if (!security_code_nil) {
  6888. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6889. }
  6890. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6891. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6892. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6893. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6894. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6895. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6896. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6897. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6898. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6899. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6900. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6901. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6902. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6903. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6904. NSString *contact_id = customer_cid;
  6905. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6906. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6907. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6908. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6909. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6910. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6911. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6912. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6913. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6914. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6915. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6916. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6917. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6918. //
  6919. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6920. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6921. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6922. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6923. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6924. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6925. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6926. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6927. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6928. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6929. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6930. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6931. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6932. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6933. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6934. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6935. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6936. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6937. 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];
  6938. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6939. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6940. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6941. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  6942. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  6943. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  6944. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  6945. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  6946. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  6947. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  6948. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  6949. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  6950. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  6951. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  6952. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  6953. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  6954. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  6955. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  6956. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  6957. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  6958. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  6959. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  6960. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  6961. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  6962. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  6963. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  6964. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  6965. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  6966. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  6967. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  6968. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  6969. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  6970. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  6971. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  6972. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  6973. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  6974. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  6975. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  6976. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  6977. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  6978. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  6979. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  6980. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  6981. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  6982. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  6983. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  6984. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  6985. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  6986. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  6987. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  6988. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  6989. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  6990. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  6991. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  6992. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  6993. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  6994. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  6995. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  6996. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  6997. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  6998. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  6999. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7000. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7001. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7002. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7003. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7004. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7005. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7006. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7007. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7008. NSString *order_status = @"status = 1,";
  7009. if (submit) {
  7010. order_status = @"status = -11,";
  7011. }
  7012. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7013. NSString *sync_sql = @"";
  7014. if (submit) {
  7015. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7016. [param setValue:sales_rep forKey:@"sales_rep"];
  7017. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7018. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7019. }
  7020. 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];
  7021. DebugLog(@"save order contactSql: %@",contactSql);
  7022. DebugLog(@"save order orderSql: %@",orderSql);
  7023. // int contact_ret = [iSalesDB execSql:contactSql];
  7024. int order_ret = [iSalesDB execSql:orderSql];
  7025. int ret = order_ret;
  7026. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7027. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7028. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7029. // [dic setValue:@"160409" forKey:@"min_ver"];
  7030. [dic setObject:so_id forKey:@"so#"];
  7031. return [RAUtils dict2data:dic];
  7032. }
  7033. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7034. {
  7035. // id foo = nil;
  7036. // NSMutableArray *a = @[].mutableCopy;
  7037. // [a addObject:foo];
  7038. return [self saveorder:param submit:NO];
  7039. }
  7040. #pragma mark add to cart by name
  7041. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7042. {
  7043. NSString *orderCode = [params objectForKey:@"orderCode"];
  7044. NSString *product_name = [params objectForKey:@"product_name"];
  7045. product_name = [self translateSingleQuote:product_name];
  7046. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7047. sqlite3 *db = [iSalesDB get_db];
  7048. __block NSMutableString *product_id_string = [NSMutableString string];
  7049. for (int i = 0; i < product_name_array.count; i++) {
  7050. NSString *name = [product_name_array objectAtIndex:i];
  7051. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7052. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7053. int product_id = sqlite3_column_int(stmt, 0);
  7054. if (i == product_name_array.count - 1) {
  7055. [product_id_string appendFormat:@"%d",product_id];
  7056. } else {
  7057. [product_id_string appendFormat:@"%d,",product_id];
  7058. }
  7059. }];
  7060. }
  7061. NSDictionary *newParams = @{
  7062. @"product_id" : product_id_string,
  7063. @"orderCode" : orderCode
  7064. };
  7065. [iSalesDB close_db:db];
  7066. return [self offline_add2cart:[newParams mutableCopy]];
  7067. }
  7068. #pragma mark reset order
  7069. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7070. {
  7071. UIApplication * app = [UIApplication sharedApplication];
  7072. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7073. appDelegate.disable_trigger=true;
  7074. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7075. appDelegate.disable_trigger=false;
  7076. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7077. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7078. return [RAUtils dict2data:dic];
  7079. }
  7080. #pragma mark submit order
  7081. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7082. {
  7083. return [self saveorder:params submit:YES];
  7084. }
  7085. #pragma mark copy order
  7086. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7087. {
  7088. NSMutableDictionary *ret = @{}.mutableCopy;
  7089. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7090. sqlite3 *db = [iSalesDB get_db];
  7091. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7092. // 首先查看联系人是否active
  7093. 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];
  7094. __block int customer_is_active = 1;
  7095. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7096. customer_is_active = sqlite3_column_int(stmt, 0);
  7097. }];
  7098. if (!customer_is_active) {
  7099. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7100. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7101. [iSalesDB close_db:db];
  7102. return [RAUtils dict2data:ret];
  7103. }
  7104. // new order
  7105. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7106. NSString *new_order_code = [self get_offline_soid:db];
  7107. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7108. user = [self translateSingleQuote:user];
  7109. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,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) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,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 from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  7110. __block int result = 1;
  7111. result = [iSalesDB execSql:insert_order_sql db:db];
  7112. if (!result) {
  7113. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7114. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7115. [iSalesDB close_db:db];
  7116. return [RAUtils dict2data:ret];
  7117. }
  7118. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7119. // __block NSString *product_id = @"";
  7120. __weak typeof(self) weakSelf = self;
  7121. 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];
  7122. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7123. int is_active = sqlite3_column_int(stmt, 1);
  7124. if (is_active) {
  7125. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7126. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7127. 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];
  7128. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7129. }
  7130. }];
  7131. // product_id = [product_id substringFromIndex:1];
  7132. //
  7133. // [self offline_add2cart:@{}.mutableCopy];
  7134. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7135. [iSalesDB close_db:db];
  7136. if (result) {
  7137. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7138. } else {
  7139. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7140. }
  7141. [ret setObject:new_order_code forKey:@"so_id"];
  7142. return [RAUtils dict2data:ret];
  7143. }
  7144. #pragma mark move wish list to cart
  7145. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7146. NSString *collectId = params[@"collectId"];
  7147. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7148. __block NSString *product_id = @"";
  7149. __block NSString *qty = @"";
  7150. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7151. int productId = sqlite3_column_int(stmt, 0);
  7152. int item_qty = sqlite3_column_int(stmt, 1);
  7153. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7154. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7155. }];
  7156. product_id = [product_id substringFromIndex:1];
  7157. qty = [qty substringFromIndex:1];
  7158. NSDictionary *newParams = @{
  7159. @"product_id" : product_id,
  7160. @"orderCode" : params[@"orderCode"],
  7161. @"qty" : qty
  7162. };
  7163. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7164. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7165. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7166. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7167. //
  7168. // int ret = [iSalesDB execSql:deleteSQL];
  7169. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7170. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7171. }
  7172. return [RAUtils dict2data:retDic];
  7173. }
  7174. #pragma mark - portfolio
  7175. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7176. int sort = [[params valueForKey:@"sort"] integerValue];
  7177. int offset = [[params valueForKey:@"offset"] integerValue];
  7178. int limit = [[params valueForKey:@"limit"] integerValue];
  7179. NSString *orderBy = @"";
  7180. switch (sort) {
  7181. case 0:{
  7182. orderBy = @"modifytime desc";
  7183. }
  7184. break;
  7185. case 1:{
  7186. orderBy = @"modifytime asc";
  7187. }
  7188. break;
  7189. case 2:{
  7190. orderBy = @"name asc";
  7191. }
  7192. break;
  7193. case 3:{
  7194. orderBy = @"name desc";
  7195. }
  7196. break;
  7197. case 4:{
  7198. orderBy = @"description asc";
  7199. }
  7200. break;
  7201. default:
  7202. break;
  7203. }
  7204. 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 group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  7205. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7206. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7207. sqlite3 *db = [iSalesDB get_db];
  7208. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7209. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7210. int product_id = sqlite3_column_int(stmt, 0);
  7211. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7212. NSString *name = [self textAtColumn:1 statement:stmt];
  7213. NSString *description = [self textAtColumn:2 statement:stmt];
  7214. double price = sqlite3_column_double(stmt, 3);
  7215. double discount = sqlite3_column_double(stmt,4);
  7216. int qty = sqlite3_column_int(stmt, 5);
  7217. int percentage = sqlite3_column_int(stmt, 6);
  7218. int item_id = sqlite3_column_int(stmt, 7);
  7219. // int fashion_id = sqlite3_column_int(stmt, 8);
  7220. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7221. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7222. double percent = sqlite3_column_double(stmt, 11);
  7223. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7224. if ([price_null isEqualToString:@"null"]) {
  7225. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7226. }
  7227. NSMutableDictionary *item = @{
  7228. @"linenotes": line_note,
  7229. @"check": @(1),
  7230. @"product_id": product_id_string,
  7231. @"available_qty": @(qty),
  7232. @"available_percent" : @(percent),
  7233. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7234. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7235. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7236. @"img": img_path,
  7237. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7238. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7239. }.mutableCopy;
  7240. if (percentage) {
  7241. [item removeObjectForKey:@"available_qty"];
  7242. } else {
  7243. [item removeObjectForKey:@"available_percent"];
  7244. }
  7245. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7246. if ([qty_null isEqualToString:@"null"]) {
  7247. [item removeObjectForKey:@"available_qty"];
  7248. }
  7249. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7250. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7251. }];
  7252. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7253. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7254. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7255. }
  7256. [iSalesDB close_db:db];
  7257. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7258. [dic setValue:@"" forKey:@"email_content"];
  7259. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7260. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7261. return [RAUtils dict2data:dic];
  7262. }
  7263. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7264. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7265. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7266. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7267. tear_name = [self translateSingleQuote:tear_name];
  7268. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7269. tear_note = [self translateSingleQuote:tear_note];
  7270. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7271. NSString *pdf_path = @"";
  7272. if (direct) {
  7273. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7274. } else {
  7275. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7276. }
  7277. pdf_path = [self translateSingleQuote:pdf_path];
  7278. NSString *create_user = [self valueInParams:params key:@"user"];
  7279. create_user = [self translateSingleQuote:create_user];
  7280. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7281. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7282. // model info
  7283. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),sheet_discount,ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7284. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7285. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7286. sqlite3 *db = [iSalesDB get_db];
  7287. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7288. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7289. int product_id = sqlite3_column_int(stmt, 0);
  7290. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7291. double price = sqlite3_column_double(stmt, 1);
  7292. double discount = sqlite3_column_double(stmt,2);
  7293. int qty = sqlite3_column_int(stmt, 3);
  7294. int percentage = sqlite3_column_int(stmt, 4);
  7295. int item_id = sqlite3_column_int(stmt, 5);
  7296. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7297. double percent = sqlite3_column_double(stmt, 7);
  7298. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7299. if ([price_null isEqualToString:@"null"]) {
  7300. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7301. }
  7302. NSMutableDictionary *item = @{
  7303. @"linenotes": line_note,
  7304. @"product_id": product_id_string,
  7305. @"available_qty": @(qty),
  7306. @"available_percent" : @(percent),
  7307. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7308. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7309. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7310. }.mutableCopy;
  7311. if (percentage) {
  7312. [item removeObjectForKey:@"available_qty"];
  7313. } else {
  7314. [item removeObjectForKey:@"available_percent"];
  7315. }
  7316. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7317. if ([qty_null isEqualToString:@"null"]) {
  7318. [item removeObjectForKey:@"available_qty"];
  7319. }
  7320. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7321. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7322. }];
  7323. [iSalesDB close_db:db];
  7324. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7325. [iSalesDB close_db:db];
  7326. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7327. return [RAUtils dict2data:resultDictionary];
  7328. }
  7329. NSString *model_info = [RAUtils dict2string:dic];
  7330. model_info = [self translateSingleQuote:model_info];
  7331. // 创建PDF
  7332. // 在preview情况下保存,则不需要新建了
  7333. if (direct) {
  7334. NSData *pdfData = [self offline_request_tearsheet:params]; // 新建PDF,获取本地路径
  7335. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7336. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7337. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7338. resultDictionary = pdfInfo.mutableCopy;
  7339. } else { // 创建PDF失败
  7340. return pdfData;
  7341. }
  7342. } else {
  7343. // pdf_path 就是本地路径
  7344. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7345. }
  7346. // 将文件移动到PDF Cache文件夹
  7347. NSString *newPath = [pdf_path lastPathComponent];
  7348. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7349. NSString *cachefolder = [paths objectAtIndex:0];
  7350. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7351. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7352. NSFileManager *fileManager = [NSFileManager defaultManager];
  7353. NSError *error = nil;
  7354. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7355. if (error) { // 移动文件失败
  7356. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7357. return [RAUtils dict2data:resultDictionary];
  7358. }
  7359. pdf_path = newPath;
  7360. // 保存信息
  7361. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,configureParams) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@','%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams];
  7362. int result = [iSalesDB execSql:save_pdf_sql];
  7363. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7364. return [RAUtils dict2data:resultDictionary];
  7365. }
  7366. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7367. return [self offline_savePDF:params direct:YES];
  7368. }
  7369. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7370. return [self offline_savePDF:params direct:NO];
  7371. }
  7372. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7373. int offset = [[params valueForKey:@"offset"] integerValue];
  7374. int limit = [[params valueForKey:@"limit"] integerValue];
  7375. NSString *keyword = [params valueForKey:@"keyWord"];
  7376. NSString *where = @"";
  7377. if (keyword.length) {
  7378. keyword = [self translateSingleQuote:keyword];
  7379. where = [NSString stringWithFormat:@"where lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  7380. }
  7381. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,tearsheets_id from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];
  7382. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7383. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7384. NSString *name = [self textAtColumn:0 statement:stmt];
  7385. NSString *note = [self textAtColumn:1 statement:stmt];
  7386. NSString *time = [self textAtColumn:2 statement:stmt];
  7387. NSString *user = [self textAtColumn:3 statement:stmt];
  7388. NSString *patch = [self textAtColumn:4 statement:stmt];
  7389. time = [self changeDateTimeFormate:time];
  7390. time = [time stringByAppendingString:@" PST"];
  7391. int sheet_id = sqlite3_column_int(stmt, 5);
  7392. NSMutableDictionary *item = @{
  7393. @"tearsheetsId": @(sheet_id),
  7394. @"pdf_path": patch,
  7395. @"create_time": time,
  7396. @"create_user": user,
  7397. @"tear_note": note,
  7398. @"tear_name": name
  7399. }.mutableCopy;
  7400. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7401. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7402. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7403. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7404. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7405. }];
  7406. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7407. dic[@"isLocalFile"]=@"true";
  7408. return [RAUtils dict2data:dic];
  7409. }
  7410. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7411. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7412. // NSString *user = [params objectForKey:@"user"];
  7413. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id,img) select * from (select p.product_id,p.name,p.description,p.item_id,i.url from product as p left join model_image as i on i.product_id = p.product_id where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio) and i.type = 1 group by p.product_id order by i.default_img desc, i._id asc );",prodct_ids];
  7414. int result = [iSalesDB execSql:sql];
  7415. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7416. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7417. if (result == RESULT_TRUE) {
  7418. NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7419. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];
  7420. }
  7421. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7422. return [RAUtils dict2data:dic];
  7423. }
  7424. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7425. // {
  7426. // "item_ids" = "249,3859,5360,2662";
  7427. // password = 123456;
  7428. // "product_ids" = "3321,3804,4045,3732";
  7429. // user = EvanK;
  7430. // }
  7431. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7432. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7433. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7434. return [RAUtils dict2data:dic];
  7435. }
  7436. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7437. NSString *product_id = [params objectForKey:@"fashionId"];
  7438. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7439. __block int result = RESULT_TRUE;
  7440. __block int qty = 0;
  7441. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7442. qty = sqlite3_column_int(stmt, 0);
  7443. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7444. result = RESULT_FALSE;
  7445. }];
  7446. NSMutableDictionary *dic = @{
  7447. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7448. @"mode" : @"Regular Mode",
  7449. @"quantity_available" : @(qty),
  7450. @"result" : @(result),
  7451. }.mutableCopy;
  7452. return [RAUtils dict2data:dic];
  7453. }
  7454. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7455. NSString *item_ids = [params objectForKey:@"item_id"];
  7456. NSString *line_notes = [params objectForKey:@"notes"];
  7457. NSString *price_str = [params objectForKey:@"price"];
  7458. NSString *discount_str = [params objectForKey:@"discount"];
  7459. NSString *percent = [params objectForKey:@"available_percent"];
  7460. NSString *qty = [params objectForKey:@"available_qty"];
  7461. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7462. int dot = 0;
  7463. if (line_notes) {
  7464. line_notes = [self translateSingleQuote:line_notes];
  7465. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7466. sql = [sql stringByAppendingString:line_notes];
  7467. dot = 1;
  7468. } else {
  7469. line_notes = @"";
  7470. }
  7471. if (price_str) {
  7472. if (dot) {
  7473. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7474. } else {
  7475. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7476. dot = 1;
  7477. }
  7478. sql = [sql stringByAppendingString:price_str];
  7479. } else {
  7480. price_str = @"";
  7481. }
  7482. if (discount_str) {
  7483. if (dot) {
  7484. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7485. } else {
  7486. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7487. dot = 1;
  7488. }
  7489. sql = [sql stringByAppendingString:discount_str];
  7490. } else {
  7491. discount_str = @"";
  7492. }
  7493. if (percent) {
  7494. if (dot) {
  7495. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7496. } else {
  7497. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7498. dot = 1;
  7499. }
  7500. sql = [sql stringByAppendingString:percent];
  7501. } else {
  7502. percent = @"";
  7503. }
  7504. if (qty) {
  7505. if (dot) {
  7506. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7507. } else {
  7508. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7509. dot = 1;
  7510. }
  7511. sql = [sql stringByAppendingString:qty];
  7512. } else {
  7513. qty = @"";
  7514. }
  7515. NSString *where = [NSString stringWithFormat:@" where item_id in (%@);",item_ids];
  7516. sql = [sql stringByAppendingString:where];
  7517. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7518. int result = [iSalesDB execSql:sql];
  7519. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7520. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7521. return [RAUtils dict2data:dic];
  7522. }
  7523. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7524. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7525. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7526. sqlite3 *db = [iSalesDB get_db];
  7527. NSString *sql = [NSString stringWithFormat:@"delete from offline_portfolio where item_id in (%@);",portfolio_ids];
  7528. int result = [iSalesDB execSql:sql db:db];
  7529. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7530. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7531. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"1=1"];
  7532. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7533. [iSalesDB close_db:db];
  7534. return [RAUtils dict2data:dic];
  7535. }
  7536. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7537. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7538. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7539. NSString *user = [params objectForKey:@"user"];
  7540. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where tearsheets_id = %d;",tearsheetsId]];
  7541. if (![create_user isEqualToString:user]) {
  7542. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7543. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7544. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7545. return [RAUtils dict2data:dic];
  7546. }
  7547. NSString *sql = [NSString stringWithFormat:@"delete from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7548. int result = [iSalesDB execSql:sql];
  7549. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7550. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7551. return [RAUtils dict2data:dic];
  7552. }
  7553. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  7554. {
  7555. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7556. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7557. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7558. NSString* where=@"1=1";
  7559. if (ver!=nil) {
  7560. where=@"is_dirty=1";
  7561. }
  7562. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,available_qty,percentage,percent,sheet_price,sheet_discount,img,line_note,is_delete,createtime,modifytime from offline_portfolio where %@",where];
  7563. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7564. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7565. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7566. NSInteger _id = sqlite3_column_int(stmt, 0);
  7567. NSInteger product_id = sqlite3_column_int(stmt, 1);
  7568. NSString *name = [self textAtColumn:2 statement:stmt];
  7569. NSString *desc = [self textAtColumn:3 statement:stmt];
  7570. NSInteger item_id = sqlite3_column_int(stmt, 4);
  7571. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  7572. NSInteger qty = sqlite3_column_int(stmt, 6);
  7573. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  7574. double percent = sqlite3_column_double(stmt, 8);
  7575. double price = sqlite3_column_double(stmt, 9);
  7576. double discount = sqlite3_column_double(stmt, 10);
  7577. NSString *img = [self textAtColumn:11 statement:stmt];
  7578. NSString *line_note = [self textAtColumn:12 statement:stmt];
  7579. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  7580. NSString *create_time = [self textAtColumn:14 statement:stmt];
  7581. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  7582. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7583. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  7584. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  7585. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  7586. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  7587. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  7588. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  7589. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  7590. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  7591. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  7592. [item setValue:name forKey:@"name"];
  7593. [item setValue:desc forKey:@"description"];
  7594. [item setValue:img forKey:@"img"];
  7595. [item setValue:line_note forKey:@"line_note"];
  7596. [item setValue:create_time forKey:@"createtime"];
  7597. [item setValue:modify_time forKey:@"modifytime"];
  7598. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7599. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7600. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7601. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7602. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7603. }];
  7604. return ret;
  7605. }
  7606. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  7607. {
  7608. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7609. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7610. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7611. NSString* where=@"1=1";
  7612. if (ver!=nil) {
  7613. where=@"is_dirty=1";
  7614. }
  7615. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,createtime from offline_pdf where %@",where];
  7616. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7617. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7618. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7619. NSInteger _id = sqlite3_column_int(stmt, 0);
  7620. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  7621. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  7622. NSString *create_user = [self textAtColumn:3 statement:stmt];
  7623. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  7624. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  7625. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7626. NSString *createtime = [self textAtColumn:7 statement:stmt];
  7627. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7628. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  7629. [item setObject:pdf_path forKey:@"pdf_path"];
  7630. [item setObject:create_user forKey:@"create_user"];
  7631. [item setObject:tear_note forKey:@"tear_note"];
  7632. [item setObject:tear_name forKey:@"tear_name"];
  7633. [item setObject:model_info forKey:@"model_info"];
  7634. [item setObject:createtime forKey:@"createtime"];
  7635. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7636. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7637. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7638. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7639. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7640. }];
  7641. return ret;
  7642. }
  7643. @end