OLDataProvider.m 516 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351
  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. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // 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
  58. 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];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. 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 order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(s_percent.length==0||is_percentage==0)
  150. {
  151. percent=100.0;
  152. if([s_qty isEqualToString:@"null"])
  153. qty=availability;
  154. }
  155. else
  156. {
  157. qty=availability;
  158. }
  159. qty=qty*percent/100+0.5;
  160. for(int i=0;i<[values[@"count"] intValue];i++)
  161. {
  162. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  163. if([valueitem[@"product_id"] intValue]==product_id)
  164. {
  165. line_note=valueitem[@"line_note"];
  166. qty=[valueitem[@"available_qty"] intValue];
  167. if([[valueitem allKeys] containsObject:@"available_qty"])
  168. s_qty=[NSString stringWithFormat:@"%d",qty];
  169. else
  170. s_qty=@"null";
  171. if(valueitem[@"regular_price"]!=nil)
  172. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  173. s_price=valueitem[@"special_price"];
  174. price=[valueitem[@"special_price"] floatValue];
  175. discount=0;
  176. }
  177. }
  178. if(is_percentage==0&&qty==0&&!show_stockout)
  179. {
  180. return;
  181. }
  182. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  183. {
  184. return;
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  374. {
  375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  376. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  377. DebugLog(@"offline_login sql:%@",sqlQuery);
  378. sqlite3_stmt * statement;
  379. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  380. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  381. {
  382. if (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  385. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  386. int can_show_price = sqlite3_column_int(statement, 0);
  387. int can_see_price = sqlite3_column_int(statement, 1);
  388. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  389. if(contact_id==nil)
  390. contact_id="";
  391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  392. int user_type = sqlite3_column_int(statement, 3);
  393. int can_cancel_order = sqlite3_column_int(statement, 4);
  394. int can_set_cart_price = sqlite3_column_int(statement, 5);
  395. int can_create_portfolio = sqlite3_column_int(statement, 6);
  396. int can_delete_order = sqlite3_column_int(statement, 7);
  397. int can_submit_order = sqlite3_column_int(statement, 8);
  398. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  399. int can_create_order = sqlite3_column_int(statement, 10);
  400. char *mode = (char*)sqlite3_column_text(statement, 11);
  401. if(mode==nil)
  402. mode="";
  403. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  404. char *username = (char*)sqlite3_column_text(statement, 12);
  405. if(username==nil)
  406. username="";
  407. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  408. int can_update_contact_info = sqlite3_column_int(statement, 13);
  409. char *first_name = (char*)sqlite3_column_text(statement, 14);
  410. if(first_name==nil)
  411. first_name="";
  412. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  413. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  414. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  415. // [header setValue:nscontact_id forKey:@"contact_id"];
  416. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  417. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  418. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  419. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  420. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  421. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  422. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  424. //
  425. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  426. //
  427. // [header setValue:nsusername forKey:@"username"];
  428. //
  429. // NSError* error=nil;
  430. //
  431. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  432. // [header setValue:statusFilter forKey:@"statusFilter"];
  433. //
  434. //
  435. //
  436. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  437. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  438. //
  439. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  440. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  441. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  442. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  443. //
  444. // [ret setObject:header forKey:@"header"];
  445. [ret setValue:nsfirst_name forKey:@"first_name"];
  446. }
  447. sqlite3_finalize(statement);
  448. }
  449. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  450. return ret;
  451. }
  452. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  453. {
  454. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  455. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  456. data[@"npd_url"]=@"www.newpacificdirect.com";
  457. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  458. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  459. NSString* date = [formatter stringFromDate:[NSDate date]];
  460. data[@"print_date"]=date;
  461. data[@"company_name"]=@"New Pacific Direct,Inc.";
  462. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  463. data[@"TOTAL_PAGE"]=0;
  464. data[@"CURRENT_PAGE"]=0;
  465. // [formatter setDateFormat:@"MM/dd/yyyy"];
  466. // date = [formatter stringFromDate:[NSDate date]];
  467. // data[@"creat_date"]=date;
  468. NSString* temp = NSTemporaryDirectory();
  469. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  470. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  471. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  472. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  473. data[@"img_barcode"]=qrpath;
  474. data[@"so_id"]=order_code;
  475. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  476. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  477. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  478. sqlite3 *db = [iSalesDB get_db];
  479. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  480. sqlite3_stmt * statement;
  481. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  482. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  483. row0[@"count"]=[NSNumber numberWithInt:1];
  484. datasource[@"row0"]=row0;
  485. NSString* print_status=@"";
  486. NSString* order_type=@"Offline Quote";
  487. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  488. {
  489. if (sqlite3_step(statement) == SQLITE_ROW)
  490. {
  491. int erpOrderStatus = sqlite3_column_int(statement, 0);
  492. int status = sqlite3_column_int(statement, 26);
  493. if(status==2)
  494. {
  495. switch (erpOrderStatus) {
  496. case 10:
  497. // order_type=@"Quoted Order";
  498. print_status=@"Quote";
  499. break;
  500. case 11:
  501. print_status=@"Booked";
  502. break;
  503. case 12:
  504. print_status=@"Picked";
  505. break;
  506. case 13:
  507. print_status=@"Shipped";
  508. break;
  509. case 14:
  510. print_status=@"Closed";
  511. break;
  512. case 15:
  513. print_status=@"Canceled";
  514. break;
  515. case 16:
  516. print_status=@"All";
  517. break;
  518. case 18:
  519. print_status=@"Processing";
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. else{
  526. switch (status) {
  527. case 0:
  528. {
  529. print_status=@"Quote";
  530. break;
  531. }
  532. case 1:
  533. {
  534. print_status=@"Saved";
  535. break;
  536. }
  537. case 3:
  538. {
  539. //canceled
  540. print_status=@"Canceled";
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. NSString* sender_name = [self textAtColumn:1 statement:statement];
  548. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  549. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  550. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  551. NSString* sender_email = [self textAtColumn:5 statement:statement];
  552. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  553. NSString* billing_name = [self textAtColumn:7 statement:statement];
  554. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  555. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  556. if(billing_phone.length>0)
  557. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  558. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  559. if(billing_fax.length>0)
  560. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  561. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  562. [billto_arr0 addObject:billing_phone];
  563. [billto_arr0 addObject:billing_fax];
  564. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  565. NSString* billing_email = [self textAtColumn:11 statement:statement];
  566. if(billing_email.length>0)
  567. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  568. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  569. if(billing_contact.length>0)
  570. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  571. NSString* receive_name = [self textAtColumn:12 statement:statement];
  572. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  573. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  574. if(receive_phone.length>0)
  575. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  576. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  577. if(receive_fax.length>0)
  578. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  579. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  580. [receive_arr0 addObject:receive_phone];
  581. [receive_arr0 addObject:receive_fax];
  582. NSString* receive_email = [self textAtColumn:16 statement:statement];
  583. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. if(receive_email.length>0)
  585. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  586. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  587. if(receive_contact.length>0)
  588. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  589. NSString* poNumber = [self textAtColumn:17 statement:statement];
  590. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  591. NSString* carrier = [self textAtColumn:19 statement:statement];
  592. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  593. NSString* terms = [self textAtColumn:21 statement:statement];
  594. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  595. NSString* port_destination = [self textAtColumn:23 statement:statement];
  596. NSString* etd = [self textAtColumn:24 statement:statement];
  597. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  598. if(sign_picpath.length==0)
  599. sign_picpath=nil;
  600. NSString* create_date = [self textAtColumn:27 statement:statement];
  601. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  602. NSString* general_notes=[self textAtColumn:33 statement:statement];
  603. NSString *customerID = [self textAtColumn:34 statement:statement];
  604. if (customerID.length == 0) {
  605. customerID = nil;
  606. }
  607. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  608. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  609. if(general_notes.length>0)
  610. {
  611. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  612. row2[@"item_0"]=@{@"g_notes":general_notes };
  613. row2[@"count"]=[NSNumber numberWithInt:1];
  614. datasource[@"row2"]=row2;
  615. }
  616. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  617. [billto_arr addObject:billing_ext];
  618. [billto_arr addObject:billing_pf];
  619. [billto_arr addObject:billing_contact];
  620. [billto_arr addObject:billing_email];
  621. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  622. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  623. [receive_arr addObject:receive_ext];
  624. [receive_arr addObject:receive_pf];
  625. [receive_arr addObject:receive_contact];
  626. [receive_arr addObject:receive_email];
  627. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  628. float shipping = sqlite3_column_double(statement, 28);
  629. float lift_gate_value = sqlite3_column_double(statement, 29);
  630. float handling_fee_value = sqlite3_column_double(statement, 30);
  631. data[@"company"]=sender_name;
  632. data[@"shipfrom_addr"]=sender_ext;
  633. data[@"shipfrom_phone"]=sender_phone;
  634. data[@"shipfrom_fax"]=sender_fax;
  635. data[@"shipfrom_email"]=sender_email;
  636. data[@"warehouse_name"]=warehouse_name;
  637. data[@"bill_to_name"]=billing_name;
  638. data[@"bill_to_ext"]=billto;
  639. data[@"ship_to_name"]=receive_name;
  640. data[@"ship_to_ext"]=receive;
  641. data[@"po_no"]=poNumber;
  642. data[@"rep"]=sales_rep;
  643. data[@"e_schdule"]=etd;
  644. data[@"sales_terms"]=sales_terms;
  645. data[@"port_destination"]=port_destination;
  646. data[@"terms"]=terms;
  647. data[@"carrier"]=carrier;
  648. data[@"vendor_no"]=vendor_no;
  649. data[@"customer_sign"]=sign_picpath;
  650. data[@"print_status"]=print_status;
  651. data[@"create_date"]=create_date;
  652. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  653. sqlite3_stmt * statement_cart;
  654. // NSDate *date1 = [NSDate date];
  655. int count=0;
  656. int cart_count=0;
  657. float credits=0;
  658. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  659. if ( dbresult== SQLITE_OK)
  660. {
  661. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  662. int row1count=0;
  663. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  664. {
  665. NSDate *row_date = [NSDate date];
  666. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  667. // int product_id = sqlite3_column_int(statement_cart, 0);
  668. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  669. int item_id = sqlite3_column_int(statement_cart, 7);
  670. double discount = sqlite3_column_double(statement_cart, 2);
  671. NSString* Price=nil;
  672. if(str_price==nil)
  673. {
  674. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  675. // NSDate *price_date = [NSDate date];
  676. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  677. // DebugLog(@"price time interval");
  678. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  679. if(price==nil)
  680. Price=@"No Price.";
  681. else
  682. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  683. }
  684. else
  685. {
  686. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  687. }
  688. float dunitprice=0;
  689. int item_count = sqlite3_column_int(statement_cart, 3);
  690. NSString* Amount=@"";
  691. if(![Price isEqualToString:@"No Price."])
  692. {
  693. dunitprice=[Price floatValue];
  694. dunitprice=dunitprice* (1-discount/100.0);
  695. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  696. credits+=dunitprice*item_count;
  697. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  698. }
  699. else
  700. {
  701. Price=@"";
  702. }
  703. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  704. // NSString *nsline_note=nil;
  705. // if(line_note!=nil)
  706. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  707. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  708. NSString *nsname=nil;
  709. if(name!=nil)
  710. nsname= [[NSString alloc]initWithUTF8String:name];
  711. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  712. NSString *nsdescription=nil;
  713. if(description!=nil)
  714. nsdescription= [[NSString alloc]initWithUTF8String:description];
  715. // int stockUom = sqlite3_column_int(statement_cart, 8);
  716. // int _id = sqlite3_column_int(statement_cart, 9);
  717. // NSDate *subtotal_date = [NSDate date];
  718. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  719. // DebugLog(@"subtotal_date time interval");
  720. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  721. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  722. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  723. // int carton=[bsubtotaljson[@"carton"] intValue];
  724. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  725. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  726. // NSDate *img_date = [NSDate date];
  727. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  728. // DebugLog(@"img_date time interval");
  729. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  730. itemjson[@"model"]=nsname;
  731. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  732. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  733. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  734. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  735. // itemjson[@"check"]=@"true";
  736. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  737. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  738. itemjson[@"unit_price"]=Price;
  739. itemjson[@"amount"]=Amount;
  740. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  741. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  742. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  743. // itemjson[@"note"]=nsline_note;
  744. // NSDate *date2 = [NSDate date];
  745. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  746. // DebugLog(@"model_bundle time interval");
  747. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  748. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  749. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  750. row1count++;
  751. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  752. for(int c=0;c<[combine[@"count"] intValue];c++)
  753. {
  754. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  755. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  756. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  757. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  758. credits+=combine_amount;
  759. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  760. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  761. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  762. row1count++;
  763. }
  764. count++;
  765. // DebugLog(@"row time interval");
  766. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  767. }
  768. row1[@"count"]=[NSNumber numberWithInt:row1count];
  769. datasource[@"row1"]=row1;
  770. data[@"table_model"]=datasource;
  771. sqlite3_finalize(statement_cart);
  772. }
  773. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  774. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  775. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  776. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  777. // if(credits>0)
  778. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  779. // else
  780. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  781. if(shipping>0)
  782. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  783. else
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  785. if(lift_gate_value>0)
  786. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  787. else
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  789. if(handling_fee_value>0)
  790. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  791. else
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  793. float total = credits+shipping+lift_gate_value+handling_fee_value;
  794. if(total>0)
  795. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  796. else
  797. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  798. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  799. fee[@"row0"]=fee_row0;
  800. data[@"table_fee"]=fee;
  801. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  802. // ret[@"sign_url"] = sign_url;
  803. //
  804. // customer_contact = [self textAtColumn:52 statement:statement];
  805. // customer_email = [self textAtColumn:53 statement:statement];
  806. // customer_phone = [self textAtColumn:54 statement:statement];
  807. // customer_fax = [self textAtColumn:55 statement:statement];
  808. //
  809. //
  810. // int offline_edit=sqlite3_column_int(statement, 56);
  811. //
  812. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  813. //
  814. // char *soid = (char*)sqlite3_column_text(statement, 1);
  815. // if(soid==nil)
  816. // soid= "";
  817. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  818. // // so#
  819. // ret[@"so#"] = nssoid;
  820. //
  821. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  822. // if(poNumber==nil)
  823. // poNumber= "";
  824. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  825. //
  826. //
  827. //
  828. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  829. // if(create_time==nil)
  830. // create_time= "";
  831. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  832. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  833. //
  834. // int status = sqlite3_column_int(statement, 4);
  835. // int erpStatus = sqlite3_column_int(statement, 49);
  836. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  837. //
  838. // // status
  839. // if (status > 1 && status != 3) {
  840. // status = erpStatus;
  841. // } else if (status == 3) {
  842. // status = 15;
  843. // }
  844. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  845. // ret[@"order_status"] = nsstatus;
  846. //
  847. //
  848. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  849. // if(company_name==nil)
  850. // company_name= "";
  851. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  852. // // company name
  853. // ret[@"company_name"] = nscompany_name;
  854. //
  855. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  856. // if(customer_contact==nil)
  857. // customer_contact= "";
  858. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  859. //
  860. //
  861. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  862. // if(addr_1==nil)
  863. // addr_1="";
  864. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  865. //
  866. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  867. // if(addr_2==nil)
  868. // addr_2="";
  869. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  870. //
  871. //
  872. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  873. // if(addr_3==nil)
  874. // addr_3="";
  875. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  876. //
  877. //
  878. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  879. // if(addr_4==nil)
  880. // addr_4="";
  881. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  882. //
  883. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  884. // [arr_addr addObject:nsaddr_1];
  885. // [arr_addr addObject:nsaddr_2];
  886. // [arr_addr addObject:nsaddr_3];
  887. // [arr_addr addObject:nsaddr_4];
  888. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  889. //
  890. //
  891. // char *logist = (char*)sqlite3_column_text(statement, 11);
  892. // if(logist==nil)
  893. // logist= "";
  894. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  895. // if (status == -11 || status == 10 || status == 11) {
  896. // nslogist = [self textAtColumn:59 statement:statement];
  897. // };
  898. //
  899. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  900. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  901. // shipping = @"Shipping To Be Quoted";
  902. // } else {
  903. // shippingFee = sqlite3_column_double(statement, 12);
  904. // }
  905. //
  906. // // Shipping
  907. // ret[@"Shipping"] = shipping;
  908. //
  909. // int have_lift_gate = sqlite3_column_int(statement, 17);
  910. // lift_gate = sqlite3_column_double(statement, 13);
  911. // // Liftgate Fee(No loading dock)
  912. // if (!have_lift_gate) {
  913. // lift_gate = 0;
  914. // }
  915. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  916. // if (sqlite3_column_int(statement, 57)) {
  917. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  918. // }
  919. //
  920. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  921. // if(general_notes==nil)
  922. // general_notes= "";
  923. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  924. //
  925. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  926. // if(internal_notes==nil)
  927. // internal_notes= "";
  928. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  929. //
  930. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  931. // if(payment_type==nil)
  932. // payment_type= "";
  933. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  934. //
  935. //
  936. //
  937. // // order info
  938. // orderinfo = [self textFileName:@"order_info.html"];
  939. //
  940. //
  941. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  942. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  943. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  944. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  945. //
  946. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  947. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  948. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  949. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  950. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  951. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  952. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  953. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  954. //
  955. // NSString *payment = nil;
  956. // if([nspayment_type isEqualToString:@"Credit Card"])
  957. // {
  958. // payment = [self textFileName:@"creditcardpayment.html"];
  959. //
  960. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  961. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  962. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  963. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  964. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  965. // NSString *card_type = [self textAtColumn:42 statement:statement];
  966. // if (card_type.length > 0) { // 显示星号
  967. // card_type = @"****";
  968. // }
  969. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  970. // if (card_number.length > 0 && card_number.length > 4) {
  971. // for (int i = 0; i < card_number.length - 4; i++) {
  972. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  973. // }
  974. // } else {
  975. // card_number = @"";
  976. // }
  977. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  978. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  979. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  980. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  981. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  982. // card_expiration = @"****";
  983. // }
  984. //
  985. //
  986. // NSString *card_city = [self textAtColumn:46 statement:statement];
  987. // NSString *card_state = [self textAtColumn:47 statement:statement];
  988. //
  989. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  990. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  991. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  992. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  993. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  994. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  995. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  996. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  997. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  998. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  999. //
  1000. //
  1001. // }
  1002. // else
  1003. // {
  1004. // payment=[self textFileName:@"normalpayment.html"];
  1005. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1006. // }
  1007. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1008. //
  1009. // ret[@"result"]= [NSNumber numberWithInt:2];
  1010. //
  1011. // // more info
  1012. // moreInfo = [self textFileName:@"more_info.html"];
  1013. //
  1014. //
  1015. // // ShipToCompany_or_&nbsp
  1016. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1017. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1018. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1019. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1020. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1021. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1022. //
  1023. //
  1024. // // ShipFromCompany_or_&nbsp
  1025. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1026. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1027. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1028. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1029. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1031. //
  1032. //
  1033. // // FreightBillToCompany_or_&nbsp
  1034. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1036. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1038. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1040. //
  1041. //
  1042. // // MerchandiseBillToCompany_or_&nbsp
  1043. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1045. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1047. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1049. //
  1050. //
  1051. // // ReturnToCompany_or_&nbsp
  1052. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1054. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1056. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1058. // //
  1059. //
  1060. // // DebugLog(@"more info : %@",moreInfo);
  1061. //
  1062. // // handling fee
  1063. // handlingFee = sqlite3_column_double(statement, 33);
  1064. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1065. // if (sqlite3_column_int(statement, 58)) {
  1066. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1067. // }
  1068. // //
  1069. //
  1070. // // customer info
  1071. // customerID = [self textAtColumn:36 statement:statement];
  1072. //
  1073. // // mode
  1074. // ret[@"mode"] = appDelegate.mode;
  1075. //
  1076. // // model_count
  1077. // ret[@"model_count"] = @(0);
  1078. }
  1079. sqlite3_finalize(statement);
  1080. }
  1081. [iSalesDB close_db:db];
  1082. data[@"order_type"]=order_type;
  1083. /*
  1084. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1085. // "customer_email" = "Shui Hu";
  1086. // "customer_fax" = "";
  1087. // "customer_first_name" = F;
  1088. // "customer_last_name" = L;
  1089. // "customer_name" = ",da He Xiang Dong Liu A";
  1090. // "customer_phone" = "Hey Xuan Feng";
  1091. contactInfo[@"customer_phone"] = customer_phone;
  1092. contactInfo[@"customer_fax"] = customer_fax;
  1093. contactInfo[@"customer_email"] = customer_email;
  1094. NSString *first_name = @"";
  1095. NSString *last_name = @"";
  1096. if ([customer_contact isEqualToString:@""]) {
  1097. } else if ([customer_contact containsString:@" "]) {
  1098. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1099. first_name = [customer_contact substringToIndex:first_space_index];
  1100. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1101. }
  1102. contactInfo[@"customer_first_name"] = first_name;
  1103. contactInfo[@"customer_last_name"] = last_name;
  1104. ret[@"customerInfo"] = contactInfo;
  1105. // models
  1106. if (nssoid) {
  1107. __block double TotalCuft = 0;
  1108. __block double TotalWeight = 0;
  1109. __block int TotalCarton = 0;
  1110. __block double allItemPrice = 0;
  1111. 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];
  1112. sqlite3 *db1 = [iSalesDB get_db];
  1113. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1114. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1115. int product_id = sqlite3_column_int(stmt, 0);
  1116. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1117. int item_id = sqlite3_column_int(stmt, 7);
  1118. NSString* Price=nil;
  1119. if(str_price==nil)
  1120. {
  1121. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1122. if(price==nil)
  1123. Price=@"No Price.";
  1124. else
  1125. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1126. }
  1127. else
  1128. {
  1129. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1130. }
  1131. double discount = sqlite3_column_double(stmt, 2);
  1132. int item_count = sqlite3_column_int(stmt, 3);
  1133. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1134. NSString *nsline_note=nil;
  1135. if(line_note!=nil)
  1136. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1137. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1138. // NSString *nsname = nil;
  1139. // if(name!=nil)
  1140. // nsname= [[NSString alloc]initWithUTF8String:name];
  1141. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1142. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1143. // NSString *nsdescription=nil;
  1144. // if(description!=nil)
  1145. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1146. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1147. // int stockUom = sqlite3_column_int(stmt, 8);
  1148. // int _id = sqlite3_column_int(stmt, 9);
  1149. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1150. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1151. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1152. int carton=[bsubtotaljson[@"carton"] intValue];
  1153. TotalCuft += cuft;
  1154. TotalWeight += weight;
  1155. TotalCarton += carton;
  1156. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1157. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1158. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1159. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1160. itemjson[@"note"]=nsline_note;
  1161. itemjson[@"origin_price"] = Price;
  1162. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1163. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1164. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1165. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1166. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1167. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1168. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1169. if(itemjson[@"combine"] != nil)
  1170. {
  1171. // int citem=0;
  1172. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1173. for(int bc=0;bc<bcount;bc++)
  1174. {
  1175. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1176. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1177. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1178. subTotal += uprice * modulus * item_count;
  1179. }
  1180. }
  1181. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1182. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1183. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1184. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1185. allItemPrice += subTotal;
  1186. }];
  1187. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1188. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1189. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1190. // payments/Credits
  1191. // payments_and_credist = sqlite3_column_double(statement, 34);
  1192. payments_and_credist = allItemPrice;
  1193. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1194. // // total
  1195. // totalPrice = sqlite3_column_double(statement, 35);
  1196. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1197. } else {
  1198. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1199. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1200. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1201. // payments/Credits
  1202. payments_and_credist = 0;
  1203. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1204. }
  1205. // total
  1206. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1207. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1208. ret[@"order_info"]= orderinfo;
  1209. ret[@"more_order_info"] = moreInfo;
  1210. return [RAUtils dict2data:ret];
  1211. */
  1212. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1213. return data;
  1214. }
  1215. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1216. {
  1217. NSString* template_file=nil;
  1218. switch([params[@"thumb"] intValue])
  1219. {
  1220. case 0:
  1221. template_file= @"so.json";
  1222. break;
  1223. case 1:
  1224. template_file= @"so_thumb.json";
  1225. break;
  1226. default:
  1227. template_file= @"so.json";
  1228. }
  1229. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1230. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1231. if(data[@"table_model"][@"row2"]==nil)
  1232. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1233. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1234. DebugLog(@"%@",file);
  1235. // return nil;
  1236. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1237. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1238. if (file) {
  1239. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1240. [dic setObject:file forKey:@"pdf_path"];
  1241. dic[@"isLocalFile"]=@"true";
  1242. return [RAUtils dict2data:dic];
  1243. }
  1244. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1245. dic[@"isLocalFile"]=@"true";
  1246. return [RAUtils dict2data:dic];
  1247. }
  1248. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1249. {
  1250. NSString* template_file=nil;
  1251. switch([params[@"pdf_style"] intValue])
  1252. {
  1253. case 0:
  1254. template_file= @"portfolio_2x3.json";
  1255. break;
  1256. case 1:
  1257. template_file= @"portfolio_3x2.json";
  1258. break;
  1259. default:
  1260. template_file= @"portfolio_3x2.json";
  1261. }
  1262. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1263. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1264. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1265. DebugLog(@"%@",file);
  1266. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1267. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1268. if (file) {
  1269. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1270. [dic setObject:file forKey:@"pdf_path"];
  1271. dic[@"isLocalFile"]=@"true";
  1272. return [RAUtils dict2data:dic];
  1273. }
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAUtils dict2data:dic];
  1277. }
  1278. +(NSString*) get_offline_soid:(sqlite3*)db
  1279. {
  1280. NSString* soid=nil;
  1281. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1282. [formatter setDateFormat:@"yyMMdd"];
  1283. NSString* date = [formatter stringFromDate:[NSDate date]];
  1284. for(int i=1;i<999;i++)
  1285. {
  1286. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1287. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1288. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1289. if(count==0)
  1290. return soid;
  1291. }
  1292. return nil;
  1293. }
  1294. +(NSArray*) enumOfflineOrder
  1295. {
  1296. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1297. sqlite3 *db = [iSalesDB get_db];
  1298. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1299. sqlite3_stmt * statement;
  1300. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1301. if ( dbresult== SQLITE_OK)
  1302. {
  1303. while (sqlite3_step(statement) == SQLITE_ROW)
  1304. {
  1305. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1306. NSString* nsso_id=nil;
  1307. if(so_id!=nil)
  1308. {
  1309. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1310. }
  1311. [ret addObject:nsso_id];
  1312. }
  1313. sqlite3_finalize(statement);
  1314. }
  1315. [iSalesDB close_db:db];
  1316. return ret;
  1317. }
  1318. //+(void) uploadFile:(NSString*) file
  1319. //{
  1320. // NSData* data = [NSData dataWithContentsOfFile: file];
  1321. // UIApplication * app = [UIApplication sharedApplication];
  1322. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1323. //
  1324. //
  1325. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1326. //
  1327. //
  1328. //
  1329. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1330. //
  1331. // if(appDelegate.user!=nil)
  1332. // [params setValue:appDelegate.user forKey:@"user"];
  1333. // // if(appDelegate.contact_id!=nil)
  1334. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1335. // if(appDelegate.password!=nil)
  1336. // [params setValue:appDelegate.password forKey:@"password"];
  1337. //
  1338. //
  1339. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1340. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1341. // } error:nil];
  1342. //
  1343. //
  1344. //
  1345. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1346. //
  1347. // NSProgress *progress = nil;
  1348. //
  1349. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1350. //
  1351. //
  1352. //
  1353. // if (error) {
  1354. //
  1355. // NSString* err_msg = [error localizedDescription];
  1356. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1357. //
  1358. //
  1359. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1360. //
  1361. // DebugLog(@"data string: %@",str);
  1362. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1363. //
  1364. // } else {
  1365. // DebugLog(@"response ");
  1366. //
  1367. //
  1368. //
  1369. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1370. //
  1371. // // 再将NSData转为字符串
  1372. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1373. //
  1374. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1375. //
  1376. // DebugLog(@"data string: %@",jsonStr);
  1377. //
  1378. // NSDictionary* json = responseObject;
  1379. //
  1380. //
  1381. // if([[json valueForKey:@"result"] intValue]==2)
  1382. // {
  1383. //// NSString* img_url_down = json[@"img_url_aname"];
  1384. //// NSString* img_url_up = json[@"img_url"];
  1385. //
  1386. //
  1387. // }
  1388. // else
  1389. // {
  1390. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1391. // }
  1392. //
  1393. //
  1394. // }
  1395. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1396. // }];
  1397. //
  1398. //
  1399. //
  1400. //
  1401. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1402. // //
  1403. //
  1404. //
  1405. //
  1406. //
  1407. //
  1408. //
  1409. // [uploadTask resume];
  1410. //
  1411. //}
  1412. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1413. {
  1414. NSString* serial= [[NSUUID UUID] UUIDString];
  1415. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1416. NSString *cachefolder = [paths objectAtIndex:0];
  1417. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1418. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1419. NSFileManager* fileManager = [NSFileManager defaultManager];
  1420. BOOL bdir=YES;
  1421. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1422. {
  1423. NSError *error = nil;
  1424. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1425. if(!bsuccess)
  1426. DebugLog(@"Create UPLOAD folder failed");
  1427. }
  1428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1429. ret[@"contact"]=[self prepareContact:serial];
  1430. ret[@"wishlist"]=[self prepareWishlist:serial];
  1431. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1432. ret[@"portfolio"] = [self preparePortfolio:serial];
  1433. ret[@"view_portfolio"] = [self preparePDF:serial];
  1434. NSString* str= [RAUtils dict2string:ret];
  1435. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1436. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1437. NSError *error=nil;
  1438. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1439. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1440. ZipArchive* zip = [[ZipArchive alloc] init];
  1441. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1442. for(int i=0;i<arr_files.count;i++)
  1443. {
  1444. NSString* file=arr_files[i];
  1445. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1446. }
  1447. if( ![zip CloseZipFile2] )
  1448. {
  1449. zippath = @"";
  1450. }
  1451. ret[@"file"]=zippath;
  1452. return ret;
  1453. }
  1454. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1455. {
  1456. if(filename.length==0)
  1457. return false;
  1458. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1459. bool ret=false;
  1460. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1461. NSString *cachefolder = [paths objectAtIndex:0];
  1462. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1463. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1464. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1465. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1466. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1467. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1468. NSFileManager* fileManager = [NSFileManager defaultManager];
  1469. BOOL bdir=NO;
  1470. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1471. {
  1472. NSError *error = nil;
  1473. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1474. {
  1475. ret=false;
  1476. }
  1477. else
  1478. {
  1479. ret=true;
  1480. }
  1481. }
  1482. return ret;
  1483. }
  1484. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1485. {
  1486. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1487. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1488. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1489. NSString* where=@" 1=1";
  1490. if (ver!=nil) {
  1491. where=@"is_dirty=1";
  1492. }
  1493. sqlite3 *db = [iSalesDB get_db];
  1494. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1495. sqlite3_stmt * statement;
  1496. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1497. int count=0;
  1498. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1499. if ( dbresult== SQLITE_OK)
  1500. {
  1501. while (sqlite3_step(statement) == SQLITE_ROW)
  1502. {
  1503. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1504. int _id = sqlite3_column_int(statement, 0);
  1505. int product_id = sqlite3_column_int(statement, 1);
  1506. int item_id = sqlite3_column_int(statement, 2);
  1507. int qty = sqlite3_column_int(statement, 3);
  1508. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1509. NSString* nscreate_time=nil;
  1510. if(create_time!=nil)
  1511. {
  1512. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1513. }
  1514. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1515. NSString* nsmodify_time=nil;
  1516. if(modify_time!=nil)
  1517. {
  1518. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1519. }
  1520. int is_delete = sqlite3_column_int(statement, 6);
  1521. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1522. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1523. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1524. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1525. itemjson[@"create_time"]=nscreate_time;
  1526. itemjson[@"modify_time"]=nsmodify_time;
  1527. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1528. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1529. count++;
  1530. }
  1531. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1532. sqlite3_finalize(statement);
  1533. }
  1534. ret[@"count"]=[NSNumber numberWithInt:count ];
  1535. [iSalesDB close_db:db];
  1536. return ret;
  1537. }
  1538. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1539. {
  1540. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1541. UIApplication * app = [UIApplication sharedApplication];
  1542. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1543. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1544. // 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 ];
  1545. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1546. DebugLog(@"offline_login sql:%@",sqlQuery);
  1547. sqlite3_stmt * statement;
  1548. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1549. int count=0;
  1550. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1551. if ( dbresult== SQLITE_OK)
  1552. {
  1553. while (sqlite3_step(statement) == SQLITE_ROW)
  1554. {
  1555. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1556. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1557. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1558. int product_id = sqlite3_column_int(statement, 0);
  1559. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1560. int item_id = sqlite3_column_int(statement, 7);
  1561. NSString* Price=nil;
  1562. if(str_price==nil)
  1563. {
  1564. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1565. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1566. if(price==nil)
  1567. Price=@"No Price.";
  1568. else
  1569. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1570. }
  1571. else
  1572. {
  1573. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1574. }
  1575. double discount = sqlite3_column_double(statement, 2);
  1576. int item_count = sqlite3_column_int(statement, 3);
  1577. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1578. NSString *nsline_note=nil;
  1579. if(line_note!=nil)
  1580. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1581. char *name = (char*)sqlite3_column_text(statement, 5);
  1582. NSString *nsname=nil;
  1583. if(name!=nil)
  1584. nsname= [[NSString alloc]initWithUTF8String:name];
  1585. char *description = (char*)sqlite3_column_text(statement, 6);
  1586. NSString *nsdescription=nil;
  1587. if(description!=nil)
  1588. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1589. // int stockUom = sqlite3_column_int(statement, 8);
  1590. // int _id = sqlite3_column_int(statement, 9);
  1591. //
  1592. //
  1593. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1594. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1595. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1596. // int carton=[bsubtotaljson[@"carton"] intValue];
  1597. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1598. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1599. itemjson[@"model"]=nsname;
  1600. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1601. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1602. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1603. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1604. // itemjson[@"check"]=@"true";
  1605. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1606. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1607. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1608. itemjson[@"unit_price"]=Price;
  1609. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1610. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1611. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1612. itemjson[@"note"]=nsline_note;
  1613. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1615. count++;
  1616. }
  1617. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1618. sqlite3_finalize(statement);
  1619. }
  1620. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1621. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1622. return ret;
  1623. }
  1624. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1625. {
  1626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1627. sqlite3 *db = [iSalesDB get_db];
  1628. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1629. // for(int i=0;i<arr_soid.count;i++)
  1630. sqlite3_stmt * statement;
  1631. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1632. //int count=0;
  1633. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1634. if ( dbresult== SQLITE_OK)
  1635. {
  1636. int count=0;
  1637. while (sqlite3_step(statement) == SQLITE_ROW)
  1638. {
  1639. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1640. int _id = sqlite3_column_int(statement, 0);
  1641. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1642. NSString* nssync_data=nil;
  1643. if(sync_data!=nil)
  1644. {
  1645. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1646. }
  1647. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1648. NSString* nsimg_1=nil;
  1649. if(img_1!=nil)
  1650. {
  1651. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1652. [self copy_upImg:serial file:nsimg_1];
  1653. }
  1654. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1655. NSString* nsso_no=nil;
  1656. if(so_no!=nil)
  1657. {
  1658. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1659. }
  1660. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1661. itemjson[@"order_type"]=@"submit order";
  1662. else
  1663. itemjson[@"order_type"]=@"archive order";
  1664. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1665. itemjson[@"json_data"]= nssync_data;
  1666. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1667. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1668. count++;
  1669. }
  1670. ret[@"count"]=[NSNumber numberWithInt:count ];
  1671. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1672. }
  1673. sqlite3_finalize(statement);
  1674. [iSalesDB close_db:db];
  1675. return ret;
  1676. }
  1677. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1678. {
  1679. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1680. sqlite3 *db = [iSalesDB get_db];
  1681. // UIApplication * app = [UIApplication sharedApplication];
  1682. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1683. 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";
  1684. sqlite3_stmt * statement;
  1685. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1686. int count=0;
  1687. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1688. if ( dbresult== SQLITE_OK)
  1689. {
  1690. while (sqlite3_step(statement) == SQLITE_ROW)
  1691. {
  1692. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1693. int _id = sqlite3_column_int(statement, 0);
  1694. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1695. NSString* nsimg_0=nil;
  1696. if(img_0!=nil)
  1697. {
  1698. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1699. [self copy_upImg:serial file:nsimg_0];
  1700. }
  1701. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1702. NSString* nsimg_1=nil;
  1703. if(img_1!=nil)
  1704. {
  1705. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1706. [self copy_upImg:serial file:nsimg_1];
  1707. }
  1708. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1709. NSString* nsimg_2=nil;
  1710. if(img_2!=nil)
  1711. {
  1712. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1713. [self copy_upImg:serial file:nsimg_2];
  1714. }
  1715. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1716. NSString* nssync_data=nil;
  1717. if(sync_data!=nil)
  1718. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1719. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1720. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1721. itemjson[@"json_data"]= nssync_data;
  1722. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1723. count++;
  1724. }
  1725. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1726. sqlite3_finalize(statement);
  1727. }
  1728. ret[@"count"]=[NSNumber numberWithInt:count ];
  1729. [iSalesDB close_db:db];
  1730. return ret;
  1731. }
  1732. +(bool) check_offlinedata
  1733. {
  1734. UIApplication * app = [UIApplication sharedApplication];
  1735. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1736. sqlite3 *db = [iSalesDB get_db];
  1737. NSString * where=@"1=1";
  1738. // if(appDelegate.user!=nil)
  1739. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1740. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1741. [iSalesDB close_db:db];
  1742. if(count==0)
  1743. {
  1744. return false;
  1745. }
  1746. return true;
  1747. //
  1748. //[iSalesDB close_db:db];
  1749. }
  1750. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1751. {
  1752. UIApplication * app = [UIApplication sharedApplication];
  1753. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1754. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1755. sqlite3 *db = [iSalesDB get_db];
  1756. NSString* collectId=params[@"collectId"];
  1757. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1758. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1759. [iSalesDB execSql:sqlQuery db:db];
  1760. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1761. [iSalesDB close_db:db];
  1762. appDelegate.wish_count =count;
  1763. [appDelegate update_count_mark];
  1764. ret[@"result"]= [NSNumber numberWithInt:2];
  1765. return ret;
  1766. }
  1767. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1768. //{
  1769. //
  1770. // UIApplication * app = [UIApplication sharedApplication];
  1771. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1772. //
  1773. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1774. // sqlite3 *db = [iSalesDB get_db];
  1775. // NSString* product_id=params[@"product_id"];
  1776. //
  1777. //
  1778. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1779. //
  1780. //
  1781. // for(int i=0;i<arr.count;i++)
  1782. // {
  1783. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1784. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1785. // if(count==0)
  1786. // {
  1787. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1788. // [iSalesDB execSql:sqlQuery db:db];
  1789. // }
  1790. // }
  1791. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1792. // [iSalesDB close_db:db];
  1793. //
  1794. // appDelegate.wish_count =count;
  1795. //
  1796. // [appDelegate update_count_mark];
  1797. // ret[@"result"]= [NSNumber numberWithInt:2];
  1798. // return ret;
  1799. // //
  1800. // //return ret;
  1801. //}
  1802. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1803. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1804. DebugLog(@"time interval: %lf",interval);
  1805. }
  1806. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1807. {
  1808. UIApplication * app = [UIApplication sharedApplication];
  1809. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1810. int sort = [[params objectForKey:@"sort"] intValue];
  1811. NSString *sort_str = @"";
  1812. switch (sort) {
  1813. case 0:{
  1814. sort_str = @"order by w.modify_time desc";
  1815. }
  1816. break;
  1817. case 1:{
  1818. sort_str = @"order by w.modify_time asc";
  1819. }
  1820. break;
  1821. case 2:{
  1822. sort_str = @"order by m.name asc";
  1823. }
  1824. break;
  1825. case 3:{
  1826. sort_str = @"order by m.name desc";
  1827. }
  1828. break;
  1829. case 4:{
  1830. sort_str = @"order by m.description asc";
  1831. }
  1832. break;
  1833. default:
  1834. break;
  1835. }
  1836. NSString* user = appDelegate.user;
  1837. sqlite3 *db = [iSalesDB get_db];
  1838. // order by w.create_time
  1839. 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];
  1840. // 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];
  1841. sqlite3_stmt * statement;
  1842. NSDate *date1 = [NSDate date];
  1843. NSDate *date2 = nil;
  1844. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1845. int count=0;
  1846. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1847. {
  1848. while (sqlite3_step(statement) == SQLITE_ROW)
  1849. {
  1850. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1851. int product_id = sqlite3_column_double(statement, 0);
  1852. char *description = (char*)sqlite3_column_text(statement, 1);
  1853. if(description==nil)
  1854. description= "";
  1855. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1856. int item_id = sqlite3_column_double(statement, 2);
  1857. NSDate *date_image = [NSDate date];
  1858. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1859. printf("image : ");
  1860. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1861. // char *url = (char*)sqlite3_column_text(statement, 3);
  1862. // if(url==nil)
  1863. // url="";
  1864. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1865. int qty = sqlite3_column_int(statement, 3);
  1866. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1867. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1868. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1869. item[@"description"]= nsdescription;
  1870. item[@"img"]= nsurl;
  1871. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1872. count++;
  1873. }
  1874. printf("total time:");
  1875. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1876. ret[@"count"]= [NSNumber numberWithInt:count];
  1877. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1878. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1879. ret[@"result"]= [NSNumber numberWithInt:2];
  1880. appDelegate.wish_count =count;
  1881. [appDelegate update_count_mark];
  1882. sqlite3_finalize(statement);
  1883. }
  1884. [iSalesDB close_db:db];
  1885. return ret;
  1886. }
  1887. +(NSDictionary*) offline_notimpl
  1888. {
  1889. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1890. ret[@"result"]=@"8";
  1891. ret[@"err_msg"]=@"offline mode does not support this function.";
  1892. return ret;
  1893. }
  1894. +(NSDictionary*) offline_home
  1895. {
  1896. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1897. NSString *cachefolder = [paths objectAtIndex:0];
  1898. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1899. NSData* json =nil;
  1900. json=[NSData dataWithContentsOfFile:img_cache];
  1901. if(json==nil)
  1902. return nil;
  1903. NSError *error=nil;
  1904. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1905. return menu;
  1906. }
  1907. +(NSDictionary*) offline_category_menu
  1908. {
  1909. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1910. NSString *cachefolder = [paths objectAtIndex:0];
  1911. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1912. NSData* json =nil;
  1913. json=[NSData dataWithContentsOfFile:img_cache];
  1914. if(json==nil)
  1915. return nil;
  1916. NSError *error=nil;
  1917. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1918. return menu;
  1919. }
  1920. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1921. {
  1922. NSString* offline_command=params[@"offline_Command"];
  1923. NSDictionary* ret=nil;
  1924. if([offline_command isEqualToString:@"model_NIYMAL"])
  1925. {
  1926. NSString* category = params[@"category"];
  1927. ret = [self refresh_model_NIYMAL:category];
  1928. }
  1929. return ret;
  1930. }
  1931. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1932. {
  1933. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1934. [ret setValue:@"2" forKey:@"result"];
  1935. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1936. sqlite3* db= [iSalesDB get_db];
  1937. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1938. [iSalesDB close_db:db];
  1939. [ret setObject:detail1_section forKey:@"detail_1"];
  1940. return ret;
  1941. }
  1942. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1943. {
  1944. UIApplication * app = [UIApplication sharedApplication];
  1945. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1946. // NSArray* arr1 = [self get_user_all_price_type];
  1947. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1948. // NSSet *set1 = [NSSet setWithArray:arr1];
  1949. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1950. // if(appDelegate.contact_id==nil)
  1951. // set2=[set1 mutableCopy];
  1952. // else
  1953. // [set2 intersectsSet:set1];
  1954. // NSArray *retarr = [set2 allObjects];
  1955. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1956. // sqlite3 *db = [iSalesDB get_db];
  1957. NSString* sqlQuery = nil;
  1958. if(appDelegate.contact_id==nil)
  1959. {
  1960. if(!appDelegate.bLogin)
  1961. return nil;
  1962. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  1963. }
  1964. else
  1965. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  1966. sqlite3_stmt * statement;
  1967. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1968. int count=0;
  1969. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1970. {
  1971. while (sqlite3_step(statement) == SQLITE_ROW)
  1972. {
  1973. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1974. char *name = (char*)sqlite3_column_text(statement, 0);
  1975. if(name==nil)
  1976. name="";
  1977. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1978. // double price = sqlite3_column_double(statement, 1);
  1979. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1980. // if(isnull==nil)
  1981. // item[nsname]= @"No Price";
  1982. // else
  1983. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1984. char *price = (char*)sqlite3_column_text(statement, 1);
  1985. if(price!=nil)
  1986. {
  1987. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1988. nsprice=[AESCrypt fastdecrypt:nsprice];
  1989. if(nsprice.length>0)
  1990. {
  1991. double dp= [nsprice doubleValue];
  1992. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1993. }
  1994. }
  1995. else
  1996. {
  1997. item[nsname]= @"No Price";
  1998. }
  1999. // int type= sqlite3_column_int(statement, 2);
  2000. //item[@"type"]=@"price";
  2001. // item[nsname]= nsprice;
  2002. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2003. count++;
  2004. }
  2005. ret[@"count"]= [NSNumber numberWithInt:count];
  2006. sqlite3_finalize(statement);
  2007. }
  2008. // [iSalesDB close_db:db];
  2009. return ret;
  2010. }
  2011. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2012. //{
  2013. // NSArray* arr1 = [self get_user_all_price_type:db];
  2014. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2015. //
  2016. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2017. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2018. // // [set2 intersectsSet:set1];
  2019. // //
  2020. // //
  2021. // // NSArray *retarr = [set2 allObjects];
  2022. //
  2023. // NSString* whereprice=nil;
  2024. // if(contact_id==nil)
  2025. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2026. // else
  2027. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2028. //
  2029. //
  2030. // // sqlite3 *db = [iSalesDB get_db];
  2031. //
  2032. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2033. // sqlite3_stmt * statement;
  2034. //
  2035. //
  2036. // NSNumber* ret = nil;
  2037. // double dprice=DBL_MAX;
  2038. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2039. // {
  2040. //
  2041. //
  2042. // while (sqlite3_step(statement) == SQLITE_ROW)
  2043. // {
  2044. //
  2045. // // double val = sqlite3_column_double(statement, 0);
  2046. // char *price = (char*)sqlite3_column_text(statement, 0);
  2047. // if(price!=nil)
  2048. // {
  2049. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2050. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2051. // if(nsprice.length>0)
  2052. // {
  2053. // double dp= [nsprice doubleValue];
  2054. // if(dp<dprice)
  2055. // dprice=dp;
  2056. // }
  2057. // }
  2058. // }
  2059. //
  2060. //
  2061. //
  2062. //
  2063. // sqlite3_finalize(statement);
  2064. //
  2065. //
  2066. //
  2067. //
  2068. // }
  2069. //
  2070. // // [iSalesDB close_db:db];
  2071. //
  2072. // if(dprice==DBL_MAX)
  2073. // ret= nil;
  2074. // else
  2075. // ret= [NSNumber numberWithDouble:dprice];
  2076. // return ret;
  2077. //}
  2078. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2079. {
  2080. NSArray* arr1 = [self get_user_all_price_type:db];
  2081. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2082. NSString* whereprice=nil;
  2083. if(contact_id==nil)
  2084. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2085. else
  2086. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2087. // sqlite3 *db = [iSalesDB get_db];
  2088. NSString *productIdCondition = @"1 = 1";
  2089. if (product_id) {
  2090. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2091. }
  2092. NSString *itemIdCondition = @"";
  2093. if (item_id) {
  2094. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2095. }
  2096. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2097. sqlite3_stmt * statement;
  2098. NSNumber* ret = nil;
  2099. double dprice=DBL_MAX;
  2100. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2101. {
  2102. while (sqlite3_step(statement) == SQLITE_ROW)
  2103. {
  2104. // double val = sqlite3_column_double(statement, 0);
  2105. char *price = (char*)sqlite3_column_text(statement, 0);
  2106. if(price!=nil)
  2107. {
  2108. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2109. nsprice=[AESCrypt fastdecrypt:nsprice];
  2110. if(nsprice.length>0)
  2111. {
  2112. double dp= [nsprice doubleValue];
  2113. if(dp<dprice)
  2114. dprice=dp;
  2115. }
  2116. }
  2117. }
  2118. sqlite3_finalize(statement);
  2119. }
  2120. // [iSalesDB close_db:db];
  2121. if(dprice==DBL_MAX)
  2122. ret= nil;
  2123. else
  2124. ret= [NSNumber numberWithDouble:dprice];
  2125. return ret;
  2126. }
  2127. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2128. {
  2129. NSArray* ret=nil;
  2130. // sqlite3 *db = [iSalesDB get_db];
  2131. // no customer assigned , use login user contact_id
  2132. UIApplication * app = [UIApplication sharedApplication];
  2133. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2134. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2135. sqlite3_stmt * statement;
  2136. // int count=0;
  2137. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2138. {
  2139. if (sqlite3_step(statement) == SQLITE_ROW)
  2140. {
  2141. char *val = (char*)sqlite3_column_text(statement, 0);
  2142. if(val==nil)
  2143. val="";
  2144. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2145. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2146. }
  2147. sqlite3_finalize(statement);
  2148. }
  2149. // [iSalesDB close_db:db];
  2150. return ret;
  2151. }
  2152. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2153. {
  2154. // sqlite3 *db = [iSalesDB get_db];
  2155. if(contact_id==nil)
  2156. {
  2157. // no customer assigned , use login user contact_id
  2158. UIApplication * app = [UIApplication sharedApplication];
  2159. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2160. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2161. sqlite3_stmt * statement;
  2162. // int count=0;
  2163. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2164. {
  2165. if (sqlite3_step(statement) == SQLITE_ROW)
  2166. {
  2167. char *val = (char*)sqlite3_column_text(statement, 0);
  2168. if(val==nil)
  2169. val="";
  2170. contact_id = [[NSString alloc]initWithUTF8String:val];
  2171. }
  2172. sqlite3_finalize(statement);
  2173. }
  2174. if(contact_id.length<=0)
  2175. {
  2176. // [iSalesDB close_db:db];
  2177. return nil;
  2178. }
  2179. }
  2180. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2181. sqlite3_stmt * statement;
  2182. NSArray* ret=nil;
  2183. // int count=0;
  2184. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2185. {
  2186. if (sqlite3_step(statement) == SQLITE_ROW)
  2187. {
  2188. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2189. if(price_type==nil)
  2190. price_type="";
  2191. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2192. if(nsprice_type.length>0)
  2193. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2194. else
  2195. ret=nil;
  2196. }
  2197. sqlite3_finalize(statement);
  2198. }
  2199. // [iSalesDB close_db:db];
  2200. return ret;
  2201. }
  2202. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2203. {
  2204. NSString* ret= nil;
  2205. // sqlite3 *db = [iSalesDB get_db];
  2206. NSString *sqlQuery = nil;
  2207. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2208. // 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;
  2209. // 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
  2210. if(product_id==nil && model_name)
  2211. 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;
  2212. else if (product_id)
  2213. 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
  2214. sqlite3_stmt * statement;
  2215. // int count=0;
  2216. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2217. {
  2218. if (sqlite3_step(statement) == SQLITE_ROW)
  2219. {
  2220. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2221. if(imgurl==nil)
  2222. imgurl="";
  2223. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2224. ret=nsimgurl;
  2225. }
  2226. sqlite3_finalize(statement);
  2227. }
  2228. else
  2229. {
  2230. [ret setValue:@"8" forKey:@"result"];
  2231. }
  2232. // [iSalesDB close_db:db];
  2233. DebugLog(@"data string: %@",ret );
  2234. return ret;
  2235. }
  2236. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2237. {
  2238. NSString* ret= nil;
  2239. sqlite3 *db = [iSalesDB get_db];
  2240. NSString *sqlQuery = nil;
  2241. if(product_id==nil)
  2242. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2243. else
  2244. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2245. sqlite3_stmt * statement;
  2246. // int count=0;
  2247. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2248. {
  2249. if (sqlite3_step(statement) == SQLITE_ROW)
  2250. {
  2251. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2252. if(default_category==nil)
  2253. default_category="";
  2254. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2255. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2256. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2257. ret=nsdefault_category;
  2258. }
  2259. sqlite3_finalize(statement);
  2260. }
  2261. else
  2262. {
  2263. [ret setValue:@"8" forKey:@"result"];
  2264. }
  2265. [iSalesDB close_db:db];
  2266. DebugLog(@"data string: %@",ret );
  2267. return ret;
  2268. }
  2269. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2270. {
  2271. NSString* model_name = [params valueForKey:@"product_name"];
  2272. NSString* product_id = [params valueForKey:@"product_id"];
  2273. NSString* category = [params valueForKey:@"category"];
  2274. if(category==nil)
  2275. category = [self model_default_category:product_id model_name:model_name];
  2276. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2277. sqlite3 *db = [iSalesDB get_db];
  2278. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2279. NSString *sqlQuery = nil;
  2280. if(product_id==nil)
  2281. 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='%@';
  2282. else
  2283. 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=%@;
  2284. sqlite3_stmt * statement;
  2285. [ret setValue:@"2" forKey:@"result"];
  2286. [ret setValue:@"3" forKey:@"detail_section_count"];
  2287. // int count=0;
  2288. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2289. {
  2290. if (sqlite3_step(statement) == SQLITE_ROW)
  2291. {
  2292. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2293. char *name = (char*)sqlite3_column_text(statement, 0);
  2294. if(name==nil)
  2295. name="";
  2296. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2297. char *description = (char*)sqlite3_column_text(statement, 1);
  2298. if(description==nil)
  2299. description="";
  2300. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2301. int product_id = sqlite3_column_int(statement, 2);
  2302. char *color = (char*)sqlite3_column_text(statement, 3);
  2303. if(color==nil)
  2304. color="";
  2305. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2306. //
  2307. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2308. // if(legcolor==nil)
  2309. // legcolor="";
  2310. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2311. //
  2312. //
  2313. int availability = sqlite3_column_int(statement, 5);
  2314. //
  2315. int incoming_stock = sqlite3_column_int(statement, 6);
  2316. char *demension = (char*)sqlite3_column_text(statement, 7);
  2317. if(demension==nil)
  2318. demension="";
  2319. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2320. // ,,,,,,,,,
  2321. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2322. if(seat_height==nil)
  2323. seat_height="";
  2324. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2325. char *material = (char*)sqlite3_column_text(statement, 9);
  2326. if(material==nil)
  2327. material="";
  2328. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2329. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2330. if(box_dim==nil)
  2331. box_dim="";
  2332. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2333. char *volume = (char*)sqlite3_column_text(statement, 11);
  2334. if(volume==nil)
  2335. volume="";
  2336. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2337. double weight = sqlite3_column_double(statement, 12);
  2338. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2339. if(model_set==nil)
  2340. model_set="";
  2341. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2342. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2343. if(load_ability==nil)
  2344. load_ability="";
  2345. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2346. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2347. if(default_category==nil)
  2348. default_category="";
  2349. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2350. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2351. if(fabric_content==nil)
  2352. fabric_content="";
  2353. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2354. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2355. if(assembling==nil)
  2356. assembling="";
  2357. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2358. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2359. if(made_in==nil)
  2360. made_in="";
  2361. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2362. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2363. if(special_remarks==nil)
  2364. special_remarks="";
  2365. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2366. int stockUcom = sqlite3_column_double(statement, 20);
  2367. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2368. if(product_group==nil)
  2369. product_group="";
  2370. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2371. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2372. // if(fashion_selector==nil)
  2373. // fashion_selector="";
  2374. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2375. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2376. if(selector_field==nil)
  2377. selector_field="";
  2378. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2379. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2380. if(property_field==nil)
  2381. property_field="";
  2382. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2383. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2384. if(packaging==nil)
  2385. packaging="";
  2386. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2387. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2388. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2389. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2390. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2391. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2392. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2393. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2394. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2395. NSString* Availability=nil;
  2396. if(availability>0)
  2397. Availability=[NSString stringWithFormat:@"%d",availability];
  2398. else
  2399. Availability = @"In Production";
  2400. [img_section setValue:Availability forKey:@"Availability"];
  2401. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2402. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2403. char *eta = (char*)sqlite3_column_text(statement, 25);
  2404. if(eta==nil)
  2405. eta="";
  2406. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2407. if ([nseta isEqualToString:@"null"]) {
  2408. nseta = @"";
  2409. }
  2410. if (availability <= 0) {
  2411. [img_section setValue:nseta forKey:@"ETA"];
  2412. }
  2413. int item_id = sqlite3_column_int(statement, 26);
  2414. NSString* Price=nil;
  2415. if(appDelegate.bLogin==false)
  2416. Price=@"Must Sign in.";
  2417. else
  2418. {
  2419. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2420. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2421. if(price==nil)
  2422. Price=@"No Price.";
  2423. else
  2424. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2425. }
  2426. [img_section setValue:Price forKey:@"price"];
  2427. [img_section setValue:nsname forKey:@"model_name"];
  2428. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2429. [ret setObject:img_section forKey:@"img_section"];
  2430. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2431. int detail0_item_count=0;
  2432. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2433. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2434. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2435. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2436. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2437. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2438. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2439. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2440. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2441. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2442. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2443. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2444. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2445. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2446. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2447. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2448. {
  2449. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2450. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2451. }
  2452. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2453. [detail0_section setValue:@"kv" forKey:@"type"];
  2454. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2455. [ret setObject:detail0_section forKey:@"detail_0"];
  2456. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2457. // [detail1_section setValue:@"detail" forKey:@"target"];
  2458. // [detail1_section setValue:@"popup" forKey:@"action"];
  2459. // [detail1_section setValue:@"content" forKey:@"type"];
  2460. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2461. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2462. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2463. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2464. [ret setObject:detail1_section forKey:@"detail_1"];
  2465. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2466. [detail2_section setValue:@"detail" forKey:@"target"];
  2467. [detail2_section setValue:@"popup" forKey:@"action"];
  2468. [detail2_section setValue:@"content" forKey:@"type"];
  2469. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2470. [detail2_section setValue:@"true" forKey:@"single_row"];
  2471. [detail2_section setValue:@"local" forKey:@"data"];
  2472. [ret setObject:detail2_section forKey:@"detail_2"];
  2473. }
  2474. sqlite3_finalize(statement);
  2475. }
  2476. else
  2477. {
  2478. [ret setValue:@"8" forKey:@"result"];
  2479. }
  2480. DebugLog(@"count:%d",count);
  2481. [iSalesDB close_db:db];
  2482. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2483. return ret;
  2484. }
  2485. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2486. NSString* orderCode = [params valueForKey:@"orderCode"];
  2487. NSString* keyword = [params valueForKey:@"keyword"];
  2488. keyword=keyword.lowercaseString;
  2489. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2490. int limit = [[params valueForKey:@"limit"] intValue];
  2491. int offset = [[params valueForKey:@"offset"] intValue];
  2492. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2493. NSString *limit_str = @"";
  2494. if (limited) {
  2495. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2496. }
  2497. sqlite3 *db = [iSalesDB get_db];
  2498. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2499. UIApplication * app = [UIApplication sharedApplication];
  2500. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2501. NSString *sqlQuery = nil;
  2502. if(exactMatch )
  2503. 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 ;
  2504. else
  2505. 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
  2506. DebugLog(@"offline_search sql:%@",sqlQuery);
  2507. sqlite3_stmt * statement;
  2508. [ret setValue:@"2" forKey:@"result"];
  2509. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2510. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2511. // int count=0;
  2512. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2513. {
  2514. int i=0;
  2515. while (sqlite3_step(statement) == SQLITE_ROW)
  2516. {
  2517. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2518. // char *name = (char*)sqlite3_column_text(statement, 1);
  2519. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2520. char *name = (char*)sqlite3_column_text(statement, 0);
  2521. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2522. char *description = (char*)sqlite3_column_text(statement, 1);
  2523. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2524. int product_id = sqlite3_column_int(statement, 2);
  2525. // char *url = (char*)sqlite3_column_text(statement, 3);
  2526. // if(url==nil)
  2527. // url="";
  2528. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2529. int wid = sqlite3_column_int(statement, 3);
  2530. int closeout = sqlite3_column_int(statement, 4);
  2531. int cid = sqlite3_column_int(statement, 5);
  2532. int wisdelete = sqlite3_column_int(statement, 6);
  2533. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2534. if(wid !=0 && wisdelete != 1)
  2535. [item setValue:@"true" forKey:@"wish_exists"];
  2536. else
  2537. [item setValue:@"false" forKey:@"wish_exists"];
  2538. if(closeout==0)
  2539. [item setValue:@"false" forKey:@"is_closeout"];
  2540. else
  2541. [item setValue:@"true" forKey:@"is_closeout"];
  2542. if(cid==0)
  2543. [item setValue:@"false" forKey:@"cart_exists"];
  2544. else
  2545. [item setValue:@"true" forKey:@"cart_exists"];
  2546. [item addEntriesFromDictionary:imgjson];
  2547. // [item setValue:nsurl forKey:@"img"];
  2548. [item setValue:nsname forKey:@"fash_name"];
  2549. [item setValue:nsdescription forKey:@"description"];
  2550. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2551. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2552. i++;
  2553. }
  2554. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2555. [ret setObject:items forKey:@"items"];
  2556. sqlite3_finalize(statement);
  2557. }
  2558. DebugLog(@"count:%d",count);
  2559. [iSalesDB close_db:db];
  2560. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2561. return ret;
  2562. }
  2563. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2564. {
  2565. return [self search:params limited:YES];
  2566. }
  2567. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2568. {
  2569. UIApplication * app = [UIApplication sharedApplication];
  2570. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2571. appDelegate.disable_trigger=true;
  2572. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2573. appDelegate.disable_trigger=false;
  2574. //
  2575. // NSString* user = [params valueForKey:@"user"];
  2576. //
  2577. // NSString* password = [params valueForKey:@"password"];
  2578. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2579. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2580. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2581. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2582. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2583. [appDelegate update_count_mark];
  2584. appDelegate.can_show_price =false;
  2585. appDelegate.can_see_price =false;
  2586. appDelegate.can_create_portfolio =false;
  2587. appDelegate.can_create_order =false;
  2588. appDelegate.can_cancel_order =false;
  2589. appDelegate.can_set_cart_price =false;
  2590. appDelegate.can_delete_order =false;
  2591. appDelegate.can_submit_order =false;
  2592. appDelegate.can_set_tearsheet_price =false;
  2593. appDelegate.can_update_contact_info = false;
  2594. appDelegate.save_order_logout = false;
  2595. appDelegate.submit_order_logout = false;
  2596. appDelegate.alert_sold_in_quantities = false;
  2597. appDelegate.ipad_perm =nil ;
  2598. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2599. appDelegate.OrderFilter= nil;
  2600. [appDelegate SetSo:nil];
  2601. [appDelegate set_main_button_panel];
  2602. // sqlite3 *db = [iSalesDB get_db];
  2603. //
  2604. //
  2605. //
  2606. //
  2607. //
  2608. // 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"]];
  2609. //
  2610. //
  2611. //
  2612. //
  2613. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2614. // sqlite3_stmt * statement;
  2615. //
  2616. //
  2617. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2618. //
  2619. //
  2620. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2621. // {
  2622. //
  2623. //
  2624. // if (sqlite3_step(statement) == SQLITE_ROW)
  2625. // {
  2626. //
  2627. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2628. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2629. //
  2630. //
  2631. //
  2632. // int can_show_price = sqlite3_column_int(statement, 0);
  2633. // int can_see_price = sqlite3_column_int(statement, 1);
  2634. //
  2635. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2636. // if(contact_id==nil)
  2637. // contact_id="";
  2638. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2639. //
  2640. // int user_type = sqlite3_column_int(statement, 3);
  2641. //
  2642. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2643. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2644. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2645. // int can_delete_order = sqlite3_column_int(statement, 7);
  2646. // int can_submit_order = sqlite3_column_int(statement, 8);
  2647. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2648. // int can_create_order = sqlite3_column_int(statement, 10);
  2649. //
  2650. //
  2651. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2652. // if(mode==nil)
  2653. // mode="";
  2654. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2655. //
  2656. //
  2657. // char *username = (char*)sqlite3_column_text(statement, 12);
  2658. // if(username==nil)
  2659. // username="";
  2660. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2661. //
  2662. //
  2663. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2664. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2665. // [header setValue:nscontact_id forKey:@"contact_id"];
  2666. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2667. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2668. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2669. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2670. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2671. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2672. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2673. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2674. //
  2675. // [header setValue:nsusername forKey:@"username"];
  2676. //
  2677. //
  2678. // [ret setObject:header forKey:@"header"];
  2679. // [ret setValue:nsmode forKey:@"mode"];
  2680. //
  2681. //
  2682. // }
  2683. //
  2684. //
  2685. //
  2686. // sqlite3_finalize(statement);
  2687. // }
  2688. //
  2689. //
  2690. //
  2691. // [iSalesDB close_db:db];
  2692. //
  2693. //
  2694. //
  2695. //
  2696. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2697. return ret;
  2698. }
  2699. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2700. {
  2701. UIApplication * app = [UIApplication sharedApplication];
  2702. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2703. appDelegate.disable_trigger=true;
  2704. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2705. appDelegate.disable_trigger=false;
  2706. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2707. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2708. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2709. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2710. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2711. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2712. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2713. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2714. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2715. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2716. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2717. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2718. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2719. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2720. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2721. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2722. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2723. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2724. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2725. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2726. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2727. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2728. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2729. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2730. [arr_name addObject:customer_first_name];
  2731. [arr_name addObject:customer_last_name];
  2732. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2733. // default ship from
  2734. 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';";
  2735. __block NSString *cid = @"";
  2736. __block NSString *name = @"";
  2737. __block NSString *ext = @"";
  2738. __block NSString *contact = @"";
  2739. __block NSString *email = @"";
  2740. __block NSString *fax = @"";
  2741. __block NSString *phone = @"";
  2742. sqlite3 *db = [iSalesDB get_db];
  2743. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2744. cid = [self textAtColumn:0 statement:statment];
  2745. name = [self textAtColumn:1 statement:statment];
  2746. ext = [self textAtColumn:2 statement:statment];
  2747. contact = [self textAtColumn:3 statement:statment];
  2748. email = [self textAtColumn:4 statement:statment];
  2749. fax = [self textAtColumn:5 statement:statment];
  2750. phone = [self textAtColumn:6 statement:statment];
  2751. }];
  2752. NSString* so_id = [self get_offline_soid:db];
  2753. if(so_id==nil)
  2754. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2755. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2756. int result =[iSalesDB execSql:sql_neworder db:db];
  2757. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2758. //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'
  2759. //soId
  2760. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2761. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2762. sqlite3_stmt * statement;
  2763. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2764. {
  2765. if (sqlite3_step(statement) == SQLITE_ROW)
  2766. {
  2767. // char *name = (char*)sqlite3_column_text(statement, 1);
  2768. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2769. //ret = sqlite3_column_int(statement, 0);
  2770. char *soId = (char*)sqlite3_column_text(statement, 0);
  2771. if(soId==nil)
  2772. soId="";
  2773. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2774. [ret setValue:nssoId forKey:@"soId"];
  2775. [ret setValue:nssoId forKey:@"orderCode"];
  2776. }
  2777. sqlite3_finalize(statement);
  2778. }
  2779. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2780. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2781. [iSalesDB close_db:db];
  2782. return [RAUtils dict2data:ret];
  2783. }
  2784. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2785. {
  2786. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2787. NSString* orderCode = [params valueForKey:@"orderCode"];
  2788. UIApplication * app = [UIApplication sharedApplication];
  2789. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2790. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2791. {
  2792. appDelegate.disable_trigger=true;
  2793. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2794. appDelegate.disable_trigger=false;
  2795. }
  2796. sqlite3 *db = [iSalesDB get_db];
  2797. int cart_count=[self query_ordercartcount:orderCode db:db];
  2798. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2799. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2800. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2801. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2802. [iSalesDB close_db:db];
  2803. return [RAUtils dict2data:ret];
  2804. }
  2805. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2806. {
  2807. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2808. sqlite3 *db = [iSalesDB get_db];
  2809. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2810. int count =0;
  2811. if(params[@"count"]!=nil)
  2812. {
  2813. count = [params[@"count"] intValue];
  2814. }
  2815. NSString* product_id=params[@"product_id"];
  2816. NSString* orderCode=params[@"orderCode"];
  2817. NSString *qty = params[@"qty"];
  2818. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2819. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2820. for(int i=0;i<arr_id.count;i++)
  2821. {
  2822. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2823. int item_qty= count;
  2824. if (qty) {
  2825. item_qty = [qty_arr[i] integerValue];
  2826. }
  2827. if(item_qty==0)
  2828. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2829. NSString* sql=nil;
  2830. sqlite3_stmt *stmt;
  2831. if(_id<0)
  2832. {
  2833. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2834. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2835. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2836. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2837. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2838. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2839. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2840. }
  2841. else
  2842. {
  2843. if (qty) {
  2844. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2845. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2846. sqlite3_bind_int(stmt, 1, _id);
  2847. } else {
  2848. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2849. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2850. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2851. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2852. }
  2853. }
  2854. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2855. [iSalesDB execSql:@"ROLLBACK" db:db];
  2856. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2857. [iSalesDB close_db:db];
  2858. DebugLog(@"add to cart error");
  2859. return [RAUtils dict2data:ret];
  2860. }
  2861. }
  2862. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2863. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2864. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2865. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2866. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2867. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2868. [iSalesDB close_db:db];
  2869. return [RAUtils dict2data:ret];
  2870. }
  2871. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2872. {
  2873. UIApplication * app = [UIApplication sharedApplication];
  2874. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2875. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2876. 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];
  2877. sqlite3_stmt * statement;
  2878. int count=0;
  2879. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2880. {
  2881. while (sqlite3_step(statement) == SQLITE_ROW)
  2882. {
  2883. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2884. int bitem_id = sqlite3_column_int(statement, 0);
  2885. int bitem_qty = sqlite3_column_int(statement, 1);
  2886. char *name = (char*)sqlite3_column_text(statement, 2);
  2887. if(name==nil)
  2888. name="";
  2889. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2890. char *description = (char*)sqlite3_column_text(statement, 3);
  2891. if(description==nil)
  2892. description="";
  2893. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2894. double unit_price = sqlite3_column_double(statement, 4);
  2895. int use_unitprice = sqlite3_column_int(statement, 5);
  2896. if(use_unitprice!=1)
  2897. {
  2898. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2899. }
  2900. itemjson[@"model"]=nsname;
  2901. itemjson[@"description"]=nsdescription;
  2902. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2903. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2904. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2905. if(compute)
  2906. {
  2907. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2908. }
  2909. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2910. count++;
  2911. }
  2912. sqlite3_finalize(statement);
  2913. }
  2914. ret[@"count"]=@(count);
  2915. if(count==0)
  2916. return nil;
  2917. else
  2918. return ret;
  2919. }
  2920. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2921. {
  2922. //compute: add part to subtotal;
  2923. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2924. dict_item[@(item_id)]=@"1";
  2925. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2926. double cuft=0;
  2927. double weight=0;
  2928. int carton=0;
  2929. 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];
  2930. sqlite3_stmt * statement;
  2931. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2932. {
  2933. if (sqlite3_step(statement) == SQLITE_ROW)
  2934. {
  2935. double ulength = sqlite3_column_double(statement, 0);
  2936. double uwidth = sqlite3_column_double(statement, 1);
  2937. double uheight = sqlite3_column_double(statement, 2);
  2938. double uweight = sqlite3_column_double(statement, 3);
  2939. double mlength = sqlite3_column_double(statement, 4);
  2940. double mwidth = sqlite3_column_double(statement, 5);
  2941. double mheight = sqlite3_column_double(statement, 6);
  2942. double mweight = sqlite3_column_double(statement, 7);
  2943. double ilength = sqlite3_column_double(statement, 8);
  2944. double iwidth = sqlite3_column_double(statement, 9);
  2945. double iheight = sqlite3_column_double(statement, 10);
  2946. double iweight = sqlite3_column_double(statement, 11);
  2947. int pcs = sqlite3_column_int(statement,12);
  2948. int mpack = sqlite3_column_int(statement, 13);
  2949. int ipack = sqlite3_column_int(statement, 14);
  2950. double ucbf = sqlite3_column_double(statement, 15);
  2951. double icbf = sqlite3_column_double(statement, 16);
  2952. double mcbf = sqlite3_column_double(statement, 17);
  2953. if(ipack==0)
  2954. {
  2955. carton= count/mpack ;
  2956. weight = mweight*carton;
  2957. cuft= carton*(mlength*mwidth*mheight);
  2958. int remain=count%mpack;
  2959. if(remain==0)
  2960. {
  2961. //do nothing;
  2962. }
  2963. else
  2964. {
  2965. carton++;
  2966. weight += uweight*remain;
  2967. cuft += (ulength*uwidth*uheight)*remain;
  2968. }
  2969. }
  2970. else
  2971. {
  2972. carton = count/(mpack*ipack);
  2973. weight = mweight*carton;
  2974. cuft= carton*(mlength*mwidth*mheight);
  2975. int remain=count%(mpack*ipack);
  2976. if(remain==0)
  2977. {
  2978. // do nothing;
  2979. }
  2980. else
  2981. {
  2982. carton++;
  2983. int icarton =remain/ipack;
  2984. int iremain=remain%ipack;
  2985. weight += iweight*icarton;
  2986. cuft += (ilength*iwidth*iheight)*icarton;
  2987. if(iremain==0)
  2988. {
  2989. //do nothing;
  2990. }
  2991. else
  2992. {
  2993. weight += uweight*iremain;
  2994. cuft += (ulength*uwidth*uheight)*iremain;
  2995. }
  2996. }
  2997. }
  2998. #ifdef BUILD_NPD
  2999. cuft=ucbf*count;
  3000. weight= uweight*count;
  3001. #endif
  3002. }
  3003. sqlite3_finalize(statement);
  3004. }
  3005. if(compute)
  3006. {
  3007. NSArray * arr_count=nil;
  3008. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3009. for(int i=0;i<arr_bundle.count;i++)
  3010. {
  3011. dict_item[arr_bundle[i]]=@"1";
  3012. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3013. cuft+=[bundlejson[@"cuft"] doubleValue];
  3014. weight+=[bundlejson[@"weight"] doubleValue];
  3015. carton+=[bundlejson[@"carton"] intValue];
  3016. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3017. }
  3018. }
  3019. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3020. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3021. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3022. ret[@"items"]=dict_item;
  3023. return ret;
  3024. }
  3025. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3026. {
  3027. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3028. // 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 ];
  3029. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3030. DebugLog(@"offline_login sql:%@",sqlQuery);
  3031. sqlite3_stmt * statement;
  3032. int cart_count=0;
  3033. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3034. if ( dbresult== SQLITE_OK)
  3035. {
  3036. while (sqlite3_step(statement) == SQLITE_ROW)
  3037. {
  3038. int item_id = sqlite3_column_int(statement, 0);
  3039. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3040. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3041. }
  3042. sqlite3_finalize(statement);
  3043. }
  3044. return cart_count;
  3045. }
  3046. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3047. {
  3048. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3049. sqlite3 *db = [iSalesDB get_db];
  3050. UIApplication * app = [UIApplication sharedApplication];
  3051. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3052. NSString* orderCode=params[@"orderCode"];
  3053. int sort = [[params objectForKey:@"sort"] intValue];
  3054. NSString *sort_str = @"";
  3055. switch (sort) {
  3056. case 0:{
  3057. sort_str = @"order by c.modify_time desc";
  3058. }
  3059. break;
  3060. case 1:{
  3061. sort_str = @"order by c.modify_time asc";
  3062. }
  3063. break;
  3064. case 2:{
  3065. sort_str = @"order by m.name asc";
  3066. }
  3067. break;
  3068. case 3:{
  3069. sort_str = @"order by m.name desc";
  3070. }
  3071. break;
  3072. case 4:{
  3073. sort_str = @"order by m.description asc";
  3074. }
  3075. break;
  3076. default:
  3077. break;
  3078. }
  3079. 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 ];
  3080. // 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 ];
  3081. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3082. DebugLog(@"offline_login sql:%@",sqlQuery);
  3083. sqlite3_stmt * statement;
  3084. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3085. NSDate *date1 = [NSDate date];
  3086. int count=0;
  3087. int cart_count=0;
  3088. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3089. if ( dbresult== SQLITE_OK)
  3090. {
  3091. while (sqlite3_step(statement) == SQLITE_ROW)
  3092. {
  3093. NSDate *row_date = [NSDate date];
  3094. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3095. int product_id = sqlite3_column_int(statement, 0);
  3096. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3097. int item_id = sqlite3_column_int(statement, 7);
  3098. NSString* Price=nil;
  3099. if(str_price==nil)
  3100. {
  3101. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3102. // NSDate *price_date = [NSDate date];
  3103. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3104. // DebugLog(@"price time interval");
  3105. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3106. if(price==nil)
  3107. Price=@"No Price.";
  3108. else
  3109. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3110. }
  3111. else
  3112. {
  3113. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3114. }
  3115. double discount = sqlite3_column_double(statement, 2);
  3116. int item_count = sqlite3_column_int(statement, 3);
  3117. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3118. NSString *nsline_note=nil;
  3119. if(line_note!=nil)
  3120. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3121. char *name = (char*)sqlite3_column_text(statement, 5);
  3122. NSString *nsname=nil;
  3123. if(name!=nil)
  3124. nsname= [[NSString alloc]initWithUTF8String:name];
  3125. char *description = (char*)sqlite3_column_text(statement, 6);
  3126. NSString *nsdescription=nil;
  3127. if(description!=nil)
  3128. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3129. int stockUom = sqlite3_column_int(statement, 8);
  3130. int _id = sqlite3_column_int(statement, 9);
  3131. // NSDate *subtotal_date = [NSDate date];
  3132. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3133. // DebugLog(@"subtotal_date time interval");
  3134. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3135. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3136. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3137. int carton=[bsubtotaljson[@"carton"] intValue];
  3138. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3139. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3140. // NSDate *img_date = [NSDate date];
  3141. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3142. // DebugLog(@"img_date time interval");
  3143. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3144. itemjson[@"model"]=nsname;
  3145. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3146. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3147. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3148. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3149. itemjson[@"check"]=@"true";
  3150. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3151. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3152. itemjson[@"unit_price"]=Price;
  3153. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3154. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3155. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3156. itemjson[@"note"]=nsline_note;
  3157. // NSDate *date2 = [NSDate date];
  3158. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3159. // DebugLog(@"model_bundle time interval");
  3160. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3161. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3162. count++;
  3163. // DebugLog(@"row time interval");
  3164. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3165. }
  3166. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3167. sqlite3_finalize(statement);
  3168. }
  3169. DebugLog(@"request cart total time interval");
  3170. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3171. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3172. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3173. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3174. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3175. ret[@"count"]=[NSNumber numberWithInt:count ];
  3176. ret[@"mode"]=@"Regular Mode";
  3177. [iSalesDB close_db:db];
  3178. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3179. DebugLog(@"general notes :%@",general_note);
  3180. ret[@"general_note"]= general_note;
  3181. return [RAUtils dict2data:ret];
  3182. }
  3183. +(NSData*) offline_login :(NSMutableDictionary *) params
  3184. {
  3185. NSString* user = [params valueForKey:@"user"];
  3186. NSString* password = [params valueForKey:@"password"];
  3187. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3188. sqlite3 *db = [iSalesDB get_db];
  3189. 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"]];
  3190. DebugLog(@"offline_login sql:%@",sqlQuery);
  3191. sqlite3_stmt * statement;
  3192. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3194. {
  3195. if (sqlite3_step(statement) == SQLITE_ROW)
  3196. {
  3197. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3198. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3199. int can_show_price = sqlite3_column_int(statement, 0);
  3200. int can_see_price = sqlite3_column_int(statement, 1);
  3201. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3202. if(contact_id==nil)
  3203. contact_id="";
  3204. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3205. int user_type = sqlite3_column_int(statement, 3);
  3206. int can_cancel_order = sqlite3_column_int(statement, 4);
  3207. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3208. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3209. int can_delete_order = sqlite3_column_int(statement, 7);
  3210. int can_submit_order = sqlite3_column_int(statement, 8);
  3211. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3212. int can_create_order = sqlite3_column_int(statement, 10);
  3213. char *mode = (char*)sqlite3_column_text(statement, 11);
  3214. if(mode==nil)
  3215. mode="";
  3216. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3217. char *username = (char*)sqlite3_column_text(statement, 12);
  3218. if(username==nil)
  3219. username="";
  3220. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3221. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3222. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3223. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3224. [header setValue:nscontact_id forKey:@"contact_id"];
  3225. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3226. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3227. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3228. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3229. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3230. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3231. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3232. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3233. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3234. [header setValue:nsusername forKey:@"username"];
  3235. NSError* error=nil;
  3236. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3237. [header setValue:statusFilter forKey:@"statusFilter"];
  3238. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3239. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3240. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3241. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3242. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3243. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3244. [ret setObject:header forKey:@"header"];
  3245. [ret setValue:nsmode forKey:@"mode"];
  3246. }
  3247. sqlite3_finalize(statement);
  3248. }
  3249. [iSalesDB close_db:db];
  3250. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3251. return [RAUtils dict2data:ret];
  3252. }
  3253. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3254. {
  3255. NSString* contactId = [params valueForKey:@"contactId"];
  3256. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3257. sqlite3 *db = [iSalesDB get_db];
  3258. NSString *sqlQuery = nil;
  3259. {
  3260. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  3261. }
  3262. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3263. sqlite3_stmt * statement;
  3264. [ret setValue:@"2" forKey:@"result"];
  3265. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3266. {
  3267. //int i = 0;
  3268. if (sqlite3_step(statement) == SQLITE_ROW)
  3269. {
  3270. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3271. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3272. // int editable = sqlite3_column_int(statement, 0);
  3273. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3274. NSString *nscompany_name =nil;
  3275. if(company_name==nil)
  3276. nscompany_name=@"";
  3277. else
  3278. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3279. char *country = (char*)sqlite3_column_text(statement, 2);
  3280. if(country==nil)
  3281. country="";
  3282. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3283. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3284. // if(addr==nil)
  3285. // addr="";
  3286. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3287. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3288. if(zipcode==nil)
  3289. zipcode="";
  3290. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3291. char *state = (char*)sqlite3_column_text(statement, 5);
  3292. if(state==nil)
  3293. state="";
  3294. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3295. char *city = (char*)sqlite3_column_text(statement, 6);
  3296. if(city==nil)
  3297. city="";
  3298. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3299. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3300. // NSString *nscontact_name = nil;
  3301. // if(contact_name==nil)
  3302. // nscontact_name=@"";
  3303. // else
  3304. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3305. char *phone = (char*)sqlite3_column_text(statement, 8);
  3306. NSString *nsphone = nil;
  3307. if(phone==nil)
  3308. nsphone=@"";
  3309. else
  3310. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3311. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3312. if(contact_id==nil)
  3313. contact_id="";
  3314. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3315. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3316. if(addr_1==nil)
  3317. addr_1="";
  3318. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3319. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3320. if(addr_2==nil)
  3321. addr_2="";
  3322. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3323. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3324. if(addr_3==nil)
  3325. addr_3="";
  3326. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3327. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3328. if(addr_4==nil)
  3329. addr_4="";
  3330. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3331. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3332. if(first_name==nil)
  3333. first_name="";
  3334. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3335. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3336. if(last_name==nil)
  3337. last_name="";
  3338. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3339. char *fax = (char*)sqlite3_column_text(statement, 16);
  3340. NSString *nsfax = nil;
  3341. if(fax==nil)
  3342. nsfax=@"";
  3343. else
  3344. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3345. char *email = (char*)sqlite3_column_text(statement, 17);
  3346. NSString *nsemail = nil;
  3347. if(email==nil)
  3348. nsemail=@"";
  3349. else
  3350. nsemail= [[NSString alloc]initWithUTF8String:email];
  3351. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3352. NSString *nsimg_0 = nil;
  3353. if(img_0==nil)
  3354. nsimg_0=@"";
  3355. else
  3356. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3357. [self copy_bcardImg:nsimg_0];
  3358. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3359. NSString *nsimg_1 = nil;
  3360. if(img_1==nil)
  3361. nsimg_1=@"";
  3362. else
  3363. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3364. [self copy_bcardImg:nsimg_1];
  3365. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3366. NSString *nsimg_2 = nil;
  3367. if(img_2==nil)
  3368. nsimg_2=@"";
  3369. else
  3370. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3371. [self copy_bcardImg:nsimg_2];
  3372. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3373. NSString *nsprice_type = nil;
  3374. if(price_type==nil)
  3375. nsprice_type=@"";
  3376. else
  3377. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3378. char *notes = (char*)sqlite3_column_text(statement, 22);
  3379. NSString *nsnotes = nil;
  3380. if(notes==nil)
  3381. nsnotes=@"";
  3382. else
  3383. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3384. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3385. NSString *nssalesrep = nil;
  3386. if(salesrep==nil)
  3387. nssalesrep=@"";
  3388. else
  3389. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3390. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3391. {
  3392. // decrypt
  3393. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3394. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3395. nsphone=[AESCrypt fastdecrypt:nsphone];
  3396. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3397. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3398. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3399. }
  3400. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3401. [arr_name addObject:nsfirst_name];
  3402. [arr_name addObject:nslast_name];
  3403. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3404. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3405. [arr_ext addObject:nsaddr_1];
  3406. [arr_ext addObject:nsaddr_2];
  3407. [arr_ext addObject:nsaddr_3];
  3408. [arr_ext addObject:nsaddr_4];
  3409. [arr_ext addObject:@"\r\n"];
  3410. [arr_ext addObject:nscity];
  3411. [arr_ext addObject:nsstate];
  3412. [arr_ext addObject:nszipcode];
  3413. [arr_ext addObject:nscountry];
  3414. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3415. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3416. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3417. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3418. [item setValue:nscountry forKey:@"customer_country"];
  3419. [item setValue:nsphone forKey:@"customer_phone"];
  3420. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3421. [item setValue:nscompany_name forKey:@"customer_name"];
  3422. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3423. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3424. [item setValue:nsext forKey:@"customer_contact_ext"];
  3425. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3426. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3427. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3428. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3429. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3430. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3431. [item setValue:nslast_name forKey:@"customer_last_name"];
  3432. [item setValue:nscity forKey:@"customer_city"];
  3433. [item setValue:nsstate forKey:@"customer_state"];
  3434. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3435. [item setValue:contactId forKey:@"customer_cid"];
  3436. [item setValue:nscontact_name forKey:@"customer_contact"];
  3437. [item setValue:nsfax forKey:@"customer_fax"];
  3438. [item setValue:nsemail forKey:@"customer_email"];
  3439. [item setValue:contact_type forKey:@"customer_contact_type"];
  3440. [ret setObject:item forKey:@"customerInfo"];
  3441. // i++;
  3442. }
  3443. UIApplication * app = [UIApplication sharedApplication];
  3444. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3445. [ret setValue:appDelegate.mode forKey:@"mode"];
  3446. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3447. sqlite3_finalize(statement);
  3448. }
  3449. [iSalesDB close_db:db];
  3450. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3451. return ret;
  3452. }
  3453. + (bool) copy_bcardImg:(NSString*) filename
  3454. {
  3455. if(filename.length==0)
  3456. return false;
  3457. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3458. bool ret=false;
  3459. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3460. NSString *cachefolder = [paths objectAtIndex:0];
  3461. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3462. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3463. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3464. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3465. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3466. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3467. //
  3468. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3469. NSFileManager* fileManager = [NSFileManager defaultManager];
  3470. BOOL bdir=NO;
  3471. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3472. {
  3473. NSError *error = nil;
  3474. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3475. {
  3476. ret=false;
  3477. }
  3478. else
  3479. {
  3480. ret=true;
  3481. }
  3482. // NSError *error = nil;
  3483. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3484. //
  3485. // if(!bsuccess)
  3486. // {
  3487. // DebugLog(@"Create offline_createimg folder failed");
  3488. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3489. // return [RAUtils dict2data:ret];
  3490. // }
  3491. // if(bsuccess)
  3492. // {
  3493. // sqlite3 *db = [self get_db];
  3494. //
  3495. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3496. // [iSalesDB close_db:db];
  3497. // }
  3498. }
  3499. return ret;
  3500. //
  3501. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3502. // if(bsuccess)
  3503. // {
  3504. // NSError *error = nil;
  3505. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3506. // {
  3507. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3508. // }
  3509. // else
  3510. // {
  3511. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3512. //
  3513. // ret[@"img_url_aname"]=filename;
  3514. // ret[@"img_url"]=savedImagePath;
  3515. // }
  3516. // }
  3517. }
  3518. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3519. {
  3520. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3521. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3522. NSString *cachefolder = [paths objectAtIndex:0];
  3523. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3524. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3525. NSFileManager* fileManager = [NSFileManager defaultManager];
  3526. BOOL bdir=YES;
  3527. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3528. {
  3529. NSError *error = nil;
  3530. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3531. if(!bsuccess)
  3532. {
  3533. DebugLog(@"Create offline_createimg folder failed");
  3534. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3535. return [RAUtils dict2data:ret];
  3536. }
  3537. // if(bsuccess)
  3538. // {
  3539. // sqlite3 *db = [self get_db];
  3540. //
  3541. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3542. // [iSalesDB close_db:db];
  3543. // }
  3544. }
  3545. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3546. //JEPG格式
  3547. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3548. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3549. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3550. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3551. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3552. if(bsuccess)
  3553. {
  3554. NSError *error = nil;
  3555. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3556. {
  3557. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3558. }
  3559. else
  3560. {
  3561. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3562. ret[@"img_url_aname"]=filename;
  3563. ret[@"img_url"]=filename;
  3564. }
  3565. }
  3566. else
  3567. {
  3568. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3569. }
  3570. return [RAUtils dict2data:ret];
  3571. }
  3572. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3573. {
  3574. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3575. category = [category substringToIndex:3];
  3576. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3577. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3578. params[@"category"]= category;
  3579. ret[@"params"]= params;
  3580. [ret setValue:@"detail" forKey:@"target"];
  3581. [ret setValue:@"popup" forKey:@"action"];
  3582. [ret setValue:@"content" forKey:@"type"];
  3583. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3584. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3585. [ret setValue:@"true" forKey:@"single_row"];
  3586. [ret setValue:@"true" forKey:@"partial_refresh"];
  3587. // sqlite3 *db = [iSalesDB get_db];
  3588. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3589. 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 ;
  3590. sqlite3_stmt * statement;
  3591. int count = 0;
  3592. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3593. // int count=0;
  3594. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3595. {
  3596. int i=0;
  3597. while (sqlite3_step(statement) == SQLITE_ROW)
  3598. {
  3599. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3600. // char *name = (char*)sqlite3_column_text(statement, 1);
  3601. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3602. char *name = (char*)sqlite3_column_text(statement, 0);
  3603. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3604. int product_id = sqlite3_column_int(statement, 1);
  3605. char *url = (char*)sqlite3_column_text(statement, 2);
  3606. if(url==nil)
  3607. url="";
  3608. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3609. [item setValue:nsurl forKey:@"picture_path"];
  3610. [item setValue:nsname forKey:@"fash_name"];
  3611. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3612. [item setValue:category forKey:@"category"];
  3613. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3614. i++;
  3615. }
  3616. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3617. sqlite3_finalize(statement);
  3618. }
  3619. DebugLog(@"count:%d",count);
  3620. // [iSalesDB close_db:db];
  3621. return ret;
  3622. }
  3623. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3624. {
  3625. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3626. [ret setValue:key forKey:@"key"];
  3627. [ret setValue:value forKey:@"val"];
  3628. [ret setValue:@"price" forKey:@"type"];
  3629. return ret;
  3630. }
  3631. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3632. {
  3633. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3634. [ret setValue:key forKey:@"key"];
  3635. [ret setValue:value forKey:@"val"];
  3636. return ret;
  3637. }
  3638. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3639. {
  3640. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3641. [ret setValue:@"0" forKey:@"img_count"];
  3642. // sqlite3 *db = [iSalesDB get_db];
  3643. 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 ;
  3644. sqlite3_stmt * statement;
  3645. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3646. {
  3647. int i=0;
  3648. if (sqlite3_step(statement) == SQLITE_ROW)
  3649. {
  3650. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3651. // char *name = (char*)sqlite3_column_text(statement, 1);
  3652. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3653. char *value = (char*)sqlite3_column_text(statement, 0);
  3654. if(value==nil)
  3655. value="";
  3656. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3657. char *key = (char*)sqlite3_column_text(statement, 1);
  3658. if(key==nil)
  3659. key="";
  3660. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3661. [item setValue:nsvalue forKey:@"val"];
  3662. [item setValue:nskey forKey:@"key"];
  3663. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3664. [ret setValue:@"1" forKey:@"count"];
  3665. i++;
  3666. }
  3667. sqlite3_finalize(statement);
  3668. }
  3669. // [iSalesDB close_db:db];
  3670. return ret;
  3671. }
  3672. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3673. {
  3674. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3675. [ret setValue:@"0" forKey:@"count"];
  3676. // sqlite3 *db = [iSalesDB get_db];
  3677. 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;
  3678. sqlite3_stmt * statement;
  3679. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3680. {
  3681. int i=0;
  3682. while (sqlite3_step(statement) == SQLITE_ROW)
  3683. {
  3684. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3685. // char *name = (char*)sqlite3_column_text(statement, 1);
  3686. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3687. char *value = (char*)sqlite3_column_text(statement, 0);
  3688. if(value==nil)
  3689. value="";
  3690. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3691. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3692. if(selector_display==nil)
  3693. selector_display="";
  3694. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3695. int product_id = sqlite3_column_int(statement, 2);
  3696. char *category = (char*)sqlite3_column_text(statement, 3);
  3697. if(category==nil)
  3698. category="";
  3699. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3700. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3701. [item setValue:nsvalue forKey:@"title"];
  3702. [item setValue:url forKey:@"pic_url"];
  3703. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3704. [params setValue:@"2" forKey:@"count"];
  3705. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3706. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3707. [param0 setValue:@"product_id" forKey:@"name"];
  3708. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3709. [param1 setValue:nscategory forKey:@"val"];
  3710. [param1 setValue:@"category" forKey:@"name"];
  3711. [params setObject:param0 forKey:@"param_0"];
  3712. [params setObject:param1 forKey:@"param_1"];
  3713. [item setObject:params forKey:@"params"];
  3714. [ret setValue:nsselector_display forKey:@"name"];
  3715. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3716. i++;
  3717. }
  3718. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3719. [ret setValue:@"switch" forKey:@"action"];
  3720. sqlite3_finalize(statement);
  3721. }
  3722. // [iSalesDB close_db:db];
  3723. return ret;
  3724. }
  3725. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3726. {
  3727. // model 在 category search 显示的图片。
  3728. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3729. // sqlite3 *db = [iSalesDB get_db];
  3730. 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];
  3731. sqlite3_stmt * statement;
  3732. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3733. {
  3734. while (sqlite3_step(statement) == SQLITE_ROW)
  3735. {
  3736. char *url = (char*)sqlite3_column_text(statement, 0);
  3737. if(url==nil)
  3738. url="";
  3739. int type = sqlite3_column_int(statement, 1);
  3740. if(type==0)
  3741. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3742. else
  3743. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3744. }
  3745. sqlite3_finalize(statement);
  3746. }
  3747. // [iSalesDB close_db:db];
  3748. return ret;
  3749. }
  3750. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3751. {
  3752. int item_id=-1;
  3753. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3754. sqlite3_stmt * statement;
  3755. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3756. {
  3757. if (sqlite3_step(statement) == SQLITE_ROW)
  3758. {
  3759. item_id = sqlite3_column_int(statement, 0);
  3760. }
  3761. sqlite3_finalize(statement);
  3762. }
  3763. return item_id;
  3764. }
  3765. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3766. {
  3767. // NSString* ret = @"";
  3768. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3769. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3770. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3771. sqlite3_stmt * statement;
  3772. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3773. {
  3774. while (sqlite3_step(statement) == SQLITE_ROW)
  3775. {
  3776. int bitem_id = sqlite3_column_int(statement, 0);
  3777. int bitem_qty = sqlite3_column_int(statement, 1);
  3778. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3779. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3780. }
  3781. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3782. sqlite3_finalize(statement);
  3783. }
  3784. // if(ret==nil)
  3785. // ret=@"";
  3786. *count=arr_count;
  3787. return arr_bundle;
  3788. }
  3789. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3790. {
  3791. // get default sold qty, return -1 if model not found;
  3792. int ret = -1;
  3793. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3794. sqlite3_stmt * statement;
  3795. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3796. {
  3797. if (sqlite3_step(statement) == SQLITE_ROW)
  3798. {
  3799. ret = sqlite3_column_int(statement, 0);
  3800. }
  3801. sqlite3_finalize(statement);
  3802. }
  3803. return ret;
  3804. }
  3805. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3806. {
  3807. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3808. [ret setValue:@"0" forKey:@"img_count"];
  3809. // sqlite3 *db = [iSalesDB get_db];
  3810. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3811. sqlite3_stmt * statement;
  3812. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3813. {
  3814. int i=0;
  3815. while (sqlite3_step(statement) == SQLITE_ROW)
  3816. {
  3817. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3818. // char *name = (char*)sqlite3_column_text(statement, 1);
  3819. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3820. char *url = (char*)sqlite3_column_text(statement, 0);
  3821. if(url==nil)
  3822. url="";
  3823. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3824. [item setValue:nsurl forKey:@"s"];
  3825. [item setValue:nsurl forKey:@"l"];
  3826. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3827. i++;
  3828. }
  3829. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3830. sqlite3_finalize(statement);
  3831. }
  3832. // [iSalesDB close_db:db];
  3833. return ret;
  3834. }
  3835. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3836. UIApplication * app = [UIApplication sharedApplication];
  3837. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3838. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3839. sqlite3 *db = [iSalesDB get_db];
  3840. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3841. NSString* product_id=params[@"product_id"];
  3842. NSString *item_count_str = params[@"item_count"];
  3843. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3844. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3845. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3846. // NSString *sql = @"";
  3847. for(int i=0;i<arr.count;i++)
  3848. {
  3849. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3850. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3851. __block int cart_count = 0;
  3852. if (!item_count_str) {
  3853. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3854. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3855. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3856. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3857. cart_count = [[model_set_components lastObject] integerValue];
  3858. }];
  3859. }
  3860. if(count==0)
  3861. {
  3862. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3863. sqlite3_stmt *stmt;
  3864. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3865. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3866. if (item_count_arr) {
  3867. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3868. } else {
  3869. sqlite3_bind_int(stmt,2,cart_count);
  3870. }
  3871. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3872. [iSalesDB execSql:@"ROLLBACK" db:db];
  3873. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3874. [iSalesDB close_db:db];
  3875. DebugLog(@"add to wishlist error");
  3876. return ret;
  3877. }
  3878. } else {
  3879. int qty = 0;
  3880. if (item_count_arr) {
  3881. qty = [item_count_arr[i] integerValue];
  3882. } else {
  3883. qty = cart_count;
  3884. }
  3885. 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]];
  3886. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3887. [iSalesDB execSql:@"ROLLBACK" db:db];
  3888. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3889. [iSalesDB close_db:db];
  3890. DebugLog(@"add to wishlist error");
  3891. return ret;
  3892. }
  3893. }
  3894. }
  3895. // [iSalesDB execSql:sql db:db];
  3896. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3897. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3898. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3899. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3900. [iSalesDB close_db:db];
  3901. appDelegate.wish_count =count;
  3902. [appDelegate update_count_mark];
  3903. ret[@"result"]= [NSNumber numberWithInt:2];
  3904. return ret;
  3905. }
  3906. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3907. // 0 category
  3908. // 1 search
  3909. // 2 itemsearch
  3910. NSData *ret = nil;
  3911. NSDictionary *items = nil;
  3912. switch (from) {
  3913. case 0:{
  3914. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3915. }
  3916. break;
  3917. case 1:{
  3918. items = [[self search:params limited:NO] objectForKey:@"items"];
  3919. }
  3920. break;
  3921. case 2:{
  3922. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3923. }
  3924. break;
  3925. default:
  3926. break;
  3927. }
  3928. if (!items) {
  3929. return ret;
  3930. }
  3931. int count = [[items objectForKey:@"count"] intValue];
  3932. NSMutableString *product_id_str = [@"" mutableCopy];
  3933. for (int i = 0; i < count; i++) {
  3934. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3935. NSDictionary *item = [items objectForKey:key];
  3936. NSString *product_id = [item objectForKey:@"product_id"];
  3937. if (i == 0) {
  3938. [product_id_str appendString:product_id];
  3939. } else {
  3940. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3941. }
  3942. }
  3943. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3944. if ([add_to isEqualToString:@"cart"]) {
  3945. NSString *order_code = [params objectForKey:@"orderCode"];
  3946. if (order_code.length) {
  3947. NSDictionary *newParams = @{
  3948. @"product_id" : product_id_str,
  3949. @"orderCode" : order_code
  3950. };
  3951. ret = [self offline_add2cart:newParams.mutableCopy];
  3952. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3953. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3954. ret = [RAUtils dict2data:retDic];
  3955. }
  3956. } else if([add_to isEqualToString:@"wishlist"]) {
  3957. NSDictionary *newParams = @{
  3958. @"product_id" : product_id_str
  3959. };
  3960. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3961. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3962. ret = [RAUtils dict2data:retDic];
  3963. } else if([add_to isEqualToString:@"portfolio"]) {
  3964. }
  3965. return ret;
  3966. }
  3967. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3968. {
  3969. return [self addAll:params from:0];
  3970. }
  3971. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3972. {
  3973. return [self addAll:params from:1];
  3974. }
  3975. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3976. {
  3977. return [self addAll:params from:2];
  3978. }
  3979. #pragma mark - Jack
  3980. #warning 做SQL操作时转义!!
  3981. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3982. // "val_227" : {
  3983. // "check" : 1,
  3984. // "value" : "US United States",
  3985. // "value_id" : "228"
  3986. // },
  3987. if (!countryCode) {
  3988. countryCode = @"US";
  3989. }
  3990. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3991. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3992. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3993. int code_id = sqlite3_column_int(stmt, 3); // id
  3994. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3995. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3996. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3997. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3998. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3999. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4000. }
  4001. long n = *count;
  4002. *count = n + 1;
  4003. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4004. [container setValue:countryDic forKey:key];
  4005. }] mutableCopy];
  4006. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4007. return ret;
  4008. }
  4009. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4010. countryCode = [self translateSingleQuote:countryCode];
  4011. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4012. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4013. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4014. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4015. if (name == NULL) {
  4016. name = "";
  4017. }
  4018. if (code == NULL) {
  4019. code = "";
  4020. }
  4021. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4022. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4023. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4024. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4025. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4026. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4027. }
  4028. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4029. [container setValue:stateDic forKey:key];
  4030. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4031. DebugLog(@"query all state error: %@",err_msg);
  4032. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4033. // [stateDic setValue:@"Other" forKey:@"value"];
  4034. // [stateDic setValue:@"" forKey:@"value_id"];
  4035. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4036. //
  4037. // if (state_code && [@"" isEqualToString:state_code]) {
  4038. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4039. // }
  4040. //
  4041. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4042. // [container setValue:stateDic forKey:key];
  4043. }] mutableCopy];
  4044. [ret removeObjectForKey:@"result"];
  4045. // failure 可以不用了,一样的
  4046. if (ret.allKeys.count == 0) {
  4047. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4048. [stateDic setValue:@"Other" forKey:@"value"];
  4049. [stateDic setValue:@"" forKey:@"value_id"];
  4050. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4051. if (state_code && [@"" isEqualToString:state_code]) {
  4052. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4053. }
  4054. NSString *key = [NSString stringWithFormat:@"val_0"];
  4055. [ret setValue:stateDic forKey:key];
  4056. }
  4057. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4058. return ret;
  4059. }
  4060. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4061. codeId = [self translateSingleQuote:codeId];
  4062. 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];
  4063. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4064. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4065. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4066. if (name == NULL) {
  4067. name = "";
  4068. }
  4069. if (code == NULL) {
  4070. code = "";
  4071. }
  4072. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4073. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4074. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4075. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4076. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4077. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4078. }
  4079. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4080. [container setValue:stateDic forKey:key];
  4081. }] mutableCopy];
  4082. [ret removeObjectForKey:@"result"];
  4083. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4084. return ret;
  4085. }
  4086. + (NSDictionary *)offline_getPrice {
  4087. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  4088. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4089. char *name = (char *) sqlite3_column_text(stmt, 1);
  4090. int type = sqlite3_column_int(stmt, 2);
  4091. int orderBy = sqlite3_column_int(stmt, 3);
  4092. if (name == NULL) {
  4093. name = "";
  4094. }
  4095. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4096. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4097. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4098. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4099. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4100. if (orderBy == 0) {
  4101. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4102. }
  4103. [container setValue:priceDic forKey:key];
  4104. }] mutableCopy];
  4105. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4106. return ret;
  4107. }
  4108. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4109. NSString *sql = @"select _id,type_name from contact_type";
  4110. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4111. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4112. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4113. int _id = sqlite3_column_int(stmt, 0);
  4114. NSString *name = [self textAtColumn:1 statement:stmt];
  4115. NSDictionary *typeDic = @{
  4116. @"value_id" : name,
  4117. @"value" : name,
  4118. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4119. };
  4120. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4121. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4122. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4123. }];
  4124. return ret;
  4125. }
  4126. + (NSDictionary *)offline_getSalesRep {
  4127. // 首先从offline_login表中取出sales_code
  4128. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4129. NSString *user = app.user;
  4130. user = [self translateSingleQuote:user];
  4131. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4132. __block NSString *user_code = @"";
  4133. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4134. char *code = (char *)sqlite3_column_text(stmt, 0);
  4135. if (code == NULL) {
  4136. code = "";
  4137. }
  4138. user_code = [NSString stringWithUTF8String:code];
  4139. }];
  4140. // 再取所有salesRep
  4141. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4142. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4143. // 1 name 2 code 3 salesrep_id
  4144. char *name = (char *)sqlite3_column_text(stmt, 1);
  4145. char *code = (char *)sqlite3_column_text(stmt, 2);
  4146. int salesrep_id = sqlite3_column_int(stmt, 3);
  4147. if (name == NULL) {
  4148. name = "";
  4149. }
  4150. if (code == NULL) {
  4151. code = "";
  4152. }
  4153. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4154. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4155. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4156. // 比较code 相等则check
  4157. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4158. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4159. }
  4160. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4161. }] mutableCopy];
  4162. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4163. return ret;
  4164. }
  4165. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4166. zipcode = [self translateSingleQuote:zipcode];
  4167. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4168. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4169. char *country = (char *)sqlite3_column_text(stmt, 0);
  4170. char *state = (char *)sqlite3_column_text(stmt, 1);
  4171. char *city = (char *)sqlite3_column_text(stmt, 2);
  4172. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4173. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4174. if (country == NULL) {
  4175. country = "";
  4176. }
  4177. if (state == NULL) {
  4178. state = "";
  4179. }
  4180. if (city == NULL) {
  4181. city = "";
  4182. }
  4183. if (country_code == NULL) {
  4184. country_code = "";
  4185. }
  4186. if (state_code == NULL) {
  4187. state_code = "";
  4188. }
  4189. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4190. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4191. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4192. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4193. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4194. }] mutableCopy];
  4195. return ret;
  4196. }
  4197. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4198. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4199. [item setValue:value forKey:valueKey];
  4200. [dic setValue:item forKey:itemKey];
  4201. }
  4202. + (NSString *)countryCodeByid:(NSString *)code_id {
  4203. NSString *ret = nil;
  4204. code_id = [self translateSingleQuote:code_id];
  4205. sqlite3 *db = [iSalesDB get_db];
  4206. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4207. sqlite3_stmt * statement;
  4208. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4209. while (sqlite3_step(statement) == SQLITE_ROW) {
  4210. char *code = (char *)sqlite3_column_text(statement, 0);
  4211. if (code == NULL) {
  4212. code = "";
  4213. }
  4214. ret = [NSString stringWithUTF8String:code];
  4215. }
  4216. sqlite3_finalize(statement);
  4217. }
  4218. [iSalesDB close_db:db];
  4219. return ret;
  4220. }
  4221. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4222. NSString *ret = nil;
  4223. code = [self translateSingleQuote:code];
  4224. sqlite3 *db = [iSalesDB get_db];
  4225. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4226. sqlite3_stmt * statement;
  4227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4228. while (sqlite3_step(statement) == SQLITE_ROW) {
  4229. char *_id = (char *)sqlite3_column_text(statement, 0);
  4230. if (_id == NULL) {
  4231. _id = "";
  4232. }
  4233. ret = [NSString stringWithFormat:@"%s",_id];
  4234. }
  4235. sqlite3_finalize(statement);
  4236. }
  4237. [iSalesDB close_db:db];
  4238. return ret;
  4239. }
  4240. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4241. NSString *name = nil;
  4242. codeId = [self translateSingleQuote:codeId];
  4243. sqlite3 *db = [iSalesDB get_db];
  4244. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4245. sqlite3_stmt * statement;
  4246. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4247. while (sqlite3_step(statement) == SQLITE_ROW) {
  4248. char *value = (char *)sqlite3_column_text(statement, 0);
  4249. if (value == NULL) {
  4250. value = "";
  4251. }
  4252. name = [NSString stringWithUTF8String:value];
  4253. }
  4254. sqlite3_finalize(statement);
  4255. }
  4256. [iSalesDB close_db:db];
  4257. return name;
  4258. }
  4259. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4260. NSString *ret = nil;
  4261. sqlite3 *db = [iSalesDB get_db];
  4262. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4263. sqlite3_stmt * statement;
  4264. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4265. while (sqlite3_step(statement) == SQLITE_ROW) {
  4266. char *name = (char *)sqlite3_column_text(statement, 0);
  4267. if (name == NULL) {
  4268. name = "";
  4269. }
  4270. ret = [NSString stringWithUTF8String:name];
  4271. }
  4272. sqlite3_finalize(statement);
  4273. }
  4274. [iSalesDB close_db:db];
  4275. return ret;
  4276. }
  4277. + (NSString *)salesRepCodeById:(NSString *)_id {
  4278. NSString *ret = nil;
  4279. _id = [self translateSingleQuote:_id];
  4280. sqlite3 *db = [iSalesDB get_db];
  4281. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4282. sqlite3_stmt * statement;
  4283. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4284. while (sqlite3_step(statement) == SQLITE_ROW) {
  4285. char *rep = (char *)sqlite3_column_text(statement, 0);
  4286. if (rep == NULL) {
  4287. rep = "";
  4288. }
  4289. ret = [NSString stringWithUTF8String:rep];
  4290. }
  4291. sqlite3_finalize(statement);
  4292. }
  4293. [iSalesDB close_db:db];
  4294. return ret;
  4295. }
  4296. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4297. char *tx = (char *)sqlite3_column_text(stmt, col);
  4298. if (tx == NULL) {
  4299. tx = "";
  4300. }
  4301. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4302. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4303. if (!text) {
  4304. text = @"";
  4305. }
  4306. // 将字符全部为' '的字符串干掉
  4307. int spaceCount = 0;
  4308. for (int i = 0; i < text.length; i++) {
  4309. if ([text characterAtIndex:i] == ' ') {
  4310. spaceCount++;
  4311. }
  4312. }
  4313. if (spaceCount == text.length) {
  4314. text = @"";
  4315. }
  4316. return text;
  4317. }
  4318. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4319. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4320. NSData *data = [NSData dataWithContentsOfFile:path];
  4321. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4322. return ret;
  4323. }
  4324. + (NSString *)textFileName:(NSString *)name {
  4325. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4326. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4327. if (!text) {
  4328. text = @"";
  4329. }
  4330. return text;
  4331. }
  4332. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4333. return [[dic objectForKey:key] mutableCopy];
  4334. }
  4335. + (id)translateSingleQuote:(NSString *)string {
  4336. if ([string isKindOfClass:[NSString class]])
  4337. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4338. return string;
  4339. }
  4340. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4341. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4342. }
  4343. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4344. NSString* ret= nil;
  4345. NSString *sqlQuery = nil;
  4346. // 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
  4347. 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];
  4348. sqlite3_stmt * statement;
  4349. // int count=0;
  4350. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4351. {
  4352. if (sqlite3_step(statement) == SQLITE_ROW)
  4353. {
  4354. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4355. if(imgurl==nil)
  4356. imgurl="";
  4357. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4358. ret=nsimgurl;
  4359. }
  4360. sqlite3_finalize(statement);
  4361. }
  4362. else
  4363. {
  4364. [ret setValue:@"8" forKey:@"result"];
  4365. }
  4366. // [iSalesDB close_db:db];
  4367. // DebugLog(@"data string: %@",ret );
  4368. return ret;
  4369. }
  4370. #pragma mark contact list
  4371. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4372. {
  4373. // contactType = "Sales_Order_Customer";
  4374. // limit = 25;
  4375. // offset = 0;
  4376. // password = 123456;
  4377. // "price_name" = 16;
  4378. // user = EvanK;
  4379. sqlite3 *db = [iSalesDB get_db];
  4380. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4381. if (contactType) {
  4382. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4383. } else {
  4384. contactType = @"1 = 1";
  4385. }
  4386. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4387. DebugLog(@"offline contact list keyword: %@",keyword);
  4388. // advanced search
  4389. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4390. if (contact_name) {
  4391. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4392. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4393. } else {
  4394. contact_name = @"";
  4395. }
  4396. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4397. if (customer_phone) {
  4398. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4399. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4400. } else {
  4401. customer_phone = @"";
  4402. }
  4403. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4404. if (customer_fax) {
  4405. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4406. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4407. } else {
  4408. customer_fax = @"";
  4409. }
  4410. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4411. if (customer_zipcode) {
  4412. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4413. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4414. } else {
  4415. customer_zipcode = @"";
  4416. }
  4417. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4418. if (customer_sales_rep) {
  4419. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4420. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4421. } else {
  4422. customer_sales_rep = @"";
  4423. }
  4424. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4425. if (customer_state) {
  4426. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4427. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4428. } else {
  4429. customer_state = @"";
  4430. }
  4431. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4432. if (customer_name) {
  4433. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4434. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4435. } else {
  4436. customer_name = @"";
  4437. }
  4438. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4439. if (customer_country) {
  4440. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4441. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4442. } else {
  4443. customer_country = @"";
  4444. }
  4445. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4446. if (customer_cid) {
  4447. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4448. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4449. } else {
  4450. customer_cid = @"";
  4451. }
  4452. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4453. if (customer_city) {
  4454. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4455. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4456. } else {
  4457. customer_city = @"";
  4458. }
  4459. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4460. if (customer_address) {
  4461. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4462. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4463. } else {
  4464. customer_address = @"";
  4465. }
  4466. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4467. if (customer_email) {
  4468. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4469. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4470. } else {
  4471. customer_email = @"";
  4472. }
  4473. NSString *price_name = [params valueForKey:@"price_name"];
  4474. if (price_name) {
  4475. if ([price_name containsString:@","]) {
  4476. // 首先从 price表中查处name
  4477. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4478. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4479. for (int i = 1;i < pArray.count;i++) {
  4480. NSString *p = pArray[i];
  4481. [mutablePStr appendFormat:@" or type = %@ ",p];
  4482. }
  4483. [mutablePStr appendString:@";"];
  4484. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4485. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4486. char *name = (char *)sqlite3_column_text(stmt, 0);
  4487. if (!name)
  4488. name = "";
  4489. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4490. }];
  4491. // 再根据name 拼sql
  4492. NSMutableString *mutable_price_name = [NSMutableString string];
  4493. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4494. for (int i = 1; i < price_name_array.count; i++) {
  4495. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4496. }
  4497. [mutable_price_name appendString:@")"];
  4498. price_name = mutable_price_name;
  4499. } else {
  4500. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4501. if ([price_name isEqualToString:@""]) {
  4502. price_name = @"";
  4503. } else {
  4504. __block NSString *price;
  4505. price_name = [self translateSingleQuote:price_name];
  4506. [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) {
  4507. char *p = (char *)sqlite3_column_text(stmt, 0);
  4508. if (p == NULL) {
  4509. p = "";
  4510. }
  4511. price = [NSString stringWithUTF8String:p];
  4512. }];
  4513. if ([price isEqualToString:@""]) {
  4514. price_name = @"";
  4515. } else {
  4516. price = [self translateSingleQuote:price];
  4517. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4518. }
  4519. }
  4520. }
  4521. } else {
  4522. price_name = @"";
  4523. }
  4524. int limit = [[params valueForKey:@"limit"] intValue];
  4525. int offset = [[params valueForKey:@"offset"] intValue];
  4526. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4527. 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];
  4528. 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];
  4529. // int result= [iSalesDB AddExFunction:db];
  4530. int count =0;
  4531. NSString *sqlQuery = nil;
  4532. if(keyword.length==0)
  4533. {
  4534. // 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];
  4535. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4536. sqlQuery = sql;
  4537. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4538. }
  4539. else
  4540. {
  4541. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4542. keyword = keyword.lowercaseString;
  4543. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4544. 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];
  4545. 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]];
  4546. }
  4547. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4548. sqlite3_stmt * statement;
  4549. [ret setValue:@"2" forKey:@"result"];
  4550. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4551. // sqlQuery = @"select editable,decrypt(company_name),country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4552. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4553. {
  4554. int i = 0;
  4555. while (sqlite3_step(statement) == SQLITE_ROW)
  4556. {
  4557. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4558. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4559. int editable = sqlite3_column_int(statement, 0);
  4560. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4561. NSString *nscompany_name =nil;
  4562. if(company_name==nil)
  4563. nscompany_name=@"";
  4564. else
  4565. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4566. char *country = (char*)sqlite3_column_text(statement, 2);
  4567. if(country==nil)
  4568. country="";
  4569. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4570. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4571. // if(addr==nil)
  4572. // addr="";
  4573. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4574. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4575. if(zipcode==nil)
  4576. zipcode="";
  4577. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4578. char *state = (char*)sqlite3_column_text(statement, 5);
  4579. if(state==nil)
  4580. state="";
  4581. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4582. char *city = (char*)sqlite3_column_text(statement, 6);
  4583. if(city==nil)
  4584. city="";
  4585. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4586. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4587. // NSString *nscontact_name = nil;
  4588. // if(contact_name==nil)
  4589. // nscontact_name=@"";
  4590. // else
  4591. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4592. char *phone = (char*)sqlite3_column_text(statement, 8);
  4593. NSString *nsphone = nil;
  4594. if(phone==nil)
  4595. nsphone=@"";
  4596. else
  4597. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4598. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4599. if(contact_id==nil)
  4600. contact_id="";
  4601. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4602. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4603. if(addr_1==nil)
  4604. addr_1="";
  4605. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4606. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4607. if(addr_2==nil)
  4608. addr_2="";
  4609. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4610. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4611. if(addr_3==nil)
  4612. addr_3="";
  4613. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4614. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4615. if(addr_4==nil)
  4616. addr_4="";
  4617. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4618. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4619. if(first_name==nil)
  4620. first_name="";
  4621. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4622. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4623. if(last_name==nil)
  4624. last_name="";
  4625. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4626. char *fax = (char*)sqlite3_column_text(statement, 16);
  4627. NSString *nsfax = nil;
  4628. if(fax==nil)
  4629. nsfax=@"";
  4630. else
  4631. {
  4632. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4633. if(nsfax.length>0)
  4634. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4635. }
  4636. char *email = (char*)sqlite3_column_text(statement, 17);
  4637. NSString *nsemail = nil;
  4638. if(email==nil)
  4639. nsemail=@"";
  4640. else
  4641. {
  4642. nsemail= [[NSString alloc]initWithUTF8String:email];
  4643. if(nsemail.length>0)
  4644. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4645. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4646. }
  4647. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4648. [arr_name addObject:nsfirst_name];
  4649. [arr_name addObject:nslast_name];
  4650. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4651. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4652. {
  4653. // decrypt
  4654. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4655. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4656. nsphone=[AESCrypt fastdecrypt:nsphone];
  4657. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4658. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4659. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4660. }
  4661. [arr_addr addObject:nscompany_name];
  4662. [arr_addr addObject:nscontact_name];
  4663. [arr_addr addObject:@"<br>"];
  4664. [arr_addr addObject:nsaddr_1];
  4665. [arr_addr addObject:nsaddr_2];
  4666. [arr_addr addObject:nsaddr_3];
  4667. [arr_addr addObject:nsaddr_4];
  4668. //[arr_addr addObject:nsaddr];
  4669. [arr_addr addObject:nszipcode];
  4670. [arr_addr addObject:nscity];
  4671. [arr_addr addObject:nsstate];
  4672. [arr_addr addObject:nscountry];
  4673. [arr_addr addObject:@"<br>"];
  4674. [arr_addr addObject:nsphone];
  4675. [arr_addr addObject:nsfax];
  4676. [arr_addr addObject:nsemail];
  4677. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4678. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4679. [item setValue:name forKey:@"name"];
  4680. [item setValue:nscontact_id forKey:@"contact_id"];
  4681. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4682. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4683. i++;
  4684. }
  4685. UIApplication * app = [UIApplication sharedApplication];
  4686. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4687. [ret setValue:appDelegate.mode forKey:@"mode"];
  4688. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4689. sqlite3_finalize(statement);
  4690. }
  4691. [iSalesDB close_db:db];
  4692. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4693. return ret;
  4694. }
  4695. #pragma mark contact Advanced search
  4696. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4697. {
  4698. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4699. return [RAUtils dict2data:contactAdvanceDic];
  4700. }
  4701. #pragma mark create new contact
  4702. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4703. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4704. NSData *data = [NSData dataWithContentsOfFile:path];
  4705. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4706. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4707. NSString *countryCode = nil;
  4708. NSString *countryCode_id = nil;
  4709. NSString *stateCode = nil;
  4710. NSString *city = nil;
  4711. NSString *zipCode = nil;
  4712. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4713. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4714. NSString *code_id = params[@"country"];
  4715. countryCode_id = code_id;
  4716. countryCode = [self countryCodeByid:code_id];
  4717. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4718. NSString *zip_code = params[@"zipcode"];
  4719. // 剔除全部为空格
  4720. int spaceCount = 0;
  4721. for (int i = 0; i < zip_code.length; i++) {
  4722. if ([zip_code characterAtIndex:i] == ' ') {
  4723. spaceCount++;
  4724. }
  4725. }
  4726. if (spaceCount == zip_code.length) {
  4727. zip_code = @"";
  4728. }
  4729. zipCode = zip_code;
  4730. if (zipCode.length > 0) {
  4731. countryCode_id = params[@"country"];
  4732. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4733. countryCode = [dic valueForKey:@"country_code"];
  4734. if (!countryCode) {
  4735. // countryCode = @"US";
  4736. NSString *code_id = params[@"country"];
  4737. countryCode = [self countryCodeByid:code_id];
  4738. }
  4739. stateCode = [dic valueForKey:@"state_code"];
  4740. if (!stateCode.length) {
  4741. stateCode = params[@"state"];
  4742. }
  4743. city = [dic valueForKey:@"city"];
  4744. if (!city.length) {
  4745. city = params[@"city"];
  4746. }
  4747. // zip code
  4748. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4749. [zipDic setValue:zipCode forKey:@"value"];
  4750. [section_0 setValue:zipDic forKey:@"item_11"];
  4751. } else {
  4752. NSString *code_id = params[@"country"];
  4753. countryCode = [self countryCodeByid:code_id];
  4754. stateCode = params[@"state"];
  4755. city = params[@"city"];
  4756. }
  4757. }
  4758. } else {
  4759. // default: US United States
  4760. countryCode = @"US";
  4761. countryCode_id = @"228";
  4762. }
  4763. // country
  4764. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4765. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4766. // state
  4767. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4768. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4769. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4770. // city
  4771. if (city) {
  4772. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4773. [cityDic setValue:city forKey:@"value"];
  4774. [section_0 setValue:cityDic forKey:@"item_13"];
  4775. }
  4776. // price type
  4777. NSDictionary *priceDic = [self offline_getPrice];
  4778. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4779. // contact type
  4780. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4781. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4782. // Sales Rep
  4783. NSDictionary *repDic = [self offline_getSalesRep];
  4784. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4785. [ret setValue:section_0 forKey:@"section_0"];
  4786. return [RAUtils dict2data:ret];
  4787. }
  4788. #pragma mark save
  4789. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4790. NSString *addr = nil;
  4791. NSString *contact_name = nil;
  4792. NSString *companyName = [params objectForKey:@"company"];
  4793. if (companyName) {
  4794. companyName = [AESCrypt fastencrypt:companyName];
  4795. } else {
  4796. companyName = @"";
  4797. }
  4798. DebugLog(@"company");
  4799. companyName = [self translateSingleQuote:companyName];
  4800. NSString *addr1 = [params objectForKey:@"address"];
  4801. NSString *addr2 = [params objectForKey:@"address2"];
  4802. NSString *addr3 = [params objectForKey:@"address_3"];
  4803. NSString *addr4 = [params objectForKey:@"address_4"];
  4804. if (!addr2) {
  4805. addr2 = @"";
  4806. }
  4807. if (!addr3) {
  4808. addr3 = @"";
  4809. }
  4810. if (!addr4) {
  4811. addr4 = @"";
  4812. }
  4813. if (!addr1) {
  4814. addr1 = @"";
  4815. }
  4816. DebugLog(@"addr");
  4817. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4818. addr = [AESCrypt fastencrypt:addr];
  4819. addr = [self translateSingleQuote:addr];
  4820. if (addr1 && ![addr1 isEqualToString:@""]) {
  4821. addr1 = [AESCrypt fastencrypt:addr1];
  4822. }
  4823. addr1 = [self translateSingleQuote:addr1];
  4824. addr2 = [self translateSingleQuote:addr2];
  4825. addr3 = [self translateSingleQuote:addr3];
  4826. addr4 = [self translateSingleQuote:addr4];
  4827. NSString *country = [params objectForKey:@"country"];
  4828. if (country) {
  4829. country = [self countryNameByCountryCodeId:country];
  4830. } else {
  4831. country = @"";
  4832. }
  4833. DebugLog(@"country");
  4834. country = [self translateSingleQuote:country];
  4835. NSString *state = [params objectForKey:@"state"];
  4836. if (!state) {
  4837. state = @"";
  4838. }
  4839. DebugLog(@"state");
  4840. state = [self translateSingleQuote:state];
  4841. NSString *city = [params objectForKey:@"city"];
  4842. if (!city) {
  4843. city = @"";
  4844. }
  4845. city = [self translateSingleQuote:city];
  4846. NSString *zipcode = [params objectForKey:@"zipcode"];
  4847. if (!zipcode) {
  4848. zipcode = @"";
  4849. }
  4850. DebugLog(@"zip");
  4851. zipcode = [self translateSingleQuote:zipcode];
  4852. NSString *fistName = [params objectForKey:@"firstname"];
  4853. if (!fistName) {
  4854. fistName = @"";
  4855. }
  4856. NSString *lastName = [params objectForKey:@"lastname"];
  4857. if (!lastName) {
  4858. lastName = @"";
  4859. }
  4860. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4861. DebugLog(@"contact_name");
  4862. contact_name = [self translateSingleQuote:contact_name];
  4863. fistName = [self translateSingleQuote:fistName];
  4864. lastName = [self translateSingleQuote:lastName];
  4865. NSString *phone = [params objectForKey:@"phone"];
  4866. if (phone) {
  4867. phone = [AESCrypt fastencrypt:phone];
  4868. } else {
  4869. phone = @"";
  4870. }
  4871. DebugLog(@"PHONE");
  4872. phone = [self translateSingleQuote:phone];
  4873. NSString *fax = [params objectForKey:@"fax"];
  4874. if (!fax) {
  4875. fax = @"";
  4876. }
  4877. DebugLog(@"FAX");
  4878. fax = [self translateSingleQuote:fax];
  4879. NSString *email = [params objectForKey:@"email"];
  4880. if (!email) {
  4881. email = @"";
  4882. }
  4883. DebugLog(@"EMAIL:%@",email);
  4884. email = [self translateSingleQuote:email];
  4885. NSString *notes = [params objectForKey:@"contact_notes"];
  4886. if (!notes) {
  4887. notes = @"";
  4888. }
  4889. DebugLog(@"NOTE:%@",notes);
  4890. notes = [self translateSingleQuote:notes];
  4891. NSString *price = [params objectForKey:@"price_name"];
  4892. if (price) {
  4893. price = [self priceNameByPriceId:price];
  4894. } else {
  4895. price = @"";
  4896. }
  4897. DebugLog(@"PRICE");
  4898. price = [self translateSingleQuote:price];
  4899. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4900. if (salesRep) {
  4901. salesRep = [self salesRepCodeById:salesRep];
  4902. } else {
  4903. salesRep = @"";
  4904. }
  4905. salesRep = [self translateSingleQuote:salesRep];
  4906. NSString *img = [params objectForKey:@"business_card"];
  4907. NSArray *array = [img componentsSeparatedByString:@","];
  4908. NSString *img_0 = array[0];
  4909. if (!img_0) {
  4910. img_0 = @"";
  4911. }
  4912. img_0 = [self translateSingleQuote:img_0];
  4913. NSString *img_1 = array[1];
  4914. if (!img_1) {
  4915. img_1 = @"";
  4916. }
  4917. img_1 = [self translateSingleQuote:img_1];
  4918. NSString *img_2 = array[2];
  4919. if (!img_2) {
  4920. img_2 = @"";
  4921. }
  4922. img_2 = [self translateSingleQuote:img_2];
  4923. NSString *contact_id = [NSUUID UUID].UUIDString;
  4924. NSString *contact_type = [params objectForKey:@"type_name"];
  4925. if (!contact_type) {
  4926. contact_type = @"";
  4927. }
  4928. contact_type = [self translateSingleQuote:contact_type];
  4929. // 判断更新时是否为customer
  4930. if (update) {
  4931. contact_id = [params objectForKey:@"contact_id"];
  4932. if (!contact_id) {
  4933. contact_id = @"";
  4934. }
  4935. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4936. __block int customer = 0;
  4937. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4938. customer = sqlite3_column_int(stmt, 0);
  4939. }];
  4940. isCustomer = customer ? YES : NO;
  4941. }
  4942. NSMutableDictionary *sync_dic = [params mutableCopy];
  4943. if (isCustomer) {
  4944. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4945. } else {
  4946. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4947. }
  4948. NSString *sync_data = nil;
  4949. NSString *sql = nil;
  4950. if (update){
  4951. contact_id = [params objectForKey:@"contact_id"];
  4952. if (!contact_id) {
  4953. contact_id = @"";
  4954. }
  4955. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4956. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4957. sync_data = [RAUtils dict2string:sync_dic];
  4958. sync_data = [self translateSingleQuote:sync_data];
  4959. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  4960. } else {
  4961. contact_id = [self translateSingleQuote:contact_id];
  4962. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4963. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4964. sync_data = [RAUtils dict2string:sync_dic];
  4965. sync_data = [self translateSingleQuote:sync_data];
  4966. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  4967. }
  4968. int result = [iSalesDB execSql:sql];
  4969. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4970. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4971. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4972. }
  4973. #pragma mark save new contact
  4974. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4975. {
  4976. return [self offline_saveContact:params update:NO isCustomer:YES];
  4977. }
  4978. #pragma mark edit contact
  4979. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4980. {
  4981. // {
  4982. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4983. // password = 123456;
  4984. // user = EvanK;
  4985. // }
  4986. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4987. NSData *data = [NSData dataWithContentsOfFile:path];
  4988. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4989. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4990. NSString *countryCode = nil;
  4991. NSString *countryCode_id = nil;
  4992. NSString *stateCode = nil;
  4993. /*------contact infor------*/
  4994. __block NSString *country = nil;
  4995. __block NSString *company_name = nil;
  4996. __block NSString *contact_id = params[@"contact_id"];
  4997. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4998. __block NSString *zipcode = nil;
  4999. __block NSString *state = nil; // state_code
  5000. __block NSString *city = nil; //
  5001. __block NSString *firt_name,*last_name;
  5002. __block NSString *phone,*fax,*email;
  5003. __block NSString *notes,*price_type,*sales_rep;
  5004. __block NSString *img_0,*img_1,*img_2;
  5005. __block NSString *contact_type;
  5006. contact_id = [self translateSingleQuote:contact_id];
  5007. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  5008. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5009. country = [self textAtColumn:0 statement:stmt]; // country name
  5010. company_name = [self textAtColumn:1 statement:stmt];
  5011. addr_1 = [self textAtColumn:2 statement:stmt];
  5012. addr_2 = [self textAtColumn:3 statement:stmt];
  5013. addr_3 = [self textAtColumn:4 statement:stmt];
  5014. addr_4 = [self textAtColumn:5 statement:stmt];
  5015. zipcode = [self textAtColumn:6 statement:stmt];
  5016. state = [self textAtColumn:7 statement:stmt]; // state code
  5017. city = [self textAtColumn:8 statement:stmt];
  5018. firt_name = [self textAtColumn:9 statement:stmt];
  5019. last_name = [self textAtColumn:10 statement:stmt];
  5020. phone = [self textAtColumn:11 statement:stmt];
  5021. fax = [self textAtColumn:12 statement:stmt];
  5022. email = [self textAtColumn:13 statement:stmt];
  5023. notes = [self textAtColumn:14 statement:stmt];
  5024. price_type = [self textAtColumn:15 statement:stmt]; // name
  5025. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5026. img_0 = [self textAtColumn:17 statement:stmt];
  5027. img_1 = [self textAtColumn:18 statement:stmt];
  5028. img_2 = [self textAtColumn:19 statement:stmt];
  5029. contact_type = [self textAtColumn:20 statement:stmt];
  5030. }];
  5031. // decrypt
  5032. if (company_name) {
  5033. company_name = [AESCrypt fastdecrypt:company_name];
  5034. }
  5035. if (addr_1) {
  5036. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5037. }
  5038. if (phone) {
  5039. phone = [AESCrypt fastdecrypt:phone];
  5040. }
  5041. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5042. countryCode = [iSalesDB jk_queryText:countrySql];
  5043. stateCode = state;
  5044. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5045. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5046. NSString *code_id = params[@"country"];
  5047. countryCode_id = code_id;
  5048. countryCode = [self countryCodeByid:code_id];
  5049. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5050. NSString *zip_code = params[@"zipcode"];
  5051. // 剔除全部为空格
  5052. int spaceCount = 0;
  5053. for (int i = 0; i < zip_code.length; i++) {
  5054. if ([zip_code characterAtIndex:i] == ' ') {
  5055. spaceCount++;
  5056. }
  5057. }
  5058. if (spaceCount == zip_code.length) {
  5059. zip_code = @"";
  5060. }
  5061. if (zipcode.length > 0) {
  5062. zipcode = zip_code;
  5063. countryCode_id = params[@"country"];
  5064. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5065. countryCode = [dic valueForKey:@"country_code"];
  5066. if (!countryCode) {
  5067. NSString *code_id = params[@"country"];
  5068. countryCode = [self countryCodeByid:code_id];
  5069. }
  5070. stateCode = [dic valueForKey:@"state_code"];
  5071. if (!stateCode.length) {
  5072. stateCode = params[@"state"];
  5073. }
  5074. city = [dic valueForKey:@"city"];
  5075. if (!city.length) {
  5076. city = params[@"city"];
  5077. }
  5078. // zip code
  5079. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5080. [zipDic setValue:zipcode forKey:@"value"];
  5081. [section_0 setValue:zipDic forKey:@"item_8"];
  5082. } else {
  5083. NSString *code_id = params[@"country"];
  5084. countryCode = [self countryCodeByid:code_id];
  5085. stateCode = params[@"state"];
  5086. city = params[@"city"];
  5087. }
  5088. }
  5089. }
  5090. // 0 Country
  5091. // 1 Company Name
  5092. // 2 Contact ID
  5093. // 3 Picture
  5094. // 4 Address 1
  5095. // 5 Address 2
  5096. // 6 Address 3
  5097. // 7 Address 4
  5098. // 8 Zip Code
  5099. // 9 State/Province
  5100. // 10 City
  5101. // 11 Contact First Name
  5102. // 12 Contact Last Name
  5103. // 13 Phone
  5104. // 14 Fax
  5105. // 15 Email
  5106. // 16 Contact Notes
  5107. // 17 Price Type
  5108. // 18 Contact Type
  5109. // 19 Sales Rep
  5110. // country
  5111. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5112. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5113. // company
  5114. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5115. // contact_id
  5116. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5117. // picture
  5118. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5119. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5120. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5121. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5122. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5123. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5124. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5125. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5126. // addr 1 2 3 4
  5127. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5128. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5129. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5130. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5131. // zip code
  5132. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5133. // state
  5134. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5135. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5136. // city
  5137. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5138. // first last
  5139. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5140. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5141. // phone fax email
  5142. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5143. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5144. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5145. // notes
  5146. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5147. // price
  5148. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5149. for (NSString *key in priceDic.allKeys) {
  5150. if ([key containsString:@"val_"]) {
  5151. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5152. if ([dic[@"value"] isEqualToString:price_type]) {
  5153. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5154. [priceDic setValue:dic forKey:key];
  5155. }
  5156. }
  5157. }
  5158. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5159. // Contact Rep
  5160. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5161. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5162. // Sales Rep
  5163. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5164. for (NSString *key in repDic.allKeys) {
  5165. if ([key containsString:@"val_"]) {
  5166. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5167. NSString *value = dic[@"value"];
  5168. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5169. if (code && [code isEqualToString:sales_rep]) {
  5170. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5171. [repDic setValue:dic forKey:key];
  5172. }
  5173. }
  5174. }
  5175. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5176. [ret setValue:section_0 forKey:@"section_0"];
  5177. return [RAUtils dict2data:ret];
  5178. }
  5179. #pragma mark save contact
  5180. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5181. {
  5182. return [self offline_saveContact:params update:YES isCustomer:YES];
  5183. }
  5184. #pragma mark category
  5185. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5186. if (ck) {
  5187. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5188. for (NSString *key in res.allKeys) {
  5189. if (![key isEqualToString:@"count"]) {
  5190. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5191. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5192. if ([val[@"value"] isEqualToString:ck]) {
  5193. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5194. }
  5195. [res setValue:val forKey:key];
  5196. }
  5197. }
  5198. [dic setValue:res forKey:valueKey];
  5199. }
  5200. }
  5201. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5202. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5203. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5204. NSString* category = [params valueForKey:@"category"];
  5205. if (!category || [category isEqualToString:@""]) {
  5206. category = @"%";
  5207. }
  5208. category = [self translateSingleQuote:category];
  5209. int limit = [[params valueForKey:@"limit"] intValue];
  5210. int offset = [[params valueForKey:@"offset"] intValue];
  5211. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5212. NSString *limit_str = @"";
  5213. if (limited) {
  5214. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5215. }
  5216. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5217. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5218. sqlite3 *db = [iSalesDB get_db];
  5219. // [iSalesDB AddExFunction:db];
  5220. int count;
  5221. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5222. 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];
  5223. double price_min = 0;
  5224. double price_max = 0;
  5225. if ([params.allKeys containsObject:@"alert"]) {
  5226. // alert
  5227. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5228. NSString *alert = params[@"alert"];
  5229. if ([alert isEqualToString:@"Display All"]) {
  5230. alert = [NSString stringWithFormat:@""];
  5231. } else {
  5232. alert = [self translateSingleQuote:alert];
  5233. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5234. }
  5235. // available
  5236. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5237. NSString *available = params[@"available"];
  5238. NSString *available_condition;
  5239. if ([available isEqualToString:@"Display All"]) {
  5240. available_condition = @"";
  5241. } else if ([available isEqualToString:@"Available Now"]) {
  5242. available_condition = @"and availability > 0";
  5243. } else {
  5244. available_condition = @"and availability == 0";
  5245. }
  5246. // best seller
  5247. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5248. NSString *best_seller = @"";
  5249. NSString *order_best_seller = @"m.name asc";
  5250. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5251. best_seller = @"and best_seller > 0";
  5252. order_best_seller = @"m.best_seller desc,m.name asc";
  5253. }
  5254. // price
  5255. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5256. NSString *price = params[@"price"];
  5257. price_min = 0;
  5258. price_max = MAXFLOAT;
  5259. if (appDelegate.user) {
  5260. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5261. NSMutableString *priceName = [NSMutableString string];
  5262. for (int i = 0; i < priceTypeArray.count; i++) {
  5263. NSString *pricetype = priceTypeArray[i];
  5264. pricetype = [self translateSingleQuote:pricetype];
  5265. if (i == 0) {
  5266. [priceName appendFormat:@"'%@'",pricetype];
  5267. } else {
  5268. [priceName appendFormat:@",'%@'",pricetype];
  5269. }
  5270. }
  5271. if ([price isEqualToString:@"Display All"]) {
  5272. price = [NSString stringWithFormat:@""];
  5273. } else if([price containsString:@"+"]){
  5274. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5275. price_min = [price doubleValue];
  5276. 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];
  5277. } else {
  5278. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5279. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5280. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5281. 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];
  5282. }
  5283. } else {
  5284. price = @"";
  5285. }
  5286. // sold_by_qty : Sold in quantities of %@
  5287. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5288. NSString *qty = params[@"sold_by_qty"];
  5289. if ([qty isEqualToString:@"Display All"]) {
  5290. qty = @"";
  5291. } else {
  5292. qty = [self translateSingleQuote:qty];
  5293. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5294. }
  5295. // cate
  5296. category = [self translateSingleQuote:category];
  5297. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5298. // where bestseller > 0 order by bestseller desc
  5299. // sql query: alert availability(int) best_seller(int) price qty
  5300. 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];
  5301. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5302. }
  5303. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5304. if (!appDelegate.user) {
  5305. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5306. }
  5307. [ret setValue:filter forKey:@"filter"];
  5308. DebugLog(@"offline_category sql:%@",sqlQuery);
  5309. sqlite3_stmt * statement;
  5310. [ret setValue:@"2" forKey:@"result"];
  5311. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5312. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5313. // int count=0;
  5314. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5315. {
  5316. int i=0;
  5317. while (sqlite3_step(statement) == SQLITE_ROW)
  5318. {
  5319. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5320. char *name = (char*)sqlite3_column_text(statement, 0);
  5321. if(name==nil)
  5322. name="";
  5323. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5324. char *description = (char*)sqlite3_column_text(statement, 1);
  5325. if(description==nil)
  5326. description="";
  5327. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5328. int product_id = sqlite3_column_int(statement, 2);
  5329. int wid = sqlite3_column_int(statement, 3);
  5330. int closeout = sqlite3_column_int(statement, 4);
  5331. int cid = sqlite3_column_int(statement, 5);
  5332. int wisdelete = sqlite3_column_int(statement, 6);
  5333. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5334. if(wid !=0 && wisdelete != 1)
  5335. [item setValue:@"true" forKey:@"wish_exists"];
  5336. else
  5337. [item setValue:@"false" forKey:@"wish_exists"];
  5338. if(closeout==0)
  5339. [item setValue:@"false" forKey:@"is_closeout"];
  5340. else
  5341. [item setValue:@"true" forKey:@"is_closeout"];
  5342. if(cid==0)
  5343. [item setValue:@"false" forKey:@"cart_exists"];
  5344. else
  5345. [item setValue:@"true" forKey:@"cart_exists"];
  5346. [item addEntriesFromDictionary:imgjson];
  5347. // [item setValue:nsurl forKey:@"img"];
  5348. [item setValue:nsname forKey:@"name"];
  5349. [item setValue:nsdescription forKey:@"description"];
  5350. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5351. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5352. i++;
  5353. }
  5354. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5355. [ret setObject:items forKey:@"items"];
  5356. sqlite3_finalize(statement);
  5357. } else {
  5358. DebugLog(@"nothing...");
  5359. }
  5360. DebugLog(@"count:%d",count);
  5361. [iSalesDB close_db:db];
  5362. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5363. return ret;
  5364. }
  5365. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5366. {
  5367. return [self categoryList:params limited:YES];
  5368. }
  5369. # pragma mark item search
  5370. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5371. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5372. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5373. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5374. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5375. // category
  5376. NSDictionary *category_menu = [self offline_category_menu];
  5377. [filter setValue:category_menu forKey:@"category"];
  5378. NSString* where= nil;
  5379. NSString* orderby= @"m.name";
  5380. if (!filterSearch) {
  5381. int covertype = [[params valueForKey:@"covertype"] intValue];
  5382. switch (covertype) {
  5383. case 0:
  5384. {
  5385. where=@"m.category like'%%#005#%%'";
  5386. break;
  5387. }
  5388. case 1:
  5389. {
  5390. where=@"m.alert like '%QS%'";
  5391. break;
  5392. }
  5393. case 2:
  5394. {
  5395. where=@"m.availability>0";
  5396. break;
  5397. }
  5398. case 3:
  5399. {
  5400. where=@"m.best_seller>0";
  5401. orderby=@"m.best_seller desc,m.name asc";
  5402. break;
  5403. }
  5404. default:
  5405. where=@"1=1";
  5406. break;
  5407. }
  5408. }
  5409. int limit = [[params valueForKey:@"limit"] intValue];
  5410. int offset = [[params valueForKey:@"offset"] intValue];
  5411. NSString *limit_str = @"";
  5412. if (limited) {
  5413. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5414. }
  5415. sqlite3 *db = [iSalesDB get_db];
  5416. // [iSalesDB AddExFunction:db];
  5417. int count;
  5418. NSString *sqlQuery = nil;
  5419. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5420. 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];
  5421. double price_min = 0;
  5422. double price_max = 0;
  5423. if (filterSearch) {
  5424. // alert
  5425. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5426. NSString *alert = params[@"alert"];
  5427. if ([alert isEqualToString:@"Display All"]) {
  5428. alert = [NSString stringWithFormat:@""];
  5429. } else {
  5430. alert = [self translateSingleQuote:alert];
  5431. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5432. }
  5433. // available
  5434. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5435. NSString *available = params[@"available"];
  5436. NSString *available_condition;
  5437. if ([available isEqualToString:@"Display All"]) {
  5438. available_condition = @"";
  5439. } else if ([available isEqualToString:@"Available Now"]) {
  5440. available_condition = @"and availability > 0";
  5441. } else {
  5442. available_condition = @"and availability == 0";
  5443. }
  5444. // best seller
  5445. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5446. NSString *best_seller = @"";
  5447. NSString *order_best_seller = @"m.name asc";
  5448. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5449. best_seller = @"and best_seller > 0";
  5450. order_best_seller = @"m.best_seller desc,m.name asc";
  5451. }
  5452. // price
  5453. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5454. NSString *price = params[@"price"];
  5455. price_min = 0;
  5456. price_max = MAXFLOAT;
  5457. if (appDelegate.user) {
  5458. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5459. NSMutableString *priceName = [NSMutableString string];
  5460. for (int i = 0; i < priceTypeArray.count; i++) {
  5461. NSString *pricetype = priceTypeArray[i];
  5462. pricetype = [self translateSingleQuote:pricetype];
  5463. if (i == 0) {
  5464. [priceName appendFormat:@"'%@'",pricetype];
  5465. } else {
  5466. [priceName appendFormat:@",'%@'",pricetype];
  5467. }
  5468. }
  5469. if ([price isEqualToString:@"Display All"]) {
  5470. price = [NSString stringWithFormat:@""];
  5471. } else if([price containsString:@"+"]){
  5472. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5473. price_min = [price doubleValue];
  5474. 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];
  5475. } else {
  5476. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5477. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5478. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5479. 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];
  5480. }
  5481. } else {
  5482. price = @"";
  5483. }
  5484. // sold_by_qty : Sold in quantities of %@
  5485. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5486. NSString *qty = params[@"sold_by_qty"];
  5487. if ([qty isEqualToString:@"Display All"]) {
  5488. qty = @"";
  5489. } else {
  5490. qty = [self translateSingleQuote:qty];
  5491. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5492. }
  5493. // category
  5494. NSString *category_id = params[@"ctgId"];
  5495. NSMutableArray *cate_id_array = nil;
  5496. NSMutableString *cateWhere = [NSMutableString string];
  5497. if ([category_id isEqualToString:@""] || !category_id) {
  5498. [cateWhere appendString:@"1 = 1"];
  5499. } else {
  5500. if ([category_id containsString:@","]) {
  5501. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5502. } else {
  5503. cate_id_array = [@[category_id] mutableCopy];
  5504. }
  5505. /*-----------*/
  5506. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5507. for (int i = 0; i < cate_id_array.count; i++) {
  5508. for (NSString *key0 in cateDic.allKeys) {
  5509. if ([key0 containsString:@"category_"]) {
  5510. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5511. for (NSString *key1 in category0.allKeys) {
  5512. if ([key1 containsString:@"category_"]) {
  5513. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5514. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5515. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5516. cate_id_array[i] = [category1 objectForKey:@"id"];
  5517. if (i == 0) {
  5518. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5519. } else {
  5520. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5521. }
  5522. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5523. [category0 setValue:category1 forKey:key1];
  5524. [cateDic setValue:category0 forKey:key0];
  5525. }
  5526. }
  5527. }
  5528. }
  5529. }
  5530. }
  5531. [filter setValue:cateDic forKey:@"category"];
  5532. }
  5533. // where bestseller > 0 order by bestseller desc
  5534. // sql query: alert availability(int) best_seller(int) price qty
  5535. 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];
  5536. // count
  5537. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5538. }
  5539. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5540. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5541. if (!appDelegate.user) {
  5542. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5543. }
  5544. [ret setValue:filter forKey:@"filter"];
  5545. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5546. sqlite3_stmt * statement;
  5547. [ret setValue:@"2" forKey:@"result"];
  5548. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5549. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5550. // int count=0;
  5551. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5552. {
  5553. int i=0;
  5554. while (sqlite3_step(statement) == SQLITE_ROW)
  5555. {
  5556. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5557. char *name = (char*)sqlite3_column_text(statement, 0);
  5558. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5559. char *description = (char*)sqlite3_column_text(statement, 1);
  5560. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5561. int product_id = sqlite3_column_int(statement, 2);
  5562. int wid = sqlite3_column_int(statement, 3);
  5563. int closeout = sqlite3_column_int(statement, 4);
  5564. int cid = sqlite3_column_int(statement, 5);
  5565. int wisdelete = sqlite3_column_int(statement, 6);
  5566. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5567. if(wid !=0 && wisdelete != 1)
  5568. [item setValue:@"true" forKey:@"wish_exists"];
  5569. else
  5570. [item setValue:@"false" forKey:@"wish_exists"];
  5571. if(closeout==0)
  5572. [item setValue:@"false" forKey:@"is_closeout"];
  5573. else
  5574. [item setValue:@"true" forKey:@"is_closeout"];
  5575. if(cid==0)
  5576. [item setValue:@"false" forKey:@"cart_exists"];
  5577. else
  5578. [item setValue:@"true" forKey:@"cart_exists"];
  5579. [item addEntriesFromDictionary:imgjson];
  5580. // [item setValue:nsurl forKey:@"img"];
  5581. [item setValue:nsname forKey:@"fash_name"];
  5582. [item setValue:nsdescription forKey:@"description"];
  5583. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5584. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5585. i++;
  5586. }
  5587. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5588. [ret setObject:items forKey:@"items"];
  5589. sqlite3_finalize(statement);
  5590. }
  5591. [iSalesDB close_db:db];
  5592. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5593. return ret;
  5594. }
  5595. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5596. {
  5597. return [self itemsearch:params limited:YES];
  5598. }
  5599. #pragma mark order detail
  5600. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5601. if (str1.length == 0) {
  5602. str1 = @"&nbsp";
  5603. }
  5604. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5605. return str;
  5606. }
  5607. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5608. {
  5609. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5610. [fromformatter setDateFormat:from];
  5611. NSDate *date = [fromformatter dateFromString:datetime];
  5612. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5613. [toformatter setDateFormat:to];
  5614. NSString * ret = [toformatter stringFromDate:date];
  5615. return ret;
  5616. }
  5617. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5618. // 把毫秒去掉
  5619. if ([dateTime containsString:@"."]) {
  5620. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5621. }
  5622. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5623. formatter.dateFormat = formate;
  5624. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5625. NSDate *date = [formatter dateFromString:dateTime];
  5626. formatter.dateFormat = newFormate;
  5627. NSString *result = [formatter stringFromDate:date];
  5628. return result ? result : @"";
  5629. }
  5630. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5631. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5632. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5633. }
  5634. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5635. {
  5636. DebugLog(@"offline oderdetail params: %@",params);
  5637. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5638. int orderId = [params[@"orderId"] intValue];
  5639. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5640. // decrypt card number and card security code
  5641. // 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 ];
  5642. 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];
  5643. sqlite3 *db = [iSalesDB get_db];
  5644. sqlite3_stmt * statement;
  5645. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5646. NSString *nssoid = @"";
  5647. NSString* orderinfo = @"";
  5648. NSString *moreInfo = @"";
  5649. double handlingFee = 0;
  5650. double payments_and_credist = 0;
  5651. double totalPrice = 0;
  5652. double shippingFee = 0;
  5653. double lift_gate = 0;
  5654. NSString *customer_contact = @"";
  5655. NSString *customer_email = @"";
  5656. NSString *customer_fax = @"";
  5657. NSString *customer_phone = @"";
  5658. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5659. {
  5660. if (sqlite3_step(statement) == SQLITE_ROW)
  5661. {
  5662. int order_id = sqlite3_column_int(statement, 0);
  5663. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5664. ret[@"sign_url"] = sign_url;
  5665. customer_contact = [self textAtColumn:52 statement:statement];
  5666. customer_email = [self textAtColumn:53 statement:statement];
  5667. customer_phone = [self textAtColumn:54 statement:statement];
  5668. customer_fax = [self textAtColumn:55 statement:statement];
  5669. int offline_edit=sqlite3_column_int(statement, 56);
  5670. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5671. char *soid = (char*)sqlite3_column_text(statement, 1);
  5672. if(soid==nil)
  5673. soid= "";
  5674. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5675. // so#
  5676. ret[@"so#"] = nssoid;
  5677. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5678. if(poNumber==nil)
  5679. poNumber= "";
  5680. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5681. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5682. if(create_time==nil)
  5683. create_time= "";
  5684. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5685. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5686. int status = sqlite3_column_int(statement, 4);
  5687. int erpStatus = sqlite3_column_int(statement, 49);
  5688. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5689. // status
  5690. if (status > 1 && status != 3) {
  5691. status = erpStatus;
  5692. } else if (status == 3) {
  5693. status = 15;
  5694. }
  5695. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5696. ret[@"order_status"] = nsstatus;
  5697. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5698. if(company_name==nil)
  5699. company_name= "";
  5700. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5701. // company name
  5702. ret[@"company_name"] = nscompany_name;
  5703. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5704. if(customer_contact==nil)
  5705. customer_contact= "";
  5706. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5707. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5708. if(addr_1==nil)
  5709. addr_1="";
  5710. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5711. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5712. if(addr_2==nil)
  5713. addr_2="";
  5714. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5715. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5716. if(addr_3==nil)
  5717. addr_3="";
  5718. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5719. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5720. if(addr_4==nil)
  5721. addr_4="";
  5722. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5723. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5724. [arr_addr addObject:nsaddr_1];
  5725. [arr_addr addObject:nsaddr_2];
  5726. [arr_addr addObject:nsaddr_3];
  5727. [arr_addr addObject:nsaddr_4];
  5728. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5729. char *logist = (char*)sqlite3_column_text(statement, 11);
  5730. if(logist==nil)
  5731. logist= "";
  5732. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5733. if (status == -11 || status == 10 || status == 11) {
  5734. nslogist = [self textAtColumn:59 statement:statement];
  5735. };
  5736. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5737. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5738. shipping = @"Shipping To Be Quoted";
  5739. } else {
  5740. shippingFee = sqlite3_column_double(statement, 12);
  5741. }
  5742. // Shipping
  5743. ret[@"Shipping"] = shipping;
  5744. int have_lift_gate = sqlite3_column_int(statement, 17);
  5745. lift_gate = sqlite3_column_double(statement, 13);
  5746. // Liftgate Fee(No loading dock)
  5747. if (!have_lift_gate) {
  5748. lift_gate = 0;
  5749. }
  5750. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5751. if (sqlite3_column_int(statement, 57)) {
  5752. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5753. }
  5754. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5755. if(general_notes==nil)
  5756. general_notes= "";
  5757. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5758. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5759. if(internal_notes==nil)
  5760. internal_notes= "";
  5761. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5762. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5763. if(payment_type==nil)
  5764. payment_type= "";
  5765. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5766. // order info
  5767. orderinfo = [self textFileName:@"order_info.html"];
  5768. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5769. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5770. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5771. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5772. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5773. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5774. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5775. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5776. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5777. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5778. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5779. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5780. NSString *payment = nil;
  5781. if([nspayment_type isEqualToString:@"Credit Card"])
  5782. {
  5783. payment = [self textFileName:@"creditcardpayment.html"];
  5784. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5785. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5786. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5787. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5788. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5789. NSString *card_type = [self textAtColumn:42 statement:statement];
  5790. if (card_type.length > 0) { // 显示星号
  5791. card_type = @"****";
  5792. }
  5793. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5794. if (card_number.length > 0 && card_number.length > 4) {
  5795. for (int i = 0; i < card_number.length - 4; i++) {
  5796. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5797. }
  5798. } else {
  5799. card_number = @"";
  5800. }
  5801. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5802. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5803. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5804. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5805. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5806. card_expiration = @"****";
  5807. }
  5808. NSString *card_city = [self textAtColumn:46 statement:statement];
  5809. NSString *card_state = [self textAtColumn:47 statement:statement];
  5810. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5811. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5812. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5813. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5814. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5815. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5816. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5817. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5818. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5819. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5820. }
  5821. else
  5822. {
  5823. payment=[self textFileName:@"normalpayment.html"];
  5824. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5825. }
  5826. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5827. ret[@"result"]= [NSNumber numberWithInt:2];
  5828. // more info
  5829. moreInfo = [self textFileName:@"more_info.html"];
  5830. /*****ship to******/
  5831. // ShipToCompany_or_&nbsp
  5832. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5833. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5834. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5835. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5836. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5837. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5838. /*****ship from******/
  5839. // ShipFromCompany_or_&nbsp
  5840. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5841. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5842. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5843. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5844. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5845. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5846. /*****freight to******/
  5847. // FreightBillToCompany_or_&nbsp
  5848. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5849. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5850. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5851. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5852. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5853. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5854. /*****merchandise to******/
  5855. // MerchandiseBillToCompany_or_&nbsp
  5856. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5857. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5858. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5859. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5860. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5861. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5862. /*****return to******/
  5863. // ReturnToCompany_or_&nbsp
  5864. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5865. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5866. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5867. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5868. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5869. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5870. //
  5871. // DebugLog(@"more info : %@",moreInfo);
  5872. // handling fee
  5873. handlingFee = sqlite3_column_double(statement, 33);
  5874. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5875. if (sqlite3_column_int(statement, 58)) {
  5876. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5877. }
  5878. //
  5879. // customer info
  5880. customerID = [self textAtColumn:36 statement:statement];
  5881. // mode
  5882. ret[@"mode"] = appDelegate.mode;
  5883. // model_count
  5884. ret[@"model_count"] = @(0);
  5885. }
  5886. sqlite3_finalize(statement);
  5887. }
  5888. [iSalesDB close_db:db];
  5889. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5890. // "customer_email" = "Shui Hu";
  5891. // "customer_fax" = "";
  5892. // "customer_first_name" = F;
  5893. // "customer_last_name" = L;
  5894. // "customer_name" = ",da He Xiang Dong Liu A";
  5895. // "customer_phone" = "Hey Xuan Feng";
  5896. contactInfo[@"customer_phone"] = customer_phone;
  5897. contactInfo[@"customer_fax"] = customer_fax;
  5898. contactInfo[@"customer_email"] = customer_email;
  5899. NSString *first_name = @"";
  5900. NSString *last_name = @"";
  5901. if ([customer_contact isEqualToString:@""]) {
  5902. } else if ([customer_contact containsString:@" "]) {
  5903. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5904. first_name = [customer_contact substringToIndex:first_space_index];
  5905. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5906. }
  5907. contactInfo[@"customer_first_name"] = first_name;
  5908. contactInfo[@"customer_last_name"] = last_name;
  5909. ret[@"customerInfo"] = contactInfo;
  5910. // models
  5911. if (nssoid) {
  5912. __block double TotalCuft = 0;
  5913. __block double TotalWeight = 0;
  5914. __block int TotalCarton = 0;
  5915. __block double allItemPrice = 0;
  5916. 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];
  5917. sqlite3 *db1 = [iSalesDB get_db];
  5918. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5919. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5920. int product_id = sqlite3_column_int(stmt, 0);
  5921. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5922. int item_id = sqlite3_column_int(stmt, 7);
  5923. NSString* Price=nil;
  5924. if(str_price==nil)
  5925. {
  5926. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  5927. if(price==nil)
  5928. Price=@"No Price.";
  5929. else
  5930. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5931. }
  5932. else
  5933. {
  5934. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5935. }
  5936. double discount = sqlite3_column_double(stmt, 2);
  5937. int item_count = sqlite3_column_int(stmt, 3);
  5938. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5939. NSString *nsline_note=nil;
  5940. if(line_note!=nil)
  5941. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5942. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5943. // NSString *nsname = nil;
  5944. // if(name!=nil)
  5945. // nsname= [[NSString alloc]initWithUTF8String:name];
  5946. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5947. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5948. // NSString *nsdescription=nil;
  5949. // if(description!=nil)
  5950. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5951. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5952. // int stockUom = sqlite3_column_int(stmt, 8);
  5953. // int _id = sqlite3_column_int(stmt, 9);
  5954. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5955. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5956. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5957. int carton=[bsubtotaljson[@"carton"] intValue];
  5958. TotalCuft += cuft;
  5959. TotalWeight += weight;
  5960. TotalCarton += carton;
  5961. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  5962. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5963. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5964. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5965. itemjson[@"note"]=nsline_note;
  5966. itemjson[@"origin_price"] = Price;
  5967. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5968. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5969. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5970. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5971. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5972. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5973. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5974. if(itemjson[@"combine"] != nil)
  5975. {
  5976. // int citem=0;
  5977. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5978. for(int bc=0;bc<bcount;bc++)
  5979. {
  5980. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5981. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5982. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5983. subTotal += uprice * modulus * item_count;
  5984. }
  5985. }
  5986. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5987. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5988. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5989. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5990. allItemPrice += subTotal;
  5991. }];
  5992. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5993. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5994. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5995. // payments/Credits
  5996. // payments_and_credist = sqlite3_column_double(statement, 34);
  5997. payments_and_credist = allItemPrice;
  5998. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5999. // // total
  6000. // totalPrice = sqlite3_column_double(statement, 35);
  6001. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6002. } else {
  6003. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6004. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6005. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6006. // payments/Credits
  6007. payments_and_credist = 0;
  6008. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6009. }
  6010. // total
  6011. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6012. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6013. ret[@"order_info"]= orderinfo;
  6014. ret[@"more_order_info"] = moreInfo;
  6015. return [RAUtils dict2data:ret];
  6016. }
  6017. #pragma mark order list
  6018. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6019. NSString *nsstatus = @"";
  6020. switch (status) {
  6021. case 0:
  6022. {
  6023. nsstatus=@"Temp Order";
  6024. break;
  6025. }
  6026. case 1:
  6027. {
  6028. nsstatus=@"Saved Order";
  6029. break;
  6030. }
  6031. case 2: {
  6032. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6033. break;
  6034. }
  6035. case 3:
  6036. case 15:
  6037. {
  6038. nsstatus=@"Cancelled";
  6039. break;
  6040. }
  6041. case 10:
  6042. {
  6043. nsstatus=@"Quote Submitted";
  6044. break;
  6045. }
  6046. case 11:
  6047. {
  6048. nsstatus=@"Sales Order Submitted";
  6049. break;
  6050. }
  6051. case 12:
  6052. {
  6053. nsstatus=@"Processing";
  6054. break;
  6055. }
  6056. case 13:
  6057. {
  6058. nsstatus=@"Shipped";
  6059. break;
  6060. }
  6061. case 14:
  6062. {
  6063. nsstatus=@"Closed";
  6064. break;
  6065. }
  6066. case -11:
  6067. {
  6068. nsstatus = @"Ready For Submit";
  6069. break;
  6070. }
  6071. default:
  6072. break;
  6073. }
  6074. return nsstatus;
  6075. }
  6076. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6077. double total = 0;
  6078. __block double payments_and_credist = 0;
  6079. __block double allItemPrice = 0;
  6080. // sqlite3 *db1 = [iSalesDB get_db];
  6081. if (so_id) {
  6082. // 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];
  6083. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6084. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6085. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6086. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6087. int product_id = sqlite3_column_int(stmt, 0);
  6088. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6089. int discount = sqlite3_column_int(stmt, 2);
  6090. int item_count = sqlite3_column_int(stmt, 3);
  6091. // int item_id = sqlite3_column_int(stmt, 7);
  6092. int item_id = sqlite3_column_int(stmt, 4);
  6093. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6094. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6095. NSString* Price=nil;
  6096. if(str_price==nil)
  6097. {
  6098. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6099. if(price==nil)
  6100. Price=@"No Price.";
  6101. else
  6102. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6103. }
  6104. else
  6105. {
  6106. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6107. }
  6108. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6109. itemjson[@"The unit price"]=Price;
  6110. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6111. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6112. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6113. if(itemjson[@"combine"] != nil)
  6114. {
  6115. // int citem=0;
  6116. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6117. for(int bc=0;bc<bcount;bc++)
  6118. {
  6119. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6120. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6121. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6122. subTotal += uprice * modulus * item_count;
  6123. }
  6124. }
  6125. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6126. allItemPrice += subTotal;
  6127. }];
  6128. payments_and_credist = allItemPrice;
  6129. } else {
  6130. // payments/Credits
  6131. payments_and_credist = 0;
  6132. }
  6133. // lift_gate handlingFee shippingFee
  6134. __block double lift_gate = 0;
  6135. __block double handlingFee = 0;
  6136. __block double shippingFee = 0;
  6137. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6138. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6139. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6140. if (have_lift_gate) {
  6141. lift_gate = sqlite3_column_double(stmt, 1);
  6142. }
  6143. handlingFee = sqlite3_column_double(stmt, 2);
  6144. shippingFee = sqlite3_column_double(stmt, 3);
  6145. }];
  6146. // total
  6147. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6148. if (close) {
  6149. [iSalesDB close_db:db1];
  6150. }
  6151. return total;
  6152. }
  6153. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6154. {
  6155. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6156. int limit = [[params valueForKey:@"limit"] intValue];
  6157. int offset = [[params valueForKey:@"offset"] intValue];
  6158. NSString* keyword = [params valueForKey:@"keyWord"];
  6159. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6160. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6161. NSString* where=@"1 = 1";
  6162. if(keyword.length>0)
  6163. 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]];
  6164. if (orderStatus.length > 0) {
  6165. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6166. if (order_status_array.count == 1) {
  6167. int status_value = [[order_status_array firstObject] integerValue];
  6168. if (status_value <= 1 || status_value == 3) {
  6169. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6170. } else {
  6171. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6172. }
  6173. } else if (order_status_array.count > 1) {
  6174. for (int i = 0; i < order_status_array.count;i++) {
  6175. NSString *status = order_status_array[i];
  6176. NSString *condition = @" or";
  6177. if (i == 0) {
  6178. condition = @" and (";
  6179. }
  6180. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6181. int status_value = [status integerValue];
  6182. if (status_value <= 1 || status_value == 3) {
  6183. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6184. } else {
  6185. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6186. }
  6187. }
  6188. where = [where stringByAppendingString:@" )"];
  6189. }
  6190. }
  6191. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6192. // DebugLog(@"order list sql: %@",sqlQuery);
  6193. sqlite3 *db = [iSalesDB get_db];
  6194. sqlite3_stmt * statement;
  6195. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6196. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6197. {
  6198. int count=0;
  6199. while (sqlite3_step(statement) == SQLITE_ROW)
  6200. {
  6201. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6202. int order_id = sqlite3_column_double(statement, 0);
  6203. char *soid = (char*)sqlite3_column_text(statement, 1);
  6204. if(soid==nil)
  6205. soid= "";
  6206. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6207. int status = sqlite3_column_double(statement, 2);
  6208. int erpStatus = sqlite3_column_double(statement, 9);
  6209. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6210. if(sales_rep==nil)
  6211. sales_rep= "";
  6212. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6213. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6214. if(create_by==nil)
  6215. create_by= "";
  6216. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6217. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6218. if(company_name==nil)
  6219. company_name= "";
  6220. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6221. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6222. if(create_time==nil)
  6223. create_time= "";
  6224. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6225. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6226. // double total_price = sqlite3_column_double(statement, 7);
  6227. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6228. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6229. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6230. int offline_edit = sqlite3_column_int(statement, 10);
  6231. // ": "JH",
  6232. // "": "$8307.00",
  6233. // "": "MOB1608050001",
  6234. // "": "ArpithaT",
  6235. // "": "1st Stage Property Transformations",
  6236. // "": "JANICE SUTTON",
  6237. // "": 2255,
  6238. // "": "08/02/2016 09:49:18",
  6239. // "": 1,
  6240. // "": "Saved Order"
  6241. // "": "1470384050483",
  6242. // "model_count": "6 / 28"
  6243. item[@"sales_rep"]= nssales_rep;
  6244. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6245. item[@"so#"]= nssoid;
  6246. item[@"create_by"]= nscreate_by;
  6247. item[@"customer_name"]= nscompany_name;
  6248. item[@"customer_contact"] = customer_contact;
  6249. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6250. item[@"purchase_time"]= nscreate_time;
  6251. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  6252. item[@"order_status"]= nsstatus;
  6253. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6254. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6255. // item[@"model_count"]
  6256. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6257. count++;
  6258. }
  6259. ret[@"count"]= [NSNumber numberWithInt:count];
  6260. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6261. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6262. ret[@"result"]= [NSNumber numberWithInt:2];
  6263. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6264. ret[@"time_zone"] = @"PST";
  6265. sqlite3_finalize(statement);
  6266. }
  6267. 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];
  6268. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6269. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6270. [iSalesDB close_db:db];
  6271. return [RAUtils dict2data:ret];
  6272. }
  6273. #pragma mark update gnotes
  6274. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6275. {
  6276. DebugLog(@"params: %@",params);
  6277. // comments = Meyoyoyoyoyoyoy;
  6278. // orderCode = MOB1608110001;
  6279. // password = 123456;
  6280. // user = EvanK;
  6281. 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]];
  6282. int ret = [iSalesDB execSql:sql];
  6283. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6284. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6285. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6286. // [dic setValue:@"160409" forKey:@"min_ver"];
  6287. return [RAUtils dict2data:dic];
  6288. }
  6289. #pragma mark move to wishlist
  6290. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6291. {
  6292. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6293. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6294. _id = [NSString stringWithFormat:@"(%@)",_id];
  6295. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6296. sqlite3 *db = [iSalesDB get_db];
  6297. __block NSString *product_id = @"";
  6298. __block NSString *item_count_str = @"";
  6299. // __block NSString *item_id = nil;
  6300. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6301. // product_id = [self textAtColumn:0 statement:stmt];
  6302. int item_count = sqlite3_column_int(stmt, 1);
  6303. // item_id = [self textAtColumn:2 statement:stmt];
  6304. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6305. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6306. if (p_id.length) {
  6307. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6308. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6309. }
  6310. }];
  6311. [iSalesDB close_db:db];
  6312. // 去除第一个,
  6313. product_id = [product_id substringFromIndex:1];
  6314. item_count_str = [item_count_str substringFromIndex:1];
  6315. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6316. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6317. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6318. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6319. sqlite3 *db1 = [iSalesDB get_db];
  6320. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6321. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6322. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6323. // 删除
  6324. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6325. int ret = [iSalesDB execSql:deleteSql db:db1];
  6326. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6327. [iSalesDB close_db:db1];
  6328. return [RAUtils dict2data:dic];
  6329. }
  6330. #pragma mark cart delete
  6331. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6332. {
  6333. // cartItemId = 548;
  6334. // orderCode = MOB1608110001;
  6335. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6336. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6337. _id = [NSString stringWithFormat:@"(%@)",_id];
  6338. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6339. int ret = [iSalesDB execSql:sql];
  6340. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6341. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6342. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6343. // [dic setValue:@"160409" forKey:@"min_ver"];
  6344. return [RAUtils dict2data:dic];
  6345. }
  6346. #pragma mark set price
  6347. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6348. {
  6349. DebugLog(@"cart set price params: %@",params);
  6350. // "cartitem_id" = 1;
  6351. // discount = "0.000000";
  6352. // "item_note" = "";
  6353. // price = "269.000000";
  6354. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6355. NSString *notes = [self valueInParams:params key:@"item_note"];
  6356. NSString *discount = [self valueInParams:params key:@"discount"];
  6357. NSString *price = [self valueInParams:params key:@"price"];
  6358. // bool badd_price_changed=false;
  6359. // sqlite3* db=[iSalesDB get_db];
  6360. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6361. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6362. // NSRange range;
  6363. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6364. //
  6365. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6366. // badd_price_changed=true;
  6367. // [iSalesDB close_db:db];
  6368. //
  6369. // if(badd_price_changed)
  6370. // {
  6371. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6372. // }
  6373. //
  6374. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6375. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6376. int ret = [iSalesDB execSql:sql];
  6377. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6378. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6379. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6380. // [dic setValue:@"160409" forKey:@"min_ver"];
  6381. return [RAUtils dict2data:dic];
  6382. }
  6383. #pragma mark set line notes
  6384. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6385. {
  6386. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6387. NSString *notes = [self valueInParams:params key:@"notes"];
  6388. notes = [self translateSingleQuote:notes];
  6389. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6390. int ret = [iSalesDB execSql:sql];
  6391. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6392. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6393. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6394. // [dic setValue:@"160409" forKey:@"min_ver"];
  6395. return [RAUtils dict2data:dic];
  6396. }
  6397. #pragma mark set qty
  6398. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6399. {
  6400. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6401. int item_count = [params[@"inputInt"] integerValue];
  6402. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6403. int ret = [iSalesDB execSql:sql];
  6404. __block int item_id = 0;
  6405. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6406. item_id = sqlite3_column_int(stmt, 0);
  6407. }];
  6408. sqlite3 *db = [iSalesDB get_db];
  6409. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6410. [iSalesDB close_db:db];
  6411. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6412. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6413. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6414. // [dic setValue:@"160409" forKey:@"min_ver"];
  6415. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6416. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6417. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6418. return [RAUtils dict2data:dic];
  6419. }
  6420. #pragma mark place order
  6421. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6422. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6423. if (aname) {
  6424. [dic setValue:aname forKey:@"aname"];
  6425. }
  6426. if (control) {
  6427. [dic setValue:control forKey:@"control"];
  6428. }
  6429. if (keyboard) {
  6430. [dic setValue:keyboard forKey:@"keyboard"];
  6431. }
  6432. if (name) {
  6433. [dic setValue:name forKey:@"name"];
  6434. }
  6435. if (value) {
  6436. [dic setValue:value forKey:@"value"];
  6437. }
  6438. return dic;
  6439. }
  6440. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6441. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6442. orderCode = [self translateSingleQuote:orderCode];
  6443. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6444. 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];
  6445. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6446. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6447. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6448. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6449. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6450. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6451. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6452. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6453. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6454. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6455. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6456. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6457. NSString *email = [self textAtColumn:11 statement:stmt];
  6458. NSString *phone = [self textAtColumn:12 statement:stmt];
  6459. NSString *fax = [self textAtColumn:13 statement:stmt];
  6460. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6461. NSString *city = [self textAtColumn:15 statement:stmt];
  6462. NSString *state = [self textAtColumn:16 statement:stmt];
  6463. NSString *country = [self textAtColumn:17 statement:stmt];
  6464. NSString *name = [self textAtColumn:18 statement:stmt];
  6465. // contact id
  6466. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6467. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6468. [contact_id_dic setValue:@"text" forKey:@"control"];
  6469. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6470. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6471. [contact_id_dic setValue:@"true" forKey:@"required"];
  6472. [contact_id_dic setValue:contact_id forKey:@"value"];
  6473. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6474. // business card
  6475. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6476. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6477. [business_card_dic setValue:@"img" forKey:@"control"];
  6478. [business_card_dic setValue:@"1" forKey:@"disable"];
  6479. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6480. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6481. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6482. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6483. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6484. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6485. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6486. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6487. // fax
  6488. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6489. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6490. // zipcode
  6491. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6492. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6493. // city
  6494. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6495. [customer_dic setValue:city_dic forKey:@"item_12"];
  6496. // state
  6497. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6498. [customer_dic setValue:state_dic forKey:@"item_13"];
  6499. // country
  6500. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6501. [customer_dic setValue:country_dic forKey:@"item_14"];
  6502. // company name
  6503. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6504. [customer_dic setValue:company_dic forKey:@"item_2"];
  6505. // addr_1
  6506. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6507. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6508. // addr_2
  6509. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6510. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6511. // addr_3
  6512. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6513. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6514. // addr_4
  6515. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6516. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6517. // Contact
  6518. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6519. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6520. // email
  6521. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6522. [customer_dic setValue:email_dic forKey:@"item_8"];
  6523. // phone
  6524. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6525. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6526. // title
  6527. [customer_dic setValue:@"Customer" forKey:@"title"];
  6528. // count
  6529. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6530. }];
  6531. // setting
  6532. NSDictionary *setting = params[@"setting"];
  6533. NSNumber *hide = setting[@"CustomerHide"];
  6534. [customer_dic setValue:hide forKey:@"hide"];
  6535. return customer_dic;
  6536. }
  6537. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6538. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6539. // setting
  6540. NSDictionary *setting = params[@"setting"];
  6541. NSNumber *hide = setting[@"ShipToHide"];
  6542. [dic setValue:hide forKey:@"hide"];
  6543. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6544. orderCode = [self translateSingleQuote:orderCode];
  6545. 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];
  6546. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6547. NSString *cid = [self textAtColumn:0 statement:stmt];
  6548. NSString *name = [self textAtColumn:1 statement:stmt];
  6549. NSString *ext = [self textAtColumn:2 statement:stmt];
  6550. NSString *contact = [self textAtColumn:3 statement:stmt];
  6551. NSString *email = [self textAtColumn:4 statement:stmt];
  6552. NSString *fax = [self textAtColumn:5 statement:stmt];
  6553. NSString *phone = [self textAtColumn:6 statement:stmt];
  6554. // count
  6555. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6556. // title
  6557. [dic setValue:@"Ship To" forKey:@"title"];
  6558. // choose
  6559. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6560. [choose_dic setValue:@"choose" forKey:@"aname"];
  6561. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6562. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6563. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6564. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6565. @"receive_contact" : @"customer_contact",
  6566. @"receive_email" : @"customer_email",
  6567. @"receive_ext" : @"customer_contact_ext",
  6568. @"receive_fax" : @"customer_fax",
  6569. @"receive_name" : @"customer_name",
  6570. @"receive_phone" : @"customer_phone"},
  6571. @"refresh" : [NSNumber numberWithInteger:1],
  6572. @"type" : @"pull"};
  6573. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6574. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6575. @"name" : @"Add new address",
  6576. @"refresh" : [NSNumber numberWithInteger:1],
  6577. @"value" : @"new_addr"
  6578. };
  6579. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6580. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6581. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6582. @"receive_contact" : @"customer_contact",
  6583. @"receive_email" : @"customer_email",
  6584. @"receive_ext" : @"customer_contact_ext",
  6585. @"receive_fax" : @"customer_fax",
  6586. @"receive_name" : @"customer_name",
  6587. @"receive_phone" : @"customer_phone"},
  6588. @"name" : @"select_ship_to",
  6589. @"refresh" : [NSNumber numberWithInteger:1],
  6590. @"value" : @"Sales_Order_Ship_To"};
  6591. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6592. [dic setValue:choose_dic forKey:@"item_0"];
  6593. // contact id
  6594. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6595. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6596. [contact_id_dic setValue:@"true" forKey:@"required"];
  6597. [dic setValue:contact_id_dic forKey:@"item_1"];
  6598. // company name
  6599. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6600. [dic setValue:company_name_dic forKey:@"item_2"];
  6601. // address
  6602. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6603. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6604. if ([required integerValue]) {
  6605. [ext_dic setValue:@"true" forKey:@"required"];
  6606. }
  6607. [dic setValue:ext_dic forKey:@"item_3"];
  6608. // contact
  6609. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6610. [dic setValue:contact_dic forKey:@"item_4"];
  6611. // phone
  6612. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6613. [dic setValue:phone_dic forKey:@"item_5"];
  6614. // fax
  6615. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6616. [dic setValue:fax_dic forKey:@"item_6"];
  6617. // email
  6618. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6619. [dic setValue:email_dic forKey:@"item_7"];
  6620. }];
  6621. return dic;
  6622. }
  6623. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6624. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6625. // setting
  6626. NSDictionary *setting = params[@"setting"];
  6627. NSNumber *hide = setting[@"ShipFromHide"];
  6628. [dic setValue:hide forKey:@"hide"];
  6629. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6630. orderCode = [self translateSingleQuote:orderCode];
  6631. 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];
  6632. 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';";
  6633. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6634. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6635. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6636. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6637. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6638. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6639. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6640. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6641. if (!cid.length) {
  6642. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6643. cid = [self textAtColumn:0 statement:statment];
  6644. name = [self textAtColumn:1 statement:statment];
  6645. ext = [self textAtColumn:2 statement:statment];
  6646. contact = [self textAtColumn:3 statement:statment];
  6647. email = [self textAtColumn:4 statement:statment];
  6648. fax = [self textAtColumn:5 statement:statment];
  6649. phone = [self textAtColumn:6 statement:statment];
  6650. }];
  6651. }
  6652. // count
  6653. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6654. // title
  6655. [dic setValue:@"Ship From" forKey:@"title"];
  6656. // hide
  6657. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6658. // choose
  6659. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6660. [choose_dic setValue:@"choose" forKey:@"aname"];
  6661. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6662. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6663. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6664. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6665. @"sender_contact" : @"customer_contact",
  6666. @"sender_email" : @"customer_email",
  6667. @"sender_ext" : @"customer_contact_ext",
  6668. @"sender_fax" : @"customer_fax",
  6669. @"sender_name" : @"customer_name",
  6670. @"sender_phone" : @"customer_phone"},
  6671. @"name" : @"select_cid",
  6672. @"refresh" : [NSNumber numberWithInteger:0],
  6673. @"value" : @"Sales_Order_Ship_From"};
  6674. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6675. [dic setValue:choose_dic forKey:@"item_0"];
  6676. // contact id
  6677. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6678. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6679. [contact_id_dic setValue:@"true" forKey:@"required"];
  6680. [dic setValue:contact_id_dic forKey:@"item_1"];
  6681. // company name
  6682. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6683. [dic setValue:company_name_dic forKey:@"item_2"];
  6684. // address
  6685. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6686. [dic setValue:ext_dic forKey:@"item_3"];
  6687. // contact
  6688. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6689. [dic setValue:contact_dic forKey:@"item_4"];
  6690. // phone
  6691. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6692. [dic setValue:phone_dic forKey:@"item_5"];
  6693. // fax
  6694. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6695. [dic setValue:fax_dic forKey:@"item_6"];
  6696. // email
  6697. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6698. [dic setValue:email_dic forKey:@"item_7"];
  6699. }];
  6700. return dic;
  6701. }
  6702. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6703. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6704. // setting
  6705. NSDictionary *setting = params[@"setting"];
  6706. NSNumber *hide = setting[@"FreightBillToHide"];
  6707. [dic setValue:hide forKey:@"hide"];
  6708. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6709. orderCode = [self translateSingleQuote:orderCode];
  6710. 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];
  6711. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6712. NSString *cid = [self textAtColumn:0 statement:stmt];
  6713. NSString *name = [self textAtColumn:1 statement:stmt];
  6714. NSString *ext = [self textAtColumn:2 statement:stmt];
  6715. NSString *contact = [self textAtColumn:3 statement:stmt];
  6716. NSString *email = [self textAtColumn:4 statement:stmt];
  6717. NSString *fax = [self textAtColumn:5 statement:stmt];
  6718. NSString *phone = [self textAtColumn:6 statement:stmt];
  6719. // count
  6720. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6721. // title
  6722. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6723. // hide
  6724. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6725. // choose
  6726. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6727. [choose_dic setValue:@"choose" forKey:@"aname"];
  6728. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6729. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6730. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6731. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6732. @"shipping_billto_contact" : @"receive_contact",
  6733. @"shipping_billto_email" : @"receive_email",
  6734. @"shipping_billto_ext" : @"receive_ext",
  6735. @"shipping_billto_fax" : @"receive_fax",
  6736. @"shipping_billto_name" : @"receive_name",
  6737. @"shipping_billto_phone" : @"receive_phone"},
  6738. @"type" : @"pull"};
  6739. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6740. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6741. @"type" : @"pull",
  6742. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6743. @"shipping_billto_contact" : @"customer_contact",
  6744. @"shipping_billto_email" : @"customer_email",
  6745. @"shipping_billto_ext" : @"customer_contact_ext",
  6746. @"shipping_billto_fax" : @"customer_fax",
  6747. @"shipping_billto_name" : @"customer_name",
  6748. @"shipping_billto_phone" : @"customer_phone"}
  6749. };
  6750. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6751. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6752. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6753. @"shipping_billto_contact" : @"sender_contact",
  6754. @"shipping_billto_email" : @"sender_email",
  6755. @"shipping_billto_ext" : @"sender_ext",
  6756. @"shipping_billto_fax" : @"sender_fax",
  6757. @"shipping_billto_name" : @"sender_name",
  6758. @"shipping_billto_phone" : @"sender_phone"},
  6759. @"type" : @"pull"
  6760. };
  6761. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6762. NSDictionary *select_freight_bill_to_dic = @{
  6763. @"aname" : @"Select freight bill to",
  6764. @"name" : @"select_cid",
  6765. @"refresh" : [NSNumber numberWithInteger:0],
  6766. @"value" : @"Sales_Order_Freight_Bill_To",
  6767. @"key_map" : @{
  6768. @"shipping_billto_cid" : @"customer_cid",
  6769. @"shipping_billto_contact" : @"customer_contact",
  6770. @"shipping_billto_email" : @"customer_email",
  6771. @"shipping_billto_ext" : @"customer_contact_ext",
  6772. @"shipping_billto_fax" : @"customer_fax",
  6773. @"shipping_billto_name" : @"customer_name",
  6774. @"shipping_billto_phone" : @"customer_phone"
  6775. }
  6776. };
  6777. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6778. [dic setValue:choose_dic forKey:@"item_0"];
  6779. // contact id
  6780. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6781. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6782. [contact_id_dic setValue:@"true" forKey:@"required"];
  6783. [dic setValue:contact_id_dic forKey:@"item_1"];
  6784. // company name
  6785. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6786. [dic setValue:company_name_dic forKey:@"item_2"];
  6787. // address
  6788. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6789. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6790. if ([ext_required integerValue]) {
  6791. [ext_dic setValue:@"true" forKey:@"required"];
  6792. }
  6793. [dic setValue:ext_dic forKey:@"item_3"];
  6794. // contact
  6795. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6796. [dic setValue:contact_dic forKey:@"item_4"];
  6797. // phone
  6798. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6799. [dic setValue:phone_dic forKey:@"item_5"];
  6800. // fax
  6801. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6802. [dic setValue:fax_dic forKey:@"item_6"];
  6803. // email
  6804. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6805. [dic setValue:email_dic forKey:@"item_7"];
  6806. }];
  6807. return dic;
  6808. }
  6809. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6810. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6811. // setting
  6812. NSDictionary *setting = params[@"setting"];
  6813. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6814. [dic setValue:hide forKey:@"hide"];
  6815. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6816. orderCode = [self translateSingleQuote:orderCode];
  6817. 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];
  6818. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6819. NSString *cid = [self textAtColumn:0 statement:stmt];
  6820. NSString *name = [self textAtColumn:1 statement:stmt];
  6821. NSString *ext = [self textAtColumn:2 statement:stmt];
  6822. NSString *contact = [self textAtColumn:3 statement:stmt];
  6823. NSString *email = [self textAtColumn:4 statement:stmt];
  6824. NSString *fax = [self textAtColumn:5 statement:stmt];
  6825. NSString *phone = [self textAtColumn:6 statement:stmt];
  6826. // count
  6827. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6828. // title
  6829. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6830. // hide
  6831. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6832. // choose
  6833. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6834. [choose_dic setValue:@"choose" forKey:@"aname"];
  6835. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6836. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6837. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6838. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6839. @"billing_contact" : @"receive_contact",
  6840. @"billing_email" : @"receive_email",
  6841. @"billing_ext" : @"receive_ext",
  6842. @"billing_fax" : @"receive_fax",
  6843. @"billing_name" : @"receive_name",
  6844. @"billing_phone" : @"receive_phone"},
  6845. @"type" : @"pull"};
  6846. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6847. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6848. @"type" : @"pull",
  6849. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6850. @"billing_contact" : @"customer_contact",
  6851. @"billing_email" : @"customer_email",
  6852. @"billing_ext" : @"customer_contact_ext",
  6853. @"billing_fax" : @"customer_fax",
  6854. @"billing_name" : @"customer_name",
  6855. @"billing_phone" : @"customer_phone"}
  6856. };
  6857. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6858. NSDictionary *select_bill_to_dic = @{
  6859. @"aname" : @"Select bill to",
  6860. @"name" : @"select_cid",
  6861. @"refresh" : [NSNumber numberWithInteger:0],
  6862. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6863. @"key_map" : @{
  6864. @"billing_cid" : @"customer_cid",
  6865. @"billing_contact" : @"customer_contact",
  6866. @"billing_email" : @"customer_email",
  6867. @"billing_ext" : @"customer_contact_ext",
  6868. @"billing_fax" : @"customer_fax",
  6869. @"billing_name" : @"customer_name",
  6870. @"billing_phone" : @"customer_phone"
  6871. }
  6872. };
  6873. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6874. [dic setValue:choose_dic forKey:@"item_0"];
  6875. // contact id
  6876. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6877. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6878. [contact_id_dic setValue:@"true" forKey:@"required"];
  6879. [dic setValue:contact_id_dic forKey:@"item_1"];
  6880. // company name
  6881. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6882. [dic setValue:company_name_dic forKey:@"item_2"];
  6883. // address
  6884. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6885. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6886. if ([ext_required integerValue]) {
  6887. [ext_dic setValue:@"true" forKey:@"required"];
  6888. }
  6889. [dic setValue:ext_dic forKey:@"item_3"];
  6890. // contact
  6891. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6892. [dic setValue:contact_dic forKey:@"item_4"];
  6893. // phone
  6894. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6895. [dic setValue:phone_dic forKey:@"item_5"];
  6896. // fax
  6897. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6898. [dic setValue:fax_dic forKey:@"item_6"];
  6899. // email
  6900. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6901. [dic setValue:email_dic forKey:@"item_7"];
  6902. }];
  6903. return dic;
  6904. }
  6905. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6906. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6907. // setting
  6908. NSDictionary *setting = params[@"setting"];
  6909. NSNumber *hide = setting[@"ReturnToHide"];
  6910. [dic setValue:hide forKey:@"hide"];
  6911. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6912. orderCode = [self translateSingleQuote:orderCode];
  6913. 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];
  6914. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6915. NSString *cid = [self textAtColumn:0 statement:stmt];
  6916. NSString *name = [self textAtColumn:1 statement:stmt];
  6917. NSString *ext = [self textAtColumn:2 statement:stmt];
  6918. NSString *contact = [self textAtColumn:3 statement:stmt];
  6919. NSString *email = [self textAtColumn:4 statement:stmt];
  6920. NSString *fax = [self textAtColumn:5 statement:stmt];
  6921. NSString *phone = [self textAtColumn:6 statement:stmt];
  6922. // count
  6923. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6924. // title
  6925. [dic setValue:@"Return To" forKey:@"title"];
  6926. // hide
  6927. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6928. // choose
  6929. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6930. [choose_dic setValue:@"choose" forKey:@"aname"];
  6931. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6932. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6933. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6934. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6935. @"returnto_contact" : @"sender_contact",
  6936. @"returnto_email" : @"sender_email",
  6937. @"returnto_ext" : @"sender_ext",
  6938. @"returnto_fax" : @"sender_fax",
  6939. @"returnto_name" : @"sender_name",
  6940. @"returnto_phone" : @"sender_phone"},
  6941. @"type" : @"pull"};
  6942. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6943. NSDictionary *select_return_to_dic = @{
  6944. @"aname" : @"Select return to",
  6945. @"name" : @"select_cid",
  6946. @"refresh" : [NSNumber numberWithInteger:0],
  6947. @"value" : @"Contact_Return_To",
  6948. @"key_map" : @{
  6949. @"returnto_cid" : @"customer_cid",
  6950. @"returnto_contact" : @"customer_contact",
  6951. @"returnto_email" : @"customer_email",
  6952. @"returnto_ext" : @"customer_contact_ext",
  6953. @"returnto_fax" : @"customer_fax",
  6954. @"returnto_name" : @"customer_name",
  6955. @"returnto_phone" : @"customer_phone"
  6956. }
  6957. };
  6958. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6959. [dic setValue:choose_dic forKey:@"item_0"];
  6960. // contact id
  6961. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6962. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6963. [contact_id_dic setValue:@"true" forKey:@"required"];
  6964. [dic setValue:contact_id_dic forKey:@"item_1"];
  6965. // company name
  6966. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6967. [dic setValue:company_name_dic forKey:@"item_2"];
  6968. // address
  6969. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6970. [dic setValue:ext_dic forKey:@"item_3"];
  6971. // contact
  6972. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6973. [dic setValue:contact_dic forKey:@"item_4"];
  6974. // phone
  6975. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6976. [dic setValue:phone_dic forKey:@"item_5"];
  6977. // fax
  6978. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6979. [dic setValue:fax_dic forKey:@"item_6"];
  6980. // email
  6981. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6982. [dic setValue:email_dic forKey:@"item_7"];
  6983. }];
  6984. return dic;
  6985. }
  6986. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6987. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6988. orderCode = [self translateSingleQuote:orderCode];
  6989. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6990. __block double TotalCuft = 0;
  6991. __block double TotalWeight = 0;
  6992. __block int TotalCarton = 0;
  6993. __block double payments = 0;
  6994. // setting
  6995. NSDictionary *setting = params[@"setting"];
  6996. NSNumber *hide = setting[@"ModelInformationHide"];
  6997. [dic setValue:hide forKey:@"hide"];
  6998. 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];
  6999. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7000. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7001. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7002. // item id
  7003. int item_id = sqlite3_column_int(stmt, 0);
  7004. // count
  7005. int item_count = sqlite3_column_int(stmt, 1);
  7006. // stockUom
  7007. int stockUom = sqlite3_column_int(stmt, 2);
  7008. // unit price
  7009. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7010. NSString* Price=nil;
  7011. if([str_price isEqualToString:@""])
  7012. {
  7013. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7014. if(price==nil)
  7015. Price=@"No Price.";
  7016. else
  7017. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7018. }
  7019. else
  7020. {
  7021. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7022. }
  7023. // discount
  7024. double discount = sqlite3_column_double(stmt, 4);
  7025. // name
  7026. NSString *name = [self textAtColumn:5 statement:stmt];
  7027. // description
  7028. NSString *description = [self textAtColumn:6 statement:stmt];
  7029. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7030. // line note
  7031. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7032. int avaulability = sqlite3_column_int(stmt, 8);
  7033. // img
  7034. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7035. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7036. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7037. if(combine != nil)
  7038. {
  7039. // int citem=0;
  7040. int bcount=[[combine valueForKey:@"count"] intValue];
  7041. for(int bc=0;bc<bcount;bc++)
  7042. {
  7043. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7044. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7045. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7046. subTotal += uprice * modulus * item_count;
  7047. }
  7048. }
  7049. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7050. [model_dic setValue:@"model" forKey:@"control"];
  7051. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7052. [model_dic setValue:description forKey:@"description"];
  7053. [model_dic setValue:line_note forKey:@"note"];
  7054. [model_dic setValue:img forKey:@"img_url"];
  7055. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7056. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7057. [model_dic setValue:Price forKey:@"unit_price"];
  7058. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7059. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7060. if (combine) {
  7061. [model_dic setValue:combine forKey:@"combine"];
  7062. }
  7063. // well,what under the row is the info for total
  7064. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7065. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7066. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7067. int carton=[bsubtotaljson[@"carton"] intValue];
  7068. TotalCuft += cuft;
  7069. TotalWeight += weight;
  7070. TotalCarton += carton;
  7071. payments += subTotal;
  7072. //---------------------------
  7073. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7074. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7075. }];
  7076. [dic setValue:@"Model Information" forKey:@"title"];
  7077. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7078. return dic;
  7079. }
  7080. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7081. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7082. [dic setValue:@"Remarks Content" forKey:@"title"];
  7083. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7084. // setting
  7085. NSDictionary *setting = params[@"setting"];
  7086. NSNumber *hide = setting[@"RemarksContentHide"];
  7087. [dic setValue:hide forKey:@"hide"];
  7088. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7089. orderCode = [self translateSingleQuote:orderCode];
  7090. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7091. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7092. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7093. int mustCall = sqlite3_column_int(stmt, 1);
  7094. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7095. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7096. NSDictionary *po_dic = @{
  7097. @"aname" : @"PO#",
  7098. @"control" : @"edit",
  7099. @"keyboard" : @"text",
  7100. @"name" : @"poNumber",
  7101. @"value" : poNumber
  7102. };
  7103. NSDictionary *must_call_dic = @{
  7104. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7105. @"control" : @"switch",
  7106. @"name" : @"must_call",
  7107. @"value" : mustCall ? @"true" : @"false"
  7108. };
  7109. NSDictionary *general_notes_dic = @{
  7110. @"aname" : @"General notes",
  7111. @"control" : @"text_view",
  7112. @"keyboard" : @"text",
  7113. @"name" : @"comments",
  7114. @"value" : generalNotes
  7115. };
  7116. NSDictionary *internal_notes_dic = @{
  7117. @"aname" : @"Internal notes",
  7118. @"control" : @"text_view",
  7119. @"keyboard" : @"text",
  7120. @"name" : @"internal_notes",
  7121. @"value" : internalNotes
  7122. };
  7123. [dic setValue:po_dic forKey:@"item_0"];
  7124. [dic setValue:must_call_dic forKey:@"item_1"];
  7125. [dic setValue:general_notes_dic forKey:@"item_2"];
  7126. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7127. }];
  7128. return dic;
  7129. }
  7130. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7131. // params
  7132. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7133. orderCode = [self translateSingleQuote:orderCode];
  7134. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7135. // setting
  7136. NSDictionary *setting = params[@"setting"];
  7137. NSNumber *hide = setting[@"OrderTotalHide"];
  7138. [dic setValue:hide forKey:@"hide"];
  7139. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7140. __block double lift_gate_value = 0;
  7141. __block double handling_fee = 0;
  7142. __block double shipping = 0;
  7143. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7144. int lift_gate = sqlite3_column_int(stmt, 0);
  7145. lift_gate_value = sqlite3_column_double(stmt, 1);
  7146. shipping = sqlite3_column_double(stmt, 2);
  7147. handling_fee = sqlite3_column_double(stmt, 3);
  7148. if (!lift_gate) {
  7149. lift_gate_value = 0;
  7150. }
  7151. }];
  7152. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7153. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7154. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7155. double payments = [[total valueForKey:@"payments"] doubleValue];
  7156. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7157. double totalPrice = payments;
  7158. [dic setValue:@"Order Total" forKey:@"title"];
  7159. NSDictionary *payments_dic = @{
  7160. @"align" : @"right",
  7161. @"aname" : @"Payments/Credits",
  7162. @"control" : @"text",
  7163. @"name" : @"paymentsAndCredits",
  7164. @"type" : @"price",
  7165. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7166. };
  7167. [dic setValue:payments_dic forKey:@"item_0"];
  7168. NSDictionary *handling_fee_dic = @{
  7169. @"align" : @"right",
  7170. @"aname" : @"Handling Fee",
  7171. @"control" : @"text",
  7172. @"name" : @"handling_fee_value",
  7173. @"required" : @"true",
  7174. @"type" : @"price",
  7175. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7176. };
  7177. NSDictionary *shipping_dic = @{
  7178. @"align" : @"right",
  7179. @"aname" : @"Shipping*",
  7180. @"control" : @"text",
  7181. @"name" : @"shipping",
  7182. @"required" : @"true",
  7183. @"type" : @"price",
  7184. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7185. };
  7186. NSDictionary *lift_gate_dic = @{
  7187. @"align" : @"right",
  7188. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7189. @"control" : @"text",
  7190. @"name" : @"lift_gate_value",
  7191. @"required" : @"true",
  7192. @"type" : @"price",
  7193. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7194. };
  7195. int item_count = 1;
  7196. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7197. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7198. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7199. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7200. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7201. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7202. } else {
  7203. }
  7204. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7205. NSDictionary *total_price_dic = @{
  7206. @"align" : @"right",
  7207. @"aname" : @"Total",
  7208. @"control" : @"text",
  7209. @"name" : @"totalPrice",
  7210. @"type" : @"price",
  7211. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7212. };
  7213. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7214. NSDictionary *total_cuft_dic = @{
  7215. @"align" : @"right",
  7216. @"aname" : @"Total Cuft",
  7217. @"control" : @"text",
  7218. @"name" : @"",
  7219. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7220. };
  7221. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7222. NSDictionary *total_weight_dic = @{
  7223. @"align" : @"right",
  7224. @"aname" : @"Total Weight",
  7225. @"control" : @"text",
  7226. @"name" : @"",
  7227. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7228. };
  7229. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7230. NSDictionary *total_carton_dic = @{
  7231. @"align" : @"right",
  7232. @"aname" : @"Total Carton",
  7233. @"control" : @"text",
  7234. @"name" : @"",
  7235. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7236. };
  7237. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7238. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7239. return dic;
  7240. }
  7241. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7242. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7243. orderCode = [self translateSingleQuote:orderCode];
  7244. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7245. // setting
  7246. NSDictionary *setting = params[@"setting"];
  7247. NSNumber *hide = setting[@"SignatureHide"];
  7248. [dic setValue:hide forKey:@"hide"];
  7249. [dic setValue:@"Signature" forKey:@"title"];
  7250. __block NSString *pic_path = @"";
  7251. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7252. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7253. pic_path = [self textAtColumn:0 statement:stmt];
  7254. }];
  7255. NSDictionary *pic_dic = @{
  7256. @"aname" : @"Signature",
  7257. @"avalue" :pic_path,
  7258. @"control" : @"signature",
  7259. @"name" : @"sign_picpath",
  7260. @"value" : pic_path
  7261. };
  7262. [dic setValue:pic_dic forKey:@"item_0"];
  7263. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7264. return dic;
  7265. }
  7266. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7267. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7268. // setting
  7269. NSDictionary *setting = params[@"setting"];
  7270. NSNumber *hide = setting[@"ShippingMethodHide"];
  7271. [dic setValue:hide forKey:@"hide"];
  7272. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7273. orderCode = [self translateSingleQuote:orderCode];
  7274. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7275. __block NSString *logist = @"";
  7276. __block NSString *lift_gate = @"";
  7277. __block int lift_gate_integer = 0;
  7278. __block NSString *logistic_note = @"";
  7279. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7280. logist = [self textAtColumn:0 statement:stmt];
  7281. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7282. logistic_note = [self textAtColumn:2 statement:stmt];
  7283. }];
  7284. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7285. [dic setValue:@"Shipping Method" forKey:@"title"];
  7286. // val_0
  7287. int PERSONAL_PICK_UP_check = 0;
  7288. int USE_MY_CARRIER_check = 0;
  7289. NSString *logistic_note_text = @"";
  7290. int will_call_check = 0;
  7291. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7292. will_call_check = 1;
  7293. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7294. PERSONAL_PICK_UP_check = 1;
  7295. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7296. USE_MY_CARRIER_check = 1;
  7297. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7298. logistic_note = [logistic_note_array firstObject];
  7299. if (logistic_note_array.count > 1) {
  7300. logistic_note_text = [logistic_note_array lastObject];
  7301. }
  7302. }
  7303. }
  7304. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7305. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7306. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7307. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7308. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7309. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7310. NSDictionary *val0_subItem_item0 = @{
  7311. @"aname" : @"Option",
  7312. @"cadedate" : @{
  7313. @"count" : [NSNumber numberWithInteger:2],
  7314. @"val_0" : @{
  7315. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7316. @"refresh" : [NSNumber numberWithInteger:0],
  7317. @"value" : @"PERSONAL PICK UP",
  7318. @"value_id" : @"PERSONAL PICK UP"
  7319. },
  7320. @"val_1" : @{
  7321. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7322. @"refresh" : [NSNumber numberWithInteger:0],
  7323. @"sub_item" : @{
  7324. @"count" : [NSNumber numberWithInteger:1],
  7325. @"item_0" : @{
  7326. @"aname" : @"BOL",
  7327. @"control" : @"edit",
  7328. @"keyboard" : @"text",
  7329. @"name" : @"logist_note_text",
  7330. @"refresh" : [NSNumber numberWithInteger:0],
  7331. @"required" : @"false",
  7332. @"value" : logistic_note_text
  7333. }
  7334. },
  7335. @"value" : @"USE MY CARRIER",
  7336. @"value_id" : @"USE MY CARRIER"
  7337. }
  7338. },
  7339. @"control" : @"enum",
  7340. @"name" : @"logistic_note",
  7341. @"required" : @"true",
  7342. @"single_select" : @"true"
  7343. };
  7344. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7345. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7346. // val_1
  7347. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7348. if([lift_gate isEqualToString:@""]) {
  7349. if (lift_gate_integer == 1) {
  7350. lift_gate = @"true";
  7351. } else {
  7352. lift_gate = @"false";
  7353. }
  7354. }
  7355. int common_carrier_check = 0;
  7356. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7357. common_carrier_check = 1;
  7358. [params setValue:lift_gate forKey:@"lift_gate"];
  7359. }
  7360. NSDictionary *val_1 = @{
  7361. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7362. @"sub_item" : @{
  7363. @"count" : [NSNumber numberWithInteger:1],
  7364. @"item_0" : @{
  7365. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7366. @"control" : @"switch",
  7367. @"name" : @"lift_gate",
  7368. @"refresh" : [NSNumber numberWithInteger:1],
  7369. @"required" : @"true",
  7370. @"value" : lift_gate
  7371. }
  7372. },
  7373. @"value" : @"COMMON CARRIER",
  7374. @"value_id" : @"COMMON CARRIER"
  7375. };
  7376. // cadedate
  7377. NSDictionary *cadedate = @{
  7378. @"count" : [NSNumber numberWithInteger:2],
  7379. @"val_0" : val_0,
  7380. @"val_1" : val_1
  7381. };
  7382. // item_0
  7383. NSMutableDictionary *item_0 = @{
  7384. @"aname" : @"Shipping",
  7385. @"cadedate" : cadedate,
  7386. @"control" : @"enum",
  7387. @"name" : @"logist",
  7388. @"refresh" : [NSNumber numberWithInteger:1],
  7389. @"single_select" : @"true",
  7390. }.mutableCopy;
  7391. NSNumber *required = setting[@"ShippingMethodRequire"];
  7392. if ([required integerValue]) {
  7393. [item_0 setValue:@"true" forKey:@"required"];
  7394. }
  7395. [dic setValue:item_0 forKey:@"item_0"];
  7396. return dic;
  7397. }
  7398. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7399. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7400. orderCode = [self translateSingleQuote:orderCode];
  7401. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7402. __block int submit_as_integer = 0;
  7403. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7404. submit_as_integer = sqlite3_column_int(stmt, 0);
  7405. }];
  7406. int check11 = 0;
  7407. int check10 = 0;
  7408. if (submit_as_integer == 11) {
  7409. check11 = 1;
  7410. }
  7411. if (submit_as_integer == 10) {
  7412. check10 = 1;
  7413. }
  7414. // section_0
  7415. NSMutableDictionary *dic = @{
  7416. @"count" : @(1),
  7417. @"item_0" : @{
  7418. @"aname" : @"Submit Order As",
  7419. @"cadedate" : @{
  7420. @"count" : @(2),
  7421. @"val_0" : @{
  7422. @"check" : [NSNumber numberWithInteger:check11],
  7423. @"value" : @"Sales Order",
  7424. @"value_id" : @(11)
  7425. },
  7426. @"val_1" : @{
  7427. @"check" : [NSNumber numberWithInteger:check10],
  7428. @"value" : @"Quote",
  7429. @"value_id" : @(10)
  7430. }
  7431. },
  7432. @"control" : @"enum",
  7433. @"name" : @"erpOrderStatus",
  7434. @"required" : @"true",
  7435. @"single_select" : @"true"
  7436. },
  7437. @"title" : @"Order Type"
  7438. }.mutableCopy;
  7439. // setting
  7440. NSDictionary *setting = params[@"setting"];
  7441. NSNumber *hide = setting[@"OrderTypeHide"];
  7442. [dic setValue:hide forKey:@"hide"];
  7443. return dic;
  7444. }
  7445. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7446. countryCode = [self translateSingleQuote:countryCode];
  7447. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7448. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7449. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7450. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7451. if (name == NULL) {
  7452. name = "";
  7453. }
  7454. if (code == NULL) {
  7455. code = "";
  7456. }
  7457. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7458. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7459. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7460. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7461. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7462. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7463. }
  7464. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7465. [container setValue:stateDic forKey:key];
  7466. }] mutableCopy];
  7467. [ret removeObjectForKey:@"result"];
  7468. // failure 可以不用了,一样的
  7469. if (ret.allKeys.count == 0) {
  7470. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7471. [stateDic setValue:@"Other" forKey:@"value"];
  7472. [stateDic setValue:@"" forKey:@"value_id"];
  7473. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7474. if (state_code && [@"" isEqualToString:state_code]) {
  7475. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7476. }
  7477. NSString *key = [NSString stringWithFormat:@"val_0"];
  7478. [ret setValue:stateDic forKey:key];
  7479. }
  7480. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7481. return ret;
  7482. }
  7483. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7484. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7485. orderCode = [self translateSingleQuote:orderCode];
  7486. 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];
  7487. __block NSString *payType = @"";
  7488. __block NSString *firstName = @"";
  7489. __block NSString *lastName = @"";
  7490. __block NSString *addr1 = @"";
  7491. __block NSString *addr2 = @"";
  7492. __block NSString *zipcode = @"";
  7493. __block NSString *cardType = @"";
  7494. __block NSString *cardNumber = @"";
  7495. __block NSString *securityCode = @"";
  7496. __block NSString *month = @"";
  7497. __block NSString *year = @"";
  7498. __block NSString *city = @"";
  7499. __block NSString *state = @"";
  7500. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7501. payType = [self textAtColumn:0 statement:stmt];
  7502. firstName = [self textAtColumn:1 statement:stmt];
  7503. lastName = [self textAtColumn:2 statement:stmt];
  7504. addr1 = [self textAtColumn:3 statement:stmt];
  7505. addr2 = [self textAtColumn:4 statement:stmt];
  7506. zipcode = [self textAtColumn:5 statement:stmt];
  7507. cardType = [self textAtColumn:6 statement:stmt];
  7508. cardNumber = [self textAtColumn:7 statement:stmt];
  7509. securityCode = [self textAtColumn:8 statement:stmt];
  7510. month = [self textAtColumn:9 statement:stmt];
  7511. year = [self textAtColumn:10 statement:stmt];
  7512. city = [self textAtColumn:11 statement:stmt];
  7513. state = [self textAtColumn:12 statement:stmt];
  7514. }];
  7515. NSString *required = @"true";
  7516. // setting
  7517. NSDictionary *setting = params[@"setting"];
  7518. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7519. if ([requiredNumber integerValue]) {
  7520. required = @"true";
  7521. } else {
  7522. required = @"false";
  7523. }
  7524. // "section_2"
  7525. NSMutableDictionary *dic = @{
  7526. @"count" : @(1),
  7527. @"item_0" : @{
  7528. @"aname" : @"Payment",
  7529. @"required" : required,
  7530. @"cadedate" : @{
  7531. @"count" : @(6),
  7532. @"val_0" : @{
  7533. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7534. @"value" : @"Check",
  7535. @"value_id" : @"Check"
  7536. },
  7537. @"val_1" : @{
  7538. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7539. @"value" : @"Cash",
  7540. @"value_id" : @"Cash"
  7541. },
  7542. @"val_2" : @{
  7543. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7544. @"value" : @"NET 30",
  7545. @"value_id" : @"NET 30"
  7546. },
  7547. @"val_3" : @{
  7548. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7549. @"value" : @"Wire Transfer",
  7550. @"value_id" : @"Wire Transfer"
  7551. },
  7552. @"val_4" : @{
  7553. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7554. @"sub_item" : @{
  7555. @"count" : @(3),
  7556. @"item_0" : @{
  7557. @"aname" : @"choose",
  7558. @"control" : @"multi_action",
  7559. @"count" : @(1),
  7560. @"item_0" : @{
  7561. @"aname" : @"Same as customer",
  7562. @"key_map" : @{
  7563. @"credit_card_address1" : @"customer_address1",
  7564. @"credit_card_address2" : @"customer_address2",
  7565. @"credit_card_city" : @"customer_city",
  7566. @"credit_card_first_name" : @"customer_first_name",
  7567. @"credit_card_last_name" : @"customer_last_name",
  7568. @"credit_card_state" : @"customer_state",
  7569. @"credit_card_zipcode" : @"customer_zipcode"
  7570. },
  7571. @"type" : @"pull"
  7572. }
  7573. },
  7574. @"item_1" : @{
  7575. @"aname" : @"",
  7576. @"color" : @"red",
  7577. @"control" : @"text",
  7578. @"name" : @"",
  7579. @"value" : @"USA Credit cards only"
  7580. },
  7581. @"item_2" : @{
  7582. @"aname" : @"Fill",
  7583. @"cadedate" : @{
  7584. @"count" : @(2),
  7585. @"val_0" : @{
  7586. @"check" : @(1),
  7587. @"sub_item" : @{
  7588. @"count" : @(11),
  7589. @"item_0" : @{
  7590. @"aname" : @"Type",
  7591. @"cadedate" : @{
  7592. @"count" : @(2),
  7593. @"val_0" : @{
  7594. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7595. @"value" : @"VISA",
  7596. @"value_id" : @(0)
  7597. },
  7598. @"val_1" : @{
  7599. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7600. @"value" : @"MASTER CARD",
  7601. @"value_id" : @(1)
  7602. }
  7603. },
  7604. @"control" : @"enum",
  7605. @"name" : @"credit_card_type",
  7606. @"required" : @"true",
  7607. @"single_select" : @"true"
  7608. },
  7609. @"item_1" : @{
  7610. @"aname" : @"Number",
  7611. @"control" : @"edit",
  7612. @"keyboard" : @"int",
  7613. @"length" : @"16",
  7614. @"name" : @"credit_card_number",
  7615. @"required" : @"true",
  7616. @"value" : cardNumber
  7617. },
  7618. @"item_10" : @{
  7619. @"aname" : @"State",
  7620. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7621. @"control" : @"enum",
  7622. @"enum" : @"true",
  7623. @"name" : @"credit_card_state",
  7624. @"required" : @"true",
  7625. @"single_select" : @"true"
  7626. },
  7627. @"item_2" : @{
  7628. @"aname" : @"Expiration Date",
  7629. @"control" : @"monthpicker",
  7630. @"name" : @"credit_card_expiration",
  7631. @"required" : @"true",
  7632. @"type" : @"date",
  7633. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7634. },
  7635. @"item_3" : @{
  7636. @"aname" : @"Security Code",
  7637. @"control" : @"edit",
  7638. @"keyboard" : @"int",
  7639. @"length" : @"3",
  7640. @"name" : @"credit_card_security_code",
  7641. @"required" : @"true",
  7642. @"value" : securityCode
  7643. },
  7644. @"item_4" : @{
  7645. @"aname" : @"First Name",
  7646. @"control" : @"edit",
  7647. @"keyboard" : @"text",
  7648. @"name" : @"credit_card_first_name",
  7649. @"required" : @"true",
  7650. @"value" : firstName
  7651. },
  7652. @"item_5" : @{
  7653. @"aname" : @"Last Name",
  7654. @"control" : @"edit",
  7655. @"keyboard" : @"text",
  7656. @"name" : @"credit_card_last_name",
  7657. @"required" : @"true",
  7658. @"value" : lastName
  7659. },
  7660. @"item_6" : @{
  7661. @"aname" : @"Address 1",
  7662. @"control" : @"edit",
  7663. @"keyboard" : @"text",
  7664. @"name" : @"credit_card_address1",
  7665. @"required" : @"true",
  7666. @"value" : addr1
  7667. },
  7668. @"item_7" : @{
  7669. @"aname" : @"Address 2",
  7670. @"control" : @"edit",
  7671. @"keyboard" : @"text",
  7672. @"name" : @"credit_card_address2",
  7673. @"value" : addr2
  7674. },
  7675. @"item_8" : @{
  7676. @"aname" : @"zip code",
  7677. @"control" : @"edit",
  7678. @"keyboard" : @"text",
  7679. @"name" : @"credit_card_zipcode",
  7680. @"required" : @"true",
  7681. @"value" : zipcode
  7682. },
  7683. @"item_9" : @{
  7684. @"aname" : @"City",
  7685. @"control" : @"edit",
  7686. @"keyboard" : @"text",
  7687. @"name" : @"credit_card_city",
  7688. @"required" : @"true",
  7689. @"value" : city
  7690. }
  7691. },
  7692. @"value" : @"Fill Now",
  7693. @"value_id" : @""
  7694. },
  7695. @"val_1" : @{
  7696. @"check" : @(0),
  7697. @"value" : @"Fill Later",
  7698. @"value_id" : @""
  7699. }
  7700. },
  7701. @"control" : @"enum",
  7702. @"name" : @"",
  7703. @"single_select" : @"true"
  7704. }
  7705. },
  7706. @"value" : @"Credit Card",
  7707. @"value_id" : @"Credit Card"
  7708. },
  7709. @"val_5" : @{
  7710. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7711. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7712. @"value_id" : @"FOLLOW EXISTING"
  7713. }
  7714. },
  7715. @"control" : @"enum",
  7716. @"name" : @"paymentType",
  7717. @"single_select" : @"true"
  7718. },
  7719. @"title" : @"Payment Information"
  7720. }.mutableCopy;
  7721. NSNumber *hide = setting[@"PaymentInformationHide"];
  7722. [dic setValue:hide forKey:@"hide"];
  7723. return dic;
  7724. }
  7725. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7726. {
  7727. sqlite3 *db = [iSalesDB get_db];
  7728. // UISetting
  7729. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7730. NSString *cachefolder = [paths objectAtIndex:0];
  7731. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7732. NSData* json =nil;
  7733. json=[NSData dataWithContentsOfFile:img_cache];
  7734. NSError *error=nil;
  7735. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7736. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7737. [params setObject:setting forKey:@"setting"];
  7738. int section_count = 0;
  7739. // params
  7740. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7741. orderCode = [self translateSingleQuote:orderCode];
  7742. // 0 Order Type 1 Shipping Method 2 Payment Information
  7743. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7744. // 0 Order Type
  7745. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7746. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7747. [ret setValue:order_type_dic forKey:key0];
  7748. // 1 Shipping Method
  7749. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7750. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7751. [ret setValue:shipping_method_dic forKey:key1];
  7752. // 2 Payment Information
  7753. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7754. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7755. [ret setValue:payment_info_dic forKey:key2];
  7756. // 3 Customer
  7757. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7758. NSDictionary *customer_dic = [self customerDic:params db:db];
  7759. [ret setValue:customer_dic forKey:key3];
  7760. // 4 Ship To
  7761. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7762. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7763. [ret setValue:ship_to_dic forKey:key4];
  7764. // 5 Ship From
  7765. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7766. if (![shipFromDisable integerValue]) {
  7767. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7768. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7769. [ret setValue:ship_from_dic forKey:key5];
  7770. }
  7771. // 6 Freight Bill To
  7772. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7773. if (![freightBillToDisable integerValue]) {
  7774. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7775. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7776. [ret setValue:freight_bill_to forKey:key6];
  7777. }
  7778. // 7 Merchandise Bill To
  7779. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7780. if (![merchandiseBillToDisable integerValue]) {
  7781. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7782. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7783. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7784. }
  7785. // 8 Return To
  7786. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7787. if (![returnToDisable integerValue]) {
  7788. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7789. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7790. [ret setValue:return_to_dic forKey:key8];
  7791. }
  7792. // 9 Model Information
  7793. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7794. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7795. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7796. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7797. [ret setValue:model_info_dic forKey:key9];
  7798. // 10 Remarks Content
  7799. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7800. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7801. [ret setValue:remarks_content_dic forKey:key10];
  7802. // 11 Order Total
  7803. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7804. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7805. [ret setValue:order_total_dic forKey:key11];
  7806. // 12 Signature
  7807. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7808. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7809. [ret setValue:sign_dic forKey:key12];
  7810. [ret setValue:@(section_count) forKey:@"section_count"];
  7811. [iSalesDB close_db:db];
  7812. return [RAUtils dict2data:ret];
  7813. // return nil;
  7814. }
  7815. #pragma mark addr editor
  7816. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7817. NSMutableDictionary *new_item = [item mutableCopy];
  7818. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7819. return new_item;
  7820. }
  7821. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7822. {
  7823. // "is_subaction" = true;
  7824. // orderCode = MOB1608240002;
  7825. // password = 123456;
  7826. // "subaction_tag" = 1;
  7827. // user = EvanK;
  7828. // {
  7829. // "is_subaction" = true;
  7830. // orderCode = MOB1608240002;
  7831. // password = 123456;
  7832. // "refresh_trigger" = zipcode;
  7833. // "subaction_tag" = 1;
  7834. // user = EvanK;
  7835. // }
  7836. NSString *country_code = nil;
  7837. NSString *zipCode = nil;
  7838. NSString *stateCode = nil;
  7839. NSString *city = nil;
  7840. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7841. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  7842. NSString *code_id = params[@"country"];
  7843. country_code = [self countryCodeByid:code_id];
  7844. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  7845. NSString *zip_code = params[@"zipcode"];
  7846. // 剔除全部为空格
  7847. int spaceCount = 0;
  7848. for (int i = 0; i < zip_code.length; i++) {
  7849. if ([zip_code characterAtIndex:i] == ' ') {
  7850. spaceCount++;
  7851. }
  7852. }
  7853. if (spaceCount == zip_code.length) {
  7854. zip_code = @"";
  7855. }
  7856. zipCode = zip_code;
  7857. if (zipCode.length > 0) {
  7858. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7859. country_code = [dic valueForKey:@"country_code"];
  7860. if (!country_code) {
  7861. // country_code = @"US";
  7862. NSString *code_id = params[@"country"];
  7863. country_code = [self countryCodeByid:code_id];
  7864. }
  7865. stateCode = [dic valueForKey:@"state_code"];
  7866. if(!stateCode.length) {
  7867. stateCode = params[@"state"];
  7868. }
  7869. city = [dic valueForKey:@"city"];
  7870. if (!city.length) {
  7871. city = params[@"city"];
  7872. }
  7873. // zip code
  7874. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  7875. // [zipDic setValue:zipCode forKey:@"value"];
  7876. // [section_0 setValue:zipDic forKey:@"item_11"];
  7877. } else {
  7878. NSString *code_id = params[@"country"];
  7879. country_code = [self countryCodeByid:code_id];
  7880. stateCode = params[@"state"];
  7881. city = params[@"city"];
  7882. }
  7883. }
  7884. }
  7885. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7886. // [ret removeObjectForKey:@"up_params"];
  7887. [ret setObject:@"New Address" forKey:@"title"];
  7888. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7889. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7890. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7891. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7892. // [country_dic removeObjectForKey:@"refresh"];
  7893. // [country_dic removeObjectForKey:@"restore"];
  7894. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7895. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7896. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7897. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7898. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7899. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7900. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7901. // [zip_code_dic removeObjectForKey:@"refresh"];
  7902. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  7903. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7904. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7905. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7906. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  7907. [cityDic setValue:city ? city : @"" forKey:@"value"];
  7908. [new_section_0 setObject:cityDic forKey:@"item_8"];
  7909. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7910. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7911. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7912. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7913. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7914. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7915. // country
  7916. NSString *countryCode = country_code == nil ? @"US" : country_code;
  7917. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7918. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7919. // state
  7920. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  7921. // NSDictionary *tmpDic = @{
  7922. // @"value" : @"Other",
  7923. // @"value_id" : @"",
  7924. // @"check" : [NSNumber numberWithInteger:0]
  7925. // };
  7926. //
  7927. // for (int i = 0; i < allState.allKeys.count; i++) {
  7928. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7929. //
  7930. // NSDictionary *tmp = allState[key];
  7931. // [allState setValue:tmpDic forKey:key];
  7932. // tmpDic = tmp;
  7933. // }
  7934. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7935. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7936. [ret setValue:new_section_0 forKey:@"section_0"];
  7937. return [RAUtils dict2data:ret];
  7938. }
  7939. #pragma mark save addr
  7940. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7941. {
  7942. // NSString *companyName = [self valueInParams:params key:@"company"];
  7943. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7944. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7945. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7946. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7947. // NSString *countryId = [self valueInParams:params key:@"country"];
  7948. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7949. // NSString *city = [self valueInParams:params key:@"city"];
  7950. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7951. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7952. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7953. // NSString *phone = [self valueInParams:params key:@"phone"];
  7954. // NSString *fax = [self valueInParams:params key:@"fax"];
  7955. // NSString *email = [self valueInParams:params key:@"email"];
  7956. return [self offline_saveContact:params update:NO isCustomer:NO];
  7957. }
  7958. #pragma mark cancel order
  7959. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7960. {
  7961. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7962. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7963. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7964. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7965. int ret = [iSalesDB execSql:sql];
  7966. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7967. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7968. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7969. // [dic setValue:@"160409" forKey:@"min_ver"];
  7970. return [RAUtils dict2data:dic];
  7971. }
  7972. #pragma mark sign order
  7973. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7974. {
  7975. //参考 offline_saveBusinesscard
  7976. DebugLog(@"sign order params: %@",params);
  7977. // orderCode = MOB1608240002;
  7978. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7979. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7980. orderCode = [self translateSingleQuote:orderCode];
  7981. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7982. picPath = [self translateSingleQuote:picPath];
  7983. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7984. int ret = [iSalesDB execSql:sql];
  7985. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7986. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7987. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7988. // [dic setValue:@"160409" forKey:@"min_ver"];
  7989. return [RAUtils dict2data:dic];
  7990. }
  7991. #pragma mark save order
  7992. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7993. NSString *ret = [self valueInParams:params key:key];
  7994. if (translate) {
  7995. ret = [self translateSingleQuote:ret];
  7996. }
  7997. return ret;
  7998. }
  7999. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8000. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8001. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8002. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8003. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8004. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8005. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8006. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8007. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8008. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8009. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8010. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8011. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8012. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8013. if (![total_price isEqualToString:@""]) {
  8014. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8015. } else {
  8016. total_price = @"";
  8017. }
  8018. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8019. if ([paymentsAndCredits isEqualToString:@""]) {
  8020. paymentsAndCredits = @"";
  8021. } else {
  8022. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8023. }
  8024. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8025. if ([handling_fee_value isEqualToString:@""]) {
  8026. handling_fee_value = @"";
  8027. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8028. handling_fee_value = @"";
  8029. } else {
  8030. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8031. }
  8032. NSString *handling_fee_placeholder = handling_fee_value;
  8033. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8034. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8035. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8036. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8037. NSString *lift_gate_placeholder = @"";
  8038. if ([lift_gate_value isEqualToString:@""]) {
  8039. lift_gate_placeholder = @"";
  8040. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8041. lift_gate_placeholder = @"";
  8042. } else {
  8043. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8044. }
  8045. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8046. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8047. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8048. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8049. 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];
  8050. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8051. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8052. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8053. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8054. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8055. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8056. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8057. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8058. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8059. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8060. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8061. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8062. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8063. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8064. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8065. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8066. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8067. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8068. if (!number_nil) {
  8069. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8070. }
  8071. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8072. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8073. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8074. if (!security_code_nil) {
  8075. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8076. }
  8077. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8078. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8079. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8080. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8081. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8082. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8083. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8084. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8085. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8086. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8087. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8088. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8089. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8090. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8091. NSString *contact_id = customer_cid;
  8092. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8093. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8094. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8095. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8096. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8097. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8098. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8099. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8100. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8101. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8102. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8103. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8104. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8105. //
  8106. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8107. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8108. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8109. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8110. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8111. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8112. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8113. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8114. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8115. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8116. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8117. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8118. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8119. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8120. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8121. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8122. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8123. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8124. 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];
  8125. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8126. if (receive_cid.length) {
  8127. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8128. }
  8129. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8130. if (receive_name.length) {
  8131. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8132. }
  8133. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8134. if (receive_ext.length) {
  8135. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8136. }
  8137. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8138. if (receive_contact.length) {
  8139. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8140. }
  8141. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8142. if (receive_phone.length) {
  8143. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8144. }
  8145. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8146. if (receive_email.length) {
  8147. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8148. }
  8149. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8150. if (receive_fax.length) {
  8151. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8152. }
  8153. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8154. if (sender_cid.length) {
  8155. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8156. }
  8157. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8158. if (sender_name.length) {
  8159. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8160. }
  8161. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8162. if (sender_ext.length) {
  8163. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8164. }
  8165. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8166. if(sender_contact.length) {
  8167. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8168. }
  8169. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8170. if (sender_phone.length) {
  8171. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8172. }
  8173. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8174. if (sender_fax.length) {
  8175. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8176. }
  8177. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8178. if (sender_email.length) {
  8179. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8180. }
  8181. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8182. if (shipping_billto_cid.length) {
  8183. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8184. }
  8185. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8186. if (shipping_billto_name.length) {
  8187. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8188. }
  8189. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8190. if (shipping_billto_ext.length) {
  8191. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8192. }
  8193. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8194. if (shipping_billto_contact.length) {
  8195. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8196. }
  8197. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8198. if (shipping_billto_phone.length) {
  8199. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8200. }
  8201. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8202. if (shipping_billto_fax.length) {
  8203. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8204. }
  8205. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8206. if (shipping_billto_email.length) {
  8207. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8208. }
  8209. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8210. if (billing_cid.length) {
  8211. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8212. }
  8213. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8214. if (billing_name.length) {
  8215. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8216. }
  8217. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8218. if (billing_ext.length) {
  8219. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8220. }
  8221. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8222. if (billing_contact.length) {
  8223. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8224. }
  8225. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8226. if (billing_phone.length) {
  8227. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8228. }
  8229. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8230. if (billing_fax.length) {
  8231. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8232. }
  8233. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8234. if (billing_email.length) {
  8235. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8236. }
  8237. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8238. if (returnto_cid.length) {
  8239. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8240. }
  8241. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8242. if (returnto_name.length) {
  8243. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8244. }
  8245. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8246. if (returnto_ext.length) {
  8247. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8248. }
  8249. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8250. if (returnto_contact.length) {
  8251. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8252. }
  8253. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8254. if (returnto_phone.length) {
  8255. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8256. }
  8257. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8258. if (returnto_fax.length) {
  8259. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8260. }
  8261. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8262. if (returnto_email.length) {
  8263. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8264. }
  8265. NSString *order_status = @"status = 1,";
  8266. if (submit) {
  8267. order_status = @"status = -11,";
  8268. }
  8269. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8270. NSString *sync_sql = @"";
  8271. if (submit) {
  8272. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8273. [param setValue:sales_rep forKey:@"sales_rep"];
  8274. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8275. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8276. }
  8277. 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];
  8278. DebugLog(@"save order contactSql: %@",contactSql);
  8279. DebugLog(@"save order orderSql: %@",orderSql);
  8280. // int contact_ret = [iSalesDB execSql:contactSql];
  8281. int order_ret = [iSalesDB execSql:orderSql];
  8282. int ret = order_ret;
  8283. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8284. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8285. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8286. // [dic setValue:@"160409" forKey:@"min_ver"];
  8287. [dic setObject:so_id forKey:@"so#"];
  8288. return [RAUtils dict2data:dic];
  8289. }
  8290. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8291. {
  8292. // id foo = nil;
  8293. // NSMutableArray *a = @[].mutableCopy;
  8294. // [a addObject:foo];
  8295. return [self saveorder:param submit:NO];
  8296. }
  8297. #pragma mark add to cart by name
  8298. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8299. {
  8300. NSString *orderCode = [params objectForKey:@"orderCode"];
  8301. NSString *product_name = [params objectForKey:@"product_name"];
  8302. product_name = [self translateSingleQuote:product_name];
  8303. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8304. sqlite3 *db = [iSalesDB get_db];
  8305. __block NSMutableString *product_id_string = [NSMutableString string];
  8306. for (int i = 0; i < product_name_array.count; i++) {
  8307. NSString *name = [product_name_array objectAtIndex:i];
  8308. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8309. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8310. int product_id = sqlite3_column_int(stmt, 0);
  8311. if (i == product_name_array.count - 1) {
  8312. [product_id_string appendFormat:@"%d",product_id];
  8313. } else {
  8314. [product_id_string appendFormat:@"%d,",product_id];
  8315. }
  8316. }];
  8317. }
  8318. NSDictionary *newParams = @{
  8319. @"product_id" : product_id_string,
  8320. @"orderCode" : orderCode
  8321. };
  8322. [iSalesDB close_db:db];
  8323. return [self offline_add2cart:[newParams mutableCopy]];
  8324. }
  8325. #pragma mark reset order
  8326. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8327. {
  8328. UIApplication * app = [UIApplication sharedApplication];
  8329. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8330. appDelegate.disable_trigger=true;
  8331. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8332. appDelegate.disable_trigger=false;
  8333. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8334. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8335. return [RAUtils dict2data:dic];
  8336. }
  8337. #pragma mark submit order
  8338. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8339. {
  8340. return [self saveorder:params submit:YES];
  8341. }
  8342. #pragma mark copy order
  8343. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8344. {
  8345. NSMutableDictionary *ret = @{}.mutableCopy;
  8346. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8347. sqlite3 *db = [iSalesDB get_db];
  8348. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8349. // 首先查看联系人是否active
  8350. 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];
  8351. __block int customer_is_active = 1;
  8352. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8353. customer_is_active = sqlite3_column_int(stmt, 0);
  8354. }];
  8355. if (!customer_is_active) {
  8356. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8357. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8358. [iSalesDB close_db:db];
  8359. return [RAUtils dict2data:ret];
  8360. }
  8361. // new order
  8362. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8363. NSString *new_order_code = [self get_offline_soid:db];
  8364. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8365. user = [self translateSingleQuote:user];
  8366. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8367. __block int result = 1;
  8368. result = [iSalesDB execSql:insert_order_sql db:db];
  8369. if (!result) {
  8370. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8371. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8372. [iSalesDB close_db:db];
  8373. return [RAUtils dict2data:ret];
  8374. }
  8375. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8376. // __block NSString *product_id = @"";
  8377. __weak typeof(self) weakSelf = self;
  8378. 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];
  8379. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8380. int is_active = sqlite3_column_int(stmt, 1);
  8381. if (is_active) {
  8382. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8383. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8384. 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];
  8385. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8386. }
  8387. }];
  8388. // product_id = [product_id substringFromIndex:1];
  8389. //
  8390. // [self offline_add2cart:@{}.mutableCopy];
  8391. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8392. [iSalesDB close_db:db];
  8393. if (result) {
  8394. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8395. } else {
  8396. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8397. }
  8398. [ret setObject:new_order_code forKey:@"so_id"];
  8399. return [RAUtils dict2data:ret];
  8400. }
  8401. #pragma mark move wish list to cart
  8402. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8403. NSString *collectId = params[@"collectId"];
  8404. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8405. __block NSString *product_id = @"";
  8406. __block NSString *qty = @"";
  8407. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8408. int productId = sqlite3_column_int(stmt, 0);
  8409. int item_qty = sqlite3_column_int(stmt, 1);
  8410. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8411. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8412. }];
  8413. product_id = [product_id substringFromIndex:1];
  8414. qty = [qty substringFromIndex:1];
  8415. NSDictionary *newParams = @{
  8416. @"product_id" : product_id,
  8417. @"orderCode" : params[@"orderCode"],
  8418. @"qty" : qty
  8419. };
  8420. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8421. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8422. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8423. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8424. //
  8425. // int ret = [iSalesDB execSql:deleteSQL];
  8426. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8427. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8428. }
  8429. return [RAUtils dict2data:retDic];
  8430. }
  8431. #pragma mark - portfolio
  8432. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8433. int sort = [[params valueForKey:@"sort"] integerValue];
  8434. int offset = [[params valueForKey:@"offset"] integerValue];
  8435. int limit = [[params valueForKey:@"limit"] integerValue];
  8436. NSString *orderBy = @"";
  8437. switch (sort) {
  8438. case 0:{
  8439. orderBy = @"modify_time desc";
  8440. }
  8441. break;
  8442. case 1:{
  8443. orderBy = @"modify_time asc";
  8444. }
  8445. break;
  8446. case 2:{
  8447. orderBy = @"name asc";
  8448. }
  8449. break;
  8450. case 3:{
  8451. orderBy = @"name desc";
  8452. }
  8453. break;
  8454. case 4:{
  8455. orderBy = @"description asc";
  8456. }
  8457. break;
  8458. default:
  8459. break;
  8460. }
  8461. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  8462. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8463. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8464. sqlite3 *db = [iSalesDB get_db];
  8465. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8466. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8467. int product_id = sqlite3_column_int(stmt, 0);
  8468. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8469. NSString *name = [self textAtColumn:1 statement:stmt];
  8470. NSString *description = [self textAtColumn:2 statement:stmt];
  8471. double price = sqlite3_column_double(stmt, 3);
  8472. double discount = sqlite3_column_double(stmt,4);
  8473. int qty = sqlite3_column_int(stmt, 5);
  8474. int percentage = sqlite3_column_int(stmt, 6);
  8475. int item_id = sqlite3_column_int(stmt, 7);
  8476. // int fashion_id = sqlite3_column_int(stmt, 8);
  8477. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8478. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8479. double percent = sqlite3_column_double(stmt, 11);
  8480. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8481. if ([price_null isEqualToString:@"null"]) {
  8482. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8483. }
  8484. NSMutableDictionary *item = @{
  8485. @"linenotes": line_note,
  8486. @"check": @(1),
  8487. @"product_id": product_id_string,
  8488. @"available_qty": @(qty),
  8489. @"available_percent" : @(percent),
  8490. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8491. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8492. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8493. @"img": img_path,
  8494. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8495. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8496. }.mutableCopy;
  8497. if (percentage) {
  8498. [item removeObjectForKey:@"available_qty"];
  8499. } else {
  8500. [item removeObjectForKey:@"available_percent"];
  8501. }
  8502. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8503. if ([qty_null isEqualToString:@"null"]) {
  8504. [item removeObjectForKey:@"available_qty"];
  8505. }
  8506. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8507. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8508. }];
  8509. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8510. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8511. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8512. }
  8513. [iSalesDB close_db:db];
  8514. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8515. [dic setValue:@"" forKey:@"email_content"];
  8516. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8517. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8518. return [RAUtils dict2data:dic];
  8519. }
  8520. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8521. __block int qty = 0;
  8522. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8523. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8524. qty = sqlite3_column_int(stmt, 0);
  8525. }];
  8526. return qty;
  8527. }
  8528. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8529. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8530. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8531. NSMutableDictionary * values = params[@"replaceValue"];
  8532. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8533. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8534. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8535. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8536. NSString *pdf_path = @"";
  8537. if (direct) {
  8538. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8539. } else {
  8540. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8541. }
  8542. NSString *create_user = [self valueInParams:params key:@"user"];
  8543. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8544. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8545. // model info
  8546. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8547. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8548. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8549. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8550. sqlite3 *db = [iSalesDB get_db];
  8551. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8552. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8553. int product_id = sqlite3_column_int(stmt, 0);
  8554. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8555. double price = sqlite3_column_double(stmt, 1);
  8556. double discount = sqlite3_column_double(stmt,2);
  8557. int qty = sqlite3_column_int(stmt, 3);
  8558. int percentage = sqlite3_column_int(stmt, 4);
  8559. int item_id = sqlite3_column_int(stmt, 5);
  8560. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8561. double percent = sqlite3_column_double(stmt, 7);
  8562. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8563. /* if ([price_null isEqualToString:@"null"]) {
  8564. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8565. }
  8566. */
  8567. [product_ids_string appendFormat:@"%@,",product_id_string];
  8568. // Regular Price
  8569. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8570. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8571. // QTY
  8572. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8573. if ([qty_null isEqualToString:@"null"]) {
  8574. // 查available
  8575. qty = [self model_QTY:product_id_string db:db];
  8576. }
  8577. if (percentage) {
  8578. qty = qty * percent / 100;
  8579. }
  8580. // Special Price
  8581. if ([price_null isEqualToString:@"null"]) {
  8582. // price = regular price
  8583. price = [regular_price_str doubleValue];
  8584. }
  8585. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8586. if (![discount_null isEqualToString:@"null"]) {
  8587. price = price * (1 - discount / 100.0);
  8588. }
  8589. NSMutableDictionary *item = @{
  8590. @"line_note": line_note,
  8591. @"product_id": product_id_string,
  8592. @"available_qty": @(qty),
  8593. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8594. @"regular_price" : regular_price_str,
  8595. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8596. }.mutableCopy;
  8597. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8598. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8599. }];
  8600. [iSalesDB close_db:db];
  8601. if (product_ids_string.length > 0) {
  8602. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8603. }
  8604. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8605. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8606. return [RAUtils dict2data:resultDictionary];
  8607. }
  8608. NSString *model_info = [RAUtils dict2string:dic];
  8609. // 创建PDF
  8610. // 在preview情况下保存,则不需要新建了
  8611. if (direct) {
  8612. NSMutableDictionary *tear_sheet_params = params;
  8613. // if (tear_sheet_id) {
  8614. // tear_sheet_params = values;
  8615. // }
  8616. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8617. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8618. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8619. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8620. resultDictionary = pdfInfo.mutableCopy;
  8621. } else { // 创建PDF失败
  8622. return pdfData;
  8623. }
  8624. } else {
  8625. // pdf_path 就是本地路径
  8626. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8627. }
  8628. // 将文件移动到PDF Cache文件夹
  8629. NSString *newPath = [pdf_path lastPathComponent];
  8630. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8631. NSString *cachefolder = [paths objectAtIndex:0];
  8632. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8633. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8634. NSFileManager *fileManager = [NSFileManager defaultManager];
  8635. NSError *error = nil;
  8636. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8637. if (error) { // 移动文件失败
  8638. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8639. return [RAUtils dict2data:resultDictionary];
  8640. }
  8641. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8642. pdf_path = [newPath lastPathComponent];
  8643. // 保存信息
  8644. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8645. NSString *off_params = [RAUtils dict2string:params];
  8646. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8647. pdf_path = [self translateSingleQuote:pdf_path];
  8648. create_user = [self translateSingleQuote:create_user];
  8649. tear_note = [self translateSingleQuote:tear_note];
  8650. tear_name = [self translateSingleQuote:tear_name];
  8651. model_info = [self translateSingleQuote:model_info];
  8652. configureParams = [self translateSingleQuote:configureParams];
  8653. off_params = [self translateSingleQuote:off_params];
  8654. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8655. if (tear_sheet_id) {
  8656. int _id = [tear_sheet_id integerValue];
  8657. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8658. }
  8659. int result = [iSalesDB execSql:save_pdf_sql];
  8660. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8661. //
  8662. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8663. if (remove_Item) {
  8664. // portfolioId
  8665. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8666. }
  8667. return [RAUtils dict2data:resultDictionary];
  8668. }
  8669. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8670. return [self offline_savePDF:params direct:YES];
  8671. }
  8672. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8673. return [self offline_savePDF:params direct:NO];
  8674. }
  8675. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8676. int offset = [[params valueForKey:@"offset"] integerValue];
  8677. int limit = [[params valueForKey:@"limit"] integerValue];
  8678. NSString *keyword = [params valueForKey:@"keyWord"];
  8679. NSString *where = @"where is_delete is null or is_delete = 0";
  8680. if (keyword.length) {
  8681. keyword = [self translateSingleQuote:keyword];
  8682. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  8683. }
  8684. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8685. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8686. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8687. NSString *cachefolder = [paths objectAtIndex:0];
  8688. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8689. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8690. NSString *name = [self textAtColumn:0 statement:stmt];
  8691. NSString *note = [self textAtColumn:1 statement:stmt];
  8692. NSString *time = [self textAtColumn:2 statement:stmt];
  8693. NSString *user = [self textAtColumn:3 statement:stmt];
  8694. NSString *path = [self textAtColumn:4 statement:stmt];
  8695. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8696. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8697. path = [pdfFolder stringByAppendingPathComponent:path];
  8698. BOOL bdir=NO;
  8699. NSFileManager* fileManager = [NSFileManager defaultManager];
  8700. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8701. {
  8702. //pdf文件不存在
  8703. path=nil;
  8704. }
  8705. time = [self changeDateTimeFormate:time];
  8706. time = [time stringByAppendingString:@" PST"];
  8707. int sheet_id = sqlite3_column_int(stmt, 5);
  8708. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8709. item[@"tearsheetsId"]=@(sheet_id);
  8710. item[@"pdf_path"]=path;
  8711. item[@"create_time"]=time;
  8712. item[@"create_user"]=user;
  8713. item[@"tear_note"]=note;
  8714. item[@"tear_name"]=name;
  8715. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  8716. item[@"model_info"]=model_info;
  8717. item[@"off_params"]=off_params;
  8718. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8719. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8720. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8721. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8722. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8723. }];
  8724. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8725. return [RAUtils dict2data:dic];
  8726. }
  8727. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8728. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8729. // NSString *user = [params objectForKey:@"user"];
  8730. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  8731. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  8732. int result = [iSalesDB execSql:sql];
  8733. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8734. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8735. if (result == RESULT_TRUE) {
  8736. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8737. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8738. sqlite3 *db = [iSalesDB get_db];
  8739. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8740. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8741. [iSalesDB close_db:db];
  8742. }
  8743. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8744. return [RAUtils dict2data:dic];
  8745. }
  8746. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8747. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8748. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8749. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8750. // Regurlar Price
  8751. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8752. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8753. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8754. __block long val_count = 0;
  8755. NSString *sql = @"select name,type,order_by from price order by order_by";
  8756. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8757. NSString *name = [self textAtColumn:0 statement:stmt];
  8758. int type = sqlite3_column_int(stmt, 1);
  8759. int order_by = sqlite3_column_int(stmt, 2);
  8760. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8761. NSDictionary *price_dic = @{
  8762. @"value" : name,
  8763. @"value_id" : [NSNumber numberWithInteger:type],
  8764. @"check" : order_by == 0 ? @(1) : @(0)
  8765. };
  8766. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8767. val_count = ++(*count);
  8768. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8769. val_count = 0;
  8770. }];
  8771. [cadedate setObject:@{@"value" : @"None",
  8772. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8773. val_count++;
  8774. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8775. [price setObject:cadedate forKey:@"cadedate"];
  8776. [section1 setObject:price forKey:@"item_2"];
  8777. [dic setObject:section1 forKey:@"section_1"];
  8778. return [RAUtils dict2data:dic];
  8779. }
  8780. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8781. NSString *product_id = [params objectForKey:@"fashionId"];
  8782. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8783. __block int result = RESULT_TRUE;
  8784. __block int qty = 0;
  8785. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8786. qty = sqlite3_column_int(stmt, 0);
  8787. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8788. result = RESULT_FALSE;
  8789. }];
  8790. NSMutableDictionary *dic = @{
  8791. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8792. @"mode" : @"Regular Mode",
  8793. @"quantity_available" : @(qty),
  8794. @"result" : @(result),
  8795. }.mutableCopy;
  8796. return [RAUtils dict2data:dic];
  8797. }
  8798. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8799. NSString *item_ids = [params objectForKey:@"item_id"];
  8800. NSString *line_notes = [params objectForKey:@"notes"];
  8801. NSString *price_str = [params objectForKey:@"price"];
  8802. NSString *discount_str = [params objectForKey:@"discount"];
  8803. NSString *percent = [params objectForKey:@"available_percent"];
  8804. NSString *qty = [params objectForKey:@"available_qty"];
  8805. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8806. int dot = 0;
  8807. if (line_notes) {
  8808. line_notes = [self translateSingleQuote:line_notes];
  8809. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8810. sql = [sql stringByAppendingString:line_notes];
  8811. dot = 1;
  8812. } else {
  8813. line_notes = @"";
  8814. }
  8815. if (price_str) {
  8816. if (dot) {
  8817. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8818. } else {
  8819. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8820. dot = 1;
  8821. }
  8822. sql = [sql stringByAppendingString:price_str];
  8823. } else {
  8824. price_str = @"";
  8825. }
  8826. if (discount_str) {
  8827. if (dot) {
  8828. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8829. } else {
  8830. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8831. dot = 1;
  8832. }
  8833. sql = [sql stringByAppendingString:discount_str];
  8834. } else {
  8835. discount_str = @"";
  8836. }
  8837. if (percent) {
  8838. if (dot) {
  8839. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8840. } else {
  8841. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8842. dot = 1;
  8843. }
  8844. sql = [sql stringByAppendingString:percent];
  8845. } else {
  8846. percent = @"";
  8847. }
  8848. if (qty) {
  8849. if (dot) {
  8850. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8851. } else {
  8852. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8853. dot = 1;
  8854. }
  8855. sql = [sql stringByAppendingString:qty];
  8856. } else {
  8857. qty = @"";
  8858. }
  8859. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8860. sql = [sql stringByAppendingString:where];
  8861. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8862. int result = [iSalesDB execSql:sql];
  8863. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8864. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8865. return [RAUtils dict2data:dic];
  8866. }
  8867. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8868. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8869. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8870. sqlite3 *db = [iSalesDB get_db];
  8871. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8872. int result = [iSalesDB execSql:sql db:db];
  8873. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8874. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8875. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8876. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8877. [iSalesDB close_db:db];
  8878. return [RAUtils dict2data:dic];
  8879. }
  8880. + (void)offline_removePDFWithName:(NSString *)name {
  8881. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8882. NSString *cachefolder = [paths objectAtIndex:0];
  8883. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8884. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8885. NSFileManager *fileManager = [NSFileManager defaultManager];
  8886. [fileManager removeItemAtPath:path error:nil];
  8887. }
  8888. + (void)offline_clear_PDFCache {
  8889. NSFileManager *fileManager = [NSFileManager defaultManager];
  8890. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8891. NSString *cachefolder = [paths objectAtIndex:0];
  8892. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8893. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8894. for (NSString *path in pdf_files) {
  8895. [self offline_removePDFWithName:[path lastPathComponent]];
  8896. }
  8897. }
  8898. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8899. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8900. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8901. NSString *user = [params objectForKey:@"user"];
  8902. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8903. if (![create_user isEqualToString:user]) {
  8904. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8905. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8906. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8907. return [RAUtils dict2data:dic];
  8908. }
  8909. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8910. __block int is_local = 0;
  8911. __block NSString *path = @"";
  8912. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8913. is_local = sqlite3_column_int(stmt, 0);
  8914. path = [self textAtColumn:1 statement:stmt];
  8915. }];
  8916. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8917. if (is_local == 1) {
  8918. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8919. // 删除文件
  8920. [self offline_removePDFWithName:path];
  8921. }
  8922. int result = [iSalesDB execSql:sql];
  8923. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8924. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8925. return [RAUtils dict2data:dic];
  8926. }
  8927. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8928. {
  8929. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8930. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8931. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8932. NSString* where=@"1=1";
  8933. if (ver!=nil) {
  8934. where=@"is_dirty=1";
  8935. }
  8936. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8937. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8938. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8939. sqlite3 *db = [iSalesDB get_db];
  8940. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8941. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8942. NSInteger _id = sqlite3_column_int(stmt, 0);
  8943. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8944. NSString *name = [self textAtColumn:2 statement:stmt];
  8945. NSString *desc = [self textAtColumn:3 statement:stmt];
  8946. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8947. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8948. NSInteger qty = sqlite3_column_int(stmt, 6);
  8949. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8950. double percent = sqlite3_column_double(stmt, 8);
  8951. double price = sqlite3_column_double(stmt, 9);
  8952. double discount = sqlite3_column_double(stmt, 10);
  8953. NSString *img = [self textAtColumn:11 statement:stmt];
  8954. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8955. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8956. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8957. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8958. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8959. if ([price_null isEqualToString:@"null"]) {
  8960. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8961. }
  8962. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8963. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8964. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8965. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8966. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8967. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8968. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8969. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8970. if ([qty_null isEqualToString:@"null"]) {
  8971. // [item setValue:@"null" forKey:@"available_qty"];
  8972. } else {
  8973. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8974. }
  8975. if ([is_percent_null isEqualToString:@"null"]) {
  8976. // [item setValue:@"null" forKey:@"percentage"];
  8977. } else {
  8978. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8979. }
  8980. if ([percent_null isEqualToString:@"null"]) {
  8981. // [item setValue:@"null" forKey:@"percent"];
  8982. } else {
  8983. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8984. }
  8985. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8986. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8987. [item setValue:name forKey:@"name"];
  8988. [item setValue:desc forKey:@"description"];
  8989. [item setValue:img forKey:@"img"];
  8990. [item setValue:line_note forKey:@"line_note"];
  8991. [item setValue:create_time forKey:@"createtime"];
  8992. [item setValue:modify_time forKey:@"modifytime"];
  8993. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8994. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8995. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8996. }];
  8997. [iSalesDB close_db:db];
  8998. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8999. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9000. }
  9001. return ret;
  9002. }
  9003. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9004. {
  9005. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9006. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9007. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9008. NSString* where=@"1=1";
  9009. if (ver!=nil) {
  9010. where=@"is_dirty=1";
  9011. }
  9012. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  9013. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9014. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9015. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9016. NSInteger _id = sqlite3_column_int(stmt, 0);
  9017. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9018. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9019. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9020. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9021. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9022. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9023. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9024. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9025. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9026. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9027. NSString *uuid = [NSUUID UUID].UUIDString;
  9028. int is_delete = sqlite3_column_int(stmt, 11);
  9029. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9030. int is_local = sqlite3_column_int(stmt, 12);
  9031. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9032. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9033. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9034. }
  9035. [item setObject:pdf_path forKey:@"pdf_path"];
  9036. [item setObject:create_user forKey:@"create_user"];
  9037. [item setObject:tear_note forKey:@"tear_note"];
  9038. [item setObject:tear_name forKey:@"tear_name"];
  9039. [item setObject:model_info forKey:@"model_info"];
  9040. [item setObject:createtime forKey:@"createtime"];
  9041. [item setObject:modifytime forKey:@"modifytime"];
  9042. [item setObject:urlParams forKey:@"urlParams"];
  9043. [item setObject:off_params forKey:@"off_params"];
  9044. [item setObject:uuid forKey:@"pdf_token"];
  9045. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9046. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9047. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9048. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9049. } else {
  9050. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9051. }
  9052. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9053. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9054. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9055. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9056. }];
  9057. return ret;
  9058. }
  9059. @end