OLDataProvider.m 556 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020
  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. #import "ImageUtils.h"
  22. @interface OLDataProvider ()
  23. @end
  24. @implementation OLDataProvider
  25. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  26. {
  27. 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];
  28. NSString* ret=@"No Price.";
  29. sqlite3_stmt * statement;
  30. // int count=0;
  31. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  32. {
  33. if (sqlite3_step(statement) == SQLITE_ROW)
  34. {
  35. char *price = (char*)sqlite3_column_text(statement, 0);
  36. if(price==nil)
  37. price="";
  38. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  39. double dp= [nsprice doubleValue];
  40. ret=[NSString stringWithFormat:@"%.2f",dp];
  41. }
  42. sqlite3_finalize(statement);
  43. }
  44. return ret;
  45. // // [iSalesDB close_db:db];
  46. //
  47. // return nil;
  48. //// if(dprice==DBL_MAX)
  49. //// ret= nil;
  50. //// else
  51. //// ret= [NSNumber numberWithDouble:dprice];
  52. //// return ret;
  53. }
  54. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  55. {
  56. NSString* ret= nil;
  57. NSString *sqlQuery = nil;
  58. // 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
  59. 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];
  60. sqlite3_stmt * statement;
  61. // int count=0;
  62. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  63. {
  64. if (sqlite3_step(statement) == SQLITE_ROW)
  65. {
  66. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  67. if(imgurl==nil)
  68. imgurl="";
  69. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  70. ret=nsimgurl;
  71. }
  72. sqlite3_finalize(statement);
  73. }
  74. else
  75. {
  76. ret=@"";
  77. }
  78. // [iSalesDB close_db:db];
  79. // DebugLog(@"data string: %@",ret );
  80. return ret;
  81. }
  82. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  83. {
  84. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  85. //生成portfolio pdf需要的数据
  86. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  87. data[@"npd_url"]=COMPANY_WEB;
  88. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  89. [formatter setDateFormat:@"MMMM yyyy"];
  90. NSString* date = [formatter stringFromDate:[NSDate date]];
  91. data[@"create_month"]=date;
  92. data[@"company_name"]=COMPANY_FULL_NAME;
  93. data[@"catalog_name"]=params[@"catalog_name"];
  94. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  95. data[@"TOTAL_PAGE"]=0;
  96. data[@"CURRENT_PAGE"]=0;
  97. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  98. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  99. bool show_stockout=[params[@"show_stockout"] boolValue];
  100. NSString *sql = nil;
  101. // v1.90 more color
  102. if(values==nil)
  103. {
  104. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  105. }
  106. else
  107. {
  108. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) order by name",params[@"product_ids"]];
  109. }
  110. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  111. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  112. sqlite3 *db = [iSalesDB get_db];
  113. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  114. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  115. int product_id = sqlite3_column_int(stmt, 0);
  116. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  117. NSString *name = [self textAtColumn:1 statement:stmt];
  118. NSString *description = [self textAtColumn:2 statement:stmt];
  119. double price = sqlite3_column_double(stmt, 3);
  120. NSString *s_price = [self textAtColumn:3 statement:stmt];
  121. double discount = sqlite3_column_double(stmt,4);
  122. int qty = sqlite3_column_int(stmt, 5);
  123. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  124. int is_percentage = sqlite3_column_int(stmt, 6);
  125. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  126. int item_id = sqlite3_column_int(stmt, 7);
  127. // int fashion_id = sqlite3_column_int(stmt, 8);
  128. NSString *line_note = [self textAtColumn:9 statement:stmt];
  129. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  130. double percent = sqlite3_column_double(stmt, 10);
  131. // int portfolio_id = sqlite3_column_int(stmt, 11);
  132. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  133. int availability = sqlite3_column_int(stmt, 12);
  134. NSString *color = [self textAtColumn:13 statement:stmt];
  135. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  136. NSString *demension = [self textAtColumn:15 statement:stmt];
  137. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  138. NSString *material = [self textAtColumn:17 statement:stmt];
  139. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  140. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  141. double volume = sqlite3_column_double(stmt, 19);
  142. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  143. double weight = sqlite3_column_double(stmt, 20);
  144. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  145. int model_set = sqlite3_column_int(stmt, 21);
  146. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  147. double load_ability = sqlite3_column_double(stmt, 22);
  148. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  149. NSString *assembling = [self textAtColumn:24 statement:stmt];
  150. NSString *made_in = [self textAtColumn:25 statement:stmt];
  151. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  152. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  153. float bprice=0;
  154. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  155. for(int b=0;b< [bundle[@"count"] intValue];b++)
  156. {
  157. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  158. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  159. }
  160. if(gprice!=nil)
  161. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  162. if(s_percent.length==0||is_percentage==0)
  163. {
  164. percent=100.0;
  165. if([s_qty isEqualToString:@"null"])
  166. qty=availability;
  167. }
  168. else
  169. {
  170. qty=availability;
  171. }
  172. qty=qty*percent/100+0.5;
  173. for(int i=0;i<[values[@"count"] intValue];i++)
  174. {
  175. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  176. if([valueitem[@"product_id"] intValue]==product_id)
  177. {
  178. line_note=valueitem[@"line_note"];
  179. qty=[valueitem[@"available_qty"] intValue];
  180. if([[valueitem allKeys] containsObject:@"available_qty"])
  181. s_qty=[NSString stringWithFormat:@"%d",qty];
  182. else
  183. s_qty=@"null";
  184. if(valueitem[@"regular_price"]!=nil)
  185. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  186. s_price=valueitem[@"special_price"];
  187. price=[valueitem[@"special_price"] floatValue];
  188. discount=0;
  189. // v1.90
  190. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  191. }
  192. }
  193. if(is_percentage==0&&qty==0&&!show_stockout)
  194. {
  195. return;
  196. }
  197. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  198. {
  199. return;
  200. }
  201. NSString* set_price=@"";
  202. if([params[@"entered_price"] boolValue])
  203. {
  204. if (s_price==nil ) {
  205. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  206. }
  207. else
  208. {
  209. if(price*(1-discount/100.0)!=gprice.floatValue)
  210. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  211. }
  212. }
  213. NSString* get_price=@"";
  214. {
  215. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  216. // DebugLog(@"price time interval");
  217. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  218. if(gprice==nil)
  219. get_price=@"Price:No Price.";
  220. else
  221. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  222. }
  223. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  224. // if ([qty_null isEqualToString:@"null"]) {
  225. // qty=availability;
  226. // }
  227. //
  228. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  229. // if ([qty_percent_null isEqualToString:@"null"]) {
  230. // percentage=1;
  231. // }
  232. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  233. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  234. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  235. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  236. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  237. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  238. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  239. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  240. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  241. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  242. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  243. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  244. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  245. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  246. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  247. if([params[@"availability"] boolValue]==false)
  248. str_availability=@"";
  249. else
  250. {
  251. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  252. str_availability=@"<b>Availability:</b> In Production";
  253. }
  254. if([params[@"color"] boolValue]==false || color.length==0 )
  255. str_color=@"";
  256. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  257. str_model_set=@"";
  258. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  259. str_legcolor=@"";
  260. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  261. str_demension=@"";
  262. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  263. str_seat_height=@"";
  264. if([params[@"material"] boolValue]==false || material.length==0 )
  265. str_meterial=@"";
  266. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  267. str_box_dim=@"";
  268. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  269. str_volume=@"";
  270. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  271. str_weight=@"";
  272. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  273. str_load_ability=@"";
  274. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  275. str_fabric_content=@"";
  276. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  277. str_assembling=@"";
  278. if([params[@"made"] boolValue]==false || made_in.length==0 )
  279. str_made_in=@"";
  280. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  281. str_line_note=@"";
  282. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  283. [arr_detail addObject:str_availability];
  284. [arr_detail addObject:str_color];
  285. [arr_detail addObject:str_model_set];
  286. [arr_detail addObject:str_legcolor];
  287. [arr_detail addObject:str_demension];
  288. [arr_detail addObject:str_seat_height];
  289. [arr_detail addObject:str_meterial];
  290. [arr_detail addObject:str_box_dim];
  291. [arr_detail addObject:str_volume];
  292. [arr_detail addObject:str_weight];
  293. [arr_detail addObject:str_load_ability];
  294. [arr_detail addObject:str_fabric_content];
  295. [arr_detail addObject:str_assembling];
  296. [arr_detail addObject:str_made_in];
  297. [arr_detail addObject:str_line_note];
  298. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  299. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  300. //model image;
  301. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  302. // qr image
  303. NSString* qrpath=nil;
  304. if([params[@"show_barcode"] boolValue])
  305. {
  306. NSString* temp = NSTemporaryDirectory();
  307. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  308. qrpath=[temp stringByAppendingPathComponent:filename];
  309. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  310. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  311. }
  312. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  313. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  314. item[@"img"]=img_path;
  315. item[@"img_qr"]=qrpath;
  316. item[@"special_price"]=set_price;
  317. item[@"price"]=get_price;
  318. item[@"name"]=name;
  319. item[@"description"]=description;
  320. item[@"detail"]=detail;
  321. item[@"img"]=img_path;
  322. if (show_group_by && more_color_int != 0) {
  323. item[@"more_color"] = more_color_path;
  324. }
  325. // @{
  326. // //@"linenotes": line_note,
  327. //// @"product_id": product_id_string,
  328. //// @"available_qty": @(qty),
  329. //// @"available_percent" : @(percent),
  330. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  331. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  332. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  333. // @"": ,
  334. // @"": ,
  335. // @"": ,
  336. // @"": ,
  337. // @"": ,
  338. // @"": ,
  339. // @"":
  340. // }.mutableCopy;
  341. // if (percentage) {
  342. // [item removeObjectForKey:@"available_qty"];
  343. // } else {
  344. // [item removeObjectForKey:@"available_percent"];
  345. // }
  346. //
  347. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  348. // if ([qty_null isEqualToString:@"null"]) {
  349. // [item removeObjectForKey:@"available_qty"];
  350. // }
  351. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  352. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  353. }];
  354. [iSalesDB close_db:db];
  355. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  356. // [dic setValue:@"" forKey:@"email_content"];
  357. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  358. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  359. //
  360. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  361. // cell[@"count"]=[NSNumber numberWithInt:10];
  362. grid[@"cell0"]=dic;
  363. data[@"grid0"]=grid;
  364. return data;
  365. }
  366. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  367. {
  368. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  369. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  370. NSString *cache_folder=[paths objectAtIndex:0];
  371. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  372. BOOL bdir=NO;
  373. NSFileManager* fileManager = [NSFileManager defaultManager];
  374. if(PDF_DEBUG)
  375. {
  376. NSData *data = [NSData dataWithContentsOfFile:default_path];
  377. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  378. return ret;
  379. }
  380. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  381. {
  382. NSError * error=nil;
  383. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  384. {
  385. return nil;
  386. }
  387. }
  388. NSData *data = [NSData dataWithContentsOfFile:template_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  393. {
  394. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  395. 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 ];
  396. DebugLog(@"offline_login sql:%@",sqlQuery);
  397. sqlite3_stmt * statement;
  398. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  400. {
  401. if (sqlite3_step(statement) == SQLITE_ROW)
  402. {
  403. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  404. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  405. int can_show_price = sqlite3_column_int(statement, 0);
  406. int can_see_price = sqlite3_column_int(statement, 1);
  407. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  408. if(contact_id==nil)
  409. contact_id="";
  410. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  411. int user_type = sqlite3_column_int(statement, 3);
  412. int can_cancel_order = sqlite3_column_int(statement, 4);
  413. int can_set_cart_price = sqlite3_column_int(statement, 5);
  414. int can_create_portfolio = sqlite3_column_int(statement, 6);
  415. int can_delete_order = sqlite3_column_int(statement, 7);
  416. int can_submit_order = sqlite3_column_int(statement, 8);
  417. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  418. int can_create_order = sqlite3_column_int(statement, 10);
  419. char *mode = (char*)sqlite3_column_text(statement, 11);
  420. if(mode==nil)
  421. mode="";
  422. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  423. char *username = (char*)sqlite3_column_text(statement, 12);
  424. if(username==nil)
  425. username="";
  426. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  427. int can_update_contact_info = sqlite3_column_int(statement, 13);
  428. char *first_name = (char*)sqlite3_column_text(statement, 14);
  429. if(first_name==nil)
  430. first_name="";
  431. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  432. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  433. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  434. // [header setValue:nscontact_id forKey:@"contact_id"];
  435. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  436. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  437. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  438. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  439. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  440. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  441. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  442. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  443. //
  444. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  445. //
  446. // [header setValue:nsusername forKey:@"username"];
  447. //
  448. // NSError* error=nil;
  449. //
  450. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  451. // [header setValue:statusFilter forKey:@"statusFilter"];
  452. //
  453. //
  454. //
  455. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  456. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  457. //
  458. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  459. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  460. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  461. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  462. //
  463. // [ret setObject:header forKey:@"header"];
  464. [ret setValue:nsfirst_name forKey:@"first_name"];
  465. }
  466. sqlite3_finalize(statement);
  467. }
  468. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  469. return ret;
  470. }
  471. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  472. {
  473. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  474. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  475. data[@"npd_url"]=COMPANY_WEB;
  476. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  477. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  478. NSString* date = [formatter stringFromDate:[NSDate date]];
  479. data[@"print_date"]=date;
  480. data[@"company_name"]=COMPANY_FULL_NAME;
  481. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  482. data[@"TOTAL_PAGE"]=0;
  483. data[@"CURRENT_PAGE"]=0;
  484. // [formatter setDateFormat:@"MM/dd/yyyy"];
  485. // date = [formatter stringFromDate:[NSDate date]];
  486. // data[@"creat_date"]=date;
  487. NSString* temp = NSTemporaryDirectory();
  488. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  489. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  490. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  491. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  492. data[@"img_barcode"]=qrpath;
  493. data[@"so_id"]=order_code;
  494. data[@"company"]=COMPANY_FULL_NAME;
  495. // 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];
  496. 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];
  497. sqlite3 *db = [iSalesDB get_db];
  498. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  499. sqlite3_stmt * statement;
  500. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  501. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  502. row0[@"count"]=[NSNumber numberWithInt:1];
  503. datasource[@"row0"]=row0;
  504. NSString* print_status=@"";
  505. NSString* order_type=@"Offline Quote";
  506. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  507. {
  508. if (sqlite3_step(statement) == SQLITE_ROW)
  509. {
  510. int erpOrderStatus = sqlite3_column_int(statement, 0);
  511. int status = sqlite3_column_int(statement, 26);
  512. if(status==2)
  513. {
  514. switch (erpOrderStatus) {
  515. case 10:
  516. // order_type=@"Quoted Order";
  517. print_status=@"Quote";
  518. break;
  519. case 11:
  520. print_status=@"Booked";
  521. break;
  522. case 12:
  523. print_status=@"Picked";
  524. break;
  525. case 13:
  526. print_status=@"Shipped";
  527. break;
  528. case 14:
  529. print_status=@"Closed";
  530. break;
  531. case 15:
  532. print_status=@"Canceled";
  533. break;
  534. case 16:
  535. print_status=@"All";
  536. break;
  537. case 18:
  538. print_status=@"Processing";
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. else{
  545. switch (status) {
  546. case 0:
  547. {
  548. print_status=@"Quote";
  549. break;
  550. }
  551. case 1:
  552. {
  553. print_status=@"Saved";
  554. break;
  555. }
  556. case 3:
  557. {
  558. //canceled
  559. print_status=@"Canceled";
  560. break;
  561. }
  562. default:
  563. break;
  564. }
  565. }
  566. NSString* sender_name = [self textAtColumn:1 statement:statement];
  567. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  568. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  569. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  570. NSString* sender_email = [self textAtColumn:5 statement:statement];
  571. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  572. NSString* billing_name = [self textAtColumn:7 statement:statement];
  573. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  574. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  575. if(billing_phone.length>0)
  576. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  577. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  578. if(billing_fax.length>0)
  579. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  580. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  581. [billto_arr0 addObject:billing_phone];
  582. [billto_arr0 addObject:billing_fax];
  583. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. NSString* billing_email = [self textAtColumn:11 statement:statement];
  585. if(billing_email.length>0)
  586. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  587. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  588. if(billing_contact.length>0)
  589. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  590. NSString* receive_name = [self textAtColumn:12 statement:statement];
  591. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  592. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  593. if(receive_phone.length>0)
  594. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  595. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  596. if(receive_fax.length>0)
  597. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  598. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  599. [receive_arr0 addObject:receive_phone];
  600. [receive_arr0 addObject:receive_fax];
  601. NSString* receive_email = [self textAtColumn:16 statement:statement];
  602. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  603. if(receive_email.length>0)
  604. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  605. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  606. if(receive_contact.length>0)
  607. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  608. NSString* poNumber = [self textAtColumn:17 statement:statement];
  609. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  610. NSString* carrier = [self textAtColumn:19 statement:statement];
  611. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  612. NSString* terms = [self textAtColumn:21 statement:statement];
  613. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  614. NSString* port_destination = [self textAtColumn:23 statement:statement];
  615. NSString* etd = [self textAtColumn:24 statement:statement];
  616. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  617. if(sign_picpath.length==0)
  618. sign_picpath=nil;
  619. NSString* create_date = [self textAtColumn:27 statement:statement];
  620. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  621. NSString* general_notes=[self textAtColumn:33 statement:statement];
  622. NSString *customerID = [self textAtColumn:34 statement:statement];
  623. if (customerID.length == 0) {
  624. customerID = nil;
  625. }
  626. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  627. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  628. if(general_notes.length>0)
  629. {
  630. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  631. row2[@"item_0"]=@{@"g_notes":general_notes };
  632. row2[@"count"]=[NSNumber numberWithInt:1];
  633. datasource[@"row2"]=row2;
  634. }
  635. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  636. [billto_arr addObject:billing_ext];
  637. [billto_arr addObject:billing_pf];
  638. [billto_arr addObject:billing_contact];
  639. [billto_arr addObject:billing_email];
  640. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  641. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  642. [receive_arr addObject:receive_ext];
  643. [receive_arr addObject:receive_pf];
  644. [receive_arr addObject:receive_contact];
  645. [receive_arr addObject:receive_email];
  646. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  647. float shipping = sqlite3_column_double(statement, 28);
  648. float lift_gate_value = sqlite3_column_double(statement, 29);
  649. float handling_fee_value = sqlite3_column_double(statement, 30);
  650. data[@"company"]=sender_name;
  651. data[@"shipfrom_addr"]=sender_ext;
  652. data[@"shipfrom_phone"]=sender_phone;
  653. data[@"shipfrom_fax"]=sender_fax;
  654. data[@"shipfrom_email"]=sender_email;
  655. data[@"warehouse_name"]=warehouse_name;
  656. data[@"bill_to_name"]=billing_name;
  657. data[@"bill_to_ext"]=billto;
  658. data[@"ship_to_name"]=receive_name;
  659. data[@"ship_to_ext"]=receive;
  660. data[@"po_no"]=poNumber;
  661. data[@"rep"]=sales_rep;
  662. data[@"e_schdule"]=etd;
  663. data[@"sales_terms"]=sales_terms;
  664. data[@"port_destination"]=port_destination;
  665. data[@"terms"]=terms;
  666. data[@"carrier"]=carrier;
  667. data[@"vendor_no"]=vendor_no;
  668. data[@"customer_sign"]=sign_picpath;
  669. data[@"print_status"]=print_status;
  670. data[@"create_date"]=create_date;
  671. 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];;
  672. sqlite3_stmt * statement_cart;
  673. // NSDate *date1 = [NSDate date];
  674. int count=0;
  675. int cart_count=0;
  676. float credits=0;
  677. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  678. if ( dbresult== SQLITE_OK)
  679. {
  680. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  681. int row1count=0;
  682. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  683. {
  684. NSDate *row_date = [NSDate date];
  685. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  686. // int product_id = sqlite3_column_int(statement_cart, 0);
  687. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  688. int item_id = sqlite3_column_int(statement_cart, 7);
  689. double discount = sqlite3_column_double(statement_cart, 2);
  690. NSString* Price=nil;
  691. if(str_price==nil)
  692. {
  693. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  694. // NSDate *price_date = [NSDate date];
  695. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  696. // DebugLog(@"price time interval");
  697. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  698. if(price==nil)
  699. Price=@"No Price.";
  700. else
  701. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  702. }
  703. else
  704. {
  705. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  706. }
  707. float dunitprice=0;
  708. int item_count = sqlite3_column_int(statement_cart, 3);
  709. NSString* Amount=@"";
  710. if(![Price isEqualToString:@"No Price."])
  711. {
  712. dunitprice=[Price floatValue];
  713. dunitprice=dunitprice* (1-discount/100.0);
  714. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  715. credits+=dunitprice*item_count;
  716. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  717. }
  718. else
  719. {
  720. Price=@"";
  721. }
  722. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  723. // NSString *nsline_note=nil;
  724. // if(line_note!=nil)
  725. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  726. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  727. NSString *nsname=nil;
  728. if(name!=nil)
  729. nsname= [[NSString alloc]initWithUTF8String:name];
  730. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  731. NSString *nsdescription=nil;
  732. if(description!=nil)
  733. nsdescription= [[NSString alloc]initWithUTF8String:description];
  734. // int stockUom = sqlite3_column_int(statement_cart, 8);
  735. // int _id = sqlite3_column_int(statement_cart, 9);
  736. // NSDate *subtotal_date = [NSDate date];
  737. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  738. // DebugLog(@"subtotal_date time interval");
  739. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  740. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  741. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  742. // int carton=[bsubtotaljson[@"carton"] intValue];
  743. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  744. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  745. // NSDate *img_date = [NSDate date];
  746. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  747. // DebugLog(@"img_date time interval");
  748. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  749. itemjson[@"model"]=nsname;
  750. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  751. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  752. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  753. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  754. // itemjson[@"check"]=@"true";
  755. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  756. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  757. itemjson[@"unit_price"]=Price;
  758. itemjson[@"amount"]=Amount;
  759. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  760. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  761. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  762. // itemjson[@"note"]=nsline_note;
  763. // NSDate *date2 = [NSDate date];
  764. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  765. // DebugLog(@"model_bundle time interval");
  766. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  767. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  768. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  769. row1count++;
  770. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  771. for(int c=0;c<[combine[@"count"] intValue];c++)
  772. {
  773. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  774. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  775. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  776. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  777. credits+=combine_amount;
  778. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  779. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  780. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  781. row1count++;
  782. }
  783. count++;
  784. // DebugLog(@"row time interval");
  785. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  786. }
  787. row1[@"count"]=[NSNumber numberWithInt:row1count];
  788. datasource[@"row1"]=row1;
  789. data[@"table_model"]=datasource;
  790. sqlite3_finalize(statement_cart);
  791. }
  792. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  793. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  794. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  795. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  796. // if(credits>0)
  797. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  798. // else
  799. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  800. if(shipping>0)
  801. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  802. else
  803. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  804. if(lift_gate_value>0)
  805. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  806. else
  807. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  808. if(handling_fee_value>0)
  809. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  810. else
  811. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  812. float total = credits+shipping+lift_gate_value+handling_fee_value;
  813. if(total>0)
  814. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  815. else
  816. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  817. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  818. fee[@"row0"]=fee_row0;
  819. data[@"table_fee"]=fee;
  820. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  821. // ret[@"sign_url"] = sign_url;
  822. //
  823. // customer_contact = [self textAtColumn:52 statement:statement];
  824. // customer_email = [self textAtColumn:53 statement:statement];
  825. // customer_phone = [self textAtColumn:54 statement:statement];
  826. // customer_fax = [self textAtColumn:55 statement:statement];
  827. //
  828. //
  829. // int offline_edit=sqlite3_column_int(statement, 56);
  830. //
  831. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  832. //
  833. // char *soid = (char*)sqlite3_column_text(statement, 1);
  834. // if(soid==nil)
  835. // soid= "";
  836. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  837. // // so#
  838. // ret[@"so#"] = nssoid;
  839. //
  840. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  841. // if(poNumber==nil)
  842. // poNumber= "";
  843. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  844. //
  845. //
  846. //
  847. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  848. // if(create_time==nil)
  849. // create_time= "";
  850. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  851. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  852. //
  853. // int status = sqlite3_column_int(statement, 4);
  854. // int erpStatus = sqlite3_column_int(statement, 49);
  855. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  856. //
  857. // // status
  858. // if (status > 1 && status != 3) {
  859. // status = erpStatus;
  860. // } else if (status == 3) {
  861. // status = 15;
  862. // }
  863. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  864. // ret[@"order_status"] = nsstatus;
  865. //
  866. //
  867. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  868. // if(company_name==nil)
  869. // company_name= "";
  870. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  871. // // company name
  872. // ret[@"company_name"] = nscompany_name;
  873. //
  874. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  875. // if(customer_contact==nil)
  876. // customer_contact= "";
  877. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  878. //
  879. //
  880. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  881. // if(addr_1==nil)
  882. // addr_1="";
  883. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  884. //
  885. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  886. // if(addr_2==nil)
  887. // addr_2="";
  888. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  889. //
  890. //
  891. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  892. // if(addr_3==nil)
  893. // addr_3="";
  894. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  895. //
  896. //
  897. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  898. // if(addr_4==nil)
  899. // addr_4="";
  900. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  901. //
  902. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  903. // [arr_addr addObject:nsaddr_1];
  904. // [arr_addr addObject:nsaddr_2];
  905. // [arr_addr addObject:nsaddr_3];
  906. // [arr_addr addObject:nsaddr_4];
  907. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  908. //
  909. //
  910. // char *logist = (char*)sqlite3_column_text(statement, 11);
  911. // if(logist==nil)
  912. // logist= "";
  913. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  914. // if (status == -11 || status == 10 || status == 11) {
  915. // nslogist = [self textAtColumn:59 statement:statement];
  916. // };
  917. //
  918. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  919. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  920. // shipping = @"Shipping To Be Quoted";
  921. // } else {
  922. // shippingFee = sqlite3_column_double(statement, 12);
  923. // }
  924. //
  925. // // Shipping
  926. // ret[@"Shipping"] = shipping;
  927. //
  928. // int have_lift_gate = sqlite3_column_int(statement, 17);
  929. // lift_gate = sqlite3_column_double(statement, 13);
  930. // // Liftgate Fee(No loading dock)
  931. // if (!have_lift_gate) {
  932. // lift_gate = 0;
  933. // }
  934. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  935. // if (sqlite3_column_int(statement, 57)) {
  936. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  937. // }
  938. //
  939. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  940. // if(general_notes==nil)
  941. // general_notes= "";
  942. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  943. //
  944. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  945. // if(internal_notes==nil)
  946. // internal_notes= "";
  947. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  948. //
  949. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  950. // if(payment_type==nil)
  951. // payment_type= "";
  952. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  953. //
  954. //
  955. //
  956. // // order info
  957. // orderinfo = [self textFileName:@"order_info.html"];
  958. //
  959. //
  960. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  964. //
  965. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  972. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  973. //
  974. // NSString *payment = nil;
  975. // if([nspayment_type isEqualToString:@"Credit Card"])
  976. // {
  977. // payment = [self textFileName:@"creditcardpayment.html"];
  978. //
  979. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  980. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  981. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  982. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  983. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  984. // NSString *card_type = [self textAtColumn:42 statement:statement];
  985. // if (card_type.length > 0) { // 显示星号
  986. // card_type = @"****";
  987. // }
  988. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  989. // if (card_number.length > 0 && card_number.length > 4) {
  990. // for (int i = 0; i < card_number.length - 4; i++) {
  991. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  992. // }
  993. // } else {
  994. // card_number = @"";
  995. // }
  996. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  997. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  998. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  999. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1000. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1001. // card_expiration = @"****";
  1002. // }
  1003. //
  1004. //
  1005. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1006. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1007. //
  1008. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1009. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1010. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1011. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1012. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1013. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1014. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1015. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1016. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1017. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1018. //
  1019. //
  1020. // }
  1021. // else
  1022. // {
  1023. // payment=[self textFileName:@"normalpayment.html"];
  1024. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1025. // }
  1026. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1027. //
  1028. // ret[@"result"]= [NSNumber numberWithInt:2];
  1029. //
  1030. // // more info
  1031. // moreInfo = [self textFileName:@"more_info.html"];
  1032. //
  1033. //
  1034. // // ShipToCompany_or_&nbsp
  1035. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1036. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1037. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1038. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1039. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1040. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1041. //
  1042. //
  1043. // // ShipFromCompany_or_&nbsp
  1044. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1045. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1046. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1047. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1048. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1049. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1050. //
  1051. //
  1052. // // FreightBillToCompany_or_&nbsp
  1053. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1055. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1056. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1057. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1058. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1059. //
  1060. //
  1061. // // MerchandiseBillToCompany_or_&nbsp
  1062. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1064. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1065. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1066. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1067. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1068. //
  1069. //
  1070. // // ReturnToCompany_or_&nbsp
  1071. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1073. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1074. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1075. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1076. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1077. // //
  1078. //
  1079. // // DebugLog(@"more info : %@",moreInfo);
  1080. //
  1081. // // handling fee
  1082. // handlingFee = sqlite3_column_double(statement, 33);
  1083. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1084. // if (sqlite3_column_int(statement, 58)) {
  1085. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1086. // }
  1087. // //
  1088. //
  1089. // // customer info
  1090. // customerID = [self textAtColumn:36 statement:statement];
  1091. //
  1092. // // mode
  1093. // ret[@"mode"] = appDelegate.mode;
  1094. //
  1095. // // model_count
  1096. // ret[@"model_count"] = @(0);
  1097. }
  1098. sqlite3_finalize(statement);
  1099. }
  1100. [iSalesDB close_db:db];
  1101. data[@"order_type"]=order_type;
  1102. /*
  1103. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1104. // "customer_email" = "Shui Hu";
  1105. // "customer_fax" = "";
  1106. // "customer_first_name" = F;
  1107. // "customer_last_name" = L;
  1108. // "customer_name" = ",da He Xiang Dong Liu A";
  1109. // "customer_phone" = "Hey Xuan Feng";
  1110. contactInfo[@"customer_phone"] = customer_phone;
  1111. contactInfo[@"customer_fax"] = customer_fax;
  1112. contactInfo[@"customer_email"] = customer_email;
  1113. NSString *first_name = @"";
  1114. NSString *last_name = @"";
  1115. if ([customer_contact isEqualToString:@""]) {
  1116. } else if ([customer_contact containsString:@" "]) {
  1117. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1118. first_name = [customer_contact substringToIndex:first_space_index];
  1119. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1120. }
  1121. contactInfo[@"customer_first_name"] = first_name;
  1122. contactInfo[@"customer_last_name"] = last_name;
  1123. ret[@"customerInfo"] = contactInfo;
  1124. // models
  1125. if (nssoid) {
  1126. __block double TotalCuft = 0;
  1127. __block double TotalWeight = 0;
  1128. __block int TotalCarton = 0;
  1129. __block double allItemPrice = 0;
  1130. 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];
  1131. sqlite3 *db1 = [iSalesDB get_db];
  1132. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1133. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1134. int product_id = sqlite3_column_int(stmt, 0);
  1135. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1136. int item_id = sqlite3_column_int(stmt, 7);
  1137. NSString* Price=nil;
  1138. if(str_price==nil)
  1139. {
  1140. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1141. if(price==nil)
  1142. Price=@"No Price.";
  1143. else
  1144. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1145. }
  1146. else
  1147. {
  1148. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1149. }
  1150. double discount = sqlite3_column_double(stmt, 2);
  1151. int item_count = sqlite3_column_int(stmt, 3);
  1152. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1153. NSString *nsline_note=nil;
  1154. if(line_note!=nil)
  1155. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1156. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1157. // NSString *nsname = nil;
  1158. // if(name!=nil)
  1159. // nsname= [[NSString alloc]initWithUTF8String:name];
  1160. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1161. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1162. // NSString *nsdescription=nil;
  1163. // if(description!=nil)
  1164. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1165. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1166. // int stockUom = sqlite3_column_int(stmt, 8);
  1167. // int _id = sqlite3_column_int(stmt, 9);
  1168. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1169. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1170. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1171. int carton=[bsubtotaljson[@"carton"] intValue];
  1172. TotalCuft += cuft;
  1173. TotalWeight += weight;
  1174. TotalCarton += carton;
  1175. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1176. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1177. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1178. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1179. itemjson[@"note"]=nsline_note;
  1180. itemjson[@"origin_price"] = Price;
  1181. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1182. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1183. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1184. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1185. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1186. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1187. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1188. if(itemjson[@"combine"] != nil)
  1189. {
  1190. // int citem=0;
  1191. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1192. for(int bc=0;bc<bcount;bc++)
  1193. {
  1194. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1195. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1196. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1197. subTotal += uprice * modulus * item_count;
  1198. }
  1199. }
  1200. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1201. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1202. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1203. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1204. allItemPrice += subTotal;
  1205. }];
  1206. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1209. // payments/Credits
  1210. // payments_and_credist = sqlite3_column_double(statement, 34);
  1211. payments_and_credist = allItemPrice;
  1212. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1213. // // total
  1214. // totalPrice = sqlite3_column_double(statement, 35);
  1215. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1216. } else {
  1217. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1219. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1220. // payments/Credits
  1221. payments_and_credist = 0;
  1222. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1223. }
  1224. // total
  1225. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1226. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1227. ret[@"order_info"]= orderinfo;
  1228. ret[@"more_order_info"] = moreInfo;
  1229. return [RAUtils dict2data:ret];
  1230. */
  1231. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1232. return data;
  1233. }
  1234. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1235. {
  1236. NSString* template_file=nil;
  1237. switch([params[@"thumb"] intValue])
  1238. {
  1239. case 0:
  1240. template_file= @"so.json";
  1241. break;
  1242. case 1:
  1243. template_file= @"so_thumb.json";
  1244. break;
  1245. default:
  1246. template_file= @"so.json";
  1247. }
  1248. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1249. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1250. if(data[@"table_model"][@"row2"]==nil)
  1251. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1252. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1253. DebugLog(@"%@",file);
  1254. // return nil;
  1255. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1256. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1257. if (file) {
  1258. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1259. [dic setObject:file forKey:@"pdf_path"];
  1260. dic[@"isLocalFile"]=@"true";
  1261. return [RAUtils dict2data:dic];
  1262. }
  1263. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1264. dic[@"isLocalFile"]=@"true";
  1265. return [RAUtils dict2data:dic];
  1266. }
  1267. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1268. {
  1269. NSString* template_file=nil;
  1270. switch([params[@"pdf_style"] intValue])
  1271. {
  1272. case 0:
  1273. template_file= @"portfolio_2x3.json";
  1274. break;
  1275. case 1:
  1276. template_file= @"portfolio_3x2.json";
  1277. break;
  1278. default:
  1279. template_file= @"portfolio_3x2.json";
  1280. }
  1281. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1282. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1283. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1284. DebugLog(@"%@",file);
  1285. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1286. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1287. if (file) {
  1288. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1289. [dic setObject:file forKey:@"pdf_path"];
  1290. dic[@"isLocalFile"]=@"true";
  1291. return [RAUtils dict2data:dic];
  1292. }
  1293. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1294. dic[@"isLocalFile"]=@"true";
  1295. return [RAUtils dict2data:dic];
  1296. }
  1297. +(NSString*) get_offline_soid:(sqlite3*)db
  1298. {
  1299. NSString* soid=nil;
  1300. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1301. [formatter setDateFormat:@"yyMMdd"];
  1302. NSString* date = [formatter stringFromDate:[NSDate date]];
  1303. for(int i=1;i<999;i++)
  1304. {
  1305. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1306. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1307. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1308. if(count==0)
  1309. return soid;
  1310. }
  1311. return nil;
  1312. }
  1313. +(NSArray*) enumOfflineOrder
  1314. {
  1315. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1316. sqlite3 *db = [iSalesDB get_db];
  1317. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1318. sqlite3_stmt * statement;
  1319. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1320. if ( dbresult== SQLITE_OK)
  1321. {
  1322. while (sqlite3_step(statement) == SQLITE_ROW)
  1323. {
  1324. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1325. NSString* nsso_id=nil;
  1326. if(so_id!=nil)
  1327. {
  1328. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1329. }
  1330. [ret addObject:nsso_id];
  1331. }
  1332. sqlite3_finalize(statement);
  1333. }
  1334. [iSalesDB close_db:db];
  1335. return ret;
  1336. }
  1337. //+(void) uploadFile:(NSString*) file
  1338. //{
  1339. // NSData* data = [NSData dataWithContentsOfFile: file];
  1340. // UIApplication * app = [UIApplication sharedApplication];
  1341. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1342. //
  1343. //
  1344. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1345. //
  1346. //
  1347. //
  1348. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1349. //
  1350. // if(appDelegate.user!=nil)
  1351. // [params setValue:appDelegate.user forKey:@"user"];
  1352. // // if(appDelegate.contact_id!=nil)
  1353. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1354. // if(appDelegate.password!=nil)
  1355. // [params setValue:appDelegate.password forKey:@"password"];
  1356. //
  1357. //
  1358. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1359. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1360. // } error:nil];
  1361. //
  1362. //
  1363. //
  1364. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1365. //
  1366. // NSProgress *progress = nil;
  1367. //
  1368. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1369. //
  1370. //
  1371. //
  1372. // if (error) {
  1373. //
  1374. // NSString* err_msg = [error localizedDescription];
  1375. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1376. //
  1377. //
  1378. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1379. //
  1380. // DebugLog(@"data string: %@",str);
  1381. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1382. //
  1383. // } else {
  1384. // DebugLog(@"response ");
  1385. //
  1386. //
  1387. //
  1388. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1389. //
  1390. // // 再将NSData转为字符串
  1391. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1392. //
  1393. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1394. //
  1395. // DebugLog(@"data string: %@",jsonStr);
  1396. //
  1397. // NSDictionary* json = responseObject;
  1398. //
  1399. //
  1400. // if([[json valueForKey:@"result"] intValue]==2)
  1401. // {
  1402. //// NSString* img_url_down = json[@"img_url_aname"];
  1403. //// NSString* img_url_up = json[@"img_url"];
  1404. //
  1405. //
  1406. // }
  1407. // else
  1408. // {
  1409. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1410. // }
  1411. //
  1412. //
  1413. // }
  1414. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1415. // }];
  1416. //
  1417. //
  1418. //
  1419. //
  1420. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1421. // //
  1422. //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. //
  1428. // [uploadTask resume];
  1429. //
  1430. //}
  1431. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1432. {
  1433. NSString* serial= [[NSUUID UUID] UUIDString];
  1434. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1435. NSString *cachefolder = [paths objectAtIndex:0];
  1436. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1437. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1438. NSFileManager* fileManager = [NSFileManager defaultManager];
  1439. BOOL bdir=YES;
  1440. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1441. {
  1442. NSError *error = nil;
  1443. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1444. if(!bsuccess)
  1445. DebugLog(@"Create UPLOAD folder failed");
  1446. }
  1447. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1448. ret[@"contact"]=[self prepareContact:serial];
  1449. ret[@"wishlist"]=[self prepareWishlist:serial];
  1450. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1451. ret[@"portfolio"] = [self preparePortfolio:serial];
  1452. ret[@"view_portfolio"] = [self preparePDF:serial];
  1453. NSString* str= [RAUtils dict2string:ret];
  1454. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1455. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1456. NSError *error=nil;
  1457. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1458. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1459. ZipArchive* zip = [[ZipArchive alloc] init];
  1460. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1461. for(int i=0;i<arr_files.count;i++)
  1462. {
  1463. NSString* file=arr_files[i];
  1464. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1465. }
  1466. if( ![zip CloseZipFile2] )
  1467. {
  1468. zippath = @"";
  1469. }
  1470. ret[@"file"]=zippath;
  1471. return ret;
  1472. }
  1473. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1474. {
  1475. if(filename.length==0)
  1476. return false;
  1477. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1478. bool ret=false;
  1479. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1480. NSString *cachefolder = [paths objectAtIndex:0];
  1481. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1482. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1483. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1484. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1485. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1486. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1487. NSFileManager* fileManager = [NSFileManager defaultManager];
  1488. BOOL bdir=NO;
  1489. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1490. {
  1491. NSError *error = nil;
  1492. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1493. {
  1494. ret=false;
  1495. }
  1496. else
  1497. {
  1498. ret=true;
  1499. }
  1500. }
  1501. return ret;
  1502. }
  1503. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1504. {
  1505. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1506. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1507. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1508. NSString* where=@" 1=1";
  1509. if (ver!=nil) {
  1510. where=@"is_dirty=1";
  1511. }
  1512. sqlite3 *db = [iSalesDB get_db];
  1513. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1514. sqlite3_stmt * statement;
  1515. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1516. int count=0;
  1517. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1518. if ( dbresult== SQLITE_OK)
  1519. {
  1520. while (sqlite3_step(statement) == SQLITE_ROW)
  1521. {
  1522. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1523. int _id = sqlite3_column_int(statement, 0);
  1524. int product_id = sqlite3_column_int(statement, 1);
  1525. int item_id = sqlite3_column_int(statement, 2);
  1526. int qty = sqlite3_column_int(statement, 3);
  1527. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1528. NSString* nscreate_time=nil;
  1529. if(create_time!=nil)
  1530. {
  1531. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1532. }
  1533. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1534. NSString* nsmodify_time=nil;
  1535. if(modify_time!=nil)
  1536. {
  1537. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1538. }
  1539. int is_delete = sqlite3_column_int(statement, 6);
  1540. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1541. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1542. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1543. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1544. itemjson[@"create_time"]=nscreate_time;
  1545. itemjson[@"modify_time"]=nsmodify_time;
  1546. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1547. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1548. count++;
  1549. }
  1550. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1551. sqlite3_finalize(statement);
  1552. }
  1553. ret[@"count"]=[NSNumber numberWithInt:count ];
  1554. [iSalesDB close_db:db];
  1555. return ret;
  1556. }
  1557. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1558. {
  1559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1560. UIApplication * app = [UIApplication sharedApplication];
  1561. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1562. 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 ];
  1563. // 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 ];
  1564. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1565. DebugLog(@"offline_login sql:%@",sqlQuery);
  1566. sqlite3_stmt * statement;
  1567. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1568. int count=0;
  1569. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1570. if ( dbresult== SQLITE_OK)
  1571. {
  1572. while (sqlite3_step(statement) == SQLITE_ROW)
  1573. {
  1574. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1575. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1576. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1577. int product_id = sqlite3_column_int(statement, 0);
  1578. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1579. int item_id = sqlite3_column_int(statement, 7);
  1580. NSString* Price=nil;
  1581. if(str_price==nil)
  1582. {
  1583. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1584. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1585. if(price==nil)
  1586. Price=@"No Price.";
  1587. else
  1588. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1589. }
  1590. else
  1591. {
  1592. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1593. }
  1594. double discount = sqlite3_column_double(statement, 2);
  1595. int item_count = sqlite3_column_int(statement, 3);
  1596. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1597. NSString *nsline_note=nil;
  1598. if(line_note!=nil)
  1599. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1600. char *name = (char*)sqlite3_column_text(statement, 5);
  1601. NSString *nsname=nil;
  1602. if(name!=nil)
  1603. nsname= [[NSString alloc]initWithUTF8String:name];
  1604. char *description = (char*)sqlite3_column_text(statement, 6);
  1605. NSString *nsdescription=nil;
  1606. if(description!=nil)
  1607. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1608. // int stockUom = sqlite3_column_int(statement, 8);
  1609. // int _id = sqlite3_column_int(statement, 9);
  1610. //
  1611. //
  1612. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1613. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1614. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1615. // int carton=[bsubtotaljson[@"carton"] intValue];
  1616. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1617. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1618. itemjson[@"model"]=nsname;
  1619. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1620. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1621. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1622. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1623. // itemjson[@"check"]=@"true";
  1624. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1625. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1626. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1627. itemjson[@"unit_price"]=Price;
  1628. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1629. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1630. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1631. itemjson[@"note"]=nsline_note;
  1632. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1633. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1634. count++;
  1635. }
  1636. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1637. sqlite3_finalize(statement);
  1638. }
  1639. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1640. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1641. return ret;
  1642. }
  1643. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1644. {
  1645. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1646. sqlite3 *db = [iSalesDB get_db];
  1647. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1648. // for(int i=0;i<arr_soid.count;i++)
  1649. sqlite3_stmt * statement;
  1650. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1651. //int count=0;
  1652. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1653. if ( dbresult== SQLITE_OK)
  1654. {
  1655. int count=0;
  1656. while (sqlite3_step(statement) == SQLITE_ROW)
  1657. {
  1658. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1659. int _id = sqlite3_column_int(statement, 0);
  1660. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1661. NSString* nssync_data=nil;
  1662. if(sync_data!=nil)
  1663. {
  1664. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1665. }
  1666. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1667. NSString* nsimg_1=nil;
  1668. if(img_1!=nil)
  1669. {
  1670. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1671. [self copy_upImg:serial file:nsimg_1];
  1672. }
  1673. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1674. NSString* nsso_no=nil;
  1675. if(so_no!=nil)
  1676. {
  1677. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1678. }
  1679. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1680. itemjson[@"order_type"]=@"submit order";
  1681. else
  1682. itemjson[@"order_type"]=@"archive order";
  1683. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1684. itemjson[@"json_data"]= nssync_data;
  1685. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1686. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1687. count++;
  1688. }
  1689. ret[@"count"]=[NSNumber numberWithInt:count ];
  1690. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1691. }
  1692. sqlite3_finalize(statement);
  1693. [iSalesDB close_db:db];
  1694. return ret;
  1695. }
  1696. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1697. {
  1698. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1699. sqlite3 *db = [iSalesDB get_db];
  1700. // UIApplication * app = [UIApplication sharedApplication];
  1701. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1702. 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";
  1703. sqlite3_stmt * statement;
  1704. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1705. int count=0;
  1706. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1707. if ( dbresult== SQLITE_OK)
  1708. {
  1709. while (sqlite3_step(statement) == SQLITE_ROW)
  1710. {
  1711. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1712. int _id = sqlite3_column_int(statement, 0);
  1713. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1714. NSString* nsimg_0=nil;
  1715. if(img_0!=nil)
  1716. {
  1717. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1718. [self copy_upImg:serial file:nsimg_0];
  1719. }
  1720. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1721. NSString* nsimg_1=nil;
  1722. if(img_1!=nil)
  1723. {
  1724. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1725. [self copy_upImg:serial file:nsimg_1];
  1726. }
  1727. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1728. NSString* nsimg_2=nil;
  1729. if(img_2!=nil)
  1730. {
  1731. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1732. [self copy_upImg:serial file:nsimg_2];
  1733. }
  1734. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1735. NSString* nssync_data=nil;
  1736. if(sync_data!=nil)
  1737. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1738. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1739. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1740. itemjson[@"json_data"]= nssync_data;
  1741. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1742. count++;
  1743. }
  1744. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1745. sqlite3_finalize(statement);
  1746. }
  1747. ret[@"count"]=[NSNumber numberWithInt:count ];
  1748. [iSalesDB close_db:db];
  1749. return ret;
  1750. }
  1751. +(bool) check_offlinedata
  1752. {
  1753. UIApplication * app = [UIApplication sharedApplication];
  1754. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1755. sqlite3 *db = [iSalesDB get_db];
  1756. NSString * where=@"1=1";
  1757. // if(appDelegate.user!=nil)
  1758. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1759. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1760. [iSalesDB close_db:db];
  1761. if(count==0)
  1762. {
  1763. return false;
  1764. }
  1765. return true;
  1766. //
  1767. //[iSalesDB close_db:db];
  1768. }
  1769. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1770. {
  1771. UIApplication * app = [UIApplication sharedApplication];
  1772. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1773. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1774. sqlite3 *db = [iSalesDB get_db];
  1775. NSString* collectId=params[@"collectId"];
  1776. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1777. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1778. [iSalesDB execSql:sqlQuery db:db];
  1779. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1780. [iSalesDB close_db:db];
  1781. appDelegate.wish_count =count;
  1782. [appDelegate update_count_mark];
  1783. ret[@"result"]= [NSNumber numberWithInt:2];
  1784. return ret;
  1785. }
  1786. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1787. //{
  1788. //
  1789. // UIApplication * app = [UIApplication sharedApplication];
  1790. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1791. //
  1792. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1793. // sqlite3 *db = [iSalesDB get_db];
  1794. // NSString* product_id=params[@"product_id"];
  1795. //
  1796. //
  1797. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1798. //
  1799. //
  1800. // for(int i=0;i<arr.count;i++)
  1801. // {
  1802. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1803. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1804. // if(count==0)
  1805. // {
  1806. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1807. // [iSalesDB execSql:sqlQuery db:db];
  1808. // }
  1809. // }
  1810. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1811. // [iSalesDB close_db:db];
  1812. //
  1813. // appDelegate.wish_count =count;
  1814. //
  1815. // [appDelegate update_count_mark];
  1816. // ret[@"result"]= [NSNumber numberWithInt:2];
  1817. // return ret;
  1818. // //
  1819. // //return ret;
  1820. //}
  1821. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1822. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1823. DebugLog(@"time interval: %lf",interval);
  1824. }
  1825. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1826. {
  1827. UIApplication * app = [UIApplication sharedApplication];
  1828. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1829. int sort = [[params objectForKey:@"sort"] intValue];
  1830. NSString *sort_str = @"";
  1831. switch (sort) {
  1832. case 0:{
  1833. sort_str = @"order by w.modify_time desc";
  1834. }
  1835. break;
  1836. case 1:{
  1837. sort_str = @"order by w.modify_time asc";
  1838. }
  1839. break;
  1840. case 2:{
  1841. sort_str = @"order by m.name asc";
  1842. }
  1843. break;
  1844. case 3:{
  1845. sort_str = @"order by m.name desc";
  1846. }
  1847. break;
  1848. case 4:{
  1849. sort_str = @"order by m.description asc";
  1850. }
  1851. break;
  1852. default:
  1853. break;
  1854. }
  1855. NSString* user = appDelegate.user;
  1856. sqlite3 *db = [iSalesDB get_db];
  1857. // order by w.create_time
  1858. 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];
  1859. // 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];
  1860. sqlite3_stmt * statement;
  1861. NSDate *date1 = [NSDate date];
  1862. NSDate *date2 = nil;
  1863. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1864. int count=0;
  1865. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1866. {
  1867. while (sqlite3_step(statement) == SQLITE_ROW)
  1868. {
  1869. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1870. int product_id = sqlite3_column_double(statement, 0);
  1871. char *description = (char*)sqlite3_column_text(statement, 1);
  1872. if(description==nil)
  1873. description= "";
  1874. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1875. int item_id = sqlite3_column_double(statement, 2);
  1876. NSDate *date_image = [NSDate date];
  1877. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1878. printf("image : ");
  1879. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1880. // char *url = (char*)sqlite3_column_text(statement, 3);
  1881. // if(url==nil)
  1882. // url="";
  1883. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1884. int qty = sqlite3_column_int(statement, 3);
  1885. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1886. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1887. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1888. item[@"description"]= nsdescription;
  1889. item[@"img"]= nsurl;
  1890. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1891. count++;
  1892. }
  1893. printf("total time:");
  1894. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1895. ret[@"count"]= [NSNumber numberWithInt:count];
  1896. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1897. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1898. ret[@"result"]= [NSNumber numberWithInt:2];
  1899. appDelegate.wish_count =count;
  1900. [appDelegate update_count_mark];
  1901. sqlite3_finalize(statement);
  1902. }
  1903. [iSalesDB close_db:db];
  1904. return ret;
  1905. }
  1906. +(NSDictionary*) offline_notimpl
  1907. {
  1908. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1909. ret[@"result"]=@"8";
  1910. ret[@"err_msg"]=@"offline mode does not support this function.";
  1911. return ret;
  1912. }
  1913. +(NSDictionary*) offline_home
  1914. {
  1915. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1916. NSString *cachefolder = [paths objectAtIndex:0];
  1917. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1918. NSData* json =nil;
  1919. json=[NSData dataWithContentsOfFile:img_cache];
  1920. if(json==nil)
  1921. return nil;
  1922. NSError *error=nil;
  1923. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1924. return menu;
  1925. }
  1926. +(NSDictionary*) offline_category_menu
  1927. {
  1928. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1929. NSString *cachefolder = [paths objectAtIndex:0];
  1930. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1931. NSData* json =nil;
  1932. json=[NSData dataWithContentsOfFile:img_cache];
  1933. if(json==nil)
  1934. return nil;
  1935. NSError *error=nil;
  1936. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1937. return menu;
  1938. }
  1939. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1940. {
  1941. NSString* offline_command=params[@"offline_Command"];
  1942. NSDictionary* ret=nil;
  1943. if([offline_command isEqualToString:@"model_NIYMAL"])
  1944. {
  1945. NSString* category = params[@"category"];
  1946. ret = [self refresh_model_NIYMAL:category];
  1947. }
  1948. return ret;
  1949. }
  1950. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1951. {
  1952. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1953. [ret setValue:@"2" forKey:@"result"];
  1954. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1955. sqlite3* db= [iSalesDB get_db];
  1956. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1957. [iSalesDB close_db:db];
  1958. [ret setObject:detail1_section forKey:@"detail_1"];
  1959. return ret;
  1960. }
  1961. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1962. {
  1963. UIApplication * app = [UIApplication sharedApplication];
  1964. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1965. // NSArray* arr1 = [self get_user_all_price_type];
  1966. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1967. // NSSet *set1 = [NSSet setWithArray:arr1];
  1968. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1969. // if(appDelegate.contact_id==nil)
  1970. // set2=[set1 mutableCopy];
  1971. // else
  1972. // [set2 intersectsSet:set1];
  1973. // NSArray *retarr = [set2 allObjects];
  1974. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1975. // sqlite3 *db = [iSalesDB get_db];
  1976. NSString* sqlQuery = nil;
  1977. if(appDelegate.contact_id==nil)
  1978. {
  1979. if(!appDelegate.bLogin)
  1980. return nil;
  1981. 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];
  1982. }
  1983. else
  1984. 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];
  1985. sqlite3_stmt * statement;
  1986. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1987. int count=0;
  1988. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1989. {
  1990. while (sqlite3_step(statement) == SQLITE_ROW)
  1991. {
  1992. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1993. char *name = (char*)sqlite3_column_text(statement, 0);
  1994. if(name==nil)
  1995. name="";
  1996. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1997. // double price = sqlite3_column_double(statement, 1);
  1998. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1999. // if(isnull==nil)
  2000. // item[nsname]= @"No Price";
  2001. // else
  2002. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2003. char *price = (char*)sqlite3_column_text(statement, 1);
  2004. if(price!=nil)
  2005. {
  2006. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2007. nsprice=[AESCrypt fastdecrypt:nsprice];
  2008. if(nsprice.length>0)
  2009. {
  2010. double dp= [nsprice doubleValue];
  2011. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2012. }
  2013. }
  2014. else
  2015. {
  2016. item[nsname]= @"No Price";
  2017. }
  2018. // int type= sqlite3_column_int(statement, 2);
  2019. //item[@"type"]=@"price";
  2020. // item[nsname]= nsprice;
  2021. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2022. count++;
  2023. }
  2024. ret[@"count"]= [NSNumber numberWithInt:count];
  2025. sqlite3_finalize(statement);
  2026. }
  2027. // [iSalesDB close_db:db];
  2028. return ret;
  2029. }
  2030. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2031. //{
  2032. // NSArray* arr1 = [self get_user_all_price_type:db];
  2033. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2034. //
  2035. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2036. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2037. // // [set2 intersectsSet:set1];
  2038. // //
  2039. // //
  2040. // // NSArray *retarr = [set2 allObjects];
  2041. //
  2042. // NSString* whereprice=nil;
  2043. // if(contact_id==nil)
  2044. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2045. // else
  2046. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2047. //
  2048. //
  2049. // // sqlite3 *db = [iSalesDB get_db];
  2050. //
  2051. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2052. // sqlite3_stmt * statement;
  2053. //
  2054. //
  2055. // NSNumber* ret = nil;
  2056. // double dprice=DBL_MAX;
  2057. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2058. // {
  2059. //
  2060. //
  2061. // while (sqlite3_step(statement) == SQLITE_ROW)
  2062. // {
  2063. //
  2064. // // double val = sqlite3_column_double(statement, 0);
  2065. // char *price = (char*)sqlite3_column_text(statement, 0);
  2066. // if(price!=nil)
  2067. // {
  2068. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2069. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2070. // if(nsprice.length>0)
  2071. // {
  2072. // double dp= [nsprice doubleValue];
  2073. // if(dp<dprice)
  2074. // dprice=dp;
  2075. // }
  2076. // }
  2077. // }
  2078. //
  2079. //
  2080. //
  2081. //
  2082. // sqlite3_finalize(statement);
  2083. //
  2084. //
  2085. //
  2086. //
  2087. // }
  2088. //
  2089. // // [iSalesDB close_db:db];
  2090. //
  2091. // if(dprice==DBL_MAX)
  2092. // ret= nil;
  2093. // else
  2094. // ret= [NSNumber numberWithDouble:dprice];
  2095. // return ret;
  2096. //}
  2097. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2098. {
  2099. NSArray* arr1 = [self get_user_all_price_type:db];
  2100. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2101. NSString* whereprice=nil;
  2102. if(contact_id==nil)
  2103. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2104. else
  2105. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2106. // sqlite3 *db = [iSalesDB get_db];
  2107. NSString *productIdCondition = @"1 = 1";
  2108. if (product_id) {
  2109. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2110. }
  2111. NSString *itemIdCondition = @"";
  2112. if (item_id) {
  2113. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2114. }
  2115. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2116. sqlite3_stmt * statement;
  2117. NSNumber* ret = nil;
  2118. double dprice=DBL_MAX;
  2119. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2120. {
  2121. while (sqlite3_step(statement) == SQLITE_ROW)
  2122. {
  2123. // double val = sqlite3_column_double(statement, 0);
  2124. char *price = (char*)sqlite3_column_text(statement, 0);
  2125. if(price!=nil)
  2126. {
  2127. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2128. nsprice=[AESCrypt fastdecrypt:nsprice];
  2129. if(nsprice.length>0)
  2130. {
  2131. double dp= [nsprice doubleValue];
  2132. if(dp<dprice)
  2133. dprice=dp;
  2134. }
  2135. }
  2136. }
  2137. sqlite3_finalize(statement);
  2138. }
  2139. // [iSalesDB close_db:db];
  2140. if(dprice==DBL_MAX)
  2141. ret= nil;
  2142. else
  2143. ret= [NSNumber numberWithDouble:dprice];
  2144. return ret;
  2145. }
  2146. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2147. {
  2148. NSArray* ret=nil;
  2149. // sqlite3 *db = [iSalesDB get_db];
  2150. // no customer assigned , use login user contact_id
  2151. UIApplication * app = [UIApplication sharedApplication];
  2152. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2153. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2154. sqlite3_stmt * statement;
  2155. // int count=0;
  2156. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2157. {
  2158. if (sqlite3_step(statement) == SQLITE_ROW)
  2159. {
  2160. char *val = (char*)sqlite3_column_text(statement, 0);
  2161. if(val==nil)
  2162. val="";
  2163. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2164. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2165. }
  2166. sqlite3_finalize(statement);
  2167. }
  2168. // [iSalesDB close_db:db];
  2169. return ret;
  2170. }
  2171. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2172. {
  2173. // sqlite3 *db = [iSalesDB get_db];
  2174. if(contact_id==nil)
  2175. {
  2176. // no customer assigned , use login user contact_id
  2177. UIApplication * app = [UIApplication sharedApplication];
  2178. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2179. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2180. sqlite3_stmt * statement;
  2181. // int count=0;
  2182. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2183. {
  2184. if (sqlite3_step(statement) == SQLITE_ROW)
  2185. {
  2186. char *val = (char*)sqlite3_column_text(statement, 0);
  2187. if(val==nil)
  2188. val="";
  2189. contact_id = [[NSString alloc]initWithUTF8String:val];
  2190. }
  2191. sqlite3_finalize(statement);
  2192. }
  2193. if(contact_id.length<=0)
  2194. {
  2195. // [iSalesDB close_db:db];
  2196. return nil;
  2197. }
  2198. }
  2199. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2200. sqlite3_stmt * statement;
  2201. NSArray* ret=nil;
  2202. // int count=0;
  2203. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2204. {
  2205. if (sqlite3_step(statement) == SQLITE_ROW)
  2206. {
  2207. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2208. if(price_type==nil)
  2209. price_type="";
  2210. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2211. if(nsprice_type.length>0)
  2212. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2213. else
  2214. ret=nil;
  2215. }
  2216. sqlite3_finalize(statement);
  2217. }
  2218. // [iSalesDB close_db:db];
  2219. return ret;
  2220. }
  2221. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2222. {
  2223. NSString* ret= nil;
  2224. // sqlite3 *db = [iSalesDB get_db];
  2225. NSString *sqlQuery = nil;
  2226. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2227. // 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;
  2228. // 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
  2229. if(product_id==nil && model_name)
  2230. 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;
  2231. else if (product_id)
  2232. 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
  2233. sqlite3_stmt * statement;
  2234. // int count=0;
  2235. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2236. {
  2237. if (sqlite3_step(statement) == SQLITE_ROW)
  2238. {
  2239. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2240. if(imgurl==nil)
  2241. imgurl="";
  2242. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2243. ret=nsimgurl;
  2244. }
  2245. sqlite3_finalize(statement);
  2246. }
  2247. else
  2248. {
  2249. [ret setValue:@"8" forKey:@"result"];
  2250. }
  2251. // [iSalesDB close_db:db];
  2252. DebugLog(@"data string: %@",ret );
  2253. return ret;
  2254. }
  2255. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2256. {
  2257. NSString* ret= nil;
  2258. sqlite3 *db = [iSalesDB get_db];
  2259. NSString *sqlQuery = nil;
  2260. if(product_id==nil)
  2261. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2262. else
  2263. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2264. sqlite3_stmt * statement;
  2265. // int count=0;
  2266. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2267. {
  2268. if (sqlite3_step(statement) == SQLITE_ROW)
  2269. {
  2270. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2271. if(default_category==nil)
  2272. default_category="";
  2273. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2274. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2275. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2276. ret=nsdefault_category;
  2277. }
  2278. sqlite3_finalize(statement);
  2279. }
  2280. else
  2281. {
  2282. [ret setValue:@"8" forKey:@"result"];
  2283. }
  2284. [iSalesDB close_db:db];
  2285. DebugLog(@"data string: %@",ret );
  2286. return ret;
  2287. }
  2288. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2289. {
  2290. NSString* model_name = [params valueForKey:@"product_name"];
  2291. NSString* product_id = [params valueForKey:@"product_id"];
  2292. NSString* category = [params valueForKey:@"category"];
  2293. if(category==nil)
  2294. category = [self model_default_category:product_id model_name:model_name];
  2295. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2296. sqlite3 *db = [iSalesDB get_db];
  2297. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2298. NSString *sqlQuery = nil;
  2299. if(product_id==nil)
  2300. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  2301. else
  2302. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  2303. sqlite3_stmt * statement;
  2304. [ret setValue:@"2" forKey:@"result"];
  2305. [ret setValue:@"3" forKey:@"detail_section_count"];
  2306. // int count=0;
  2307. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2308. {
  2309. if (sqlite3_step(statement) == SQLITE_ROW)
  2310. {
  2311. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2312. char *name = (char*)sqlite3_column_text(statement, 0);
  2313. if(name==nil)
  2314. name="";
  2315. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2316. char *description = (char*)sqlite3_column_text(statement, 1);
  2317. if(description==nil)
  2318. description="";
  2319. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2320. int product_id = sqlite3_column_int(statement, 2);
  2321. char *color = (char*)sqlite3_column_text(statement, 3);
  2322. if(color==nil)
  2323. color="";
  2324. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2325. //
  2326. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2327. // if(legcolor==nil)
  2328. // legcolor="";
  2329. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2330. //
  2331. //
  2332. int availability = sqlite3_column_int(statement, 5);
  2333. //
  2334. int incoming_stock = sqlite3_column_int(statement, 6);
  2335. char *demension = (char*)sqlite3_column_text(statement, 7);
  2336. if(demension==nil)
  2337. demension="";
  2338. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2339. // ,,,,,,,,,
  2340. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2341. if(seat_height==nil)
  2342. seat_height="";
  2343. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2344. char *material = (char*)sqlite3_column_text(statement, 9);
  2345. if(material==nil)
  2346. material="";
  2347. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2348. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2349. if(box_dim==nil)
  2350. box_dim="";
  2351. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2352. char *volume = (char*)sqlite3_column_text(statement, 11);
  2353. if(volume==nil)
  2354. volume="";
  2355. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2356. double weight = sqlite3_column_double(statement, 12);
  2357. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2358. if(model_set==nil)
  2359. model_set="";
  2360. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2361. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2362. if(load_ability==nil)
  2363. load_ability="";
  2364. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2365. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2366. if(default_category==nil)
  2367. default_category="";
  2368. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2369. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2370. if(fabric_content==nil)
  2371. fabric_content="";
  2372. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2373. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2374. if(assembling==nil)
  2375. assembling="";
  2376. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2377. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2378. if(made_in==nil)
  2379. made_in="";
  2380. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2381. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2382. if(special_remarks==nil)
  2383. special_remarks="";
  2384. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2385. int stockUcom = sqlite3_column_double(statement, 20);
  2386. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2387. if(product_group==nil)
  2388. product_group="";
  2389. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2390. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2391. // if(fashion_selector==nil)
  2392. // fashion_selector="";
  2393. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2394. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2395. if(selector_field==nil)
  2396. selector_field="";
  2397. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2398. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2399. if(property_field==nil)
  2400. property_field="";
  2401. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2402. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2403. if(packaging==nil)
  2404. packaging="";
  2405. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2406. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2407. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2408. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2409. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2410. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2411. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2412. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2413. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2414. NSString* Availability=nil;
  2415. if(availability>0)
  2416. Availability=[NSString stringWithFormat:@"%d",availability];
  2417. else
  2418. Availability = @"Out of Stock";
  2419. [img_section setValue:Availability forKey:@"Availability"];
  2420. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2421. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2422. char *eta = (char*)sqlite3_column_text(statement, 25);
  2423. if(eta==nil)
  2424. eta="";
  2425. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2426. if ([nseta isEqualToString:@"null"]) {
  2427. nseta = @"";
  2428. }
  2429. if (availability <= 0) {
  2430. [img_section setValue:nseta forKey:@"ETA"];
  2431. }
  2432. int item_id = sqlite3_column_int(statement, 26);
  2433. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2434. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2435. NSString* Price=nil;
  2436. if(appDelegate.bLogin==false)
  2437. Price=@"Must Sign in.";
  2438. else
  2439. {
  2440. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2441. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2442. if(price==nil)
  2443. Price=@"No Price.";
  2444. else
  2445. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2446. }
  2447. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2448. [img_section setValue:Price forKey:@"price"];
  2449. [img_section setValue:nsname forKey:@"model_name"];
  2450. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2451. if (appDelegate.order_code) { // 离线order code即so#
  2452. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2453. __block int cartQTY = 0;
  2454. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2455. cartQTY = sqlite3_column_int(stmt, 0);
  2456. }];
  2457. if (cartQTY > 0) {
  2458. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2459. }
  2460. }
  2461. [ret setObject:img_section forKey:@"img_section"];
  2462. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2463. int detail0_item_count=0;
  2464. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2465. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2466. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2467. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2468. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2469. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2470. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2471. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2472. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2473. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2474. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2475. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2476. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2477. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2478. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2479. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2480. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2481. {
  2482. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2483. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2484. }
  2485. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2486. [detail0_section setValue:@"kv" forKey:@"type"];
  2487. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2488. [ret setObject:detail0_section forKey:@"detail_0"];
  2489. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2490. // [detail1_section setValue:@"detail" forKey:@"target"];
  2491. // [detail1_section setValue:@"popup" forKey:@"action"];
  2492. // [detail1_section setValue:@"content" forKey:@"type"];
  2493. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2494. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2495. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2496. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2497. [ret setObject:detail1_section forKey:@"detail_1"];
  2498. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2499. [detail2_section setValue:@"detail" forKey:@"target"];
  2500. [detail2_section setValue:@"popup" forKey:@"action"];
  2501. [detail2_section setValue:@"content" forKey:@"type"];
  2502. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2503. [detail2_section setValue:@"true" forKey:@"single_row"];
  2504. [detail2_section setValue:@"local" forKey:@"data"];
  2505. [ret setObject:detail2_section forKey:@"detail_2"];
  2506. }
  2507. sqlite3_finalize(statement);
  2508. }
  2509. else
  2510. {
  2511. [ret setValue:@"8" forKey:@"result"];
  2512. }
  2513. DebugLog(@"count:%d",count);
  2514. [iSalesDB close_db:db];
  2515. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2516. return ret;
  2517. }
  2518. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2519. NSString* orderCode = [params valueForKey:@"orderCode"];
  2520. NSString* keyword = [params valueForKey:@"keyword"];
  2521. keyword=keyword.lowercaseString;
  2522. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2523. int limit = [[params valueForKey:@"limit"] intValue];
  2524. int offset = [[params valueForKey:@"offset"] intValue];
  2525. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2526. NSString *limit_str = @"";
  2527. if (limited) {
  2528. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2529. }
  2530. sqlite3 *db = [iSalesDB get_db];
  2531. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2532. UIApplication * app = [UIApplication sharedApplication];
  2533. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2534. NSString *sqlQuery = nil;
  2535. if(exactMatch )
  2536. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",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 ;
  2537. else
  2538. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2539. DebugLog(@"offline_search sql:%@",sqlQuery);
  2540. sqlite3_stmt * statement;
  2541. [ret setValue:@"2" forKey:@"result"];
  2542. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2543. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2544. // int count=0;
  2545. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2546. {
  2547. int i=0;
  2548. while (sqlite3_step(statement) == SQLITE_ROW)
  2549. {
  2550. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2551. // char *name = (char*)sqlite3_column_text(statement, 1);
  2552. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2553. char *name = (char*)sqlite3_column_text(statement, 0);
  2554. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2555. char *description = (char*)sqlite3_column_text(statement, 1);
  2556. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2557. int product_id = sqlite3_column_int(statement, 2);
  2558. // char *url = (char*)sqlite3_column_text(statement, 3);
  2559. // if(url==nil)
  2560. // url="";
  2561. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2562. int wid = sqlite3_column_int(statement, 3);
  2563. int closeout = sqlite3_column_int(statement, 4);
  2564. int cid = sqlite3_column_int(statement, 5);
  2565. int wisdelete = sqlite3_column_int(statement, 6);
  2566. int more_color = sqlite3_column_int(statement, 7);
  2567. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2568. if(wid !=0 && wisdelete != 1)
  2569. [item setValue:@"true" forKey:@"wish_exists"];
  2570. else
  2571. [item setValue:@"false" forKey:@"wish_exists"];
  2572. if(closeout==0)
  2573. [item setValue:@"false" forKey:@"is_closeout"];
  2574. else
  2575. [item setValue:@"true" forKey:@"is_closeout"];
  2576. if(cid==0)
  2577. [item setValue:@"false" forKey:@"cart_exists"];
  2578. else
  2579. [item setValue:@"true" forKey:@"cart_exists"];
  2580. if (more_color == 0) {
  2581. [item setObject:@(false) forKey:@"more_color"];
  2582. } else if (more_color == 1) {
  2583. [item setObject:@(true) forKey:@"more_color"];
  2584. }
  2585. [item addEntriesFromDictionary:imgjson];
  2586. // [item setValue:nsurl forKey:@"img"];
  2587. [item setValue:nsname forKey:@"fash_name"];
  2588. [item setValue:nsdescription forKey:@"description"];
  2589. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2590. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2591. i++;
  2592. }
  2593. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2594. [ret setObject:items forKey:@"items"];
  2595. sqlite3_finalize(statement);
  2596. }
  2597. DebugLog(@"count:%d",count);
  2598. [iSalesDB close_db:db];
  2599. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2600. return ret;
  2601. }
  2602. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2603. {
  2604. return [self search:params limited:YES];
  2605. }
  2606. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2607. {
  2608. UIApplication * app = [UIApplication sharedApplication];
  2609. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2610. appDelegate.disable_trigger=true;
  2611. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2612. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2613. appDelegate.disable_trigger=false;
  2614. //
  2615. // NSString* user = [params valueForKey:@"user"];
  2616. //
  2617. // NSString* password = [params valueForKey:@"password"];
  2618. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2619. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2620. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2621. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2622. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2623. [appDelegate update_count_mark];
  2624. appDelegate.can_show_price =false;
  2625. appDelegate.can_see_price =false;
  2626. appDelegate.can_create_portfolio =false;
  2627. appDelegate.can_create_order =false;
  2628. appDelegate.can_cancel_order =false;
  2629. appDelegate.can_set_cart_price =false;
  2630. appDelegate.can_delete_order =false;
  2631. appDelegate.can_submit_order =false;
  2632. appDelegate.can_set_tearsheet_price =false;
  2633. appDelegate.can_update_contact_info = false;
  2634. appDelegate.save_order_logout = false;
  2635. appDelegate.submit_order_logout = false;
  2636. appDelegate.alert_sold_in_quantities = false;
  2637. appDelegate.ipad_perm =nil ;
  2638. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2639. appDelegate.OrderFilter= nil;
  2640. [appDelegate SetSo:nil];
  2641. [appDelegate set_main_button_panel];
  2642. // sqlite3 *db = [iSalesDB get_db];
  2643. //
  2644. //
  2645. //
  2646. //
  2647. //
  2648. // 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"]];
  2649. //
  2650. //
  2651. //
  2652. //
  2653. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2654. // sqlite3_stmt * statement;
  2655. //
  2656. //
  2657. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2658. //
  2659. //
  2660. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2661. // {
  2662. //
  2663. //
  2664. // if (sqlite3_step(statement) == SQLITE_ROW)
  2665. // {
  2666. //
  2667. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2668. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2669. //
  2670. //
  2671. //
  2672. // int can_show_price = sqlite3_column_int(statement, 0);
  2673. // int can_see_price = sqlite3_column_int(statement, 1);
  2674. //
  2675. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2676. // if(contact_id==nil)
  2677. // contact_id="";
  2678. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2679. //
  2680. // int user_type = sqlite3_column_int(statement, 3);
  2681. //
  2682. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2683. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2684. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2685. // int can_delete_order = sqlite3_column_int(statement, 7);
  2686. // int can_submit_order = sqlite3_column_int(statement, 8);
  2687. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2688. // int can_create_order = sqlite3_column_int(statement, 10);
  2689. //
  2690. //
  2691. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2692. // if(mode==nil)
  2693. // mode="";
  2694. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2695. //
  2696. //
  2697. // char *username = (char*)sqlite3_column_text(statement, 12);
  2698. // if(username==nil)
  2699. // username="";
  2700. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2701. //
  2702. //
  2703. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2704. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2705. // [header setValue:nscontact_id forKey:@"contact_id"];
  2706. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2707. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2708. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2709. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2710. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2711. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2712. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2713. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2714. //
  2715. // [header setValue:nsusername forKey:@"username"];
  2716. //
  2717. //
  2718. // [ret setObject:header forKey:@"header"];
  2719. // [ret setValue:nsmode forKey:@"mode"];
  2720. //
  2721. //
  2722. // }
  2723. //
  2724. //
  2725. //
  2726. // sqlite3_finalize(statement);
  2727. // }
  2728. //
  2729. //
  2730. //
  2731. // [iSalesDB close_db:db];
  2732. //
  2733. //
  2734. //
  2735. //
  2736. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2737. return ret;
  2738. }
  2739. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2740. {
  2741. UIApplication * app = [UIApplication sharedApplication];
  2742. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2743. appDelegate.disable_trigger=true;
  2744. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2745. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2746. appDelegate.disable_trigger=false;
  2747. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2748. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2749. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2750. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2751. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2752. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2753. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2754. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2755. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2756. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2757. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2758. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2759. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2760. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2761. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2762. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2763. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2764. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2765. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2766. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2767. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2768. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2769. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2770. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2771. [arr_name addObject:customer_first_name];
  2772. [arr_name addObject:customer_last_name];
  2773. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2774. // default ship from
  2775. 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';";
  2776. __block NSString *cid = @"";
  2777. __block NSString *name = @"";
  2778. __block NSString *ext = @"";
  2779. __block NSString *contact = @"";
  2780. __block NSString *email = @"";
  2781. __block NSString *fax = @"";
  2782. __block NSString *phone = @"";
  2783. sqlite3 *db = [iSalesDB get_db];
  2784. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2785. cid = [self textAtColumn:0 statement:statment];
  2786. name = [self textAtColumn:1 statement:statment];
  2787. ext = [self textAtColumn:2 statement:statment];
  2788. contact = [self textAtColumn:3 statement:statment];
  2789. email = [self textAtColumn:4 statement:statment];
  2790. fax = [self textAtColumn:5 statement:statment];
  2791. phone = [self textAtColumn:6 statement:statment];
  2792. }];
  2793. NSString* so_id = [self get_offline_soid:db];
  2794. if(so_id==nil)
  2795. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2796. 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];
  2797. int result =[iSalesDB execSql:sql_neworder db:db];
  2798. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2799. //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'
  2800. //soId
  2801. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2802. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2803. sqlite3_stmt * statement;
  2804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2805. {
  2806. if (sqlite3_step(statement) == SQLITE_ROW)
  2807. {
  2808. // char *name = (char*)sqlite3_column_text(statement, 1);
  2809. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2810. //ret = sqlite3_column_int(statement, 0);
  2811. char *soId = (char*)sqlite3_column_text(statement, 0);
  2812. if(soId==nil)
  2813. soId="";
  2814. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2815. [ret setValue:nssoId forKey:@"soId"];
  2816. [ret setValue:nssoId forKey:@"orderCode"];
  2817. }
  2818. sqlite3_finalize(statement);
  2819. }
  2820. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2821. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2822. [iSalesDB close_db:db];
  2823. return [RAUtils dict2data:ret];
  2824. }
  2825. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2826. {
  2827. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2828. NSString* orderCode = [params valueForKey:@"orderCode"];
  2829. UIApplication * app = [UIApplication sharedApplication];
  2830. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2831. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2832. {
  2833. appDelegate.disable_trigger=true;
  2834. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2835. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2836. appDelegate.disable_trigger=false;
  2837. }
  2838. sqlite3 *db = [iSalesDB get_db];
  2839. int cart_count=[self query_ordercartcount:orderCode db:db];
  2840. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2841. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2842. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2843. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2844. [iSalesDB close_db:db];
  2845. return [RAUtils dict2data:ret];
  2846. }
  2847. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2848. {
  2849. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2850. sqlite3 *db = [iSalesDB get_db];
  2851. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2852. int count =0;
  2853. if(params[@"count"]!=nil)
  2854. {
  2855. count = [params[@"count"] intValue];
  2856. }
  2857. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2858. NSString* product_id=params[@"product_id"];
  2859. NSString* orderCode=params[@"orderCode"];
  2860. NSString *qty = params[@"qty"];
  2861. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2862. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2863. __block int number_of_outOfStock = 0;
  2864. for(int i=0;i<arr_id.count;i++)
  2865. {
  2866. NSInteger item_qty= count;
  2867. if (qty) {
  2868. item_qty = [qty_arr[i] integerValue];
  2869. }
  2870. if(item_qty==0)
  2871. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2872. // 检查新加Model数量是否大于库存
  2873. if (!appDelegate.can_create_backorder) {
  2874. __block BOOL needContinue = NO;
  2875. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2876. int availability = sqlite3_column_int(stmt, 0);
  2877. // 库存小于购买量为缺货
  2878. if (availability < item_qty || availability <= 0) {
  2879. number_of_outOfStock++;
  2880. needContinue = YES;
  2881. }
  2882. }];
  2883. if (needContinue) {
  2884. continue;
  2885. }
  2886. }
  2887. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2888. NSString* sql=nil;
  2889. sqlite3_stmt *stmt = nil;
  2890. BOOL shouldStep = NO;
  2891. if(_id<0)
  2892. {
  2893. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2894. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2895. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2896. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2897. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2898. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2899. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2900. shouldStep = YES;
  2901. }
  2902. else
  2903. {
  2904. if (qty) { // wish list move to cart
  2905. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2906. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2907. sqlite3_bind_int(stmt, 1, _id);
  2908. shouldStep = YES;
  2909. } else {
  2910. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  2911. __block BOOL update = YES;
  2912. if (!appDelegate.can_create_backorder) {
  2913. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2914. int newQTY = sqlite3_column_int(stmt, 0);
  2915. int availability = sqlite3_column_int(stmt, 1);
  2916. if (newQTY > availability) { // 库存不够
  2917. update = NO;
  2918. number_of_outOfStock++;
  2919. }
  2920. }];
  2921. }
  2922. if (update) {
  2923. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2924. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2925. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2926. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2927. shouldStep = YES;
  2928. }
  2929. }
  2930. }
  2931. if (shouldStep) {
  2932. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2933. [iSalesDB execSql:@"ROLLBACK" db:db];
  2934. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2935. [iSalesDB close_db:db];
  2936. DebugLog(@"add to cart error");
  2937. return [RAUtils dict2data:ret];
  2938. }
  2939. }
  2940. }
  2941. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2942. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2943. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2944. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2945. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2946. if (!appDelegate.can_create_backorder) {
  2947. if (number_of_outOfStock > 0) {
  2948. ret[@"result"]=[NSNumber numberWithInt:8];
  2949. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2950. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2951. }
  2952. }
  2953. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2954. [iSalesDB close_db:db];
  2955. return [RAUtils dict2data:ret];
  2956. }
  2957. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2958. {
  2959. // UIApplication * app = [UIApplication sharedApplication];
  2960. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2961. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2962. 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];
  2963. sqlite3_stmt * statement;
  2964. int count=0;
  2965. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2966. {
  2967. while (sqlite3_step(statement) == SQLITE_ROW)
  2968. {
  2969. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2970. int bitem_id = sqlite3_column_int(statement, 0);
  2971. int bitem_qty = sqlite3_column_int(statement, 1);
  2972. char *name = (char*)sqlite3_column_text(statement, 2);
  2973. if(name==nil)
  2974. name="";
  2975. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2976. char *description = (char*)sqlite3_column_text(statement, 3);
  2977. if(description==nil)
  2978. description="";
  2979. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2980. double unit_price = sqlite3_column_double(statement, 4);
  2981. int use_unitprice = sqlite3_column_int(statement, 5);
  2982. if(use_unitprice!=1)
  2983. {
  2984. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2985. }
  2986. itemjson[@"model"]=nsname;
  2987. itemjson[@"description"]=nsdescription;
  2988. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2989. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2990. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2991. if(compute)
  2992. {
  2993. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2994. }
  2995. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2996. count++;
  2997. }
  2998. sqlite3_finalize(statement);
  2999. }
  3000. ret[@"count"]=@(count);
  3001. if(count==0)
  3002. return nil;
  3003. else
  3004. return ret;
  3005. }
  3006. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3007. {
  3008. //compute: add part to subtotal;
  3009. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3010. dict_item[@(item_id)]=@"1";
  3011. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3012. double cuft=0;
  3013. double weight=0;
  3014. int carton=0;
  3015. 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];
  3016. sqlite3_stmt * statement;
  3017. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3018. {
  3019. if (sqlite3_step(statement) == SQLITE_ROW)
  3020. {
  3021. double ulength = sqlite3_column_double(statement, 0);
  3022. double uwidth = sqlite3_column_double(statement, 1);
  3023. double uheight = sqlite3_column_double(statement, 2);
  3024. double uweight = sqlite3_column_double(statement, 3);
  3025. double mlength = sqlite3_column_double(statement, 4);
  3026. double mwidth = sqlite3_column_double(statement, 5);
  3027. double mheight = sqlite3_column_double(statement, 6);
  3028. double mweight = sqlite3_column_double(statement, 7);
  3029. double ilength = sqlite3_column_double(statement, 8);
  3030. double iwidth = sqlite3_column_double(statement, 9);
  3031. double iheight = sqlite3_column_double(statement, 10);
  3032. double iweight = sqlite3_column_double(statement, 11);
  3033. int pcs = sqlite3_column_int(statement,12);
  3034. int mpack = sqlite3_column_int(statement, 13);
  3035. int ipack = sqlite3_column_int(statement, 14);
  3036. double ucbf = sqlite3_column_double(statement, 15);
  3037. double icbf = sqlite3_column_double(statement, 16);
  3038. double mcbf = sqlite3_column_double(statement, 17);
  3039. if(ipack==0)
  3040. {
  3041. carton= count/mpack ;
  3042. weight = mweight*carton;
  3043. cuft= carton*(mlength*mwidth*mheight);
  3044. int remain=count%mpack;
  3045. if(remain==0)
  3046. {
  3047. //do nothing;
  3048. }
  3049. else
  3050. {
  3051. carton++;
  3052. weight += uweight*remain;
  3053. cuft += (ulength*uwidth*uheight)*remain;
  3054. }
  3055. }
  3056. else
  3057. {
  3058. carton = count/(mpack*ipack);
  3059. weight = mweight*carton;
  3060. cuft= carton*(mlength*mwidth*mheight);
  3061. int remain=count%(mpack*ipack);
  3062. if(remain==0)
  3063. {
  3064. // do nothing;
  3065. }
  3066. else
  3067. {
  3068. carton++;
  3069. int icarton =remain/ipack;
  3070. int iremain=remain%ipack;
  3071. weight += iweight*icarton;
  3072. cuft += (ilength*iwidth*iheight)*icarton;
  3073. if(iremain==0)
  3074. {
  3075. //do nothing;
  3076. }
  3077. else
  3078. {
  3079. weight += uweight*iremain;
  3080. cuft += (ulength*uwidth*uheight)*iremain;
  3081. }
  3082. }
  3083. }
  3084. #ifdef BUILD_NPD
  3085. cuft=ucbf*count;
  3086. weight= uweight*count;
  3087. #endif
  3088. #ifdef BUILD_HOMER
  3089. cuft=ucbf*count;
  3090. weight= uweight*count;
  3091. #endif
  3092. #ifdef BUILD_GATIT
  3093. cuft=ucbf*count;
  3094. weight= uweight*count;
  3095. #endif
  3096. }
  3097. sqlite3_finalize(statement);
  3098. }
  3099. if(compute)
  3100. {
  3101. NSArray * arr_count=nil;
  3102. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3103. for(int i=0;i<arr_bundle.count;i++)
  3104. {
  3105. dict_item[arr_bundle[i]]=@"1";
  3106. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3107. cuft+=[bundlejson[@"cuft"] doubleValue];
  3108. weight+=[bundlejson[@"weight"] doubleValue];
  3109. carton+=[bundlejson[@"carton"] intValue];
  3110. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3111. }
  3112. }
  3113. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3114. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3115. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3116. ret[@"items"]=dict_item;
  3117. return ret;
  3118. }
  3119. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3120. {
  3121. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3122. // 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 ];
  3123. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3124. DebugLog(@"offline_login sql:%@",sqlQuery);
  3125. sqlite3_stmt * statement;
  3126. int cart_count=0;
  3127. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3128. if ( dbresult== SQLITE_OK)
  3129. {
  3130. while (sqlite3_step(statement) == SQLITE_ROW)
  3131. {
  3132. int item_id = sqlite3_column_int(statement, 0);
  3133. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3134. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3135. }
  3136. sqlite3_finalize(statement);
  3137. }
  3138. return cart_count;
  3139. }
  3140. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3141. {
  3142. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3143. sqlite3 *db = [iSalesDB get_db];
  3144. UIApplication * app = [UIApplication sharedApplication];
  3145. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3146. NSString* orderCode=params[@"orderCode"];
  3147. int sort = [[params objectForKey:@"sort"] intValue];
  3148. NSString *sort_str = @"";
  3149. switch (sort) {
  3150. case 0:{
  3151. sort_str = @"order by c.modify_time desc";
  3152. }
  3153. break;
  3154. case 1:{
  3155. sort_str = @"order by c.modify_time asc";
  3156. }
  3157. break;
  3158. case 2:{
  3159. sort_str = @"order by m.name asc";
  3160. }
  3161. break;
  3162. case 3:{
  3163. sort_str = @"order by m.name desc";
  3164. }
  3165. break;
  3166. case 4:{
  3167. sort_str = @"order by m.description asc";
  3168. }
  3169. break;
  3170. default:
  3171. break;
  3172. }
  3173. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3174. // 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 ];
  3175. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3176. DebugLog(@"offline_login sql:%@",sqlQuery);
  3177. sqlite3_stmt * statement;
  3178. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3179. NSDate *date1 = [NSDate date];
  3180. int count=0;
  3181. int cart_count=0;
  3182. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3183. if ( dbresult== SQLITE_OK)
  3184. {
  3185. while (sqlite3_step(statement) == SQLITE_ROW)
  3186. {
  3187. // NSDate *row_date = [NSDate date];
  3188. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3189. int product_id = sqlite3_column_int(statement, 0);
  3190. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3191. int item_id = sqlite3_column_int(statement, 7);
  3192. NSString* Price=nil;
  3193. if(str_price==nil)
  3194. {
  3195. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3196. // NSDate *price_date = [NSDate date];
  3197. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3198. // DebugLog(@"price time interval");
  3199. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3200. if(price==nil)
  3201. Price=@"No Price.";
  3202. else
  3203. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3204. }
  3205. else
  3206. {
  3207. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3208. }
  3209. double discount = sqlite3_column_double(statement, 2);
  3210. int item_count = sqlite3_column_int(statement, 3);
  3211. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3212. NSString *nsline_note=nil;
  3213. if(line_note!=nil)
  3214. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3215. char *name = (char*)sqlite3_column_text(statement, 5);
  3216. NSString *nsname=nil;
  3217. if(name!=nil)
  3218. nsname= [[NSString alloc]initWithUTF8String:name];
  3219. char *description = (char*)sqlite3_column_text(statement, 6);
  3220. NSString *nsdescription=nil;
  3221. if(description!=nil)
  3222. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3223. int stockUom = sqlite3_column_int(statement, 8);
  3224. int _id = sqlite3_column_int(statement, 9);
  3225. int availability = sqlite3_column_int(statement, 10);
  3226. // NSDate *subtotal_date = [NSDate date];
  3227. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3228. // DebugLog(@"subtotal_date time interval");
  3229. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3230. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3231. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3232. int carton=[bsubtotaljson[@"carton"] intValue];
  3233. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3234. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3235. // NSDate *img_date = [NSDate date];
  3236. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3237. // DebugLog(@"img_date time interval");
  3238. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3239. itemjson[@"model"]=nsname;
  3240. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3241. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3242. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3243. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3244. itemjson[@"check"]=@"true";
  3245. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3246. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3247. itemjson[@"unit_price"]=Price;
  3248. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3249. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3250. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3251. itemjson[@"note"]=nsline_note;
  3252. if (!appDelegate.can_create_backorder) {
  3253. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3254. }
  3255. // NSDate *date2 = [NSDate date];
  3256. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3257. // DebugLog(@"model_bundle time interval");
  3258. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3259. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3260. count++;
  3261. // DebugLog(@"row time interval");
  3262. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3263. }
  3264. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3265. sqlite3_finalize(statement);
  3266. }
  3267. DebugLog(@"request cart total time interval");
  3268. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3269. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3270. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3271. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3272. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3273. ret[@"count"]=[NSNumber numberWithInt:count ];
  3274. ret[@"mode"]=@"Regular Mode";
  3275. [iSalesDB close_db:db];
  3276. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3277. DebugLog(@"general notes :%@",general_note);
  3278. ret[@"general_note"]= general_note;
  3279. return [RAUtils dict2data:ret];
  3280. }
  3281. +(NSData*) offline_login :(NSMutableDictionary *) params
  3282. {
  3283. NSString* user = [params valueForKey:@"user"];
  3284. NSString* password = [params valueForKey:@"password"];
  3285. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3286. sqlite3 *db = [iSalesDB get_db];
  3287. 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"]];
  3288. DebugLog(@"offline_login sql:%@",sqlQuery);
  3289. sqlite3_stmt * statement;
  3290. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3291. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3292. {
  3293. if (sqlite3_step(statement) == SQLITE_ROW)
  3294. {
  3295. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3296. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3297. int can_show_price = sqlite3_column_int(statement, 0);
  3298. int can_see_price = sqlite3_column_int(statement, 1);
  3299. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3300. if(contact_id==nil)
  3301. contact_id="";
  3302. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3303. int user_type = sqlite3_column_int(statement, 3);
  3304. int can_cancel_order = sqlite3_column_int(statement, 4);
  3305. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3306. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3307. int can_delete_order = sqlite3_column_int(statement, 7);
  3308. int can_submit_order = sqlite3_column_int(statement, 8);
  3309. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3310. int can_create_order = sqlite3_column_int(statement, 10);
  3311. char *mode = (char*)sqlite3_column_text(statement, 11);
  3312. if(mode==nil)
  3313. mode="";
  3314. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3315. char *username = (char*)sqlite3_column_text(statement, 12);
  3316. if(username==nil)
  3317. username="";
  3318. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3319. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3320. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3321. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3322. [header setValue:nscontact_id forKey:@"contact_id"];
  3323. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3324. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3325. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3326. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3327. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3328. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3329. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3330. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3331. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3332. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3333. [header setValue:nsusername forKey:@"username"];
  3334. NSError* error=nil;
  3335. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3336. [header setValue:statusFilter forKey:@"statusFilter"];
  3337. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3338. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3339. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3340. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3341. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3342. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3343. [ret setObject:header forKey:@"header"];
  3344. [ret setValue:nsmode forKey:@"mode"];
  3345. }
  3346. sqlite3_finalize(statement);
  3347. }
  3348. [iSalesDB close_db:db];
  3349. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3350. return [RAUtils dict2data:ret];
  3351. }
  3352. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3353. {
  3354. NSString* contactId = [params valueForKey:@"contactId"];
  3355. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3356. sqlite3 *db = [iSalesDB get_db];
  3357. NSString *sqlQuery = nil;
  3358. {
  3359. 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];
  3360. }
  3361. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3362. sqlite3_stmt * statement;
  3363. [ret setValue:@"2" forKey:@"result"];
  3364. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3365. {
  3366. //int i = 0;
  3367. if (sqlite3_step(statement) == SQLITE_ROW)
  3368. {
  3369. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3370. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3371. // int editable = sqlite3_column_int(statement, 0);
  3372. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3373. NSString *nscompany_name =nil;
  3374. if(company_name==nil)
  3375. nscompany_name=@"";
  3376. else
  3377. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3378. char *country = (char*)sqlite3_column_text(statement, 2);
  3379. if(country==nil)
  3380. country="";
  3381. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3382. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3383. // if(addr==nil)
  3384. // addr="";
  3385. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3386. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3387. if(zipcode==nil)
  3388. zipcode="";
  3389. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3390. char *state = (char*)sqlite3_column_text(statement, 5);
  3391. if(state==nil)
  3392. state="";
  3393. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3394. char *city = (char*)sqlite3_column_text(statement, 6);
  3395. if(city==nil)
  3396. city="";
  3397. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3398. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3399. // NSString *nscontact_name = nil;
  3400. // if(contact_name==nil)
  3401. // nscontact_name=@"";
  3402. // else
  3403. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3404. char *phone = (char*)sqlite3_column_text(statement, 8);
  3405. NSString *nsphone = nil;
  3406. if(phone==nil)
  3407. nsphone=@"";
  3408. else
  3409. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3410. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3411. if(contact_id==nil)
  3412. contact_id="";
  3413. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3414. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3415. if(addr_1==nil)
  3416. addr_1="";
  3417. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3418. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3419. if(addr_2==nil)
  3420. addr_2="";
  3421. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3422. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3423. if(addr_3==nil)
  3424. addr_3="";
  3425. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3426. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3427. if(addr_4==nil)
  3428. addr_4="";
  3429. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3430. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3431. if(first_name==nil)
  3432. first_name="";
  3433. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3434. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3435. if(last_name==nil)
  3436. last_name="";
  3437. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3438. char *fax = (char*)sqlite3_column_text(statement, 16);
  3439. NSString *nsfax = nil;
  3440. if(fax==nil)
  3441. nsfax=@"";
  3442. else
  3443. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3444. char *email = (char*)sqlite3_column_text(statement, 17);
  3445. NSString *nsemail = nil;
  3446. if(email==nil)
  3447. nsemail=@"";
  3448. else
  3449. nsemail= [[NSString alloc]initWithUTF8String:email];
  3450. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3451. NSString *nsimg_0 = nil;
  3452. if(img_0==nil)
  3453. nsimg_0=@"";
  3454. else
  3455. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3456. [self copy_bcardImg:nsimg_0];
  3457. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3458. NSString *nsimg_1 = nil;
  3459. if(img_1==nil)
  3460. nsimg_1=@"";
  3461. else
  3462. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3463. [self copy_bcardImg:nsimg_1];
  3464. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3465. NSString *nsimg_2 = nil;
  3466. if(img_2==nil)
  3467. nsimg_2=@"";
  3468. else
  3469. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3470. [self copy_bcardImg:nsimg_2];
  3471. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3472. NSString *nsprice_type = nil;
  3473. if(price_type==nil)
  3474. nsprice_type=@"";
  3475. else
  3476. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3477. char *notes = (char*)sqlite3_column_text(statement, 22);
  3478. NSString *nsnotes = nil;
  3479. if(notes==nil)
  3480. nsnotes=@"";
  3481. else
  3482. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3483. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3484. NSString *nssalesrep = nil;
  3485. if(salesrep==nil)
  3486. nssalesrep=@"";
  3487. else
  3488. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3489. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3490. {
  3491. // decrypt
  3492. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3493. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3494. nsphone=[AESCrypt fastdecrypt:nsphone];
  3495. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3496. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3497. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3498. }
  3499. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3500. [arr_name addObject:nsfirst_name];
  3501. [arr_name addObject:nslast_name];
  3502. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3503. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3504. [arr_ext addObject:nsaddr_1];
  3505. [arr_ext addObject:nsaddr_2];
  3506. [arr_ext addObject:nsaddr_3];
  3507. [arr_ext addObject:nsaddr_4];
  3508. [arr_ext addObject:@"\r\n"];
  3509. [arr_ext addObject:nscity];
  3510. [arr_ext addObject:nsstate];
  3511. [arr_ext addObject:nszipcode];
  3512. [arr_ext addObject:nscountry];
  3513. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3514. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3515. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3516. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3517. [item setValue:nscountry forKey:@"customer_country"];
  3518. [item setValue:nsphone forKey:@"customer_phone"];
  3519. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3520. [item setValue:nscompany_name forKey:@"customer_name"];
  3521. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3522. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3523. [item setValue:nsext forKey:@"customer_contact_ext"];
  3524. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3525. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3526. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3527. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3528. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3529. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3530. [item setValue:nslast_name forKey:@"customer_last_name"];
  3531. [item setValue:nscity forKey:@"customer_city"];
  3532. [item setValue:nsstate forKey:@"customer_state"];
  3533. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3534. [item setValue:contactId forKey:@"customer_cid"];
  3535. [item setValue:nscontact_name forKey:@"customer_contact"];
  3536. [item setValue:nsfax forKey:@"customer_fax"];
  3537. [item setValue:nsemail forKey:@"customer_email"];
  3538. [item setValue:contact_type forKey:@"customer_contact_type"];
  3539. [ret setObject:item forKey:@"customerInfo"];
  3540. // i++;
  3541. }
  3542. UIApplication * app = [UIApplication sharedApplication];
  3543. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3544. [ret setValue:appDelegate.mode forKey:@"mode"];
  3545. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3546. sqlite3_finalize(statement);
  3547. }
  3548. [iSalesDB close_db:db];
  3549. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3550. return ret;
  3551. }
  3552. + (bool) copy_bcardImg:(NSString*) filename
  3553. {
  3554. if(filename.length==0)
  3555. return false;
  3556. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3557. bool ret=false;
  3558. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3559. NSString *cachefolder = [paths objectAtIndex:0];
  3560. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3561. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3562. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3563. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3564. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3565. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3566. //
  3567. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3568. NSFileManager* fileManager = [NSFileManager defaultManager];
  3569. BOOL bdir=NO;
  3570. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3571. {
  3572. NSError *error = nil;
  3573. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3574. {
  3575. ret=false;
  3576. }
  3577. else
  3578. {
  3579. ret=true;
  3580. }
  3581. // NSError *error = nil;
  3582. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3583. //
  3584. // if(!bsuccess)
  3585. // {
  3586. // DebugLog(@"Create offline_createimg folder failed");
  3587. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3588. // return [RAUtils dict2data:ret];
  3589. // }
  3590. // if(bsuccess)
  3591. // {
  3592. // sqlite3 *db = [self get_db];
  3593. //
  3594. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3595. // [iSalesDB close_db:db];
  3596. // }
  3597. }
  3598. return ret;
  3599. //
  3600. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3601. // if(bsuccess)
  3602. // {
  3603. // NSError *error = nil;
  3604. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3605. // {
  3606. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3607. // }
  3608. // else
  3609. // {
  3610. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3611. //
  3612. // ret[@"img_url_aname"]=filename;
  3613. // ret[@"img_url"]=savedImagePath;
  3614. // }
  3615. // }
  3616. }
  3617. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3618. {
  3619. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3620. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3621. NSString *cachefolder = [paths objectAtIndex:0];
  3622. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3623. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3624. NSFileManager* fileManager = [NSFileManager defaultManager];
  3625. BOOL bdir=YES;
  3626. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3627. {
  3628. NSError *error = nil;
  3629. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3630. if(!bsuccess)
  3631. {
  3632. DebugLog(@"Create offline_createimg folder failed");
  3633. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3634. return [RAUtils dict2data:ret];
  3635. }
  3636. // if(bsuccess)
  3637. // {
  3638. // sqlite3 *db = [self get_db];
  3639. //
  3640. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3641. // [iSalesDB close_db:db];
  3642. // }
  3643. }
  3644. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3645. //JEPG格式
  3646. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3647. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3648. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3649. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3650. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3651. if(bsuccess)
  3652. {
  3653. NSError *error = nil;
  3654. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3655. {
  3656. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3657. }
  3658. else
  3659. {
  3660. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3661. ret[@"img_url_aname"]=filename;
  3662. ret[@"img_url"]=filename;
  3663. }
  3664. }
  3665. else
  3666. {
  3667. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3668. }
  3669. return [RAUtils dict2data:ret];
  3670. }
  3671. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3672. {
  3673. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3674. category = [category substringToIndex:3];
  3675. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3676. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3677. params[@"category"]= category;
  3678. ret[@"params"]= params;
  3679. [ret setValue:@"detail" forKey:@"target"];
  3680. [ret setValue:@"popup" forKey:@"action"];
  3681. [ret setValue:@"content" forKey:@"type"];
  3682. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3683. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3684. [ret setValue:@"true" forKey:@"single_row"];
  3685. [ret setValue:@"true" forKey:@"partial_refresh"];
  3686. // sqlite3 *db = [iSalesDB get_db];
  3687. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3688. 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 ;
  3689. sqlite3_stmt * statement;
  3690. int count = 0;
  3691. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3692. // int count=0;
  3693. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3694. {
  3695. int i=0;
  3696. while (sqlite3_step(statement) == SQLITE_ROW)
  3697. {
  3698. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3699. // char *name = (char*)sqlite3_column_text(statement, 1);
  3700. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3701. char *name = (char*)sqlite3_column_text(statement, 0);
  3702. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3703. int product_id = sqlite3_column_int(statement, 1);
  3704. char *url = (char*)sqlite3_column_text(statement, 2);
  3705. if(url==nil)
  3706. url="";
  3707. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3708. [item setValue:nsurl forKey:@"picture_path"];
  3709. [item setValue:nsname forKey:@"fash_name"];
  3710. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3711. [item setValue:category forKey:@"category"];
  3712. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3713. i++;
  3714. }
  3715. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3716. sqlite3_finalize(statement);
  3717. }
  3718. DebugLog(@"count:%d",count);
  3719. // [iSalesDB close_db:db];
  3720. return ret;
  3721. }
  3722. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3723. {
  3724. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3725. [ret setValue:key forKey:@"key"];
  3726. [ret setValue:value forKey:@"val"];
  3727. [ret setValue:@"price" forKey:@"type"];
  3728. return ret;
  3729. }
  3730. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3731. {
  3732. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3733. [ret setValue:key forKey:@"key"];
  3734. [ret setValue:value forKey:@"val"];
  3735. return ret;
  3736. }
  3737. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3738. {
  3739. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3740. [ret setValue:@"0" forKey:@"img_count"];
  3741. // sqlite3 *db = [iSalesDB get_db];
  3742. 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 ;
  3743. sqlite3_stmt * statement;
  3744. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3745. {
  3746. int i=0;
  3747. if (sqlite3_step(statement) == SQLITE_ROW)
  3748. {
  3749. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3750. // char *name = (char*)sqlite3_column_text(statement, 1);
  3751. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3752. char *value = (char*)sqlite3_column_text(statement, 0);
  3753. if(value==nil)
  3754. value="";
  3755. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3756. char *key = (char*)sqlite3_column_text(statement, 1);
  3757. if(key==nil)
  3758. key="";
  3759. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3760. [item setValue:nsvalue forKey:@"val"];
  3761. [item setValue:nskey forKey:@"key"];
  3762. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3763. [ret setValue:@"1" forKey:@"count"];
  3764. i++;
  3765. }
  3766. sqlite3_finalize(statement);
  3767. }
  3768. // [iSalesDB close_db:db];
  3769. return ret;
  3770. }
  3771. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3772. {
  3773. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3774. [ret setValue:@"0" forKey:@"count"];
  3775. // sqlite3 *db = [iSalesDB get_db];
  3776. 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;
  3777. sqlite3_stmt * statement;
  3778. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3779. {
  3780. int i=0;
  3781. while (sqlite3_step(statement) == SQLITE_ROW)
  3782. {
  3783. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3784. // char *name = (char*)sqlite3_column_text(statement, 1);
  3785. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3786. char *value = (char*)sqlite3_column_text(statement, 0);
  3787. if(value==nil)
  3788. value="";
  3789. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3790. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3791. if(selector_display==nil)
  3792. selector_display="";
  3793. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3794. int product_id = sqlite3_column_int(statement, 2);
  3795. char *category = (char*)sqlite3_column_text(statement, 3);
  3796. if(category==nil)
  3797. category="";
  3798. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3799. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3800. [item setValue:nsvalue forKey:@"title"];
  3801. [item setValue:url forKey:@"pic_url"];
  3802. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3803. [params setValue:@"2" forKey:@"count"];
  3804. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3805. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3806. [param0 setValue:@"product_id" forKey:@"name"];
  3807. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3808. [param1 setValue:nscategory forKey:@"val"];
  3809. [param1 setValue:@"category" forKey:@"name"];
  3810. [params setObject:param0 forKey:@"param_0"];
  3811. [params setObject:param1 forKey:@"param_1"];
  3812. [item setObject:params forKey:@"params"];
  3813. [ret setValue:nsselector_display forKey:@"name"];
  3814. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3815. i++;
  3816. }
  3817. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3818. [ret setValue:@"switch" forKey:@"action"];
  3819. sqlite3_finalize(statement);
  3820. }
  3821. // [iSalesDB close_db:db];
  3822. return ret;
  3823. }
  3824. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3825. {
  3826. // model 在 category search 显示的图片。
  3827. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3828. // sqlite3 *db = [iSalesDB get_db];
  3829. 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];
  3830. sqlite3_stmt * statement;
  3831. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3832. {
  3833. while (sqlite3_step(statement) == SQLITE_ROW)
  3834. {
  3835. char *url = (char*)sqlite3_column_text(statement, 0);
  3836. if(url==nil)
  3837. url="";
  3838. int type = sqlite3_column_int(statement, 1);
  3839. if(type==0)
  3840. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3841. else
  3842. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3843. }
  3844. sqlite3_finalize(statement);
  3845. }
  3846. // [iSalesDB close_db:db];
  3847. return ret;
  3848. }
  3849. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3850. {
  3851. int item_id=-1;
  3852. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3853. sqlite3_stmt * statement;
  3854. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3855. {
  3856. if (sqlite3_step(statement) == SQLITE_ROW)
  3857. {
  3858. item_id = sqlite3_column_int(statement, 0);
  3859. }
  3860. sqlite3_finalize(statement);
  3861. }
  3862. return item_id;
  3863. }
  3864. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3865. {
  3866. // NSString* ret = @"";
  3867. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3868. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3869. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3870. sqlite3_stmt * statement;
  3871. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3872. {
  3873. while (sqlite3_step(statement) == SQLITE_ROW)
  3874. {
  3875. int bitem_id = sqlite3_column_int(statement, 0);
  3876. int bitem_qty = sqlite3_column_int(statement, 1);
  3877. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3878. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3879. }
  3880. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3881. sqlite3_finalize(statement);
  3882. }
  3883. // if(ret==nil)
  3884. // ret=@"";
  3885. *count=arr_count;
  3886. return arr_bundle;
  3887. }
  3888. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3889. {
  3890. // get default sold qty, return -1 if model not found;
  3891. int ret = -1;
  3892. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3893. sqlite3_stmt * statement;
  3894. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3895. {
  3896. if (sqlite3_step(statement) == SQLITE_ROW)
  3897. {
  3898. ret = sqlite3_column_int(statement, 0);
  3899. }
  3900. sqlite3_finalize(statement);
  3901. }
  3902. return ret;
  3903. }
  3904. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3905. {
  3906. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3907. [ret setValue:@"0" forKey:@"img_count"];
  3908. // sqlite3 *db = [iSalesDB get_db];
  3909. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3910. sqlite3_stmt * statement;
  3911. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3912. {
  3913. int i=0;
  3914. while (sqlite3_step(statement) == SQLITE_ROW)
  3915. {
  3916. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3917. // char *name = (char*)sqlite3_column_text(statement, 1);
  3918. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3919. char *url = (char*)sqlite3_column_text(statement, 0);
  3920. if(url==nil)
  3921. url="";
  3922. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3923. [item setValue:nsurl forKey:@"s"];
  3924. [item setValue:nsurl forKey:@"l"];
  3925. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3926. i++;
  3927. }
  3928. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3929. sqlite3_finalize(statement);
  3930. }
  3931. // [iSalesDB close_db:db];
  3932. return ret;
  3933. }
  3934. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3935. UIApplication * app = [UIApplication sharedApplication];
  3936. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3937. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3938. sqlite3 *db = [iSalesDB get_db];
  3939. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3940. NSString* product_id=params[@"product_id"];
  3941. NSString *item_count_str = params[@"item_count"];
  3942. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3943. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3944. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3945. // NSString *sql = @"";
  3946. for(int i=0;i<arr.count;i++)
  3947. {
  3948. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3949. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3950. __block int cart_count = 0;
  3951. if (!item_count_str) {
  3952. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3953. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3954. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3955. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3956. cart_count = [[model_set_components lastObject] integerValue];
  3957. }];
  3958. }
  3959. if(count==0)
  3960. {
  3961. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3962. sqlite3_stmt *stmt;
  3963. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3964. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3965. if (item_count_arr) {
  3966. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3967. } else {
  3968. sqlite3_bind_int(stmt,2,cart_count);
  3969. }
  3970. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3971. [iSalesDB execSql:@"ROLLBACK" db:db];
  3972. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3973. [iSalesDB close_db:db];
  3974. DebugLog(@"add to wishlist error");
  3975. return ret;
  3976. }
  3977. } else {
  3978. int qty = 0;
  3979. if (item_count_arr) {
  3980. qty = [item_count_arr[i] integerValue];
  3981. } else {
  3982. qty = cart_count;
  3983. }
  3984. 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]];
  3985. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3986. [iSalesDB execSql:@"ROLLBACK" db:db];
  3987. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3988. [iSalesDB close_db:db];
  3989. DebugLog(@"add to wishlist error");
  3990. return ret;
  3991. }
  3992. }
  3993. }
  3994. // [iSalesDB execSql:sql db:db];
  3995. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3996. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3997. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3998. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3999. [iSalesDB close_db:db];
  4000. appDelegate.wish_count =count;
  4001. [appDelegate update_count_mark];
  4002. ret[@"result"]= [NSNumber numberWithInt:2];
  4003. return ret;
  4004. }
  4005. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4006. // 0 category
  4007. // 1 search
  4008. // 2 itemsearch
  4009. NSData *ret = nil;
  4010. NSDictionary *items = nil;
  4011. switch (from) {
  4012. case 0:{
  4013. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4014. }
  4015. break;
  4016. case 1:{
  4017. items = [[self search:params limited:NO] objectForKey:@"items"];
  4018. }
  4019. break;
  4020. case 2:{
  4021. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4022. }
  4023. break;
  4024. default:
  4025. break;
  4026. }
  4027. if (!items) {
  4028. return ret;
  4029. }
  4030. int count = [[items objectForKey:@"count"] intValue];
  4031. NSMutableString *product_id_str = [@"" mutableCopy];
  4032. for (int i = 0; i < count; i++) {
  4033. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4034. NSDictionary *item = [items objectForKey:key];
  4035. NSString *product_id = [item objectForKey:@"product_id"];
  4036. if (i == 0) {
  4037. [product_id_str appendString:product_id];
  4038. } else {
  4039. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4040. }
  4041. }
  4042. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4043. if ([add_to isEqualToString:@"cart"]) {
  4044. NSString *order_code = [params objectForKey:@"orderCode"];
  4045. if (order_code.length) {
  4046. NSDictionary *newParams = @{
  4047. @"product_id" : product_id_str,
  4048. @"orderCode" : order_code
  4049. };
  4050. ret = [self offline_add2cart:newParams.mutableCopy];
  4051. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4052. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4053. ret = [RAUtils dict2data:retDic];
  4054. }
  4055. } else if([add_to isEqualToString:@"wishlist"]) {
  4056. NSDictionary *newParams = @{
  4057. @"product_id" : product_id_str
  4058. };
  4059. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4060. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4061. ret = [RAUtils dict2data:retDic];
  4062. } else if([add_to isEqualToString:@"portfolio"]) {
  4063. NSDictionary *newParams = @{
  4064. @"product_id" : product_id_str
  4065. };
  4066. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4067. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4068. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4069. ret = [RAUtils dict2data:retDic];
  4070. }
  4071. return ret;
  4072. }
  4073. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4074. {
  4075. return [self addAll:params from:0];
  4076. }
  4077. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4078. {
  4079. return [self addAll:params from:1];
  4080. }
  4081. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4082. {
  4083. return [self addAll:params from:2];
  4084. }
  4085. #pragma mark - Jack
  4086. #warning 做SQL操作时转义!!
  4087. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4088. // "val_227" : {
  4089. // "check" : 1,
  4090. // "value" : "US United States",
  4091. // "value_id" : "228"
  4092. // },
  4093. if (!countryCode) {
  4094. countryCode = @"US";
  4095. }
  4096. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4097. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4098. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4099. int code_id = sqlite3_column_int(stmt, 3); // id
  4100. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4101. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4102. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4103. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4104. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4105. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4106. }
  4107. long n = *count;
  4108. *count = n + 1;
  4109. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4110. [container setValue:countryDic forKey:key];
  4111. }] mutableCopy];
  4112. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4113. return ret;
  4114. }
  4115. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4116. countryCode = [self translateSingleQuote:countryCode];
  4117. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4118. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4119. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4120. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4121. if (name == NULL) {
  4122. name = "";
  4123. }
  4124. if (code == NULL) {
  4125. code = "";
  4126. }
  4127. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4128. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4129. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4130. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4131. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4132. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4133. }
  4134. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4135. [container setValue:stateDic forKey:key];
  4136. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4137. DebugLog(@"query all state error: %@",err_msg);
  4138. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4139. // [stateDic setValue:@"Other" forKey:@"value"];
  4140. // [stateDic setValue:@"" forKey:@"value_id"];
  4141. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4142. //
  4143. // if (state_code && [@"" isEqualToString:state_code]) {
  4144. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4145. // }
  4146. //
  4147. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4148. // [container setValue:stateDic forKey:key];
  4149. }] mutableCopy];
  4150. [ret removeObjectForKey:@"result"];
  4151. // failure 可以不用了,一样的
  4152. if (ret.allKeys.count == 0) {
  4153. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4154. [stateDic setValue:@"Other" forKey:@"value"];
  4155. [stateDic setValue:@"" forKey:@"value_id"];
  4156. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4157. if (state_code && [@"" isEqualToString:state_code]) {
  4158. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4159. }
  4160. NSString *key = [NSString stringWithFormat:@"val_0"];
  4161. [ret setValue:stateDic forKey:key];
  4162. }
  4163. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4164. return ret;
  4165. }
  4166. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4167. codeId = [self translateSingleQuote:codeId];
  4168. 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];
  4169. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4170. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4171. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4172. if (name == NULL) {
  4173. name = "";
  4174. }
  4175. if (code == NULL) {
  4176. code = "";
  4177. }
  4178. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4179. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4180. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4181. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4182. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4183. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4184. }
  4185. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4186. [container setValue:stateDic forKey:key];
  4187. }] mutableCopy];
  4188. [ret removeObjectForKey:@"result"];
  4189. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4190. return ret;
  4191. }
  4192. + (NSDictionary *)offline_getPrice {
  4193. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4194. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4195. char *name = (char *) sqlite3_column_text(stmt, 1);
  4196. int type = sqlite3_column_int(stmt, 2);
  4197. int orderBy = sqlite3_column_int(stmt, 3);
  4198. if (name == NULL) {
  4199. name = "";
  4200. }
  4201. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4202. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4203. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4204. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4205. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4206. if (orderBy == 0) {
  4207. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4208. }
  4209. [container setValue:priceDic forKey:key];
  4210. }] mutableCopy];
  4211. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4212. return ret;
  4213. }
  4214. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4215. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4216. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4217. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4218. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4219. int _id = sqlite3_column_int(stmt, 0);
  4220. NSString *name = [self textAtColumn:1 statement:stmt];
  4221. NSDictionary *typeDic = @{
  4222. @"value_id" : name,
  4223. @"value" : name,
  4224. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4225. };
  4226. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4227. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4228. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4229. }];
  4230. return ret;
  4231. }
  4232. + (NSDictionary *)offline_getSalesRep {
  4233. // 首先从offline_login表中取出sales_code
  4234. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4235. NSString *user = app.user;
  4236. user = [self translateSingleQuote:user];
  4237. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4238. __block NSString *user_code = @"";
  4239. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4240. char *code = (char *)sqlite3_column_text(stmt, 0);
  4241. if (code == NULL) {
  4242. code = "";
  4243. }
  4244. user_code = [NSString stringWithUTF8String:code];
  4245. }];
  4246. // 再取所有salesRep
  4247. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4248. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4249. // 1 name 2 code 3 salesrep_id
  4250. char *name = (char *)sqlite3_column_text(stmt, 1);
  4251. char *code = (char *)sqlite3_column_text(stmt, 2);
  4252. int salesrep_id = sqlite3_column_int(stmt, 3);
  4253. if (name == NULL) {
  4254. name = "";
  4255. }
  4256. if (code == NULL) {
  4257. code = "";
  4258. }
  4259. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4260. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4261. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4262. // 比较code 相等则check
  4263. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4264. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4265. }
  4266. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4267. }] mutableCopy];
  4268. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4269. return ret;
  4270. }
  4271. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4272. zipcode = [self translateSingleQuote:zipcode];
  4273. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4274. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4275. char *country = (char *)sqlite3_column_text(stmt, 0);
  4276. char *state = (char *)sqlite3_column_text(stmt, 1);
  4277. char *city = (char *)sqlite3_column_text(stmt, 2);
  4278. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4279. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4280. if (country == NULL) {
  4281. country = "";
  4282. }
  4283. if (state == NULL) {
  4284. state = "";
  4285. }
  4286. if (city == NULL) {
  4287. city = "";
  4288. }
  4289. if (country_code == NULL) {
  4290. country_code = "";
  4291. }
  4292. if (state_code == NULL) {
  4293. state_code = "";
  4294. }
  4295. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4296. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4297. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4298. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4299. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4300. }] mutableCopy];
  4301. return ret;
  4302. }
  4303. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4304. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4305. [item setValue:value forKey:valueKey];
  4306. [dic setValue:item forKey:itemKey];
  4307. }
  4308. + (NSString *)countryCodeByid:(NSString *)code_id {
  4309. NSString *ret = nil;
  4310. code_id = [self translateSingleQuote:code_id];
  4311. sqlite3 *db = [iSalesDB get_db];
  4312. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4313. sqlite3_stmt * statement;
  4314. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4315. while (sqlite3_step(statement) == SQLITE_ROW) {
  4316. char *code = (char *)sqlite3_column_text(statement, 0);
  4317. if (code == NULL) {
  4318. code = "";
  4319. }
  4320. ret = [NSString stringWithUTF8String:code];
  4321. }
  4322. sqlite3_finalize(statement);
  4323. }
  4324. [iSalesDB close_db:db];
  4325. return ret;
  4326. }
  4327. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4328. NSString *ret = nil;
  4329. code = [self translateSingleQuote:code];
  4330. sqlite3 *db = [iSalesDB get_db];
  4331. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4332. sqlite3_stmt * statement;
  4333. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4334. while (sqlite3_step(statement) == SQLITE_ROW) {
  4335. char *_id = (char *)sqlite3_column_text(statement, 0);
  4336. if (_id == NULL) {
  4337. _id = "";
  4338. }
  4339. ret = [NSString stringWithFormat:@"%s",_id];
  4340. }
  4341. sqlite3_finalize(statement);
  4342. }
  4343. [iSalesDB close_db:db];
  4344. return ret;
  4345. }
  4346. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4347. NSString *name = nil;
  4348. codeId = [self translateSingleQuote:codeId];
  4349. sqlite3 *db = [iSalesDB get_db];
  4350. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4351. sqlite3_stmt * statement;
  4352. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4353. while (sqlite3_step(statement) == SQLITE_ROW) {
  4354. char *value = (char *)sqlite3_column_text(statement, 0);
  4355. if (value == NULL) {
  4356. value = "";
  4357. }
  4358. name = [NSString stringWithUTF8String:value];
  4359. }
  4360. sqlite3_finalize(statement);
  4361. }
  4362. [iSalesDB close_db:db];
  4363. return name;
  4364. }
  4365. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4366. NSString *ret = nil;
  4367. sqlite3 *db = [iSalesDB get_db];
  4368. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4369. sqlite3_stmt * statement;
  4370. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4371. while (sqlite3_step(statement) == SQLITE_ROW) {
  4372. char *name = (char *)sqlite3_column_text(statement, 0);
  4373. if (name == NULL) {
  4374. name = "";
  4375. }
  4376. ret = [NSString stringWithUTF8String:name];
  4377. }
  4378. sqlite3_finalize(statement);
  4379. }
  4380. [iSalesDB close_db:db];
  4381. return ret;
  4382. }
  4383. + (NSString *)salesRepCodeById:(NSString *)_id {
  4384. NSString *ret = nil;
  4385. _id = [self translateSingleQuote:_id];
  4386. sqlite3 *db = [iSalesDB get_db];
  4387. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4388. sqlite3_stmt * statement;
  4389. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4390. while (sqlite3_step(statement) == SQLITE_ROW) {
  4391. char *rep = (char *)sqlite3_column_text(statement, 0);
  4392. if (rep == NULL) {
  4393. rep = "";
  4394. }
  4395. ret = [NSString stringWithUTF8String:rep];
  4396. }
  4397. sqlite3_finalize(statement);
  4398. }
  4399. [iSalesDB close_db:db];
  4400. return ret;
  4401. }
  4402. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4403. char *tx = (char *)sqlite3_column_text(stmt, col);
  4404. if (tx == NULL) {
  4405. tx = "";
  4406. }
  4407. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4408. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4409. if (!text) {
  4410. text = @"";
  4411. }
  4412. // 将字符全部为' '的字符串干掉
  4413. int spaceCount = 0;
  4414. for (int i = 0; i < text.length; i++) {
  4415. if ([text characterAtIndex:i] == ' ') {
  4416. spaceCount++;
  4417. }
  4418. }
  4419. if (spaceCount == text.length) {
  4420. text = @"";
  4421. }
  4422. return text;
  4423. }
  4424. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4425. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4426. NSData *data = [NSData dataWithContentsOfFile:path];
  4427. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4428. return ret;
  4429. }
  4430. + (NSString *)textFileName:(NSString *)name {
  4431. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4432. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4433. if (!text) {
  4434. text = @"";
  4435. }
  4436. return text;
  4437. }
  4438. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4439. return [[dic objectForKey:key] mutableCopy];
  4440. }
  4441. + (id)translateSingleQuote:(NSString *)string {
  4442. if ([string isKindOfClass:[NSString class]])
  4443. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4444. return string;
  4445. }
  4446. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4447. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4448. }
  4449. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4450. NSString* ret= nil;
  4451. NSString *sqlQuery = nil;
  4452. // 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
  4453. 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];
  4454. sqlite3_stmt * statement;
  4455. // int count=0;
  4456. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4457. {
  4458. if (sqlite3_step(statement) == SQLITE_ROW)
  4459. {
  4460. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4461. if(imgurl==nil)
  4462. imgurl="";
  4463. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4464. ret=nsimgurl;
  4465. }
  4466. sqlite3_finalize(statement);
  4467. }
  4468. else
  4469. {
  4470. [ret setValue:@"8" forKey:@"result"];
  4471. }
  4472. // [iSalesDB close_db:db];
  4473. // DebugLog(@"data string: %@",ret );
  4474. return ret;
  4475. }
  4476. #pragma mark contact list
  4477. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4478. {
  4479. // contactType = "Sales_Order_Customer";
  4480. // limit = 25;
  4481. // offset = 0;
  4482. // password = 123456;
  4483. // "price_name" = 16;
  4484. // user = EvanK;
  4485. sqlite3 *db = [iSalesDB get_db];
  4486. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4487. if (contactType) {
  4488. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4489. } else {
  4490. contactType = @"1 = 1";
  4491. }
  4492. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4493. DebugLog(@"offline contact list keyword: %@",keyword);
  4494. // advanced search
  4495. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4496. if (contact_name) {
  4497. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4498. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4499. } else {
  4500. contact_name = @"";
  4501. }
  4502. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4503. if (customer_phone) {
  4504. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4505. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4506. } else {
  4507. customer_phone = @"";
  4508. }
  4509. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4510. if (customer_fax) {
  4511. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4512. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4513. } else {
  4514. customer_fax = @"";
  4515. }
  4516. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4517. if (customer_zipcode) {
  4518. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4519. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4520. } else {
  4521. customer_zipcode = @"";
  4522. }
  4523. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4524. if (customer_sales_rep) {
  4525. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4526. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4527. } else {
  4528. customer_sales_rep = @"";
  4529. }
  4530. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4531. if (customer_state) {
  4532. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4533. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4534. } else {
  4535. customer_state = @"";
  4536. }
  4537. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4538. if (customer_name) {
  4539. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4540. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4541. } else {
  4542. customer_name = @"";
  4543. }
  4544. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4545. if (customer_country) {
  4546. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4547. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4548. } else {
  4549. customer_country = @"";
  4550. }
  4551. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4552. if (customer_cid) {
  4553. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4554. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4555. } else {
  4556. customer_cid = @"";
  4557. }
  4558. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4559. if (customer_city) {
  4560. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4561. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4562. } else {
  4563. customer_city = @"";
  4564. }
  4565. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4566. if (customer_address) {
  4567. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4568. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4569. } else {
  4570. customer_address = @"";
  4571. }
  4572. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4573. if (customer_email) {
  4574. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4575. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4576. } else {
  4577. customer_email = @"";
  4578. }
  4579. NSString *price_name = [params valueForKey:@"price_name"];
  4580. if (price_name) {
  4581. if ([price_name containsString:@","]) {
  4582. // 首先从 price表中查处name
  4583. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4584. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4585. for (int i = 1;i < pArray.count;i++) {
  4586. NSString *p = pArray[i];
  4587. [mutablePStr appendFormat:@" or type = %@ ",p];
  4588. }
  4589. [mutablePStr appendString:@";"];
  4590. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4591. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4592. char *name = (char *)sqlite3_column_text(stmt, 0);
  4593. if (!name)
  4594. name = "";
  4595. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4596. }];
  4597. // 再根据name 拼sql
  4598. NSMutableString *mutable_price_name = [NSMutableString string];
  4599. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4600. for (int i = 1; i < price_name_array.count; i++) {
  4601. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4602. }
  4603. [mutable_price_name appendString:@")"];
  4604. price_name = mutable_price_name;
  4605. } else {
  4606. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4607. if ([price_name isEqualToString:@""]) {
  4608. price_name = @"";
  4609. } else {
  4610. __block NSString *price;
  4611. price_name = [self translateSingleQuote:price_name];
  4612. [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) {
  4613. char *p = (char *)sqlite3_column_text(stmt, 0);
  4614. if (p == NULL) {
  4615. p = "";
  4616. }
  4617. price = [NSString stringWithUTF8String:p];
  4618. }];
  4619. if ([price isEqualToString:@""]) {
  4620. price_name = @"";
  4621. } else {
  4622. price = [self translateSingleQuote:price];
  4623. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4624. }
  4625. }
  4626. }
  4627. } else {
  4628. price_name = @"";
  4629. }
  4630. int limit = [[params valueForKey:@"limit"] intValue];
  4631. int offset = [[params valueForKey:@"offset"] intValue];
  4632. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4633. 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];
  4634. 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];
  4635. // int result= [iSalesDB AddExFunction:db];
  4636. int count =0;
  4637. NSString *sqlQuery = nil;
  4638. if(keyword.length==0)
  4639. {
  4640. // 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];
  4641. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4642. sqlQuery = sql;
  4643. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4644. }
  4645. else
  4646. {
  4647. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4648. keyword = keyword.lowercaseString;
  4649. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4650. 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];
  4651. 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]];
  4652. }
  4653. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4654. sqlite3_stmt * statement;
  4655. [ret setValue:@"2" forKey:@"result"];
  4656. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4657. // 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";
  4658. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4659. {
  4660. int i = 0;
  4661. while (sqlite3_step(statement) == SQLITE_ROW)
  4662. {
  4663. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4664. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4665. int editable = sqlite3_column_int(statement, 0);
  4666. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4667. NSString *nscompany_name =nil;
  4668. if(company_name==nil)
  4669. nscompany_name=@"";
  4670. else
  4671. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4672. char *country = (char*)sqlite3_column_text(statement, 2);
  4673. if(country==nil)
  4674. country="";
  4675. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4676. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4677. // if(addr==nil)
  4678. // addr="";
  4679. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4680. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4681. if(zipcode==nil)
  4682. zipcode="";
  4683. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4684. char *state = (char*)sqlite3_column_text(statement, 5);
  4685. if(state==nil)
  4686. state="";
  4687. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4688. char *city = (char*)sqlite3_column_text(statement, 6);
  4689. if(city==nil)
  4690. city="";
  4691. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4692. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4693. // NSString *nscontact_name = nil;
  4694. // if(contact_name==nil)
  4695. // nscontact_name=@"";
  4696. // else
  4697. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4698. char *phone = (char*)sqlite3_column_text(statement, 8);
  4699. NSString *nsphone = nil;
  4700. if(phone==nil)
  4701. nsphone=@"";
  4702. else
  4703. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4704. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4705. if(contact_id==nil)
  4706. contact_id="";
  4707. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4708. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4709. if(addr_1==nil)
  4710. addr_1="";
  4711. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4712. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4713. if(addr_2==nil)
  4714. addr_2="";
  4715. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4716. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4717. if(addr_3==nil)
  4718. addr_3="";
  4719. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4720. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4721. if(addr_4==nil)
  4722. addr_4="";
  4723. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4724. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4725. if(first_name==nil)
  4726. first_name="";
  4727. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4728. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4729. if(last_name==nil)
  4730. last_name="";
  4731. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4732. char *fax = (char*)sqlite3_column_text(statement, 16);
  4733. NSString *nsfax = nil;
  4734. if(fax==nil)
  4735. nsfax=@"";
  4736. else
  4737. {
  4738. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4739. if(nsfax.length>0)
  4740. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4741. }
  4742. char *email = (char*)sqlite3_column_text(statement, 17);
  4743. NSString *nsemail = nil;
  4744. if(email==nil)
  4745. nsemail=@"";
  4746. else
  4747. {
  4748. nsemail= [[NSString alloc]initWithUTF8String:email];
  4749. if(nsemail.length>0)
  4750. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4751. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4752. }
  4753. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4754. [arr_name addObject:nsfirst_name];
  4755. [arr_name addObject:nslast_name];
  4756. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4757. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4758. {
  4759. // decrypt
  4760. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4761. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4762. nsphone=[AESCrypt fastdecrypt:nsphone];
  4763. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4764. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4765. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4766. }
  4767. [arr_addr addObject:nscompany_name];
  4768. [arr_addr addObject:nscontact_name];
  4769. [arr_addr addObject:@"<br>"];
  4770. [arr_addr addObject:nsaddr_1];
  4771. [arr_addr addObject:nsaddr_2];
  4772. [arr_addr addObject:nsaddr_3];
  4773. [arr_addr addObject:nsaddr_4];
  4774. //[arr_addr addObject:nsaddr];
  4775. [arr_addr addObject:nszipcode];
  4776. [arr_addr addObject:nscity];
  4777. [arr_addr addObject:nsstate];
  4778. [arr_addr addObject:nscountry];
  4779. [arr_addr addObject:@"<br>"];
  4780. [arr_addr addObject:nsphone];
  4781. [arr_addr addObject:nsfax];
  4782. [arr_addr addObject:nsemail];
  4783. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4784. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4785. [item setValue:name forKey:@"name"];
  4786. [item setValue:nscontact_id forKey:@"contact_id"];
  4787. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4788. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4789. i++;
  4790. }
  4791. UIApplication * app = [UIApplication sharedApplication];
  4792. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4793. [ret setValue:appDelegate.mode forKey:@"mode"];
  4794. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4795. sqlite3_finalize(statement);
  4796. }
  4797. [iSalesDB close_db:db];
  4798. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4799. return ret;
  4800. }
  4801. #pragma mark contact Advanced search
  4802. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4803. {
  4804. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4805. return [RAUtils dict2data:contactAdvanceDic];
  4806. }
  4807. #pragma mark create new contact
  4808. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4809. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4810. NSData *data = [NSData dataWithContentsOfFile:path];
  4811. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4812. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4813. NSString *countryCode = nil;
  4814. NSString *countryCode_id = nil;
  4815. NSString *stateCode = nil;
  4816. NSString *city = nil;
  4817. NSString *zipCode = nil;
  4818. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4819. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4820. NSString *code_id = params[@"country"];
  4821. countryCode_id = code_id;
  4822. countryCode = [self countryCodeByid:code_id];
  4823. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4824. NSString *zip_code = params[@"zipcode"];
  4825. // 剔除全部为空格
  4826. int spaceCount = 0;
  4827. for (int i = 0; i < zip_code.length; i++) {
  4828. if ([zip_code characterAtIndex:i] == ' ') {
  4829. spaceCount++;
  4830. }
  4831. }
  4832. if (spaceCount == zip_code.length) {
  4833. zip_code = @"";
  4834. }
  4835. zipCode = zip_code;
  4836. if (zipCode.length > 0) {
  4837. countryCode_id = params[@"country"];
  4838. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4839. countryCode = [dic valueForKey:@"country_code"];
  4840. if (!countryCode) {
  4841. // countryCode = @"US";
  4842. NSString *code_id = params[@"country"];
  4843. countryCode = [self countryCodeByid:code_id];
  4844. }
  4845. stateCode = [dic valueForKey:@"state_code"];
  4846. if (!stateCode.length) {
  4847. stateCode = params[@"state"];
  4848. }
  4849. city = [dic valueForKey:@"city"];
  4850. if (!city.length) {
  4851. city = params[@"city"];
  4852. }
  4853. // zip code
  4854. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4855. [zipDic setValue:zipCode forKey:@"value"];
  4856. [section_0 setValue:zipDic forKey:@"item_11"];
  4857. } else {
  4858. NSString *code_id = params[@"country"];
  4859. countryCode = [self countryCodeByid:code_id];
  4860. stateCode = params[@"state"];
  4861. city = params[@"city"];
  4862. }
  4863. }
  4864. } else {
  4865. // default: US United States
  4866. countryCode = @"US";
  4867. countryCode_id = @"228";
  4868. }
  4869. // country
  4870. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4871. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4872. // state
  4873. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4874. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4875. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4876. // city
  4877. if (city) {
  4878. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4879. [cityDic setValue:city forKey:@"value"];
  4880. [section_0 setValue:cityDic forKey:@"item_13"];
  4881. }
  4882. // price type
  4883. NSDictionary *priceDic = [self offline_getPrice];
  4884. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4885. // contact type
  4886. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4887. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4888. // Sales Rep
  4889. NSDictionary *repDic = [self offline_getSalesRep];
  4890. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4891. [ret setValue:section_0 forKey:@"section_0"];
  4892. return [RAUtils dict2data:ret];
  4893. }
  4894. #pragma mark save
  4895. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4896. NSString *addr = nil;
  4897. NSString *contact_name = nil;
  4898. NSString *companyName = [params objectForKey:@"company"];
  4899. if (companyName) {
  4900. companyName = [AESCrypt fastencrypt:companyName];
  4901. } else {
  4902. companyName = @"";
  4903. }
  4904. DebugLog(@"company");
  4905. companyName = [self translateSingleQuote:companyName];
  4906. NSString *addr1 = [params objectForKey:@"address"];
  4907. NSString *addr2 = [params objectForKey:@"address2"];
  4908. NSString *addr3 = [params objectForKey:@"address_3"];
  4909. NSString *addr4 = [params objectForKey:@"address_4"];
  4910. if (!addr2) {
  4911. addr2 = @"";
  4912. }
  4913. if (!addr3) {
  4914. addr3 = @"";
  4915. }
  4916. if (!addr4) {
  4917. addr4 = @"";
  4918. }
  4919. if (!addr1) {
  4920. addr1 = @"";
  4921. }
  4922. DebugLog(@"addr");
  4923. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4924. addr = [AESCrypt fastencrypt:addr];
  4925. addr = [self translateSingleQuote:addr];
  4926. if (addr1 && ![addr1 isEqualToString:@""]) {
  4927. addr1 = [AESCrypt fastencrypt:addr1];
  4928. }
  4929. addr1 = [self translateSingleQuote:addr1];
  4930. addr2 = [self translateSingleQuote:addr2];
  4931. addr3 = [self translateSingleQuote:addr3];
  4932. addr4 = [self translateSingleQuote:addr4];
  4933. NSString *country = [params objectForKey:@"country"];
  4934. if (country) {
  4935. country = [self countryNameByCountryCodeId:country];
  4936. } else {
  4937. country = @"";
  4938. }
  4939. DebugLog(@"country");
  4940. country = [self translateSingleQuote:country];
  4941. NSString *state = [params objectForKey:@"state"];
  4942. if (!state) {
  4943. state = @"";
  4944. }
  4945. DebugLog(@"state");
  4946. state = [self translateSingleQuote:state];
  4947. NSString *city = [params objectForKey:@"city"];
  4948. if (!city) {
  4949. city = @"";
  4950. }
  4951. city = [self translateSingleQuote:city];
  4952. NSString *zipcode = [params objectForKey:@"zipcode"];
  4953. if (!zipcode) {
  4954. zipcode = @"";
  4955. }
  4956. DebugLog(@"zip");
  4957. zipcode = [self translateSingleQuote:zipcode];
  4958. NSString *fistName = [params objectForKey:@"firstname"];
  4959. if (!fistName) {
  4960. fistName = @"";
  4961. }
  4962. NSString *lastName = [params objectForKey:@"lastname"];
  4963. if (!lastName) {
  4964. lastName = @"";
  4965. }
  4966. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4967. DebugLog(@"contact_name");
  4968. contact_name = [self translateSingleQuote:contact_name];
  4969. fistName = [self translateSingleQuote:fistName];
  4970. lastName = [self translateSingleQuote:lastName];
  4971. NSString *phone = [params objectForKey:@"phone"];
  4972. if (phone) {
  4973. phone = [AESCrypt fastencrypt:phone];
  4974. } else {
  4975. phone = @"";
  4976. }
  4977. DebugLog(@"PHONE");
  4978. phone = [self translateSingleQuote:phone];
  4979. NSString *fax = [params objectForKey:@"fax"];
  4980. if (!fax) {
  4981. fax = @"";
  4982. }
  4983. DebugLog(@"FAX");
  4984. fax = [self translateSingleQuote:fax];
  4985. NSString *email = [params objectForKey:@"email"];
  4986. if (!email) {
  4987. email = @"";
  4988. }
  4989. DebugLog(@"EMAIL:%@",email);
  4990. email = [self translateSingleQuote:email];
  4991. NSString *notes = [params objectForKey:@"contact_notes"];
  4992. if (!notes) {
  4993. notes = @"";
  4994. }
  4995. DebugLog(@"NOTE:%@",notes);
  4996. notes = [self translateSingleQuote:notes];
  4997. NSString *price = [params objectForKey:@"price_name"];
  4998. if (price) {
  4999. price = [self priceNameByPriceId:price];
  5000. } else {
  5001. price = @"";
  5002. }
  5003. DebugLog(@"PRICE");
  5004. price = [self translateSingleQuote:price];
  5005. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5006. if (salesRep) {
  5007. salesRep = [self salesRepCodeById:salesRep];
  5008. } else {
  5009. salesRep = @"";
  5010. }
  5011. salesRep = [self translateSingleQuote:salesRep];
  5012. NSString *img = [params objectForKey:@"business_card"];
  5013. NSArray *array = [img componentsSeparatedByString:@","];
  5014. NSString *img_0 = array[0];
  5015. if (!img_0) {
  5016. img_0 = @"";
  5017. }
  5018. img_0 = [self translateSingleQuote:img_0];
  5019. NSString *img_1 = array[1];
  5020. if (!img_1) {
  5021. img_1 = @"";
  5022. }
  5023. img_1 = [self translateSingleQuote:img_1];
  5024. NSString *img_2 = array[2];
  5025. if (!img_2) {
  5026. img_2 = @"";
  5027. }
  5028. img_2 = [self translateSingleQuote:img_2];
  5029. NSString *contact_id = [NSUUID UUID].UUIDString;
  5030. NSString *contact_type = [params objectForKey:@"type_name"];
  5031. if (!contact_type) {
  5032. contact_type = @"";
  5033. }
  5034. contact_type = [self translateSingleQuote:contact_type];
  5035. // 判断更新时是否为customer
  5036. if (update) {
  5037. contact_id = [params objectForKey:@"contact_id"];
  5038. if (!contact_id) {
  5039. contact_id = @"";
  5040. }
  5041. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5042. __block int customer = 0;
  5043. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5044. customer = sqlite3_column_int(stmt, 0);
  5045. }];
  5046. isCustomer = customer ? YES : NO;
  5047. }
  5048. NSMutableDictionary *sync_dic = [params mutableCopy];
  5049. if (isCustomer) {
  5050. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5051. } else {
  5052. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5053. }
  5054. NSString *sync_data = nil;
  5055. NSString *sql = nil;
  5056. if (update){
  5057. contact_id = [params objectForKey:@"contact_id"];
  5058. if (!contact_id) {
  5059. contact_id = @"";
  5060. }
  5061. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5062. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5063. sync_data = [RAUtils dict2string:sync_dic];
  5064. sync_data = [self translateSingleQuote:sync_data];
  5065. 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];
  5066. } else {
  5067. contact_id = [self translateSingleQuote:contact_id];
  5068. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5069. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5070. sync_data = [RAUtils dict2string:sync_dic];
  5071. sync_data = [self translateSingleQuote:sync_data];
  5072. 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];
  5073. }
  5074. int result = [iSalesDB execSql:sql];
  5075. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5076. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5077. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5078. }
  5079. #pragma mark save new contact
  5080. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5081. {
  5082. return [self offline_saveContact:params update:NO isCustomer:YES];
  5083. }
  5084. #pragma mark edit contact
  5085. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5086. {
  5087. // {
  5088. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5089. // password = 123456;
  5090. // user = EvanK;
  5091. // }
  5092. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5093. NSData *data = [NSData dataWithContentsOfFile:path];
  5094. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5095. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5096. NSString *countryCode = nil;
  5097. NSString *countryCode_id = nil;
  5098. NSString *stateCode = nil;
  5099. /*------contact infor------*/
  5100. __block NSString *country = nil;
  5101. __block NSString *company_name = nil;
  5102. __block NSString *contact_id = params[@"contact_id"];
  5103. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5104. __block NSString *zipcode = nil;
  5105. __block NSString *state = nil; // state_code
  5106. __block NSString *city = nil; //
  5107. __block NSString *firt_name,*last_name;
  5108. __block NSString *phone,*fax,*email;
  5109. __block NSString *notes,*price_type,*sales_rep;
  5110. __block NSString *img_0,*img_1,*img_2;
  5111. __block NSString *contact_type;
  5112. contact_id = [self translateSingleQuote:contact_id];
  5113. 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];
  5114. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5115. country = [self textAtColumn:0 statement:stmt]; // country name
  5116. company_name = [self textAtColumn:1 statement:stmt];
  5117. addr_1 = [self textAtColumn:2 statement:stmt];
  5118. addr_2 = [self textAtColumn:3 statement:stmt];
  5119. addr_3 = [self textAtColumn:4 statement:stmt];
  5120. addr_4 = [self textAtColumn:5 statement:stmt];
  5121. zipcode = [self textAtColumn:6 statement:stmt];
  5122. state = [self textAtColumn:7 statement:stmt]; // state code
  5123. city = [self textAtColumn:8 statement:stmt];
  5124. firt_name = [self textAtColumn:9 statement:stmt];
  5125. last_name = [self textAtColumn:10 statement:stmt];
  5126. phone = [self textAtColumn:11 statement:stmt];
  5127. fax = [self textAtColumn:12 statement:stmt];
  5128. email = [self textAtColumn:13 statement:stmt];
  5129. notes = [self textAtColumn:14 statement:stmt];
  5130. price_type = [self textAtColumn:15 statement:stmt]; // name
  5131. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5132. img_0 = [self textAtColumn:17 statement:stmt];
  5133. img_1 = [self textAtColumn:18 statement:stmt];
  5134. img_2 = [self textAtColumn:19 statement:stmt];
  5135. contact_type = [self textAtColumn:20 statement:stmt];
  5136. }];
  5137. // decrypt
  5138. if (company_name) {
  5139. company_name = [AESCrypt fastdecrypt:company_name];
  5140. }
  5141. if (addr_1) {
  5142. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5143. }
  5144. if (phone) {
  5145. phone = [AESCrypt fastdecrypt:phone];
  5146. }
  5147. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5148. countryCode = [iSalesDB jk_queryText:countrySql];
  5149. stateCode = state;
  5150. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5151. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5152. NSString *code_id = params[@"country"];
  5153. countryCode_id = code_id;
  5154. countryCode = [self countryCodeByid:code_id];
  5155. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5156. NSString *zip_code = params[@"zipcode"];
  5157. // 剔除全部为空格
  5158. int spaceCount = 0;
  5159. for (int i = 0; i < zip_code.length; i++) {
  5160. if ([zip_code characterAtIndex:i] == ' ') {
  5161. spaceCount++;
  5162. }
  5163. }
  5164. if (spaceCount == zip_code.length) {
  5165. zip_code = @"";
  5166. }
  5167. if (zipcode.length > 0) {
  5168. zipcode = zip_code;
  5169. countryCode_id = params[@"country"];
  5170. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5171. countryCode = [dic valueForKey:@"country_code"];
  5172. if (!countryCode) {
  5173. NSString *code_id = params[@"country"];
  5174. countryCode = [self countryCodeByid:code_id];
  5175. }
  5176. stateCode = [dic valueForKey:@"state_code"];
  5177. if (!stateCode.length) {
  5178. stateCode = params[@"state"];
  5179. }
  5180. city = [dic valueForKey:@"city"];
  5181. if (!city.length) {
  5182. city = params[@"city"];
  5183. }
  5184. // zip code
  5185. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5186. [zipDic setValue:zipcode forKey:@"value"];
  5187. [section_0 setValue:zipDic forKey:@"item_8"];
  5188. } else {
  5189. NSString *code_id = params[@"country"];
  5190. countryCode = [self countryCodeByid:code_id];
  5191. stateCode = params[@"state"];
  5192. city = params[@"city"];
  5193. }
  5194. }
  5195. }
  5196. // 0 Country
  5197. // 1 Company Name
  5198. // 2 Contact ID
  5199. // 3 Picture
  5200. // 4 Address 1
  5201. // 5 Address 2
  5202. // 6 Address 3
  5203. // 7 Address 4
  5204. // 8 Zip Code
  5205. // 9 State/Province
  5206. // 10 City
  5207. // 11 Contact First Name
  5208. // 12 Contact Last Name
  5209. // 13 Phone
  5210. // 14 Fax
  5211. // 15 Email
  5212. // 16 Contact Notes
  5213. // 17 Price Type
  5214. // 18 Contact Type
  5215. // 19 Sales Rep
  5216. // country
  5217. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5218. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5219. // company
  5220. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5221. // contact_id
  5222. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5223. // picture
  5224. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5225. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5226. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5227. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5228. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5229. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5230. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5231. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5232. // addr 1 2 3 4
  5233. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5234. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5235. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5236. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5237. // zip code
  5238. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5239. // state
  5240. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5241. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5242. // city
  5243. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5244. // first last
  5245. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5246. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5247. // phone fax email
  5248. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5249. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5250. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5251. // notes
  5252. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5253. // price
  5254. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5255. for (NSString *key in priceDic.allKeys) {
  5256. if ([key containsString:@"val_"]) {
  5257. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5258. if ([dic[@"value"] isEqualToString:price_type]) {
  5259. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5260. [priceDic setValue:dic forKey:key];
  5261. }
  5262. }
  5263. }
  5264. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5265. // Contact Rep
  5266. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5267. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5268. // Sales Rep
  5269. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5270. for (NSString *key in repDic.allKeys) {
  5271. if ([key containsString:@"val_"]) {
  5272. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5273. NSString *value = dic[@"value"];
  5274. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5275. if (code && [code isEqualToString:sales_rep]) {
  5276. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5277. [repDic setValue:dic forKey:key];
  5278. }
  5279. }
  5280. }
  5281. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5282. [ret setValue:section_0 forKey:@"section_0"];
  5283. return [RAUtils dict2data:ret];
  5284. }
  5285. #pragma mark save contact
  5286. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5287. {
  5288. return [self offline_saveContact:params update:YES isCustomer:YES];
  5289. }
  5290. #pragma mark category
  5291. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5292. if (ck) {
  5293. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5294. for (NSString *key in res.allKeys) {
  5295. if (![key isEqualToString:@"count"]) {
  5296. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5297. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5298. if ([val[@"value"] isEqualToString:ck]) {
  5299. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5300. }
  5301. [res setValue:val forKey:key];
  5302. }
  5303. }
  5304. [dic setValue:res forKey:valueKey];
  5305. }
  5306. }
  5307. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5308. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5309. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5310. NSString* category = [params valueForKey:@"category"];
  5311. if (!category || [category isEqualToString:@""]) {
  5312. category = @"%";
  5313. }
  5314. category = [self translateSingleQuote:category];
  5315. int limit = [[params valueForKey:@"limit"] intValue];
  5316. int offset = [[params valueForKey:@"offset"] intValue];
  5317. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5318. NSString *limit_str = @"";
  5319. if (limited) {
  5320. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5321. }
  5322. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5323. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5324. sqlite3 *db = [iSalesDB get_db];
  5325. // [iSalesDB AddExFunction:db];
  5326. int count;
  5327. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5328. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5329. double price_min = 0;
  5330. double price_max = 0;
  5331. if ([params.allKeys containsObject:@"alert"]) {
  5332. // alert
  5333. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5334. NSString *alert = params[@"alert"];
  5335. if ([alert isEqualToString:@"Display All"]) {
  5336. alert = [NSString stringWithFormat:@""];
  5337. } else {
  5338. alert = [self translateSingleQuote:alert];
  5339. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5340. }
  5341. // available
  5342. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5343. NSString *available = params[@"available"];
  5344. NSString *available_condition;
  5345. if ([available isEqualToString:@"Display All"]) {
  5346. available_condition = @"";
  5347. } else if ([available isEqualToString:@"Available Now"]) {
  5348. available_condition = @"and availability > 0";
  5349. } else {
  5350. available_condition = @"and availability == 0";
  5351. }
  5352. // best seller
  5353. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5354. NSString *best_seller = @"";
  5355. NSString *order_best_seller = @"m.name asc";
  5356. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5357. best_seller = @"and best_seller > 0";
  5358. order_best_seller = @"m.best_seller desc,m.name asc";
  5359. }
  5360. // price
  5361. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5362. NSString *price = params[@"price"];
  5363. price_min = 0;
  5364. price_max = MAXFLOAT;
  5365. if (appDelegate.user && price != nil) {
  5366. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5367. NSMutableString *priceName = [NSMutableString string];
  5368. for (int i = 0; i < priceTypeArray.count; i++) {
  5369. NSString *pricetype = priceTypeArray[i];
  5370. pricetype = [self translateSingleQuote:pricetype];
  5371. if (i == 0) {
  5372. [priceName appendFormat:@"'%@'",pricetype];
  5373. } else {
  5374. [priceName appendFormat:@",'%@'",pricetype];
  5375. }
  5376. }
  5377. if ([price isEqualToString:@"Display All"]) {
  5378. price = [NSString stringWithFormat:@""];
  5379. } else if([price containsString:@"+"]){
  5380. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5381. price_min = [price doubleValue];
  5382. 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];
  5383. } else {
  5384. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5385. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5386. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5387. 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];
  5388. }
  5389. } else {
  5390. price = @"";
  5391. }
  5392. // sold_by_qty : Sold in quantities of %@
  5393. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5394. NSString *qty = params[@"sold_by_qty"];
  5395. if ([qty isEqualToString:@"Display All"]) {
  5396. qty = @"";
  5397. } else {
  5398. qty = [self translateSingleQuote:qty];
  5399. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5400. }
  5401. // cate
  5402. // category = [self translateSingleQuote:category];
  5403. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5404. // cate mutiple selection
  5405. NSString *category_id = params[@"category"];
  5406. NSMutableArray *cate_id_array = nil;
  5407. NSMutableString *cateWhere = [NSMutableString string];
  5408. if ([category_id isEqualToString:@""] || !category_id) {
  5409. [cateWhere appendString:@"1 = 1"];
  5410. } else {
  5411. if ([category_id containsString:@","]) {
  5412. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5413. } else {
  5414. cate_id_array = [@[category_id] mutableCopy];
  5415. }
  5416. [cateWhere appendString:@"("];
  5417. for (int i = 0; i < cate_id_array.count; i++) {
  5418. if (i == 0) {
  5419. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5420. } else {
  5421. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5422. }
  5423. }
  5424. [cateWhere appendString:@")"];
  5425. }
  5426. // where bestseller > 0 order by bestseller desc
  5427. // sql query: alert availability(int) best_seller(int) price qty
  5428. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5429. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5430. }
  5431. DebugLog(@"offline category where: %@",where);
  5432. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5433. if (!appDelegate.user) {
  5434. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5435. }
  5436. [ret setValue:filter forKey:@"filter"];
  5437. DebugLog(@"offline_category sql:%@",sqlQuery);
  5438. sqlite3_stmt * statement;
  5439. [ret setValue:@"2" forKey:@"result"];
  5440. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5441. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5442. // int count=0;
  5443. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5444. {
  5445. int i=0;
  5446. while (sqlite3_step(statement) == SQLITE_ROW)
  5447. {
  5448. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5449. char *name = (char*)sqlite3_column_text(statement, 0);
  5450. if(name==nil)
  5451. name="";
  5452. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5453. char *description = (char*)sqlite3_column_text(statement, 1);
  5454. if(description==nil)
  5455. description="";
  5456. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5457. int product_id = sqlite3_column_int(statement, 2);
  5458. int wid = sqlite3_column_int(statement, 3);
  5459. int closeout = sqlite3_column_int(statement, 4);
  5460. int cid = sqlite3_column_int(statement, 5);
  5461. int wisdelete = sqlite3_column_int(statement, 6);
  5462. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5463. int more_color = sqlite3_column_int(statement, 8);
  5464. // Defaul Category ID
  5465. __block NSString *categoryID = nil;
  5466. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5467. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5468. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5469. if(default_category==nil)
  5470. default_category="";
  5471. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5472. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5473. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5474. categoryID = nsdefault_category;
  5475. }];
  5476. if (!categoryID.length) {
  5477. NSString *cateIDs = params[@"category"];
  5478. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5479. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5480. for (NSString *cateID in requestCategoryArr) {
  5481. BOOL needBreak = NO;
  5482. for (NSString *itemCateIDBox in itemCategoryArr) {
  5483. if (itemCateIDBox.length > 4) {
  5484. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5485. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5486. if ([itemCategoryID isEqualToString:cateID]) {
  5487. needBreak = YES;
  5488. categoryID = itemCategoryID;
  5489. break;
  5490. }
  5491. }
  5492. }
  5493. if (needBreak) {
  5494. break;
  5495. }
  5496. }
  5497. }
  5498. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5499. if(wid !=0 && wisdelete != 1)
  5500. [item setValue:@"true" forKey:@"wish_exists"];
  5501. else
  5502. [item setValue:@"false" forKey:@"wish_exists"];
  5503. if(closeout==0)
  5504. [item setValue:@"false" forKey:@"is_closeout"];
  5505. else
  5506. [item setValue:@"true" forKey:@"is_closeout"];
  5507. if(cid==0)
  5508. [item setValue:@"false" forKey:@"cart_exists"];
  5509. else
  5510. [item setValue:@"true" forKey:@"cart_exists"];
  5511. if (more_color == 0) {
  5512. [item setObject:@(false) forKey:@"more_color"];
  5513. } else if (more_color == 1) {
  5514. [item setObject:@(true) forKey:@"more_color"];
  5515. }
  5516. [item addEntriesFromDictionary:imgjson];
  5517. // [item setValue:nsurl forKey:@"img"];
  5518. [item setValue:nsname forKey:@"name"];
  5519. [item setValue:nsdescription forKey:@"description"];
  5520. if (categoryID) {
  5521. [item setValue:categoryID forKey:@"item_category_id"];
  5522. }
  5523. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5524. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5525. i++;
  5526. }
  5527. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5528. [ret setObject:items forKey:@"items"];
  5529. sqlite3_finalize(statement);
  5530. } else {
  5531. DebugLog(@"nothing...");
  5532. }
  5533. DebugLog(@"count:%d",count);
  5534. [iSalesDB close_db:db];
  5535. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5536. return ret;
  5537. }
  5538. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5539. {
  5540. return [self categoryList:params limited:YES];
  5541. }
  5542. # pragma mark item search
  5543. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5544. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5545. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5546. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5547. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5548. // category
  5549. NSDictionary *category_menu = [self offline_category_menu];
  5550. [filter setValue:category_menu forKey:@"category"];
  5551. NSString* where= nil;
  5552. NSString* orderby= @"m.name";
  5553. if (!filterSearch) {
  5554. int covertype = [[params valueForKey:@"covertype"] intValue];
  5555. switch (covertype) {
  5556. case 0:
  5557. {
  5558. where=@"m.category like'%%#005#%%'";
  5559. break;
  5560. }
  5561. case 1:
  5562. {
  5563. where=@"m.alert like '%QS%'";
  5564. break;
  5565. }
  5566. case 2:
  5567. {
  5568. where=@"m.availability>0";
  5569. break;
  5570. }
  5571. case 3:
  5572. {
  5573. where=@"m.best_seller>0";
  5574. orderby=@"m.best_seller desc,m.name asc";
  5575. break;
  5576. }
  5577. default:
  5578. where=@"1=1";
  5579. break;
  5580. }
  5581. }
  5582. int limit = [[params valueForKey:@"limit"] intValue];
  5583. int offset = [[params valueForKey:@"offset"] intValue];
  5584. NSString *limit_str = @"";
  5585. if (limited) {
  5586. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5587. }
  5588. sqlite3 *db = [iSalesDB get_db];
  5589. // [iSalesDB AddExFunction:db];
  5590. int count;
  5591. NSString *sqlQuery = nil;
  5592. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5593. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5594. double price_min = 0;
  5595. double price_max = 0;
  5596. if (filterSearch) {
  5597. // alert
  5598. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5599. NSString *alert = params[@"alert"];
  5600. if ([alert isEqualToString:@"Display All"]) {
  5601. alert = [NSString stringWithFormat:@""];
  5602. } else {
  5603. alert = [self translateSingleQuote:alert];
  5604. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5605. }
  5606. // available
  5607. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5608. NSString *available = params[@"available"];
  5609. NSString *available_condition;
  5610. if ([available isEqualToString:@"Display All"]) {
  5611. available_condition = @"";
  5612. } else if ([available isEqualToString:@"Available Now"]) {
  5613. available_condition = @"and availability > 0";
  5614. } else {
  5615. available_condition = @"and availability == 0";
  5616. }
  5617. // best seller
  5618. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5619. NSString *best_seller = @"";
  5620. NSString *order_best_seller = @"m.name asc";
  5621. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5622. best_seller = @"and best_seller > 0";
  5623. order_best_seller = @"m.best_seller desc,m.name asc";
  5624. }
  5625. // price
  5626. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5627. NSString *price = params[@"price"];
  5628. price_min = 0;
  5629. price_max = MAXFLOAT;
  5630. if (appDelegate.user) {
  5631. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5632. NSMutableString *priceName = [NSMutableString string];
  5633. for (int i = 0; i < priceTypeArray.count; i++) {
  5634. NSString *pricetype = priceTypeArray[i];
  5635. pricetype = [self translateSingleQuote:pricetype];
  5636. if (i == 0) {
  5637. [priceName appendFormat:@"'%@'",pricetype];
  5638. } else {
  5639. [priceName appendFormat:@",'%@'",pricetype];
  5640. }
  5641. }
  5642. if ([price isEqualToString:@"Display All"]) {
  5643. price = [NSString stringWithFormat:@""];
  5644. } else if([price containsString:@"+"]){
  5645. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5646. price_min = [price doubleValue];
  5647. 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];
  5648. } else {
  5649. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5650. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5651. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5652. 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];
  5653. }
  5654. } else {
  5655. price = @"";
  5656. }
  5657. // sold_by_qty : Sold in quantities of %@
  5658. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5659. NSString *qty = params[@"sold_by_qty"];
  5660. if ([qty isEqualToString:@"Display All"]) {
  5661. qty = @"";
  5662. } else {
  5663. qty = [self translateSingleQuote:qty];
  5664. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5665. }
  5666. // category
  5667. NSString *category_id = params[@"ctgId"];
  5668. NSMutableArray *cate_id_array = nil;
  5669. NSMutableString *cateWhere = [NSMutableString string];
  5670. if ([category_id isEqualToString:@""] || !category_id) {
  5671. [cateWhere appendString:@"1 = 1"];
  5672. } else {
  5673. if ([category_id containsString:@","]) {
  5674. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5675. } else {
  5676. cate_id_array = [@[category_id] mutableCopy];
  5677. }
  5678. /*-----------*/
  5679. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5680. [cateWhere appendString:@"("];
  5681. for (int i = 0; i < cate_id_array.count; i++) {
  5682. for (NSString *key0 in cateDic.allKeys) {
  5683. if ([key0 containsString:@"category_"]) {
  5684. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5685. for (NSString *key1 in category0.allKeys) {
  5686. if ([key1 containsString:@"category_"]) {
  5687. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5688. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5689. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5690. cate_id_array[i] = [category1 objectForKey:@"id"];
  5691. if (i == 0) {
  5692. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5693. } else {
  5694. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5695. }
  5696. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5697. [category0 setValue:category1 forKey:key1];
  5698. [cateDic setValue:category0 forKey:key0];
  5699. }
  5700. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5701. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5702. cate_id_array[i] = [category0 objectForKey:@"id"];
  5703. if (i == 0) {
  5704. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5705. } else {
  5706. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5707. }
  5708. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5709. [cateDic setValue:category0 forKey:key0];
  5710. }
  5711. }
  5712. }
  5713. }
  5714. }
  5715. }
  5716. [cateWhere appendString:@")"];
  5717. [filter setValue:cateDic forKey:@"category"];
  5718. }
  5719. // where bestseller > 0 order by bestseller desc
  5720. // sql query: alert availability(int) best_seller(int) price qty
  5721. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5722. // count
  5723. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5724. }
  5725. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5726. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5727. if (!appDelegate.user) {
  5728. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5729. }
  5730. [ret setValue:filter forKey:@"filter"];
  5731. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5732. sqlite3_stmt * statement;
  5733. [ret setValue:@"2" forKey:@"result"];
  5734. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5735. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5736. // int count=0;
  5737. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5738. {
  5739. int i=0;
  5740. while (sqlite3_step(statement) == SQLITE_ROW)
  5741. {
  5742. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5743. char *name = (char*)sqlite3_column_text(statement, 0);
  5744. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5745. char *description = (char*)sqlite3_column_text(statement, 1);
  5746. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5747. int product_id = sqlite3_column_int(statement, 2);
  5748. int wid = sqlite3_column_int(statement, 3);
  5749. int closeout = sqlite3_column_int(statement, 4);
  5750. int cid = sqlite3_column_int(statement, 5);
  5751. int wisdelete = sqlite3_column_int(statement, 6);
  5752. int more_color = sqlite3_column_int(statement, 7);
  5753. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5754. if(wid !=0 && wisdelete != 1)
  5755. [item setValue:@"true" forKey:@"wish_exists"];
  5756. else
  5757. [item setValue:@"false" forKey:@"wish_exists"];
  5758. if(closeout==0)
  5759. [item setValue:@"false" forKey:@"is_closeout"];
  5760. else
  5761. [item setValue:@"true" forKey:@"is_closeout"];
  5762. if(cid==0)
  5763. [item setValue:@"false" forKey:@"cart_exists"];
  5764. else
  5765. [item setValue:@"true" forKey:@"cart_exists"];
  5766. if (more_color == 0) {
  5767. [item setObject:@(false) forKey:@"more_color"];
  5768. } else if (more_color == 1) {
  5769. [item setObject:@(true) forKey:@"more_color"];
  5770. }
  5771. [item addEntriesFromDictionary:imgjson];
  5772. // [item setValue:nsurl forKey:@"img"];
  5773. [item setValue:nsname forKey:@"fash_name"];
  5774. [item setValue:nsdescription forKey:@"description"];
  5775. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5776. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5777. i++;
  5778. }
  5779. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5780. [ret setObject:items forKey:@"items"];
  5781. sqlite3_finalize(statement);
  5782. }
  5783. [iSalesDB close_db:db];
  5784. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5785. return ret;
  5786. }
  5787. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5788. {
  5789. return [self itemsearch:params limited:YES];
  5790. }
  5791. #pragma mark order detail
  5792. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5793. if (str1.length == 0) {
  5794. str1 = @"&nbsp";
  5795. }
  5796. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5797. return str;
  5798. }
  5799. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5800. {
  5801. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5802. [fromformatter setDateFormat:from];
  5803. NSDate *date = [fromformatter dateFromString:datetime];
  5804. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5805. [toformatter setDateFormat:to];
  5806. NSString * ret = [toformatter stringFromDate:date];
  5807. return ret;
  5808. }
  5809. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5810. // 把毫秒去掉
  5811. if ([dateTime containsString:@"."]) {
  5812. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5813. }
  5814. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5815. formatter.dateFormat = formate;
  5816. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5817. NSDate *date = [formatter dateFromString:dateTime];
  5818. formatter.dateFormat = newFormate;
  5819. NSString *result = [formatter stringFromDate:date];
  5820. return result ? result : @"";
  5821. }
  5822. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5823. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5824. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5825. }
  5826. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5827. {
  5828. DebugLog(@"offline oderdetail params: %@",params);
  5829. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5830. int orderId = [params[@"orderId"] intValue];
  5831. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5832. // decrypt card number and card security code
  5833. // 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 ];
  5834. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country 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];
  5835. sqlite3 *db = [iSalesDB get_db];
  5836. sqlite3_stmt * statement;
  5837. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5838. NSString *nssoid = @"";
  5839. NSString* orderinfo = @"";
  5840. NSString *moreInfo = @"";
  5841. double handlingFee = 0;
  5842. double payments_and_credist = 0;
  5843. double totalPrice = 0;
  5844. double shippingFee = 0;
  5845. double lift_gate = 0;
  5846. NSString *customer_contact = @"";
  5847. NSString *customer_email = @"";
  5848. NSString *customer_fax = @"";
  5849. NSString *customer_phone = @"";
  5850. NSString *customer_city = @"";
  5851. NSString *customer_state = @"";
  5852. NSString *customer_zipcode = @"";
  5853. NSString *customer_country = @"";
  5854. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5855. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5856. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5857. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5858. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5859. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5860. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5861. {
  5862. if (sqlite3_step(statement) == SQLITE_ROW)
  5863. {
  5864. int order_id = sqlite3_column_int(statement, 0);
  5865. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5866. // ret[@"sign_url"] = sign_url;
  5867. section_1 = @{
  5868. @"data":sign_url,
  5869. @"title":@"Signature",
  5870. @"type":@"sign_url"
  5871. }.mutableCopy;
  5872. [ret setObject:section_1 forKey:@"section_1"];
  5873. customer_contact = [self textAtColumn:52 statement:statement];
  5874. customer_email = [self textAtColumn:53 statement:statement];
  5875. customer_phone = [self textAtColumn:54 statement:statement];
  5876. customer_fax = [self textAtColumn:55 statement:statement];
  5877. customer_city = [self textAtColumn:60 statement:statement];
  5878. customer_state = [self textAtColumn:61 statement:statement];
  5879. customer_zipcode = [self textAtColumn:62 statement:statement];
  5880. customer_country = [self textAtColumn:63 statement:statement];
  5881. int offline_edit=sqlite3_column_int(statement, 56);
  5882. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5883. char *soid = (char*)sqlite3_column_text(statement, 1);
  5884. if(soid==nil)
  5885. soid= "";
  5886. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5887. // so#
  5888. ret[@"so#"] = nssoid;
  5889. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5890. if(poNumber==nil)
  5891. poNumber= "";
  5892. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5893. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5894. if(create_time==nil)
  5895. create_time= "";
  5896. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5897. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5898. int status = sqlite3_column_int(statement, 4);
  5899. int erpStatus = sqlite3_column_int(statement, 49);
  5900. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5901. // status
  5902. if (status > 1 && status != 3) {
  5903. status = erpStatus;
  5904. } else if (status == 3) {
  5905. status = 15;
  5906. }
  5907. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5908. ret[@"order_status"] = nsstatus;
  5909. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5910. if(company_name==nil)
  5911. company_name= "";
  5912. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5913. // company name
  5914. ret[@"company_name"] = nscompany_name;
  5915. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5916. if(customer_contact==nil)
  5917. customer_contact= "";
  5918. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5919. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5920. if(addr_1==nil)
  5921. addr_1="";
  5922. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5923. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5924. if(addr_2==nil)
  5925. addr_2="";
  5926. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5927. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5928. if(addr_3==nil)
  5929. addr_3="";
  5930. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5931. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5932. if(addr_4==nil)
  5933. addr_4="";
  5934. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5935. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5936. [arr_addr addObject:nsaddr_1];
  5937. [arr_addr addObject:nsaddr_2];
  5938. [arr_addr addObject:nsaddr_3];
  5939. [arr_addr addObject:nsaddr_4];
  5940. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5941. [arr_addr addObject:customer_state];
  5942. [arr_addr addObject:customer_zipcode];
  5943. [arr_addr addObject:customer_country];
  5944. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@" " trim:true];
  5945. char *logist = (char*)sqlite3_column_text(statement, 11);
  5946. if(logist==nil)
  5947. logist= "";
  5948. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5949. if (/*status == -11 || */status == 10 || status == 11) {
  5950. nslogist = [self textAtColumn:59 statement:statement];
  5951. };
  5952. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5953. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5954. shipping = @"Shipping To Be Quoted";
  5955. } else {
  5956. shippingFee = sqlite3_column_double(statement, 12);
  5957. }
  5958. // Shipping
  5959. // ret[@"Shipping"] = shipping;
  5960. NSDictionary *shipping_item = @{
  5961. @"title":@"Shipping",
  5962. @"value":shipping
  5963. };
  5964. [price_data setObject:shipping_item forKey:@"item_1"];
  5965. int have_lift_gate = sqlite3_column_int(statement, 17);
  5966. lift_gate = sqlite3_column_double(statement, 13);
  5967. // Liftgate Fee(No loading dock)
  5968. if (!have_lift_gate) {
  5969. lift_gate = 0;
  5970. }
  5971. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5972. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5973. if (sqlite3_column_int(statement, 57)) {
  5974. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5975. liftgate_value = @"Shipping To Be Quoted";
  5976. }
  5977. NSDictionary *liftgate_item = @{
  5978. @"title":@"Liftgate Fee(No loading dock)",
  5979. @"value":liftgate_value
  5980. };
  5981. [price_data setObject:liftgate_item forKey:@"item_2"];
  5982. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  5983. // [ret removeObjectForKey:@"Shipping"];
  5984. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5985. // }
  5986. //
  5987. // if (have_lift_gate) {
  5988. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5989. // }
  5990. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5991. if(general_notes==nil)
  5992. general_notes= "";
  5993. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5994. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5995. if(internal_notes==nil)
  5996. internal_notes= "";
  5997. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5998. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5999. if(payment_type==nil)
  6000. payment_type= "";
  6001. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6002. // order info
  6003. orderinfo = [self textFileName:@"order_info.html"];
  6004. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6005. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6006. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6007. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6008. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6009. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6010. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6011. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6012. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6013. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6014. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6015. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6016. NSString *payment = nil;
  6017. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6018. {
  6019. payment = [self textFileName:@"creditcardpayment.html"];
  6020. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6021. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6022. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6023. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6024. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6025. NSString *card_type = [self textAtColumn:42 statement:statement];
  6026. if (card_type.length > 0) { // 显示星号
  6027. card_type = @"****";
  6028. }
  6029. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6030. if (card_number.length > 0 && card_number.length > 4) {
  6031. for (int i = 0; i < card_number.length - 4; i++) {
  6032. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6033. }
  6034. } else {
  6035. card_number = @"";
  6036. }
  6037. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6038. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6039. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6040. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6041. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6042. card_expiration = @"****";
  6043. }
  6044. NSString *card_city = [self textAtColumn:46 statement:statement];
  6045. NSString *card_state = [self textAtColumn:47 statement:statement];
  6046. payment = [self replaceHtml:payment String:@"Payment_Type" withString:nspayment_type];
  6047. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6048. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6049. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6050. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6051. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6052. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6053. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6054. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6055. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6056. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6057. }
  6058. else
  6059. {
  6060. payment=[self textFileName:@"normalpayment.html"];
  6061. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6062. }
  6063. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6064. ret[@"result"]= [NSNumber numberWithInt:2];
  6065. // more info
  6066. moreInfo = [self textFileName:@"more_info.html"];
  6067. /*****ship to******/
  6068. // ShipToCompany_or_&nbsp
  6069. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6070. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6071. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6072. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6073. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6074. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6075. /*****ship from******/
  6076. // ShipFromCompany_or_&nbsp
  6077. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6078. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6079. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6080. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6081. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6082. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6083. /*****freight to******/
  6084. // FreightBillToCompany_or_&nbsp
  6085. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6086. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6087. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6088. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6089. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6090. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6091. /*****merchandise to******/
  6092. // MerchandiseBillToCompany_or_&nbsp
  6093. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6094. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6095. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6096. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6097. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6098. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6099. /*****return to******/
  6100. // ReturnToCompany_or_&nbsp
  6101. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6102. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6103. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6104. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6105. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6106. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6107. //
  6108. // DebugLog(@"more info : %@",moreInfo);
  6109. // handling fee
  6110. handlingFee = sqlite3_column_double(statement, 33);
  6111. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6112. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6113. if (sqlite3_column_int(statement, 58)) {
  6114. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6115. handling_fee_value = @"Shipping To Be Quoted";
  6116. }
  6117. NSDictionary *handling_fee_item = @{
  6118. @"title":@"Handling Fee",
  6119. @"value":handling_fee_value
  6120. };
  6121. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6122. //
  6123. // customer info
  6124. customerID = [self textAtColumn:36 statement:statement];
  6125. // mode
  6126. ret[@"mode"] = appDelegate.mode;
  6127. // model_count
  6128. ret[@"model_count"] = @(0);
  6129. }
  6130. sqlite3_finalize(statement);
  6131. }
  6132. [iSalesDB close_db:db];
  6133. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6134. // "customer_email" = "Shui Hu";
  6135. // "customer_fax" = "";
  6136. // "customer_first_name" = F;
  6137. // "customer_last_name" = L;
  6138. // "customer_name" = ",da He Xiang Dong Liu A";
  6139. // "customer_phone" = "Hey Xuan Feng";
  6140. contactInfo[@"customer_phone"] = customer_phone;
  6141. contactInfo[@"customer_fax"] = customer_fax;
  6142. contactInfo[@"customer_email"] = customer_email;
  6143. NSString *first_name = @"";
  6144. NSString *last_name = @"";
  6145. if ([customer_contact isEqualToString:@""]) {
  6146. } else if ([customer_contact containsString:@" "]) {
  6147. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6148. first_name = [customer_contact substringToIndex:first_space_index];
  6149. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6150. }
  6151. contactInfo[@"customer_first_name"] = first_name;
  6152. contactInfo[@"customer_last_name"] = last_name;
  6153. ret[@"customerInfo"] = contactInfo;
  6154. // models
  6155. if (nssoid) {
  6156. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6157. __block double TotalCuft = 0;
  6158. __block double TotalWeight = 0;
  6159. __block int TotalCarton = 0;
  6160. __block double allItemPrice = 0;
  6161. 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];
  6162. sqlite3 *db1 = [iSalesDB get_db];
  6163. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6164. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6165. int product_id = sqlite3_column_int(stmt, 0);
  6166. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6167. int item_id = sqlite3_column_int(stmt, 7);
  6168. NSString* Price=nil;
  6169. if(str_price==nil)
  6170. {
  6171. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6172. if(price==nil)
  6173. Price=@"No Price.";
  6174. else
  6175. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6176. }
  6177. else
  6178. {
  6179. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6180. }
  6181. double discount = sqlite3_column_double(stmt, 2);
  6182. int item_count = sqlite3_column_int(stmt, 3);
  6183. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6184. NSString *nsline_note=nil;
  6185. if(line_note!=nil)
  6186. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6187. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6188. // NSString *nsname = nil;
  6189. // if(name!=nil)
  6190. // nsname= [[NSString alloc]initWithUTF8String:name];
  6191. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6192. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6193. // NSString *nsdescription=nil;
  6194. // if(description!=nil)
  6195. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6196. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6197. // int stockUom = sqlite3_column_int(stmt, 8);
  6198. // int _id = sqlite3_column_int(stmt, 9);
  6199. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6200. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6201. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6202. int carton=[bsubtotaljson[@"carton"] intValue];
  6203. TotalCuft += cuft;
  6204. TotalWeight += weight;
  6205. TotalCarton += carton;
  6206. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6207. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6208. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6209. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6210. itemjson[@"note"]=nsline_note;
  6211. itemjson[@"origin_price"] = Price;
  6212. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6213. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6214. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6215. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6216. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6217. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6218. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6219. if(itemjson[@"combine"] != nil)
  6220. {
  6221. // int citem=0;
  6222. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6223. for(int bc=0;bc<bcount;bc++)
  6224. {
  6225. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6226. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6227. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6228. subTotal += uprice * modulus * item_count;
  6229. }
  6230. }
  6231. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6232. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6233. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6234. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6235. itemjson[@"type"] = @"order_item";
  6236. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6237. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6238. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6239. allItemPrice += subTotal;
  6240. }];
  6241. section_3[@"data"] = model_data;
  6242. section_3[@"type"] = @"sub_order";
  6243. section_3[@"title"] = @"Models";
  6244. section_3[@"switch"] = @(false);
  6245. ret[@"section_3"] = section_3;
  6246. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6247. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6248. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6249. // payments/Credits
  6250. // payments_and_credist = sqlite3_column_double(statement, 34);
  6251. payments_and_credist = allItemPrice;
  6252. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6253. NSDictionary *sub_total_item = @{
  6254. @"title":@"Sub-Total",
  6255. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6256. };
  6257. [price_data setObject:sub_total_item forKey:@"item_0"];
  6258. // // total
  6259. // totalPrice = sqlite3_column_double(statement, 35);
  6260. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6261. } else {
  6262. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6263. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6264. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6265. // payments/Credits
  6266. payments_and_credist = 0;
  6267. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6268. NSDictionary *sub_total_item = @{
  6269. @"title":@"Sub-Total",
  6270. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6271. };
  6272. [price_data setObject:sub_total_item forKey:@"item_0"];
  6273. }
  6274. // total
  6275. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6276. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6277. NSDictionary *total_item = @{
  6278. @"title":@"Total",
  6279. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6280. };
  6281. [price_data setObject:total_item forKey:@"item_4"];
  6282. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6283. section_4 = @{
  6284. @"data":price_data,
  6285. @"title":@"Price Info",
  6286. @"type":@"price_info"
  6287. }.mutableCopy;
  6288. ret[@"section_4"] = section_4;
  6289. // ret[@"order_info"]= orderinfo;
  6290. section_0 = @{
  6291. @"data":orderinfo,
  6292. @"title":@"Order Info",
  6293. @"type":@"order_info"
  6294. }.mutableCopy;
  6295. ret[@"section_0"] = section_0;
  6296. // ret[@"more_order_info"] = moreInfo;
  6297. section_2 = @{
  6298. @"data":moreInfo,
  6299. @"title":@"More Info",
  6300. @"type":@"more_order_info"
  6301. }.mutableCopy;
  6302. ret[@"section_2"] = section_2;
  6303. ret[@"count"] = @(5);
  6304. return [RAUtils dict2data:ret];
  6305. }
  6306. #pragma mark order list
  6307. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6308. NSString *nsstatus = @"";
  6309. switch (status) {
  6310. case 0:
  6311. {
  6312. nsstatus=@"Temp Order";
  6313. break;
  6314. }
  6315. case 1:
  6316. {
  6317. nsstatus=@"Saved Order";
  6318. break;
  6319. }
  6320. case 2: {
  6321. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6322. break;
  6323. }
  6324. case 3:
  6325. case 15:
  6326. {
  6327. nsstatus=@"Cancelled";
  6328. break;
  6329. }
  6330. case 10:
  6331. {
  6332. nsstatus=@"Quote Submitted";
  6333. break;
  6334. }
  6335. case 11:
  6336. {
  6337. nsstatus=@"Sales Order Submitted";
  6338. break;
  6339. }
  6340. case 12:
  6341. {
  6342. nsstatus=@"Processing";
  6343. break;
  6344. }
  6345. case 13:
  6346. {
  6347. nsstatus=@"Shipped";
  6348. break;
  6349. }
  6350. case 14:
  6351. {
  6352. nsstatus=@"Closed";
  6353. break;
  6354. }
  6355. case -11:
  6356. {
  6357. nsstatus = @"Ready For Submit";
  6358. break;
  6359. }
  6360. default:
  6361. break;
  6362. }
  6363. return nsstatus;
  6364. }
  6365. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6366. double total = 0;
  6367. __block double payments_and_credist = 0;
  6368. __block double allItemPrice = 0;
  6369. // sqlite3 *db1 = [iSalesDB get_db];
  6370. if (so_id) {
  6371. // 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];
  6372. 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];
  6373. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6374. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6375. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6376. int product_id = sqlite3_column_int(stmt, 0);
  6377. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6378. int discount = sqlite3_column_int(stmt, 2);
  6379. int item_count = sqlite3_column_int(stmt, 3);
  6380. // int item_id = sqlite3_column_int(stmt, 7);
  6381. int item_id = sqlite3_column_int(stmt, 4);
  6382. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6383. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6384. NSString* Price=nil;
  6385. if(str_price==nil)
  6386. {
  6387. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6388. if(price==nil)
  6389. Price=@"No Price.";
  6390. else
  6391. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6392. }
  6393. else
  6394. {
  6395. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6396. }
  6397. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6398. itemjson[@"The unit price"]=Price;
  6399. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6400. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6401. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6402. if(itemjson[@"combine"] != nil)
  6403. {
  6404. // int citem=0;
  6405. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6406. for(int bc=0;bc<bcount;bc++)
  6407. {
  6408. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6409. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6410. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6411. subTotal += uprice * modulus * item_count;
  6412. }
  6413. }
  6414. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6415. allItemPrice += subTotal;
  6416. }];
  6417. payments_and_credist = allItemPrice;
  6418. } else {
  6419. // payments/Credits
  6420. payments_and_credist = 0;
  6421. }
  6422. // lift_gate handlingFee shippingFee
  6423. __block double lift_gate = 0;
  6424. __block double handlingFee = 0;
  6425. __block double shippingFee = 0;
  6426. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6427. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6428. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6429. if (have_lift_gate) {
  6430. lift_gate = sqlite3_column_double(stmt, 1);
  6431. }
  6432. handlingFee = sqlite3_column_double(stmt, 2);
  6433. shippingFee = sqlite3_column_double(stmt, 3);
  6434. }];
  6435. // total
  6436. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6437. if (close) {
  6438. [iSalesDB close_db:db1];
  6439. }
  6440. return total;
  6441. }
  6442. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6443. {
  6444. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6445. int limit = [[params valueForKey:@"limit"] intValue];
  6446. int offset = [[params valueForKey:@"offset"] intValue];
  6447. NSString* keyword = [params valueForKey:@"keyWord"];
  6448. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6449. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6450. NSString* where=@"1 = 1";
  6451. if(keyword.length>0)
  6452. 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]];
  6453. if (orderStatus.length > 0) {
  6454. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6455. if (order_status_array.count == 1) {
  6456. int status_value = [[order_status_array firstObject] integerValue];
  6457. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6458. if (status_value == 15 || status_value == 3) {
  6459. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6460. } else {
  6461. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6462. }
  6463. } else {
  6464. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6465. }
  6466. } else if (order_status_array.count > 1) {
  6467. for (int i = 0; i < order_status_array.count;i++) {
  6468. NSString *status = order_status_array[i];
  6469. NSString *condition = @" or";
  6470. if (i == 0) {
  6471. condition = @" and (";
  6472. }
  6473. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6474. int status_value = [status integerValue];
  6475. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6476. if (status_value == 15 || status_value == 3) {
  6477. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6478. } else {
  6479. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6480. }
  6481. } else {
  6482. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6483. }
  6484. }
  6485. where = [where stringByAppendingString:@" )"];
  6486. }
  6487. }
  6488. 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];
  6489. // DebugLog(@"order list sql: %@",sqlQuery);
  6490. sqlite3 *db = [iSalesDB get_db];
  6491. sqlite3_stmt * statement;
  6492. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6493. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6494. {
  6495. int count=0;
  6496. while (sqlite3_step(statement) == SQLITE_ROW)
  6497. {
  6498. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6499. int order_id = sqlite3_column_double(statement, 0);
  6500. char *soid = (char*)sqlite3_column_text(statement, 1);
  6501. if(soid==nil)
  6502. soid= "";
  6503. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6504. int status = sqlite3_column_double(statement, 2);
  6505. int erpStatus = sqlite3_column_double(statement, 9);
  6506. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6507. if(sales_rep==nil)
  6508. sales_rep= "";
  6509. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6510. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6511. if(create_by==nil)
  6512. create_by= "";
  6513. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6514. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6515. if(company_name==nil)
  6516. company_name= "";
  6517. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6518. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6519. if(create_time==nil)
  6520. create_time= "";
  6521. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6522. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6523. // double total_price = sqlite3_column_double(statement, 7);
  6524. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6525. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6526. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6527. int offline_edit = sqlite3_column_int(statement, 10);
  6528. // ": "JH",
  6529. // "": "$8307.00",
  6530. // "": "MOB1608050001",
  6531. // "": "ArpithaT",
  6532. // "": "1st Stage Property Transformations",
  6533. // "": "JANICE SUTTON",
  6534. // "": 2255,
  6535. // "": "08/02/2016 09:49:18",
  6536. // "": 1,
  6537. // "": "Saved Order"
  6538. // "": "1470384050483",
  6539. // "model_count": "6 / 28"
  6540. item[@"sales_rep"]= nssales_rep;
  6541. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6542. item[@"so#"]= nssoid;
  6543. item[@"create_by"]= nscreate_by;
  6544. item[@"customer_name"]= nscompany_name;
  6545. item[@"customer_contact"] = customer_contact;
  6546. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6547. item[@"purchase_time"]= nscreate_time;
  6548. int statusCode = status;
  6549. if (statusCode == 2) {
  6550. statusCode = erpStatus;
  6551. } else if (statusCode == 3) {
  6552. statusCode = 15;
  6553. }
  6554. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6555. item[@"order_status"]= nsstatus;
  6556. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6557. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6558. // item[@"model_count"]
  6559. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6560. count++;
  6561. }
  6562. ret[@"count"]= [NSNumber numberWithInt:count];
  6563. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6564. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6565. ret[@"result"]= [NSNumber numberWithInt:2];
  6566. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6567. ret[@"time_zone"] = @"PST";
  6568. sqlite3_finalize(statement);
  6569. }
  6570. 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];
  6571. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6572. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6573. [iSalesDB close_db:db];
  6574. return [RAUtils dict2data:ret];
  6575. }
  6576. #pragma mark update gnotes
  6577. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6578. {
  6579. DebugLog(@"params: %@",params);
  6580. // comments = Meyoyoyoyoyoyoy;
  6581. // orderCode = MOB1608110001;
  6582. // password = 123456;
  6583. // user = EvanK;
  6584. 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]];
  6585. int ret = [iSalesDB execSql:sql];
  6586. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6587. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6588. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6589. // [dic setValue:@"160409" forKey:@"min_ver"];
  6590. return [RAUtils dict2data:dic];
  6591. }
  6592. #pragma mark move to wishlist
  6593. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6594. {
  6595. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6596. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6597. _id = [NSString stringWithFormat:@"(%@)",_id];
  6598. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6599. sqlite3 *db = [iSalesDB get_db];
  6600. __block NSString *product_id = @"";
  6601. __block NSString *item_count_str = @"";
  6602. // __block NSString *item_id = nil;
  6603. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6604. // product_id = [self textAtColumn:0 statement:stmt];
  6605. int item_count = sqlite3_column_int(stmt, 1);
  6606. // item_id = [self textAtColumn:2 statement:stmt];
  6607. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6608. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6609. if (p_id.length) {
  6610. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6611. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6612. }
  6613. }];
  6614. [iSalesDB close_db:db];
  6615. // 去除第一个,
  6616. if (product_id.length > 1) {
  6617. product_id = [product_id substringFromIndex:1];
  6618. }
  6619. if (item_count_str.length > 1) {
  6620. item_count_str = [item_count_str substringFromIndex:1];
  6621. }
  6622. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6623. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6624. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6625. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6626. sqlite3 *db1 = [iSalesDB get_db];
  6627. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6628. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6629. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6630. // 删除
  6631. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6632. int ret = [iSalesDB execSql:deleteSql db:db1];
  6633. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6634. [iSalesDB close_db:db1];
  6635. return [RAUtils dict2data:dic];
  6636. }
  6637. #pragma mark cart delete
  6638. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6639. {
  6640. // cartItemId = 548;
  6641. // orderCode = MOB1608110001;
  6642. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6643. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6644. _id = [NSString stringWithFormat:@"(%@)",_id];
  6645. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6646. int ret = [iSalesDB execSql:sql];
  6647. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6648. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6649. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6650. // [dic setValue:@"160409" forKey:@"min_ver"];
  6651. return [RAUtils dict2data:dic];
  6652. }
  6653. #pragma mark set price
  6654. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6655. {
  6656. DebugLog(@"cart set price params: %@",params);
  6657. // "cartitem_id" = 1;
  6658. // discount = "0.000000";
  6659. // "item_note" = "";
  6660. // price = "269.000000";
  6661. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6662. NSString *notes = [self valueInParams:params key:@"item_note"];
  6663. NSString *discount = [self valueInParams:params key:@"discount"];
  6664. NSString *price = [self valueInParams:params key:@"price"];
  6665. // bool badd_price_changed=false;
  6666. // sqlite3* db=[iSalesDB get_db];
  6667. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6668. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6669. // NSRange range;
  6670. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6671. //
  6672. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6673. // badd_price_changed=true;
  6674. // [iSalesDB close_db:db];
  6675. //
  6676. // if(badd_price_changed)
  6677. // {
  6678. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6679. // }
  6680. //
  6681. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6682. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6683. int ret = [iSalesDB execSql:sql];
  6684. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6685. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6686. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6687. // [dic setValue:@"160409" forKey:@"min_ver"];
  6688. return [RAUtils dict2data:dic];
  6689. }
  6690. #pragma mark set line notes
  6691. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6692. {
  6693. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6694. NSString *notes = [self valueInParams:params key:@"notes"];
  6695. notes = [self translateSingleQuote:notes];
  6696. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6697. int ret = [iSalesDB execSql:sql];
  6698. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6699. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6700. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6701. // [dic setValue:@"160409" forKey:@"min_ver"];
  6702. return [RAUtils dict2data:dic];
  6703. }
  6704. #pragma mark set qty
  6705. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6706. {
  6707. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6708. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6709. int item_count = [params[@"inputInt"] intValue];
  6710. // 购买检查数量大于库存
  6711. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6712. if (!appDelegate.can_create_backorder) {
  6713. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6714. __block BOOL out_of_stock = NO;
  6715. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6716. int availability = sqlite3_column_int(stmt, 0);
  6717. if (availability < item_count) {
  6718. out_of_stock = YES;
  6719. }
  6720. }];
  6721. if (out_of_stock) { // 缺货
  6722. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6723. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6724. return [RAUtils dict2data:dic];
  6725. }
  6726. }
  6727. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6728. int ret = [iSalesDB execSql:sql];
  6729. __block int item_id = 0;
  6730. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6731. item_id = sqlite3_column_int(stmt, 0);
  6732. }];
  6733. sqlite3 *db = [iSalesDB get_db];
  6734. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6735. [iSalesDB close_db:db];
  6736. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6737. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6738. // [dic setValue:@"160409" forKey:@"min_ver"];
  6739. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6740. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6741. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6742. return [RAUtils dict2data:dic];
  6743. }
  6744. #pragma mark place order
  6745. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6746. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6747. if (aname) {
  6748. [dic setValue:aname forKey:@"aname"];
  6749. }
  6750. if (control) {
  6751. [dic setValue:control forKey:@"control"];
  6752. }
  6753. if (keyboard) {
  6754. [dic setValue:keyboard forKey:@"keyboard"];
  6755. }
  6756. if (name) {
  6757. [dic setValue:name forKey:@"name"];
  6758. }
  6759. if (value) {
  6760. [dic setValue:value forKey:@"value"];
  6761. }
  6762. return dic;
  6763. }
  6764. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6765. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6766. orderCode = [self translateSingleQuote:orderCode];
  6767. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6768. 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];
  6769. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6770. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6771. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6772. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6773. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6774. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6775. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6776. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6777. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6778. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6779. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6780. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6781. NSString *email = [self textAtColumn:11 statement:stmt];
  6782. NSString *phone = [self textAtColumn:12 statement:stmt];
  6783. NSString *fax = [self textAtColumn:13 statement:stmt];
  6784. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6785. NSString *city = [self textAtColumn:15 statement:stmt];
  6786. NSString *state = [self textAtColumn:16 statement:stmt];
  6787. NSString *country = [self textAtColumn:17 statement:stmt];
  6788. NSString *name = [self textAtColumn:18 statement:stmt];
  6789. // contact id
  6790. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6791. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6792. [contact_id_dic setValue:@"text" forKey:@"control"];
  6793. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6794. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6795. [contact_id_dic setValue:@"true" forKey:@"required"];
  6796. [contact_id_dic setValue:contact_id forKey:@"value"];
  6797. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6798. // business card
  6799. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6800. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6801. [business_card_dic setValue:@"img" forKey:@"control"];
  6802. [business_card_dic setValue:@"1" forKey:@"disable"];
  6803. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6804. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6805. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6806. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6807. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6808. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6809. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6810. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6811. // fax
  6812. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6813. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6814. // zipcode
  6815. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6816. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6817. // city
  6818. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6819. [customer_dic setValue:city_dic forKey:@"item_12"];
  6820. // state
  6821. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6822. [customer_dic setValue:state_dic forKey:@"item_13"];
  6823. // country
  6824. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6825. [customer_dic setValue:country_dic forKey:@"item_14"];
  6826. // company name
  6827. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6828. [customer_dic setValue:company_dic forKey:@"item_2"];
  6829. // addr_1
  6830. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6831. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6832. // addr_2
  6833. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6834. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6835. // addr_3
  6836. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6837. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6838. // addr_4
  6839. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6840. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6841. // Contact
  6842. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6843. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6844. // email
  6845. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6846. [customer_dic setValue:email_dic forKey:@"item_8"];
  6847. // phone
  6848. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6849. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6850. // title
  6851. [customer_dic setValue:@"Customer" forKey:@"title"];
  6852. // count
  6853. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6854. }];
  6855. // setting
  6856. NSDictionary *setting = params[@"setting"];
  6857. NSNumber *hide = setting[@"CustomerHide"];
  6858. [customer_dic setValue:hide forKey:@"hide"];
  6859. return customer_dic;
  6860. }
  6861. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6862. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6863. // setting
  6864. NSDictionary *setting = params[@"setting"];
  6865. NSNumber *hide = setting[@"ShipToHide"];
  6866. [dic setValue:hide forKey:@"hide"];
  6867. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6868. orderCode = [self translateSingleQuote:orderCode];
  6869. 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];
  6870. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6871. NSString *cid = [self textAtColumn:0 statement:stmt];
  6872. NSString *name = [self textAtColumn:1 statement:stmt];
  6873. NSString *ext = [self textAtColumn:2 statement:stmt];
  6874. NSString *contact = [self textAtColumn:3 statement:stmt];
  6875. NSString *email = [self textAtColumn:4 statement:stmt];
  6876. NSString *fax = [self textAtColumn:5 statement:stmt];
  6877. NSString *phone = [self textAtColumn:6 statement:stmt];
  6878. // count
  6879. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6880. // title
  6881. [dic setValue:@"Ship To" forKey:@"title"];
  6882. // choose
  6883. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6884. [choose_dic setValue:@"choose" forKey:@"aname"];
  6885. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6886. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6887. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6888. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6889. @"receive_contact" : @"customer_contact",
  6890. @"receive_email" : @"customer_email",
  6891. @"receive_ext" : @"customer_contact_ext",
  6892. @"receive_fax" : @"customer_fax",
  6893. @"receive_name" : @"customer_name",
  6894. @"receive_phone" : @"customer_phone"},
  6895. @"refresh" : [NSNumber numberWithInteger:1],
  6896. @"type" : @"pull"};
  6897. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6898. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6899. @"name" : @"Add new address",
  6900. @"refresh" : [NSNumber numberWithInteger:1],
  6901. @"value" : @"new_addr"
  6902. };
  6903. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6904. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6905. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6906. @"receive_contact" : @"customer_contact",
  6907. @"receive_email" : @"customer_email",
  6908. @"receive_ext" : @"customer_contact_ext",
  6909. @"receive_fax" : @"customer_fax",
  6910. @"receive_name" : @"customer_name",
  6911. @"receive_phone" : @"customer_phone"},
  6912. @"name" : @"select_ship_to",
  6913. @"refresh" : [NSNumber numberWithInteger:1],
  6914. @"value" : @"Sales_Order_Ship_To"};
  6915. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6916. [dic setValue:choose_dic forKey:@"item_0"];
  6917. // contact id
  6918. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6919. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6920. [contact_id_dic setValue:@"true" forKey:@"required"];
  6921. [dic setValue:contact_id_dic forKey:@"item_1"];
  6922. // company name
  6923. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6924. [dic setValue:company_name_dic forKey:@"item_2"];
  6925. // address
  6926. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6927. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6928. if ([required integerValue]) {
  6929. [ext_dic setValue:@"true" forKey:@"required"];
  6930. }
  6931. [dic setValue:ext_dic forKey:@"item_3"];
  6932. // contact
  6933. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6934. [dic setValue:contact_dic forKey:@"item_4"];
  6935. // phone
  6936. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6937. [dic setValue:phone_dic forKey:@"item_5"];
  6938. // fax
  6939. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6940. [dic setValue:fax_dic forKey:@"item_6"];
  6941. // email
  6942. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6943. [dic setValue:email_dic forKey:@"item_7"];
  6944. }];
  6945. return dic;
  6946. }
  6947. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6948. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6949. // setting
  6950. NSDictionary *setting = params[@"setting"];
  6951. NSNumber *hide = setting[@"ShipFromHide"];
  6952. [dic setValue:hide forKey:@"hide"];
  6953. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6954. orderCode = [self translateSingleQuote:orderCode];
  6955. 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];
  6956. 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';";
  6957. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6958. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6959. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6960. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6961. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6962. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6963. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6964. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6965. if (!cid.length) {
  6966. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6967. cid = [self textAtColumn:0 statement:statment];
  6968. name = [self textAtColumn:1 statement:statment];
  6969. ext = [self textAtColumn:2 statement:statment];
  6970. contact = [self textAtColumn:3 statement:statment];
  6971. email = [self textAtColumn:4 statement:statment];
  6972. fax = [self textAtColumn:5 statement:statment];
  6973. phone = [self textAtColumn:6 statement:statment];
  6974. }];
  6975. }
  6976. // count
  6977. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6978. // title
  6979. [dic setValue:@"Ship From" forKey:@"title"];
  6980. // hide
  6981. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6982. // choose
  6983. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6984. [choose_dic setValue:@"choose" forKey:@"aname"];
  6985. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6986. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6987. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6988. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6989. @"sender_contact" : @"customer_contact",
  6990. @"sender_email" : @"customer_email",
  6991. @"sender_ext" : @"customer_contact_ext",
  6992. @"sender_fax" : @"customer_fax",
  6993. @"sender_name" : @"customer_name",
  6994. @"sender_phone" : @"customer_phone"},
  6995. @"name" : @"select_cid",
  6996. @"refresh" : [NSNumber numberWithInteger:0],
  6997. @"value" : @"Sales_Order_Ship_From"};
  6998. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6999. [dic setValue:choose_dic forKey:@"item_0"];
  7000. // contact id
  7001. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7002. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7003. [contact_id_dic setValue:@"true" forKey:@"required"];
  7004. [dic setValue:contact_id_dic forKey:@"item_1"];
  7005. // company name
  7006. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7007. [dic setValue:company_name_dic forKey:@"item_2"];
  7008. // address
  7009. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7010. [dic setValue:ext_dic forKey:@"item_3"];
  7011. // contact
  7012. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7013. [dic setValue:contact_dic forKey:@"item_4"];
  7014. // phone
  7015. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7016. [dic setValue:phone_dic forKey:@"item_5"];
  7017. // fax
  7018. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7019. [dic setValue:fax_dic forKey:@"item_6"];
  7020. // email
  7021. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7022. [dic setValue:email_dic forKey:@"item_7"];
  7023. }];
  7024. return dic;
  7025. }
  7026. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7027. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7028. // setting
  7029. NSDictionary *setting = params[@"setting"];
  7030. NSNumber *hide = setting[@"FreightBillToHide"];
  7031. [dic setValue:hide forKey:@"hide"];
  7032. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7033. orderCode = [self translateSingleQuote:orderCode];
  7034. 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];
  7035. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7036. NSString *cid = [self textAtColumn:0 statement:stmt];
  7037. NSString *name = [self textAtColumn:1 statement:stmt];
  7038. NSString *ext = [self textAtColumn:2 statement:stmt];
  7039. NSString *contact = [self textAtColumn:3 statement:stmt];
  7040. NSString *email = [self textAtColumn:4 statement:stmt];
  7041. NSString *fax = [self textAtColumn:5 statement:stmt];
  7042. NSString *phone = [self textAtColumn:6 statement:stmt];
  7043. // count
  7044. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7045. // title
  7046. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7047. // hide
  7048. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7049. // choose
  7050. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7051. [choose_dic setValue:@"choose" forKey:@"aname"];
  7052. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7053. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7054. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7055. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7056. @"shipping_billto_contact" : @"receive_contact",
  7057. @"shipping_billto_email" : @"receive_email",
  7058. @"shipping_billto_ext" : @"receive_ext",
  7059. @"shipping_billto_fax" : @"receive_fax",
  7060. @"shipping_billto_name" : @"receive_name",
  7061. @"shipping_billto_phone" : @"receive_phone"},
  7062. @"type" : @"pull"};
  7063. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7064. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7065. @"type" : @"pull",
  7066. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7067. @"shipping_billto_contact" : @"customer_contact",
  7068. @"shipping_billto_email" : @"customer_email",
  7069. @"shipping_billto_ext" : @"customer_contact_ext",
  7070. @"shipping_billto_fax" : @"customer_fax",
  7071. @"shipping_billto_name" : @"customer_name",
  7072. @"shipping_billto_phone" : @"customer_phone"}
  7073. };
  7074. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7075. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7076. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7077. @"shipping_billto_contact" : @"sender_contact",
  7078. @"shipping_billto_email" : @"sender_email",
  7079. @"shipping_billto_ext" : @"sender_ext",
  7080. @"shipping_billto_fax" : @"sender_fax",
  7081. @"shipping_billto_name" : @"sender_name",
  7082. @"shipping_billto_phone" : @"sender_phone"},
  7083. @"type" : @"pull"
  7084. };
  7085. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7086. NSDictionary *select_freight_bill_to_dic = @{
  7087. @"aname" : @"Select freight bill to",
  7088. @"name" : @"select_cid",
  7089. @"refresh" : [NSNumber numberWithInteger:0],
  7090. @"value" : @"Sales_Order_Freight_Bill_To",
  7091. @"key_map" : @{
  7092. @"shipping_billto_cid" : @"customer_cid",
  7093. @"shipping_billto_contact" : @"customer_contact",
  7094. @"shipping_billto_email" : @"customer_email",
  7095. @"shipping_billto_ext" : @"customer_contact_ext",
  7096. @"shipping_billto_fax" : @"customer_fax",
  7097. @"shipping_billto_name" : @"customer_name",
  7098. @"shipping_billto_phone" : @"customer_phone"
  7099. }
  7100. };
  7101. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7102. [dic setValue:choose_dic forKey:@"item_0"];
  7103. // contact id
  7104. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7105. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7106. [contact_id_dic setValue:@"true" forKey:@"required"];
  7107. [dic setValue:contact_id_dic forKey:@"item_1"];
  7108. // company name
  7109. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7110. [dic setValue:company_name_dic forKey:@"item_2"];
  7111. // address
  7112. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7113. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7114. if ([ext_required integerValue]) {
  7115. [ext_dic setValue:@"true" forKey:@"required"];
  7116. }
  7117. [dic setValue:ext_dic forKey:@"item_3"];
  7118. // contact
  7119. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7120. [dic setValue:contact_dic forKey:@"item_4"];
  7121. // phone
  7122. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7123. [dic setValue:phone_dic forKey:@"item_5"];
  7124. // fax
  7125. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7126. [dic setValue:fax_dic forKey:@"item_6"];
  7127. // email
  7128. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7129. [dic setValue:email_dic forKey:@"item_7"];
  7130. }];
  7131. return dic;
  7132. }
  7133. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7134. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7135. // setting
  7136. NSDictionary *setting = params[@"setting"];
  7137. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7138. [dic setValue:hide forKey:@"hide"];
  7139. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7140. orderCode = [self translateSingleQuote:orderCode];
  7141. 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];
  7142. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7143. NSString *cid = [self textAtColumn:0 statement:stmt];
  7144. NSString *name = [self textAtColumn:1 statement:stmt];
  7145. NSString *ext = [self textAtColumn:2 statement:stmt];
  7146. NSString *contact = [self textAtColumn:3 statement:stmt];
  7147. NSString *email = [self textAtColumn:4 statement:stmt];
  7148. NSString *fax = [self textAtColumn:5 statement:stmt];
  7149. NSString *phone = [self textAtColumn:6 statement:stmt];
  7150. // count
  7151. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7152. // title
  7153. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7154. // hide
  7155. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7156. // choose
  7157. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7158. [choose_dic setValue:@"choose" forKey:@"aname"];
  7159. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7160. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7161. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7162. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7163. @"billing_contact" : @"receive_contact",
  7164. @"billing_email" : @"receive_email",
  7165. @"billing_ext" : @"receive_ext",
  7166. @"billing_fax" : @"receive_fax",
  7167. @"billing_name" : @"receive_name",
  7168. @"billing_phone" : @"receive_phone"},
  7169. @"type" : @"pull"};
  7170. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7171. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7172. @"type" : @"pull",
  7173. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7174. @"billing_contact" : @"customer_contact",
  7175. @"billing_email" : @"customer_email",
  7176. @"billing_ext" : @"customer_contact_ext",
  7177. @"billing_fax" : @"customer_fax",
  7178. @"billing_name" : @"customer_name",
  7179. @"billing_phone" : @"customer_phone"}
  7180. };
  7181. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7182. NSDictionary *select_bill_to_dic = @{
  7183. @"aname" : @"Select bill to",
  7184. @"name" : @"select_cid",
  7185. @"refresh" : [NSNumber numberWithInteger:0],
  7186. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7187. @"key_map" : @{
  7188. @"billing_cid" : @"customer_cid",
  7189. @"billing_contact" : @"customer_contact",
  7190. @"billing_email" : @"customer_email",
  7191. @"billing_ext" : @"customer_contact_ext",
  7192. @"billing_fax" : @"customer_fax",
  7193. @"billing_name" : @"customer_name",
  7194. @"billing_phone" : @"customer_phone"
  7195. }
  7196. };
  7197. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7198. [dic setValue:choose_dic forKey:@"item_0"];
  7199. // contact id
  7200. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7201. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7202. [contact_id_dic setValue:@"true" forKey:@"required"];
  7203. [dic setValue:contact_id_dic forKey:@"item_1"];
  7204. // company name
  7205. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7206. [dic setValue:company_name_dic forKey:@"item_2"];
  7207. // address
  7208. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7209. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7210. if ([ext_required integerValue]) {
  7211. [ext_dic setValue:@"true" forKey:@"required"];
  7212. }
  7213. [dic setValue:ext_dic forKey:@"item_3"];
  7214. // contact
  7215. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7216. [dic setValue:contact_dic forKey:@"item_4"];
  7217. // phone
  7218. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7219. [dic setValue:phone_dic forKey:@"item_5"];
  7220. // fax
  7221. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7222. [dic setValue:fax_dic forKey:@"item_6"];
  7223. // email
  7224. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7225. [dic setValue:email_dic forKey:@"item_7"];
  7226. }];
  7227. return dic;
  7228. }
  7229. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7230. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7231. // setting
  7232. NSDictionary *setting = params[@"setting"];
  7233. NSNumber *hide = setting[@"ReturnToHide"];
  7234. [dic setValue:hide forKey:@"hide"];
  7235. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7236. orderCode = [self translateSingleQuote:orderCode];
  7237. 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];
  7238. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7239. NSString *cid = [self textAtColumn:0 statement:stmt];
  7240. NSString *name = [self textAtColumn:1 statement:stmt];
  7241. NSString *ext = [self textAtColumn:2 statement:stmt];
  7242. NSString *contact = [self textAtColumn:3 statement:stmt];
  7243. NSString *email = [self textAtColumn:4 statement:stmt];
  7244. NSString *fax = [self textAtColumn:5 statement:stmt];
  7245. NSString *phone = [self textAtColumn:6 statement:stmt];
  7246. // count
  7247. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7248. // title
  7249. [dic setValue:@"Return To" forKey:@"title"];
  7250. // hide
  7251. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7252. // choose
  7253. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7254. [choose_dic setValue:@"choose" forKey:@"aname"];
  7255. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7256. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7257. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7258. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7259. @"returnto_contact" : @"sender_contact",
  7260. @"returnto_email" : @"sender_email",
  7261. @"returnto_ext" : @"sender_ext",
  7262. @"returnto_fax" : @"sender_fax",
  7263. @"returnto_name" : @"sender_name",
  7264. @"returnto_phone" : @"sender_phone"},
  7265. @"type" : @"pull"};
  7266. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7267. NSDictionary *select_return_to_dic = @{
  7268. @"aname" : @"Select return to",
  7269. @"name" : @"select_cid",
  7270. @"refresh" : [NSNumber numberWithInteger:0],
  7271. @"value" : @"Contact_Return_To",
  7272. @"key_map" : @{
  7273. @"returnto_cid" : @"customer_cid",
  7274. @"returnto_contact" : @"customer_contact",
  7275. @"returnto_email" : @"customer_email",
  7276. @"returnto_ext" : @"customer_contact_ext",
  7277. @"returnto_fax" : @"customer_fax",
  7278. @"returnto_name" : @"customer_name",
  7279. @"returnto_phone" : @"customer_phone"
  7280. }
  7281. };
  7282. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7283. [dic setValue:choose_dic forKey:@"item_0"];
  7284. // contact id
  7285. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7286. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7287. [contact_id_dic setValue:@"true" forKey:@"required"];
  7288. [dic setValue:contact_id_dic forKey:@"item_1"];
  7289. // company name
  7290. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7291. [dic setValue:company_name_dic forKey:@"item_2"];
  7292. // address
  7293. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7294. [dic setValue:ext_dic forKey:@"item_3"];
  7295. // contact
  7296. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7297. [dic setValue:contact_dic forKey:@"item_4"];
  7298. // phone
  7299. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7300. [dic setValue:phone_dic forKey:@"item_5"];
  7301. // fax
  7302. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7303. [dic setValue:fax_dic forKey:@"item_6"];
  7304. // email
  7305. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7306. [dic setValue:email_dic forKey:@"item_7"];
  7307. }];
  7308. return dic;
  7309. }
  7310. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7311. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7312. orderCode = [self translateSingleQuote:orderCode];
  7313. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7314. __block double TotalCuft = 0;
  7315. __block double TotalWeight = 0;
  7316. __block int TotalCarton = 0;
  7317. __block double payments = 0;
  7318. // setting
  7319. NSDictionary *setting = params[@"setting"];
  7320. NSNumber *hide = setting[@"ModelInformationHide"];
  7321. [dic setValue:hide forKey:@"hide"];
  7322. 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];
  7323. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7324. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7325. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7326. // item id
  7327. int item_id = sqlite3_column_int(stmt, 0);
  7328. // count
  7329. int item_count = sqlite3_column_int(stmt, 1);
  7330. // stockUom
  7331. int stockUom = sqlite3_column_int(stmt, 2);
  7332. // unit price
  7333. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7334. NSString* Price=nil;
  7335. if([str_price isEqualToString:@""])
  7336. {
  7337. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7338. if(price==nil)
  7339. Price=@"No Price.";
  7340. else
  7341. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7342. }
  7343. else
  7344. {
  7345. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7346. }
  7347. // discount
  7348. double discount = sqlite3_column_double(stmt, 4);
  7349. // name
  7350. NSString *name = [self textAtColumn:5 statement:stmt];
  7351. // description
  7352. NSString *description = [self textAtColumn:6 statement:stmt];
  7353. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7354. // line note
  7355. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7356. int avaulability = sqlite3_column_int(stmt, 8);
  7357. // img
  7358. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7359. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7360. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7361. if(combine != nil)
  7362. {
  7363. // int citem=0;
  7364. int bcount=[[combine valueForKey:@"count"] intValue];
  7365. for(int bc=0;bc<bcount;bc++)
  7366. {
  7367. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7368. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7369. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7370. subTotal += uprice * modulus * item_count;
  7371. }
  7372. }
  7373. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7374. [model_dic setValue:@"model" forKey:@"control"];
  7375. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7376. [model_dic setValue:description forKey:@"description"];
  7377. [model_dic setValue:line_note forKey:@"note"];
  7378. [model_dic setValue:img forKey:@"img_url"];
  7379. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7380. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7381. [model_dic setValue:Price forKey:@"unit_price"];
  7382. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7383. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7384. if (combine) {
  7385. [model_dic setValue:combine forKey:@"combine"];
  7386. }
  7387. // well,what under the row is the info for total
  7388. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7389. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7390. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7391. int carton=[bsubtotaljson[@"carton"] intValue];
  7392. TotalCuft += cuft;
  7393. TotalWeight += weight;
  7394. TotalCarton += carton;
  7395. payments += subTotal;
  7396. //---------------------------
  7397. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7398. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7399. }];
  7400. [dic setValue:@"Model Information" forKey:@"title"];
  7401. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7402. return dic;
  7403. }
  7404. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7405. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7406. [dic setValue:@"Remarks Content" forKey:@"title"];
  7407. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7408. // setting
  7409. NSDictionary *setting = params[@"setting"];
  7410. NSNumber *hide = setting[@"RemarksContentHide"];
  7411. [dic setValue:hide forKey:@"hide"];
  7412. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7413. orderCode = [self translateSingleQuote:orderCode];
  7414. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7415. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7416. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7417. int mustCall = sqlite3_column_int(stmt, 1);
  7418. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7419. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7420. NSDictionary *po_dic = @{
  7421. @"aname" : @"PO#",
  7422. @"control" : @"edit",
  7423. @"keyboard" : @"text",
  7424. @"name" : @"poNumber",
  7425. @"value" : poNumber
  7426. };
  7427. NSDictionary *must_call_dic = @{
  7428. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7429. @"control" : @"switch",
  7430. @"name" : @"must_call",
  7431. @"value" : mustCall ? @"true" : @"false"
  7432. };
  7433. NSDictionary *general_notes_dic = @{
  7434. @"aname" : @"General notes",
  7435. @"control" : @"text_view",
  7436. @"keyboard" : @"text",
  7437. @"name" : @"comments",
  7438. @"value" : generalNotes
  7439. };
  7440. NSDictionary *internal_notes_dic = @{
  7441. @"aname" : @"Internal notes",
  7442. @"control" : @"text_view",
  7443. @"keyboard" : @"text",
  7444. @"name" : @"internal_notes",
  7445. @"value" : internalNotes
  7446. };
  7447. [dic setValue:po_dic forKey:@"item_0"];
  7448. [dic setValue:must_call_dic forKey:@"item_1"];
  7449. [dic setValue:general_notes_dic forKey:@"item_2"];
  7450. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7451. }];
  7452. return dic;
  7453. }
  7454. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7455. // params
  7456. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7457. orderCode = [self translateSingleQuote:orderCode];
  7458. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7459. // setting
  7460. NSDictionary *setting = params[@"setting"];
  7461. NSNumber *hide = setting[@"OrderTotalHide"];
  7462. [dic setValue:hide forKey:@"hide"];
  7463. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7464. __block double lift_gate_value = 0;
  7465. __block double handling_fee = 0;
  7466. __block double shipping = 0;
  7467. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7468. int lift_gate = sqlite3_column_int(stmt, 0);
  7469. lift_gate_value = sqlite3_column_double(stmt, 1);
  7470. shipping = sqlite3_column_double(stmt, 2);
  7471. handling_fee = sqlite3_column_double(stmt, 3);
  7472. if (!lift_gate) {
  7473. lift_gate_value = 0;
  7474. }
  7475. }];
  7476. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7477. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7478. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7479. double payments = [[total valueForKey:@"payments"] doubleValue];
  7480. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7481. double totalPrice = payments;
  7482. [dic setValue:@"Order Total" forKey:@"title"];
  7483. NSDictionary *payments_dic = @{
  7484. @"align" : @"right",
  7485. @"aname" : @"Payments/Credits",
  7486. @"control" : @"text",
  7487. @"name" : @"paymentsAndCredits",
  7488. @"type" : @"price",
  7489. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7490. };
  7491. [dic setValue:payments_dic forKey:@"item_0"];
  7492. // version 1.71 remove
  7493. // NSDictionary *handling_fee_dic = @{
  7494. // @"align" : @"right",
  7495. // @"aname" : @"Handling Fee",
  7496. // @"control" : @"text",
  7497. // @"name" : @"handling_fee_value",
  7498. // @"required" : @"true",
  7499. // @"type" : @"price",
  7500. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7501. // };
  7502. NSDictionary *shipping_dic = @{
  7503. @"align" : @"right",
  7504. @"aname" : @"Shipping*",
  7505. @"control" : @"text",
  7506. @"name" : @"shipping",
  7507. @"required" : @"true",
  7508. @"type" : @"price",
  7509. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7510. };
  7511. NSDictionary *lift_gate_dic = @{
  7512. @"align" : @"right",
  7513. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7514. @"control" : @"text",
  7515. @"name" : @"lift_gate_value",
  7516. @"required" : @"true",
  7517. @"type" : @"price",
  7518. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7519. };
  7520. int item_count = 1;
  7521. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7522. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7523. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7524. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7525. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7526. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7527. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7528. } else {
  7529. }
  7530. }
  7531. // version 1.71 remove
  7532. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7533. NSDictionary *total_price_dic = @{
  7534. @"align" : @"right",
  7535. @"aname" : @"Total",
  7536. @"control" : @"text",
  7537. @"name" : @"totalPrice",
  7538. @"type" : @"price",
  7539. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7540. };
  7541. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7542. NSDictionary *total_cuft_dic = @{
  7543. @"align" : @"right",
  7544. @"aname" : @"Total Cuft",
  7545. @"control" : @"text",
  7546. @"name" : @"",
  7547. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7548. };
  7549. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7550. NSDictionary *total_weight_dic = @{
  7551. @"align" : @"right",
  7552. @"aname" : @"Total Weight",
  7553. @"control" : @"text",
  7554. @"name" : @"",
  7555. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7556. };
  7557. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7558. NSDictionary *total_carton_dic = @{
  7559. @"align" : @"right",
  7560. @"aname" : @"Total Carton",
  7561. @"control" : @"text",
  7562. @"name" : @"",
  7563. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7564. };
  7565. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7566. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7567. return dic;
  7568. }
  7569. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7570. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7571. orderCode = [self translateSingleQuote:orderCode];
  7572. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7573. // setting
  7574. NSDictionary *setting = params[@"setting"];
  7575. NSNumber *hide = setting[@"SignatureHide"];
  7576. [dic setValue:hide forKey:@"hide"];
  7577. [dic setValue:@"Signature" forKey:@"title"];
  7578. __block NSString *pic_path = @"";
  7579. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7580. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7581. pic_path = [self textAtColumn:0 statement:stmt];
  7582. }];
  7583. NSDictionary *pic_dic = @{
  7584. @"aname" : @"Signature",
  7585. @"avalue" :pic_path,
  7586. @"control" : @"signature",
  7587. @"name" : @"sign_picpath",
  7588. @"value" : pic_path
  7589. };
  7590. [dic setValue:pic_dic forKey:@"item_0"];
  7591. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7592. return dic;
  7593. }
  7594. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7595. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7596. // setting
  7597. NSDictionary *setting = params[@"setting"];
  7598. NSNumber *hide = setting[@"ShippingMethodHide"];
  7599. [dic setValue:hide forKey:@"hide"];
  7600. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7601. orderCode = [self translateSingleQuote:orderCode];
  7602. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7603. __block NSString *logist = @"";
  7604. __block NSString *lift_gate = @"";
  7605. __block int lift_gate_integer = 0;
  7606. __block NSString *logistic_note = @"";
  7607. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7608. logist = [self textAtColumn:0 statement:stmt];
  7609. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7610. logistic_note = [self textAtColumn:2 statement:stmt];
  7611. }];
  7612. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7613. [dic setValue:@"Shipping Method" forKey:@"title"];
  7614. // val_0
  7615. int PERSONAL_PICK_UP_check = 0;
  7616. int USE_MY_CARRIER_check = 0;
  7617. NSString *logistic_note_text = @"";
  7618. int will_call_check = 0;
  7619. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7620. will_call_check = 1;
  7621. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7622. PERSONAL_PICK_UP_check = 1;
  7623. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7624. USE_MY_CARRIER_check = 1;
  7625. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7626. logistic_note = [logistic_note_array firstObject];
  7627. if (logistic_note_array.count > 1) {
  7628. logistic_note_text = [logistic_note_array lastObject];
  7629. }
  7630. }
  7631. }
  7632. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7633. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7634. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7635. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7636. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7637. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7638. NSDictionary *val0_subItem_item0 = @{
  7639. @"aname" : @"Option",
  7640. @"cadedate" : @{
  7641. @"count" : [NSNumber numberWithInteger:2],
  7642. @"val_0" : @{
  7643. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7644. @"refresh" : [NSNumber numberWithInteger:0],
  7645. @"value" : @"PERSONAL PICK UP",
  7646. @"value_id" : @"PERSONAL PICK UP"
  7647. },
  7648. @"val_1" : @{
  7649. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7650. @"refresh" : [NSNumber numberWithInteger:0],
  7651. @"sub_item" : @{
  7652. @"count" : [NSNumber numberWithInteger:1],
  7653. @"item_0" : @{
  7654. @"aname" : @"BOL",
  7655. @"control" : @"edit",
  7656. @"keyboard" : @"text",
  7657. @"name" : @"logist_note_text",
  7658. @"refresh" : [NSNumber numberWithInteger:0],
  7659. @"required" : @"false",
  7660. @"value" : logistic_note_text
  7661. }
  7662. },
  7663. @"value" : @"USE MY CARRIER",
  7664. @"value_id" : @"USE MY CARRIER"
  7665. }
  7666. },
  7667. @"control" : @"enum",
  7668. @"name" : @"logistic_note",
  7669. @"required" : @"true",
  7670. @"single_select" : @"true"
  7671. };
  7672. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7673. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7674. // val_1
  7675. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7676. if([lift_gate isEqualToString:@""]) {
  7677. if (lift_gate_integer == 1) {
  7678. lift_gate = @"true";
  7679. } else {
  7680. lift_gate = @"false";
  7681. }
  7682. }
  7683. int common_carrier_check = 0;
  7684. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7685. common_carrier_check = 1;
  7686. [params setValue:lift_gate forKey:@"lift_gate"];
  7687. }
  7688. NSDictionary *val_1 = @{
  7689. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7690. @"sub_item" : @{
  7691. @"count" : [NSNumber numberWithInteger:1],
  7692. @"item_0" : @{
  7693. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7694. @"control" : @"switch",
  7695. @"name" : @"lift_gate",
  7696. @"refresh" : [NSNumber numberWithInteger:1],
  7697. @"required" : @"true",
  7698. @"value" : lift_gate
  7699. }
  7700. },
  7701. @"value" : @"COMMON CARRIER",
  7702. @"value_id" : @"COMMON CARRIER"
  7703. };
  7704. // cadedate
  7705. NSDictionary *cadedate = @{
  7706. @"count" : [NSNumber numberWithInteger:2],
  7707. @"val_0" : val_0,
  7708. @"val_1" : val_1
  7709. };
  7710. // item_0
  7711. NSMutableDictionary *item_0 = @{
  7712. @"aname" : @"Shipping",
  7713. @"cadedate" : cadedate,
  7714. @"control" : @"enum",
  7715. @"name" : @"logist",
  7716. @"refresh" : [NSNumber numberWithInteger:1],
  7717. @"single_select" : @"true",
  7718. }.mutableCopy;
  7719. NSNumber *required = setting[@"ShippingMethodRequire"];
  7720. if ([required integerValue]) {
  7721. [item_0 setValue:@"true" forKey:@"required"];
  7722. }
  7723. [dic setValue:item_0 forKey:@"item_0"];
  7724. if ([logist isEqualToString:@"WILL CALL"]) {
  7725. [dic removeObjectForKey:@"lift_gate"];
  7726. }
  7727. return dic;
  7728. }
  7729. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7730. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7731. orderCode = [self translateSingleQuote:orderCode];
  7732. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7733. __block int submit_as_integer = 0;
  7734. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7735. submit_as_integer = sqlite3_column_int(stmt, 0);
  7736. }];
  7737. int check11 = 0;
  7738. int check10 = 0;
  7739. if (submit_as_integer == 11) {
  7740. check11 = 1;
  7741. }
  7742. if (submit_as_integer == 10) {
  7743. check10 = 1;
  7744. }
  7745. // section_0
  7746. NSMutableDictionary *dic = @{
  7747. @"count" : @(1),
  7748. @"item_0" : @{
  7749. @"aname" : @"Submit Order As",
  7750. @"cadedate" : @{
  7751. @"count" : @(2),
  7752. @"val_0" : @{
  7753. @"check" : [NSNumber numberWithInteger:check11],
  7754. @"value" : @"Sales Order",
  7755. @"value_id" : @(11)
  7756. },
  7757. @"val_1" : @{
  7758. @"check" : [NSNumber numberWithInteger:check10],
  7759. @"value" : @"Quote",
  7760. @"value_id" : @(10)
  7761. }
  7762. },
  7763. @"control" : @"enum",
  7764. @"name" : @"erpOrderStatus",
  7765. @"required" : @"true",
  7766. @"single_select" : @"true"
  7767. },
  7768. @"title" : @"Order Type"
  7769. }.mutableCopy;
  7770. // setting
  7771. NSDictionary *setting = params[@"setting"];
  7772. NSNumber *hide = setting[@"OrderTypeHide"];
  7773. [dic setValue:hide forKey:@"hide"];
  7774. return dic;
  7775. }
  7776. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7777. countryCode = [self translateSingleQuote:countryCode];
  7778. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7779. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7780. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7781. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7782. if (name == NULL) {
  7783. name = "";
  7784. }
  7785. if (code == NULL) {
  7786. code = "";
  7787. }
  7788. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7789. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7790. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7791. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7792. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7793. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7794. }
  7795. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7796. [container setValue:stateDic forKey:key];
  7797. }] mutableCopy];
  7798. [ret removeObjectForKey:@"result"];
  7799. // failure 可以不用了,一样的
  7800. if (ret.allKeys.count == 0) {
  7801. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7802. [stateDic setValue:@"Other" forKey:@"value"];
  7803. [stateDic setValue:@"" forKey:@"value_id"];
  7804. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7805. if (state_code && [@"" isEqualToString:state_code]) {
  7806. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7807. }
  7808. NSString *key = [NSString stringWithFormat:@"val_0"];
  7809. [ret setValue:stateDic forKey:key];
  7810. }
  7811. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7812. return ret;
  7813. }
  7814. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7815. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7816. orderCode = [self translateSingleQuote:orderCode];
  7817. 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];
  7818. __block NSString *payType = @"";
  7819. __block NSString *firstName = @"";
  7820. __block NSString *lastName = @"";
  7821. __block NSString *addr1 = @"";
  7822. __block NSString *addr2 = @"";
  7823. __block NSString *zipcode = @"";
  7824. __block NSString *cardType = @"";
  7825. __block NSString *cardNumber = @"";
  7826. __block NSString *securityCode = @"";
  7827. __block NSString *month = @"";
  7828. __block NSString *year = @"";
  7829. __block NSString *city = @"";
  7830. __block NSString *state = @"";
  7831. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7832. payType = [self textAtColumn:0 statement:stmt];
  7833. firstName = [self textAtColumn:1 statement:stmt];
  7834. lastName = [self textAtColumn:2 statement:stmt];
  7835. addr1 = [self textAtColumn:3 statement:stmt];
  7836. addr2 = [self textAtColumn:4 statement:stmt];
  7837. zipcode = [self textAtColumn:5 statement:stmt];
  7838. cardType = [self textAtColumn:6 statement:stmt];
  7839. cardNumber = [self textAtColumn:7 statement:stmt];
  7840. securityCode = [self textAtColumn:8 statement:stmt];
  7841. month = [self textAtColumn:9 statement:stmt];
  7842. year = [self textAtColumn:10 statement:stmt];
  7843. city = [self textAtColumn:11 statement:stmt];
  7844. state = [self textAtColumn:12 statement:stmt];
  7845. }];
  7846. NSString *required = @"true";
  7847. // setting
  7848. NSDictionary *setting = params[@"setting"];
  7849. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7850. if ([requiredNumber integerValue]) {
  7851. required = @"true";
  7852. } else {
  7853. required = @"false";
  7854. }
  7855. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7856. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7857. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7858. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7859. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7860. NSString *type = [self textAtColumn:1 statement:stmt];
  7861. NSMutableDictionary *val = @{
  7862. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7863. @"value" : type,
  7864. @"value_id" : type_id
  7865. }.mutableCopy;
  7866. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7867. NSDictionary *sub_item = @{
  7868. @"count" : @(3),
  7869. @"item_0" : @{
  7870. @"aname" : @"choose",
  7871. @"control" : @"multi_action",
  7872. @"count" : @(1),
  7873. @"item_0" : @{
  7874. @"aname" : @"Same as customer",
  7875. @"key_map" : @{
  7876. @"credit_card_address1" : @"customer_address1",
  7877. @"credit_card_address2" : @"customer_address2",
  7878. @"credit_card_city" : @"customer_city",
  7879. @"credit_card_first_name" : @"customer_first_name",
  7880. @"credit_card_last_name" : @"customer_last_name",
  7881. @"credit_card_state" : @"customer_state",
  7882. @"credit_card_zipcode" : @"customer_zipcode"
  7883. },
  7884. @"type" : @"pull"
  7885. }
  7886. },
  7887. @"item_1" : @{
  7888. @"aname" : @"",
  7889. @"color" : @"red",
  7890. @"control" : @"text",
  7891. @"name" : @"",
  7892. @"value" : @"USA Credit cards only"
  7893. },
  7894. @"item_2" : @{
  7895. @"aname" : @"Fill",
  7896. @"cadedate" : @{
  7897. @"count" : @(2),
  7898. @"val_0" : @{
  7899. @"check" : @(1),
  7900. @"sub_item" : @{
  7901. @"count" : @(11),
  7902. @"item_0" : @{
  7903. @"aname" : @"Type",
  7904. @"cadedate" : @{
  7905. @"count" : @(2),
  7906. @"val_0" : @{
  7907. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7908. @"value" : @"VISA",
  7909. @"value_id" : @"VISA"/*@(0)*/
  7910. },
  7911. @"val_1" : @{
  7912. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7913. @"value" : @"MASTER CARD",
  7914. @"value_id" : @"MASTER CARD"/*@(1)*/
  7915. }
  7916. },
  7917. @"control" : @"enum",
  7918. @"name" : @"credit_card_type",
  7919. @"required" : @"true",
  7920. @"single_select" : @"true"
  7921. },
  7922. @"item_1" : @{
  7923. @"aname" : @"Number",
  7924. @"control" : @"edit",
  7925. @"keyboard" : @"int",
  7926. @"length" : @"16",
  7927. @"name" : @"credit_card_number",
  7928. @"required" : @"true",
  7929. @"value" : cardNumber
  7930. },
  7931. @"item_10" : @{
  7932. @"aname" : @"State",
  7933. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7934. @"control" : @"enum",
  7935. @"enum" : @"true",
  7936. @"name" : @"credit_card_state",
  7937. @"required" : @"true",
  7938. @"single_select" : @"true"
  7939. },
  7940. @"item_2" : @{
  7941. @"aname" : @"Expiration Date",
  7942. @"control" : @"monthpicker",
  7943. @"name" : @"credit_card_expiration",
  7944. @"required" : @"true",
  7945. @"type" : @"date",
  7946. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7947. },
  7948. @"item_3" : @{
  7949. @"aname" : @"Security Code",
  7950. @"control" : @"edit",
  7951. @"keyboard" : @"int",
  7952. @"length" : @"3",
  7953. @"name" : @"credit_card_security_code",
  7954. @"required" : @"true",
  7955. @"value" : securityCode
  7956. },
  7957. @"item_4" : @{
  7958. @"aname" : @"First Name",
  7959. @"control" : @"edit",
  7960. @"keyboard" : @"text",
  7961. @"name" : @"credit_card_first_name",
  7962. @"required" : @"true",
  7963. @"value" : firstName
  7964. },
  7965. @"item_5" : @{
  7966. @"aname" : @"Last Name",
  7967. @"control" : @"edit",
  7968. @"keyboard" : @"text",
  7969. @"name" : @"credit_card_last_name",
  7970. @"required" : @"true",
  7971. @"value" : lastName
  7972. },
  7973. @"item_6" : @{
  7974. @"aname" : @"Address 1",
  7975. @"control" : @"edit",
  7976. @"keyboard" : @"text",
  7977. @"name" : @"credit_card_address1",
  7978. @"required" : @"true",
  7979. @"value" : addr1
  7980. },
  7981. @"item_7" : @{
  7982. @"aname" : @"Address 2",
  7983. @"control" : @"edit",
  7984. @"keyboard" : @"text",
  7985. @"name" : @"credit_card_address2",
  7986. @"value" : addr2
  7987. },
  7988. @"item_8" : @{
  7989. @"aname" : @"zip code",
  7990. @"control" : @"edit",
  7991. @"keyboard" : @"text",
  7992. @"name" : @"credit_card_zipcode",
  7993. @"required" : @"true",
  7994. @"value" : zipcode
  7995. },
  7996. @"item_9" : @{
  7997. @"aname" : @"City",
  7998. @"control" : @"edit",
  7999. @"keyboard" : @"text",
  8000. @"name" : @"credit_card_city",
  8001. @"required" : @"true",
  8002. @"value" : city
  8003. }
  8004. },
  8005. @"value" : @"Fill Now",
  8006. @"value_id" : @""
  8007. },
  8008. @"val_1" : @{
  8009. @"check" : @(0),
  8010. @"value" : @"Fill Later",
  8011. @"value_id" : @""
  8012. }
  8013. },
  8014. @"control" : @"enum",
  8015. @"name" : @"",
  8016. @"single_select" : @"true"
  8017. }
  8018. };
  8019. [val setObject:sub_item forKey:@"sub_item"];
  8020. }
  8021. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8022. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8023. }];
  8024. // "section_2"
  8025. NSMutableDictionary *dic = @{
  8026. @"count" : @(1),
  8027. @"item_0" : @{
  8028. @"aname" : @"Payment",
  8029. @"required" : required,
  8030. @"cadedate" : cadedate,
  8031. // @{
  8032. // @"count" : @(6),
  8033. // @"val_3" : @{
  8034. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8035. // @"value" : @"Check",
  8036. // @"value_id" : @"Check"
  8037. // },
  8038. // @"val_2" : @{
  8039. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8040. // @"value" : @"Cash",
  8041. // @"value_id" : @"Cash"
  8042. // },
  8043. // @"val_1" : @{
  8044. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8045. // @"value" : @"NET 60",
  8046. // @"value_id" : @"NET 30"
  8047. // },
  8048. // @"val_4" : @{
  8049. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8050. // @"value" : @"Wire Transfer",
  8051. // @"value_id" : @"Wire Transfer"
  8052. // },
  8053. // @"val_0" : @{
  8054. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8055. // @"sub_item" : @{
  8056. // @"count" : @(3),
  8057. // @"item_0" : @{
  8058. // @"aname" : @"choose",
  8059. // @"control" : @"multi_action",
  8060. // @"count" : @(1),
  8061. // @"item_0" : @{
  8062. // @"aname" : @"Same as customer",
  8063. // @"key_map" : @{
  8064. // @"credit_card_address1" : @"customer_address1",
  8065. // @"credit_card_address2" : @"customer_address2",
  8066. // @"credit_card_city" : @"customer_city",
  8067. // @"credit_card_first_name" : @"customer_first_name",
  8068. // @"credit_card_last_name" : @"customer_last_name",
  8069. // @"credit_card_state" : @"customer_state",
  8070. // @"credit_card_zipcode" : @"customer_zipcode"
  8071. // },
  8072. // @"type" : @"pull"
  8073. // }
  8074. // },
  8075. // @"item_1" : @{
  8076. // @"aname" : @"",
  8077. // @"color" : @"red",
  8078. // @"control" : @"text",
  8079. // @"name" : @"",
  8080. // @"value" : @"USA Credit cards only"
  8081. // },
  8082. // @"item_2" : @{
  8083. // @"aname" : @"Fill",
  8084. // @"cadedate" : @{
  8085. // @"count" : @(2),
  8086. // @"val_0" : @{
  8087. // @"check" : @(1),
  8088. // @"sub_item" : @{
  8089. // @"count" : @(11),
  8090. // @"item_0" : @{
  8091. // @"aname" : @"Type",
  8092. // @"cadedate" : @{
  8093. // @"count" : @(2),
  8094. // @"val_0" : @{
  8095. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8096. // @"value" : @"VISA",
  8097. // @"value_id" : @(0)
  8098. // },
  8099. // @"val_1" : @{
  8100. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8101. // @"value" : @"MASTER CARD",
  8102. // @"value_id" : @(1)
  8103. // }
  8104. // },
  8105. // @"control" : @"enum",
  8106. // @"name" : @"credit_card_type",
  8107. // @"required" : @"true",
  8108. // @"single_select" : @"true"
  8109. // },
  8110. // @"item_1" : @{
  8111. // @"aname" : @"Number",
  8112. // @"control" : @"edit",
  8113. // @"keyboard" : @"int",
  8114. // @"length" : @"16",
  8115. // @"name" : @"credit_card_number",
  8116. // @"required" : @"true",
  8117. // @"value" : cardNumber
  8118. // },
  8119. // @"item_10" : @{
  8120. // @"aname" : @"State",
  8121. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8122. // @"control" : @"enum",
  8123. // @"enum" : @"true",
  8124. // @"name" : @"credit_card_state",
  8125. // @"required" : @"true",
  8126. // @"single_select" : @"true"
  8127. // },
  8128. // @"item_2" : @{
  8129. // @"aname" : @"Expiration Date",
  8130. // @"control" : @"monthpicker",
  8131. // @"name" : @"credit_card_expiration",
  8132. // @"required" : @"true",
  8133. // @"type" : @"date",
  8134. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8135. // },
  8136. // @"item_3" : @{
  8137. // @"aname" : @"Security Code",
  8138. // @"control" : @"edit",
  8139. // @"keyboard" : @"int",
  8140. // @"length" : @"3",
  8141. // @"name" : @"credit_card_security_code",
  8142. // @"required" : @"true",
  8143. // @"value" : securityCode
  8144. // },
  8145. // @"item_4" : @{
  8146. // @"aname" : @"First Name",
  8147. // @"control" : @"edit",
  8148. // @"keyboard" : @"text",
  8149. // @"name" : @"credit_card_first_name",
  8150. // @"required" : @"true",
  8151. // @"value" : firstName
  8152. // },
  8153. // @"item_5" : @{
  8154. // @"aname" : @"Last Name",
  8155. // @"control" : @"edit",
  8156. // @"keyboard" : @"text",
  8157. // @"name" : @"credit_card_last_name",
  8158. // @"required" : @"true",
  8159. // @"value" : lastName
  8160. // },
  8161. // @"item_6" : @{
  8162. // @"aname" : @"Address 1",
  8163. // @"control" : @"edit",
  8164. // @"keyboard" : @"text",
  8165. // @"name" : @"credit_card_address1",
  8166. // @"required" : @"true",
  8167. // @"value" : addr1
  8168. // },
  8169. // @"item_7" : @{
  8170. // @"aname" : @"Address 2",
  8171. // @"control" : @"edit",
  8172. // @"keyboard" : @"text",
  8173. // @"name" : @"credit_card_address2",
  8174. // @"value" : addr2
  8175. // },
  8176. // @"item_8" : @{
  8177. // @"aname" : @"zip code",
  8178. // @"control" : @"edit",
  8179. // @"keyboard" : @"text",
  8180. // @"name" : @"credit_card_zipcode",
  8181. // @"required" : @"true",
  8182. // @"value" : zipcode
  8183. // },
  8184. // @"item_9" : @{
  8185. // @"aname" : @"City",
  8186. // @"control" : @"edit",
  8187. // @"keyboard" : @"text",
  8188. // @"name" : @"credit_card_city",
  8189. // @"required" : @"true",
  8190. // @"value" : city
  8191. // }
  8192. // },
  8193. // @"value" : @"Fill Now",
  8194. // @"value_id" : @""
  8195. // },
  8196. // @"val_1" : @{
  8197. // @"check" : @(0),
  8198. // @"value" : @"Fill Later",
  8199. // @"value_id" : @""
  8200. // }
  8201. // },
  8202. // @"control" : @"enum",
  8203. // @"name" : @"",
  8204. // @"single_select" : @"true"
  8205. // }
  8206. // },
  8207. // @"value" : @"Visa/Master",
  8208. // @"value_id" : @"Credit Card"
  8209. // },
  8210. // @"val_5" : @{
  8211. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8212. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8213. // @"value_id" : @"FOLLOW EXISTING"
  8214. // }
  8215. // },
  8216. @"control" : @"enum",
  8217. @"name" : @"paymentType",
  8218. @"single_select" : @"true"
  8219. },
  8220. @"title" : @"Payment Information"
  8221. }.mutableCopy;
  8222. NSNumber *hide = setting[@"PaymentInformationHide"];
  8223. [dic setValue:hide forKey:@"hide"];
  8224. return dic;
  8225. }
  8226. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8227. // params
  8228. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8229. orderCode = [self translateSingleQuote:orderCode];
  8230. // 缺货检查
  8231. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  8232. __block BOOL outOfStock = NO;
  8233. sqlite3 *database = db;
  8234. if (!db) {
  8235. database = [iSalesDB get_db];
  8236. }
  8237. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8238. int availability = sqlite3_column_int(stmt, 0);
  8239. int item_qty = sqlite3_column_int(stmt, 1);
  8240. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8241. }];
  8242. if (!db) {
  8243. [iSalesDB close_db:database];
  8244. }
  8245. return outOfStock;
  8246. }
  8247. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8248. {
  8249. sqlite3 *db = [iSalesDB get_db];
  8250. // params
  8251. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8252. orderCode = [self translateSingleQuote:orderCode];
  8253. // 缺货检查
  8254. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8255. if (!appDelegate.can_create_backorder) {
  8256. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8257. if (out_of_stock) {
  8258. [iSalesDB close_db:db];
  8259. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8260. [resultDic setObject:[NSString stringWithFormat:@"some item(s) are out of stock,please remove them from cart before placing order with %@ or try again after sync",COMPANY_SHORT_NAME] forKey:@"err_msg"];
  8261. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8262. return [RAUtils dict2data:resultDic];
  8263. }
  8264. }
  8265. // UISetting
  8266. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8267. NSString *cachefolder = [paths objectAtIndex:0];
  8268. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8269. NSData* json =nil;
  8270. json=[NSData dataWithContentsOfFile:img_cache];
  8271. NSError *error=nil;
  8272. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8273. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8274. [params setObject:setting forKey:@"setting"];
  8275. int section_count = 0;
  8276. // 0 Order Type 1 Shipping Method 2 Payment Information
  8277. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8278. // 0 Order Type
  8279. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8280. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8281. [ret setValue:order_type_dic forKey:key0];
  8282. // 1 Shipping Method
  8283. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8284. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8285. [ret setValue:shipping_method_dic forKey:key1];
  8286. // 2 Payment Information
  8287. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8288. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8289. [ret setValue:payment_info_dic forKey:key2];
  8290. // 3 Customer
  8291. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8292. NSDictionary *customer_dic = [self customerDic:params db:db];
  8293. [ret setValue:customer_dic forKey:key3];
  8294. // 4 Ship To
  8295. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8296. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8297. [ret setValue:ship_to_dic forKey:key4];
  8298. // 5 Ship From
  8299. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8300. if (![shipFromDisable integerValue]) {
  8301. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8302. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8303. [ret setValue:ship_from_dic forKey:key5];
  8304. }
  8305. // 6 Freight Bill To
  8306. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8307. if (![freightBillToDisable integerValue]) {
  8308. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8309. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8310. [ret setValue:freight_bill_to forKey:key6];
  8311. }
  8312. // 7 Merchandise Bill To
  8313. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8314. if (![merchandiseBillToDisable integerValue]) {
  8315. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8316. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8317. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8318. }
  8319. // 8 Return To
  8320. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8321. if (![returnToDisable integerValue]) {
  8322. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8323. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8324. [ret setValue:return_to_dic forKey:key8];
  8325. }
  8326. // 9 Model Information
  8327. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8328. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8329. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8330. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8331. [ret setValue:model_info_dic forKey:key9];
  8332. // 10 Remarks Content
  8333. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8334. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8335. [ret setValue:remarks_content_dic forKey:key10];
  8336. // 11 Order Total
  8337. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8338. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8339. [ret setValue:order_total_dic forKey:key11];
  8340. // 12 Signature
  8341. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8342. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8343. [ret setValue:sign_dic forKey:key12];
  8344. [ret setValue:@(section_count) forKey:@"section_count"];
  8345. [iSalesDB close_db:db];
  8346. return [RAUtils dict2data:ret];
  8347. // return nil;
  8348. }
  8349. #pragma mark addr editor
  8350. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8351. NSMutableDictionary *new_item = [item mutableCopy];
  8352. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8353. return new_item;
  8354. }
  8355. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8356. {
  8357. // "is_subaction" = true;
  8358. // orderCode = MOB1608240002;
  8359. // password = 123456;
  8360. // "subaction_tag" = 1;
  8361. // user = EvanK;
  8362. // {
  8363. // "is_subaction" = true;
  8364. // orderCode = MOB1608240002;
  8365. // password = 123456;
  8366. // "refresh_trigger" = zipcode;
  8367. // "subaction_tag" = 1;
  8368. // user = EvanK;
  8369. // }
  8370. NSString *country_code = nil;
  8371. NSString *zipCode = nil;
  8372. NSString *stateCode = nil;
  8373. NSString *city = nil;
  8374. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8375. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8376. NSString *code_id = params[@"country"];
  8377. country_code = [self countryCodeByid:code_id];
  8378. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8379. NSString *zip_code = params[@"zipcode"];
  8380. // 剔除全部为空格
  8381. int spaceCount = 0;
  8382. for (int i = 0; i < zip_code.length; i++) {
  8383. if ([zip_code characterAtIndex:i] == ' ') {
  8384. spaceCount++;
  8385. }
  8386. }
  8387. if (spaceCount == zip_code.length) {
  8388. zip_code = @"";
  8389. }
  8390. zipCode = zip_code;
  8391. if (zipCode.length > 0) {
  8392. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8393. country_code = [dic valueForKey:@"country_code"];
  8394. if (!country_code) {
  8395. // country_code = @"US";
  8396. NSString *code_id = params[@"country"];
  8397. country_code = [self countryCodeByid:code_id];
  8398. }
  8399. stateCode = [dic valueForKey:@"state_code"];
  8400. if(!stateCode.length) {
  8401. stateCode = params[@"state"];
  8402. }
  8403. city = [dic valueForKey:@"city"];
  8404. if (!city.length) {
  8405. city = params[@"city"];
  8406. }
  8407. // zip code
  8408. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8409. // [zipDic setValue:zipCode forKey:@"value"];
  8410. // [section_0 setValue:zipDic forKey:@"item_11"];
  8411. } else {
  8412. NSString *code_id = params[@"country"];
  8413. country_code = [self countryCodeByid:code_id];
  8414. stateCode = params[@"state"];
  8415. city = params[@"city"];
  8416. }
  8417. }
  8418. }
  8419. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8420. // [ret removeObjectForKey:@"up_params"];
  8421. [ret setObject:@"New Address" forKey:@"title"];
  8422. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8423. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8424. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8425. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8426. // [country_dic removeObjectForKey:@"refresh"];
  8427. // [country_dic removeObjectForKey:@"restore"];
  8428. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8429. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8430. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8431. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8432. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8433. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8434. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8435. // [zip_code_dic removeObjectForKey:@"refresh"];
  8436. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8437. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8438. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8439. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8440. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8441. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8442. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8443. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8444. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8445. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8446. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8447. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8448. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8449. // country
  8450. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8451. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8452. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8453. // state
  8454. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8455. // NSDictionary *tmpDic = @{
  8456. // @"value" : @"Other",
  8457. // @"value_id" : @"",
  8458. // @"check" : [NSNumber numberWithInteger:0]
  8459. // };
  8460. //
  8461. // for (int i = 0; i < allState.allKeys.count; i++) {
  8462. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8463. //
  8464. // NSDictionary *tmp = allState[key];
  8465. // [allState setValue:tmpDic forKey:key];
  8466. // tmpDic = tmp;
  8467. // }
  8468. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8469. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8470. [ret setValue:new_section_0 forKey:@"section_0"];
  8471. return [RAUtils dict2data:ret];
  8472. }
  8473. #pragma mark save addr
  8474. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8475. {
  8476. // NSString *companyName = [self valueInParams:params key:@"company"];
  8477. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8478. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8479. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8480. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8481. // NSString *countryId = [self valueInParams:params key:@"country"];
  8482. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8483. // NSString *city = [self valueInParams:params key:@"city"];
  8484. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8485. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8486. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8487. // NSString *phone = [self valueInParams:params key:@"phone"];
  8488. // NSString *fax = [self valueInParams:params key:@"fax"];
  8489. // NSString *email = [self valueInParams:params key:@"email"];
  8490. return [self offline_saveContact:params update:NO isCustomer:NO];
  8491. }
  8492. #pragma mark cancel order
  8493. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8494. {
  8495. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8496. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8497. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8498. if (order_id.length) {
  8499. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8500. }
  8501. int ret = [iSalesDB execSql:sql];
  8502. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8503. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8504. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8505. // [dic setValue:@"160409" forKey:@"min_ver"];
  8506. return [RAUtils dict2data:dic];
  8507. }
  8508. #pragma mark sign order
  8509. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8510. {
  8511. //参考 offline_saveBusinesscard
  8512. DebugLog(@"sign order params: %@",params);
  8513. // orderCode = MOB1608240002;
  8514. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8515. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8516. orderCode = [self translateSingleQuote:orderCode];
  8517. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8518. picPath = [self translateSingleQuote:picPath];
  8519. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8520. int ret = [iSalesDB execSql:sql];
  8521. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8522. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8523. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8524. // [dic setValue:@"160409" forKey:@"min_ver"];
  8525. return [RAUtils dict2data:dic];
  8526. }
  8527. #pragma mark save order
  8528. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8529. NSString *ret = [self valueInParams:params key:key];
  8530. if (translate) {
  8531. ret = [self translateSingleQuote:ret];
  8532. }
  8533. return ret;
  8534. }
  8535. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8536. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8537. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8538. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8539. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8540. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8541. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8542. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8543. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8544. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8545. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8546. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8547. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8548. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8549. if (![total_price isEqualToString:@""]) {
  8550. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8551. } else {
  8552. total_price = @"";
  8553. }
  8554. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8555. if ([paymentsAndCredits isEqualToString:@""]) {
  8556. paymentsAndCredits = @"";
  8557. } else {
  8558. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8559. }
  8560. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8561. if ([handling_fee_value isEqualToString:@""]) {
  8562. handling_fee_value = @"";
  8563. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8564. handling_fee_value = @"";
  8565. } else {
  8566. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8567. }
  8568. NSString *handling_fee_placeholder = handling_fee_value;
  8569. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8570. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8571. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8572. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8573. NSString *lift_gate_placeholder = @"";
  8574. if ([lift_gate_value isEqualToString:@""]) {
  8575. lift_gate_placeholder = @"";
  8576. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8577. lift_gate_placeholder = @"";
  8578. } else {
  8579. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8580. }
  8581. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8582. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8583. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8584. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8585. 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];
  8586. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8587. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8588. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8589. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8590. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8591. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8592. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8593. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8594. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8595. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8596. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8597. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8598. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8599. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8600. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8601. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8602. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8603. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8604. if (!number_nil) {
  8605. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8606. }
  8607. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8608. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8609. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8610. if (!security_code_nil) {
  8611. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8612. }
  8613. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8614. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8615. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8616. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8617. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8618. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8619. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8620. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8621. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8622. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8623. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8624. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8625. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8626. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8627. NSString *contact_id = customer_cid;
  8628. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8629. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8630. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8631. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8632. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8633. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8634. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8635. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8636. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8637. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8638. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8639. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8640. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8641. //
  8642. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8643. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8644. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8645. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8646. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8647. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8648. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8649. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8650. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8651. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8652. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8653. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8654. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8655. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8656. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8657. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8658. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8659. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8660. 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];
  8661. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8662. if (receive_cid.length) {
  8663. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8664. }
  8665. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8666. if (receive_name.length) {
  8667. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8668. }
  8669. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8670. if (receive_ext.length) {
  8671. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8672. }
  8673. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8674. if (receive_contact.length) {
  8675. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8676. }
  8677. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8678. if (receive_phone.length) {
  8679. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8680. }
  8681. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8682. if (receive_email.length) {
  8683. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8684. }
  8685. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8686. if (receive_fax.length) {
  8687. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8688. }
  8689. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8690. if (sender_cid.length) {
  8691. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8692. }
  8693. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8694. if (sender_name.length) {
  8695. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8696. }
  8697. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8698. if (sender_ext.length) {
  8699. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8700. }
  8701. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8702. if(sender_contact.length) {
  8703. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8704. }
  8705. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8706. if (sender_phone.length) {
  8707. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8708. }
  8709. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8710. if (sender_fax.length) {
  8711. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8712. }
  8713. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8714. if (sender_email.length) {
  8715. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8716. }
  8717. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8718. if (shipping_billto_cid.length) {
  8719. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8720. }
  8721. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8722. if (shipping_billto_name.length) {
  8723. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8724. }
  8725. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8726. if (shipping_billto_ext.length) {
  8727. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8728. }
  8729. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8730. if (shipping_billto_contact.length) {
  8731. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8732. }
  8733. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8734. if (shipping_billto_phone.length) {
  8735. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8736. }
  8737. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8738. if (shipping_billto_fax.length) {
  8739. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8740. }
  8741. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8742. if (shipping_billto_email.length) {
  8743. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8744. }
  8745. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8746. if (billing_cid.length) {
  8747. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8748. }
  8749. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8750. if (billing_name.length) {
  8751. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8752. }
  8753. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8754. if (billing_ext.length) {
  8755. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8756. }
  8757. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8758. if (billing_contact.length) {
  8759. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8760. }
  8761. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8762. if (billing_phone.length) {
  8763. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8764. }
  8765. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8766. if (billing_fax.length) {
  8767. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8768. }
  8769. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8770. if (billing_email.length) {
  8771. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8772. }
  8773. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8774. if (returnto_cid.length) {
  8775. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8776. }
  8777. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8778. if (returnto_name.length) {
  8779. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8780. }
  8781. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8782. if (returnto_ext.length) {
  8783. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8784. }
  8785. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8786. if (returnto_contact.length) {
  8787. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8788. }
  8789. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8790. if (returnto_phone.length) {
  8791. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8792. }
  8793. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8794. if (returnto_fax.length) {
  8795. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8796. }
  8797. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8798. if (returnto_email.length) {
  8799. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8800. }
  8801. NSString *order_status = @"status = 1,";
  8802. if (submit) {
  8803. order_status = @"status = -11,";
  8804. }
  8805. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8806. NSString *sync_sql = @"";
  8807. if (submit) {
  8808. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8809. [param setValue:sales_rep forKey:@"sales_rep"];
  8810. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8811. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8812. }
  8813. 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];
  8814. DebugLog(@"save order contactSql: %@",contactSql);
  8815. DebugLog(@"save order orderSql: %@",orderSql);
  8816. // int contact_ret = [iSalesDB execSql:contactSql];
  8817. int order_ret = [iSalesDB execSql:orderSql];
  8818. int ret = order_ret;
  8819. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8820. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8821. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8822. // [dic setValue:@"160409" forKey:@"min_ver"];
  8823. [dic setObject:so_id forKey:@"so#"];
  8824. return [RAUtils dict2data:dic];
  8825. }
  8826. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8827. {
  8828. // id foo = nil;
  8829. // NSMutableArray *a = @[].mutableCopy;
  8830. // [a addObject:foo];
  8831. return [self saveorder:param submit:NO];
  8832. }
  8833. #pragma mark add to cart by name
  8834. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8835. {
  8836. NSString *orderCode = [params objectForKey:@"orderCode"];
  8837. NSString *product_name = [params objectForKey:@"product_name"];
  8838. product_name = [self translateSingleQuote:product_name];
  8839. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8840. sqlite3 *db = [iSalesDB get_db];
  8841. __block NSMutableString *product_id_string = [NSMutableString string];
  8842. for (int i = 0; i < product_name_array.count; i++) {
  8843. NSString *name = [product_name_array objectAtIndex:i];
  8844. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8845. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8846. int product_id = sqlite3_column_int(stmt, 0);
  8847. if (i == product_name_array.count - 1) {
  8848. [product_id_string appendFormat:@"%d",product_id];
  8849. } else {
  8850. [product_id_string appendFormat:@"%d,",product_id];
  8851. }
  8852. }];
  8853. }
  8854. NSDictionary *newParams = @{
  8855. @"product_id" : product_id_string,
  8856. @"orderCode" : orderCode
  8857. };
  8858. [iSalesDB close_db:db];
  8859. return [self offline_add2cart:[newParams mutableCopy]];
  8860. }
  8861. #pragma mark reset order
  8862. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8863. {
  8864. UIApplication * app = [UIApplication sharedApplication];
  8865. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8866. appDelegate.disable_trigger=true;
  8867. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8868. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8869. appDelegate.disable_trigger=false;
  8870. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8871. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8872. return [RAUtils dict2data:dic];
  8873. }
  8874. #pragma mark submit order
  8875. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8876. {
  8877. return [self saveorder:params submit:YES];
  8878. }
  8879. #pragma mark copy order
  8880. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8881. {
  8882. NSMutableDictionary *ret = @{}.mutableCopy;
  8883. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8884. sqlite3 *db = [iSalesDB get_db];
  8885. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8886. // 首先查看联系人是否active
  8887. 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];
  8888. __block int customer_is_active = 1;
  8889. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8890. customer_is_active = sqlite3_column_int(stmt, 0);
  8891. }];
  8892. if (!customer_is_active) {
  8893. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8894. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8895. [iSalesDB close_db:db];
  8896. return [RAUtils dict2data:ret];
  8897. }
  8898. // new order
  8899. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8900. NSString *new_order_code = [self get_offline_soid:db];
  8901. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8902. user = [self translateSingleQuote:user];
  8903. 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
  8904. __block int result = 1;
  8905. result = [iSalesDB execSql:insert_order_sql db:db];
  8906. if (!result) {
  8907. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8908. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8909. [iSalesDB close_db:db];
  8910. return [RAUtils dict2data:ret];
  8911. }
  8912. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8913. // __block NSString *product_id = @"";
  8914. __weak typeof(self) weakSelf = self;
  8915. 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];
  8916. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8917. int is_active = sqlite3_column_int(stmt, 1);
  8918. if (is_active) {
  8919. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8920. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8921. 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];
  8922. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8923. }
  8924. }];
  8925. // product_id = [product_id substringFromIndex:1];
  8926. //
  8927. // [self offline_add2cart:@{}.mutableCopy];
  8928. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8929. [iSalesDB close_db:db];
  8930. if (result) {
  8931. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8932. } else {
  8933. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8934. }
  8935. [ret setObject:new_order_code forKey:@"so_id"];
  8936. return [RAUtils dict2data:ret];
  8937. }
  8938. #pragma mark move wish list to cart
  8939. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8940. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8941. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8942. NSString *collectId = params[@"collectId"];
  8943. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  8944. __block NSString *product_id = @"";
  8945. __block NSString *qty = @"";
  8946. __block int number_of_outOfStock = 0;
  8947. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  8948. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8949. int productId = sqlite3_column_int(stmt, 0);
  8950. int item_qty = sqlite3_column_int(stmt, 1);
  8951. int availability = sqlite3_column_int(stmt, 2);
  8952. int _id = sqlite3_column_int(stmt, 3);
  8953. if (!appDelegate.can_create_backorder) {
  8954. // 库存小于购买量为缺货
  8955. if (availability >= item_qty) {
  8956. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8957. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8958. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8959. } else {
  8960. number_of_outOfStock++;
  8961. }
  8962. } else {
  8963. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8964. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8965. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8966. }
  8967. }];
  8968. NSMutableDictionary *retDic = nil;
  8969. if (!appDelegate.can_create_backorder) {
  8970. if (delete_collectId.count == 0) {
  8971. retDic = [NSMutableDictionary dictionary];
  8972. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8973. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8974. return [RAUtils dict2data:retDic];
  8975. }
  8976. }
  8977. if (product_id.length > 1) {
  8978. product_id = [product_id substringFromIndex:1];
  8979. }
  8980. if (qty.length > 1) {
  8981. qty = [qty substringFromIndex:1];
  8982. }
  8983. NSDictionary *newParams = @{
  8984. @"product_id" : product_id,
  8985. @"orderCode" : params[@"orderCode"],
  8986. @"qty" : qty
  8987. };
  8988. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8989. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8990. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8991. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  8992. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8993. }
  8994. if (!appDelegate.can_create_backorder) {
  8995. if (number_of_outOfStock > 0) {
  8996. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8997. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8998. }
  8999. }
  9000. return [RAUtils dict2data:retDic];
  9001. }
  9002. #pragma mark - portfolio
  9003. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9004. int sort = [[params valueForKey:@"sort"] intValue];
  9005. int offset = [[params valueForKey:@"offset"] intValue];
  9006. int limit = [[params valueForKey:@"limit"] intValue];
  9007. NSString *orderBy = @"";
  9008. switch (sort) {
  9009. case 0:{
  9010. orderBy = @"p.modify_time desc";
  9011. }
  9012. break;
  9013. case 1:{
  9014. orderBy = @"modify_time asc";
  9015. }
  9016. break;
  9017. case 2:{
  9018. orderBy = @"p.name asc";
  9019. }
  9020. break;
  9021. case 3:{
  9022. orderBy = @"p.name desc";
  9023. }
  9024. break;
  9025. case 4:{
  9026. orderBy = @"p.description asc";
  9027. }
  9028. break;
  9029. case 5: {
  9030. orderBy = @"m.default_category asc";
  9031. }
  9032. default:
  9033. break;
  9034. }
  9035. // 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];
  9036. NSString *sql = [NSString stringWithFormat:@"select * from (select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent,p.modify_time from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  9037. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9038. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9039. sqlite3 *db = [iSalesDB get_db];
  9040. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9041. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9042. int product_id = sqlite3_column_int(stmt, 0);
  9043. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9044. NSString *name = [self textAtColumn:1 statement:stmt];
  9045. NSString *description = [self textAtColumn:2 statement:stmt];
  9046. double price = sqlite3_column_double(stmt, 3);
  9047. double discount = sqlite3_column_double(stmt,4);
  9048. int qty = sqlite3_column_int(stmt, 5);
  9049. int percentage = sqlite3_column_int(stmt, 6);
  9050. int item_id = sqlite3_column_int(stmt, 7);
  9051. // int fashion_id = sqlite3_column_int(stmt, 8);
  9052. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9053. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9054. double percent = sqlite3_column_double(stmt, 11);
  9055. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9056. if ([price_null isEqualToString:@"null"]) {
  9057. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9058. }
  9059. NSMutableDictionary *item = @{
  9060. @"linenotes": line_note,
  9061. @"check": @(1),
  9062. @"product_id": product_id_string,
  9063. @"available_qty": @(qty),
  9064. @"available_percent" : @(percent),
  9065. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9066. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9067. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9068. @"img": img_path,
  9069. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9070. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9071. }.mutableCopy;
  9072. if (percentage) {
  9073. [item removeObjectForKey:@"available_qty"];
  9074. } else {
  9075. [item removeObjectForKey:@"available_percent"];
  9076. }
  9077. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9078. if ([qty_null isEqualToString:@"null"]) {
  9079. [item removeObjectForKey:@"available_qty"];
  9080. }
  9081. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9082. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9083. }];
  9084. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9085. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9086. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9087. }
  9088. [iSalesDB close_db:db];
  9089. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  9090. [dic setValue:@"" forKey:@"email_content"];
  9091. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9092. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9093. return [RAUtils dict2data:dic];
  9094. }
  9095. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9096. __block int qty = 0;
  9097. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9098. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9099. qty = sqlite3_column_int(stmt, 0);
  9100. }];
  9101. return qty;
  9102. }
  9103. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  9104. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9105. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9106. NSMutableDictionary * values = params[@"replaceValue"];
  9107. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9108. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9109. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9110. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9111. NSString *pdf_path = @"";
  9112. if (direct) {
  9113. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9114. } else {
  9115. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9116. }
  9117. NSString *create_user = [self valueInParams:params key:@"user"];
  9118. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9119. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9120. // model info
  9121. // 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];
  9122. // V1.90 more color
  9123. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  9124. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9125. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9126. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9127. sqlite3 *db = [iSalesDB get_db];
  9128. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9129. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9130. int product_id = sqlite3_column_int(stmt, 0);
  9131. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9132. double price = sqlite3_column_double(stmt, 1);
  9133. double discount = sqlite3_column_double(stmt,2);
  9134. int qty = sqlite3_column_int(stmt, 3);
  9135. int percentage = sqlite3_column_int(stmt, 4);
  9136. int item_id = sqlite3_column_int(stmt, 5);
  9137. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9138. double percent = sqlite3_column_double(stmt, 7);
  9139. int more_color = sqlite3_column_int(stmt, 8);
  9140. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9141. /* if ([price_null isEqualToString:@"null"]) {
  9142. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9143. }
  9144. */
  9145. [product_ids_string appendFormat:@"%@,",product_id_string];
  9146. // Regular Price
  9147. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  9148. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  9149. // QTY
  9150. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9151. if ([qty_null isEqualToString:@"null"]) {
  9152. // 查available
  9153. qty = [self model_QTY:product_id_string db:db];
  9154. }
  9155. if (percentage) {
  9156. qty = qty * percent / 100;
  9157. }
  9158. // Special Price
  9159. if ([price_null isEqualToString:@"null"]) {
  9160. // price = regular price
  9161. price = [regular_price_str doubleValue];
  9162. }
  9163. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9164. if (![discount_null isEqualToString:@"null"]) {
  9165. price = price * (1 - discount / 100.0);
  9166. }
  9167. NSMutableDictionary *item = @{
  9168. @"line_note": line_note,
  9169. @"product_id": product_id_string,
  9170. @"available_qty": @(qty),
  9171. @"more_color":@(more_color),
  9172. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9173. @"regular_price" : regular_price_str,
  9174. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9175. }.mutableCopy;
  9176. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9177. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9178. }];
  9179. [iSalesDB close_db:db];
  9180. if (product_ids_string.length > 0) {
  9181. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9182. }
  9183. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9184. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9185. return [RAUtils dict2data:resultDictionary];
  9186. }
  9187. NSString *model_info = [RAUtils dict2string:dic];
  9188. // 创建PDF
  9189. // 在preview情况下保存,则不需要新建了
  9190. if (direct) {
  9191. NSMutableDictionary *tear_sheet_params = params;
  9192. // if (tear_sheet_id) {
  9193. // tear_sheet_params = values;
  9194. // }
  9195. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9196. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9197. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9198. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9199. resultDictionary = pdfInfo.mutableCopy;
  9200. } else { // 创建PDF失败
  9201. return pdfData;
  9202. }
  9203. } else {
  9204. // pdf_path 就是本地路径
  9205. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9206. }
  9207. // 将文件移动到PDF Cache文件夹
  9208. NSString *newPath = [pdf_path lastPathComponent];
  9209. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9210. NSString *cachefolder = [paths objectAtIndex:0];
  9211. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9212. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9213. NSFileManager *fileManager = [NSFileManager defaultManager];
  9214. NSError *error = nil;
  9215. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9216. if (error) { // 移动文件失败
  9217. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9218. return [RAUtils dict2data:resultDictionary];
  9219. }
  9220. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9221. pdf_path = [newPath lastPathComponent];
  9222. // 保存信息
  9223. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9224. NSString *off_params = [RAUtils dict2string:params];
  9225. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9226. pdf_path = [self translateSingleQuote:pdf_path];
  9227. create_user = [self translateSingleQuote:create_user];
  9228. tear_note = [self translateSingleQuote:tear_note];
  9229. tear_name = [self translateSingleQuote:tear_name];
  9230. model_info = [self translateSingleQuote:model_info];
  9231. configureParams = [self translateSingleQuote:configureParams];
  9232. off_params = [self translateSingleQuote:off_params];
  9233. 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];
  9234. if (tear_sheet_id) {
  9235. int _id = [tear_sheet_id integerValue];
  9236. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9237. }
  9238. int result = [iSalesDB execSql:save_pdf_sql];
  9239. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9240. //
  9241. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9242. if (remove_Item) {
  9243. // portfolioId
  9244. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9245. }
  9246. return [RAUtils dict2data:resultDictionary];
  9247. }
  9248. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9249. return [self offline_savePDF:params direct:YES];
  9250. }
  9251. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9252. return [self offline_savePDF:params direct:NO];
  9253. }
  9254. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9255. int offset = [[params valueForKey:@"offset"] integerValue];
  9256. int limit = [[params valueForKey:@"limit"] integerValue];
  9257. NSString *keyword = [params valueForKey:@"keyWord"];
  9258. NSString *where = @"where is_delete is null or is_delete = 0";
  9259. if (keyword.length) {
  9260. keyword = [self translateSingleQuote:keyword];
  9261. 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];
  9262. }
  9263. 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
  9264. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9265. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9266. NSString *cachefolder = [paths objectAtIndex:0];
  9267. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9268. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9269. NSString *name = [self textAtColumn:0 statement:stmt];
  9270. NSString *note = [self textAtColumn:1 statement:stmt];
  9271. NSString *time = [self textAtColumn:2 statement:stmt];
  9272. NSString *user = [self textAtColumn:3 statement:stmt];
  9273. NSString *path = [self textAtColumn:4 statement:stmt];
  9274. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9275. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9276. path = [pdfFolder stringByAppendingPathComponent:path];
  9277. BOOL bdir=NO;
  9278. NSFileManager* fileManager = [NSFileManager defaultManager];
  9279. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9280. {
  9281. //pdf文件不存在
  9282. path=nil;
  9283. }
  9284. time = [self changeDateTimeFormate:time];
  9285. time = [time stringByAppendingString:@" PST"];
  9286. int sheet_id = sqlite3_column_int(stmt, 5);
  9287. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9288. item[@"tearsheetsId"]=@(sheet_id);
  9289. item[@"pdf_path"]=path;
  9290. item[@"create_time"]=time;
  9291. item[@"create_user"]=user;
  9292. item[@"tear_note"]=note;
  9293. item[@"tear_name"]=name;
  9294. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9295. item[@"model_info"]=model_info;
  9296. item[@"off_params"]=off_params;
  9297. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9298. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9299. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9300. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9301. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9302. }];
  9303. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9304. return [RAUtils dict2data:dic];
  9305. }
  9306. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9307. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9308. // NSString *user = [params objectForKey:@"user"];
  9309. // 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];
  9310. 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];
  9311. int result = [iSalesDB execSql:sql];
  9312. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9313. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9314. if (result == RESULT_TRUE) {
  9315. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9316. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9317. sqlite3 *db = [iSalesDB get_db];
  9318. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9319. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9320. [iSalesDB close_db:db];
  9321. }
  9322. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9323. return [RAUtils dict2data:dic];
  9324. }
  9325. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9326. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9327. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9328. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9329. // Regurlar Price
  9330. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9331. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9332. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9333. __block long val_count = 0;
  9334. NSString *sql = @"select name,type,order_by from price order by order_by";
  9335. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9336. NSString *name = [self textAtColumn:0 statement:stmt];
  9337. int type = sqlite3_column_int(stmt, 1);
  9338. int order_by = sqlite3_column_int(stmt, 2);
  9339. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9340. NSDictionary *price_dic = @{
  9341. @"value" : name,
  9342. @"value_id" : [NSNumber numberWithInteger:type],
  9343. @"check" : order_by == 0 ? @(1) : @(0)
  9344. };
  9345. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9346. val_count = ++(*count);
  9347. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9348. val_count = 0;
  9349. }];
  9350. [cadedate setObject:@{@"value" : @"None",
  9351. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9352. val_count++;
  9353. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9354. [price setObject:cadedate forKey:@"cadedate"];
  9355. [section1 setObject:price forKey:@"item_2"];
  9356. [dic setObject:section1 forKey:@"section_1"];
  9357. return [RAUtils dict2data:dic];
  9358. }
  9359. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9360. NSString *product_id = [params objectForKey:@"fashionId"];
  9361. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9362. __block int result = RESULT_TRUE;
  9363. __block int qty = 0;
  9364. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9365. qty = sqlite3_column_int(stmt, 0);
  9366. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9367. result = RESULT_FALSE;
  9368. }];
  9369. NSMutableDictionary *dic = @{
  9370. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9371. @"mode" : @"Regular Mode",
  9372. @"quantity_available" : @(qty),
  9373. @"result" : @(result),
  9374. }.mutableCopy;
  9375. return [RAUtils dict2data:dic];
  9376. }
  9377. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9378. NSString *item_ids = [params objectForKey:@"item_id"];
  9379. NSString *line_notes = [params objectForKey:@"notes"];
  9380. NSString *price_str = [params objectForKey:@"price"];
  9381. NSString *discount_str = [params objectForKey:@"discount"];
  9382. NSString *percent = [params objectForKey:@"available_percent"];
  9383. NSString *qty = [params objectForKey:@"available_qty"];
  9384. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9385. int dot = 0;
  9386. if (line_notes) {
  9387. line_notes = [self translateSingleQuote:line_notes];
  9388. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9389. sql = [sql stringByAppendingString:line_notes];
  9390. dot = 1;
  9391. } else {
  9392. line_notes = @"";
  9393. }
  9394. if (price_str) {
  9395. if (dot) {
  9396. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9397. } else {
  9398. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9399. dot = 1;
  9400. }
  9401. sql = [sql stringByAppendingString:price_str];
  9402. } else {
  9403. price_str = @"";
  9404. }
  9405. if (discount_str) {
  9406. if (dot) {
  9407. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9408. } else {
  9409. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9410. dot = 1;
  9411. }
  9412. sql = [sql stringByAppendingString:discount_str];
  9413. } else {
  9414. discount_str = @"";
  9415. }
  9416. if (percent) {
  9417. if (dot) {
  9418. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9419. } else {
  9420. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9421. dot = 1;
  9422. }
  9423. sql = [sql stringByAppendingString:percent];
  9424. } else {
  9425. percent = @"";
  9426. }
  9427. if (qty) {
  9428. if (dot) {
  9429. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9430. } else {
  9431. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9432. dot = 1;
  9433. }
  9434. sql = [sql stringByAppendingString:qty];
  9435. } else {
  9436. qty = @"";
  9437. }
  9438. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9439. sql = [sql stringByAppendingString:where];
  9440. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9441. int result = [iSalesDB execSql:sql];
  9442. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9443. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9444. return [RAUtils dict2data:dic];
  9445. }
  9446. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9447. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9448. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9449. sqlite3 *db = [iSalesDB get_db];
  9450. 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];
  9451. int result = [iSalesDB execSql:sql db:db];
  9452. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9453. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9454. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9455. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9456. [iSalesDB close_db:db];
  9457. return [RAUtils dict2data:dic];
  9458. }
  9459. + (void)offline_removePDFWithName:(NSString *)name {
  9460. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9461. NSString *cachefolder = [paths objectAtIndex:0];
  9462. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9463. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9464. NSFileManager *fileManager = [NSFileManager defaultManager];
  9465. [fileManager removeItemAtPath:path error:nil];
  9466. }
  9467. + (void)offline_clear_PDFCache {
  9468. NSFileManager *fileManager = [NSFileManager defaultManager];
  9469. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9470. NSString *cachefolder = [paths objectAtIndex:0];
  9471. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9472. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9473. for (NSString *path in pdf_files) {
  9474. [self offline_removePDFWithName:[path lastPathComponent]];
  9475. }
  9476. }
  9477. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9478. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9479. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9480. NSString *user = [params objectForKey:@"user"];
  9481. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9482. if (![create_user isEqualToString:user]) {
  9483. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9484. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9485. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9486. return [RAUtils dict2data:dic];
  9487. }
  9488. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9489. __block int is_local = 0;
  9490. __block NSString *path = @"";
  9491. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9492. is_local = sqlite3_column_int(stmt, 0);
  9493. path = [self textAtColumn:1 statement:stmt];
  9494. }];
  9495. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9496. if (is_local == 1) {
  9497. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9498. // 删除文件
  9499. [self offline_removePDFWithName:path];
  9500. }
  9501. int result = [iSalesDB execSql:sql];
  9502. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9503. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9504. return [RAUtils dict2data:dic];
  9505. }
  9506. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9507. {
  9508. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9509. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9510. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9511. NSString* where=@"1=1";
  9512. if (ver!=nil) {
  9513. where=@"is_dirty=1";
  9514. }
  9515. 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];
  9516. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9517. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9518. sqlite3 *db = [iSalesDB get_db];
  9519. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9520. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9521. NSInteger _id = sqlite3_column_int(stmt, 0);
  9522. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9523. NSString *name = [self textAtColumn:2 statement:stmt];
  9524. NSString *desc = [self textAtColumn:3 statement:stmt];
  9525. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9526. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9527. NSInteger qty = sqlite3_column_int(stmt, 6);
  9528. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9529. double percent = sqlite3_column_double(stmt, 8);
  9530. double price = sqlite3_column_double(stmt, 9);
  9531. double discount = sqlite3_column_double(stmt, 10);
  9532. NSString *img = [self textAtColumn:11 statement:stmt];
  9533. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9534. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9535. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9536. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9537. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9538. if ([price_null isEqualToString:@"null"]) {
  9539. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9540. }
  9541. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9542. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9543. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9544. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9545. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9546. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9547. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9548. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9549. if ([qty_null isEqualToString:@"null"]) {
  9550. // [item setValue:@"null" forKey:@"available_qty"];
  9551. } else {
  9552. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9553. }
  9554. if ([is_percent_null isEqualToString:@"null"]) {
  9555. // [item setValue:@"null" forKey:@"percentage"];
  9556. } else {
  9557. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9558. }
  9559. if ([percent_null isEqualToString:@"null"]) {
  9560. // [item setValue:@"null" forKey:@"percent"];
  9561. } else {
  9562. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9563. }
  9564. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9565. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9566. [item setValue:name forKey:@"name"];
  9567. [item setValue:desc forKey:@"description"];
  9568. [item setValue:img forKey:@"img"];
  9569. [item setValue:line_note forKey:@"line_note"];
  9570. [item setValue:create_time forKey:@"createtime"];
  9571. [item setValue:modify_time forKey:@"modifytime"];
  9572. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9573. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9574. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9575. }];
  9576. [iSalesDB close_db:db];
  9577. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9578. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9579. }
  9580. return ret;
  9581. }
  9582. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9583. {
  9584. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9585. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9586. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9587. NSString* where=@"1=1";
  9588. if (ver!=nil) {
  9589. where=@"is_dirty=1";
  9590. }
  9591. 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];
  9592. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9593. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9594. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9595. NSInteger _id = sqlite3_column_int(stmt, 0);
  9596. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9597. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9598. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9599. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9600. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9601. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9602. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9603. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9604. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9605. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9606. NSString *uuid = [NSUUID UUID].UUIDString;
  9607. int is_delete = sqlite3_column_int(stmt, 11);
  9608. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9609. int is_local = sqlite3_column_int(stmt, 12);
  9610. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9611. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9612. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9613. }
  9614. [item setObject:pdf_path forKey:@"pdf_path"];
  9615. [item setObject:create_user forKey:@"create_user"];
  9616. [item setObject:tear_note forKey:@"tear_note"];
  9617. [item setObject:tear_name forKey:@"tear_name"];
  9618. [item setObject:model_info forKey:@"model_info"];
  9619. [item setObject:createtime forKey:@"createtime"];
  9620. [item setObject:modifytime forKey:@"modifytime"];
  9621. [item setObject:urlParams forKey:@"urlParams"];
  9622. [item setObject:off_params forKey:@"off_params"];
  9623. [item setObject:uuid forKey:@"pdf_token"];
  9624. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9625. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9626. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9627. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9628. } else {
  9629. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9630. }
  9631. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9632. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9633. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9634. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9635. }];
  9636. return ret;
  9637. }
  9638. @end