OLDataProvider.m 724 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "AppDelegate.h"
  15. #import "pdfCreator.h"
  16. #import "RASingleton.h"
  17. #import "QRCodeGenerator.h"
  18. #import "config.h"
  19. #import "ImageUtils.h"
  20. #import "RAConvertor.h"
  21. #import "ActiveViewController.h"
  22. #import "RANetwork.h"
  23. #import "RADataProvider.h"
  24. @interface OLDataProvider ()
  25. @end
  26. @implementation OLDataProvider
  27. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  28. {
  29. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  30. NSString* ret=@"No Price.";
  31. sqlite3_stmt * statement;
  32. // int count=0;
  33. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  34. {
  35. if (sqlite3_step(statement) == SQLITE_ROW)
  36. {
  37. char *price = (char*)sqlite3_column_text(statement, 0);
  38. if(price==nil)
  39. price="";
  40. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  41. double dp= [nsprice doubleValue];
  42. ret=[NSString stringWithFormat:@"%.2f",dp];
  43. }
  44. sqlite3_finalize(statement);
  45. }
  46. return ret;
  47. // // [iSalesDB close_db:db];
  48. //
  49. // return nil;
  50. //// if(dprice==DBL_MAX)
  51. //// ret= nil;
  52. //// else
  53. //// ret= [NSNumber numberWithDouble:dprice];
  54. //// return ret;
  55. }
  56. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  57. {
  58. NSString* ret= nil;
  59. NSString *sqlQuery = nil;
  60. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  61. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  62. sqlite3_stmt * statement;
  63. // int count=0;
  64. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  65. {
  66. if (sqlite3_step(statement) == SQLITE_ROW)
  67. {
  68. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  69. if(imgurl==nil)
  70. imgurl="";
  71. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  72. ret=nsimgurl;
  73. }
  74. sqlite3_finalize(statement);
  75. }
  76. else
  77. {
  78. ret=@"";
  79. }
  80. // [iSalesDB close_db:db];
  81. // DebugLog(@"data string: %@",ret );
  82. return ret;
  83. }
  84. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  85. {
  86. // assert(params[@"contact_id"]!=nil);
  87. assert(params[@"user"]!=nil);
  88. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  89. //生成portfolio pdf需要的数据
  90. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  91. data[@"npd_url"]=COMPANY_WEB;
  92. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  93. [formatter setDateFormat:@"MMMM yyyy"];
  94. NSString* date = [formatter stringFromDate:[NSDate date]];
  95. data[@"create_month"]=date;
  96. data[@"company_name"]=COMPANY_FULL_NAME;
  97. data[@"catalog_name"]=params[@"catalog_name"];
  98. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  99. data[@"TOTAL_PAGE"]=0;
  100. data[@"CURRENT_PAGE"]=0;
  101. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  102. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  103. bool show_stockout=[params[@"show_stockout"] boolValue];
  104. NSString *sql = nil;
  105. // v1.90 more color
  106. NSString *group_by = @"";
  107. if(values==nil)
  108. {
  109. if (show_group_by) {
  110. group_by = @"group by b.product_group ";
  111. }
  112. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  113. }
  114. else
  115. {
  116. if (show_group_by) {
  117. group_by = @"group by product_group ";
  118. }
  119. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  120. }
  121. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  122. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  123. sqlite3 *db = [iSalesDB get_db];
  124. // NSDictionary *resultDic =
  125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  126. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  127. int product_id = sqlite3_column_int(stmt, 0);
  128. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  129. NSString *name = [self textAtColumn:1 statement:stmt];
  130. NSString *description = [self textAtColumn:2 statement:stmt];
  131. double price = sqlite3_column_double(stmt, 3);
  132. NSString *s_price = [self textAtColumn:3 statement:stmt];
  133. double discount = sqlite3_column_double(stmt,4);
  134. int qty = sqlite3_column_int(stmt, 5);
  135. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  136. int is_percentage = sqlite3_column_int(stmt, 6);
  137. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  138. int item_id = sqlite3_column_int(stmt, 7);
  139. // int fashion_id = sqlite3_column_int(stmt, 8);
  140. NSString *line_note = [self textAtColumn:9 statement:stmt];
  141. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  142. double percent = sqlite3_column_double(stmt, 10);
  143. // int portfolio_id = sqlite3_column_int(stmt, 11);
  144. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  145. int availability = sqlite3_column_int(stmt, 12);
  146. NSString *color = [self textAtColumn:13 statement:stmt];
  147. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  148. NSString *demension = [self textAtColumn:15 statement:stmt];
  149. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  150. NSString *material = [self textAtColumn:17 statement:stmt];
  151. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  152. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  153. double volume = sqlite3_column_double(stmt, 19);
  154. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  155. double weight = sqlite3_column_double(stmt, 20);
  156. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  157. int model_set = sqlite3_column_int(stmt, 21);
  158. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  159. double load_ability = sqlite3_column_double(stmt, 22);
  160. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  161. NSString *assembling = [self textAtColumn:24 statement:stmt];
  162. NSString *made_in = [self textAtColumn:25 statement:stmt];
  163. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  164. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  165. float bprice=0;
  166. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  167. for(int b=0;b< [bundle[@"count"] intValue];b++)
  168. {
  169. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  170. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  171. }
  172. if(gprice!=nil)
  173. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  174. if(s_percent.length==0||is_percentage==0)
  175. {
  176. percent=100.0;
  177. if([s_qty isEqualToString:@"null"])
  178. qty=availability;
  179. }
  180. else
  181. {
  182. qty=availability;
  183. }
  184. qty=qty*percent/100+0.5;
  185. for(int i=0;i<[values[@"count"] intValue];i++)
  186. {
  187. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  188. if([valueitem[@"product_id"] intValue]==product_id)
  189. {
  190. line_note=valueitem[@"line_note"];
  191. qty=[valueitem[@"available_qty"] intValue];
  192. if([[valueitem allKeys] containsObject:@"available_qty"])
  193. s_qty=[NSString stringWithFormat:@"%d",qty];
  194. else
  195. s_qty=@"null";
  196. if(valueitem[@"regular_price"]!=nil)
  197. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  198. s_price=valueitem[@"special_price"];
  199. price=[valueitem[@"special_price"] floatValue];
  200. discount=0;
  201. // v1.90
  202. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  203. }
  204. }
  205. if(is_percentage==0&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  210. {
  211. return;
  212. }
  213. NSString* set_price=@"";
  214. if([params[@"entered_price"] boolValue])
  215. {
  216. if (s_price==nil ) {
  217. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  218. }
  219. else
  220. {
  221. if(price*(1-discount/100.0)!=gprice.floatValue)
  222. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  223. }
  224. }
  225. NSString* get_price=@"";
  226. {
  227. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  228. // DebugLog(@"price time interval");
  229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  230. if(gprice==nil)
  231. get_price=@"Price:No Price.";
  232. else
  233. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  234. }
  235. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  236. // if ([qty_null isEqualToString:@"null"]) {
  237. // qty=availability;
  238. // }
  239. //
  240. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  241. // if ([qty_percent_null isEqualToString:@"null"]) {
  242. // percentage=1;
  243. // }
  244. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  245. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  246. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  247. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  248. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  249. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  250. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  251. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  252. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  253. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  254. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  255. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  256. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  257. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  258. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  259. if([params[@"availability"] boolValue]==false)
  260. str_availability=@"";
  261. else
  262. {
  263. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  264. str_availability=@"<b>Availability:</b> In Production";
  265. }
  266. if([params[@"color"] boolValue]==false || color.length==0 )
  267. str_color=@"";
  268. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  269. str_model_set=@"";
  270. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  271. str_legcolor=@"";
  272. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  273. str_demension=@"";
  274. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  275. str_seat_height=@"";
  276. if([params[@"material"] boolValue]==false || material.length==0 )
  277. str_meterial=@"";
  278. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  279. str_box_dim=@"";
  280. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  281. str_volume=@"";
  282. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  283. str_weight=@"";
  284. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  285. str_load_ability=@"";
  286. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  287. str_fabric_content=@"";
  288. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  289. str_assembling=@"";
  290. if([params[@"made"] boolValue]==false || made_in.length==0 )
  291. str_made_in=@"";
  292. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  293. str_line_note=@"";
  294. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  295. [arr_detail addObject:str_availability];
  296. [arr_detail addObject:str_color];
  297. [arr_detail addObject:str_model_set];
  298. [arr_detail addObject:str_legcolor];
  299. [arr_detail addObject:str_demension];
  300. [arr_detail addObject:str_seat_height];
  301. [arr_detail addObject:str_meterial];
  302. [arr_detail addObject:str_box_dim];
  303. [arr_detail addObject:str_volume];
  304. [arr_detail addObject:str_weight];
  305. [arr_detail addObject:str_load_ability];
  306. [arr_detail addObject:str_fabric_content];
  307. [arr_detail addObject:str_assembling];
  308. [arr_detail addObject:str_made_in];
  309. [arr_detail addObject:str_line_note];
  310. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  311. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  312. //model image;
  313. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  314. // qr image
  315. NSString* qrpath=nil;
  316. if([params[@"show_barcode"] boolValue])
  317. {
  318. NSString* temp = NSTemporaryDirectory();
  319. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  320. qrpath=[temp stringByAppendingPathComponent:filename];
  321. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  322. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  323. }
  324. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  326. item[@"img"]=img_path;
  327. item[@"img_qr"]=qrpath;
  328. item[@"special_price"]=set_price;
  329. item[@"price"]=get_price;
  330. item[@"name"]=name;
  331. item[@"description"]=description;
  332. item[@"detail"]=detail;
  333. item[@"img"]=img_path;
  334. if (show_group_by && more_color_int != 0) {
  335. item[@"more_color"] = more_color_path;
  336. }
  337. // @{
  338. // //@"linenotes": line_note,
  339. //// @"product_id": product_id_string,
  340. //// @"available_qty": @(qty),
  341. //// @"available_percent" : @(percent),
  342. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  343. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  344. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"":
  352. // }.mutableCopy;
  353. // if (percentage) {
  354. // [item removeObjectForKey:@"available_qty"];
  355. // } else {
  356. // [item removeObjectForKey:@"available_percent"];
  357. // }
  358. //
  359. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  360. // if ([qty_null isEqualToString:@"null"]) {
  361. // [item removeObjectForKey:@"available_qty"];
  362. // }
  363. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  364. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  365. }];
  366. [iSalesDB close_db:db];
  367. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  368. // [dic setValue:@"" forKey:@"email_content"];
  369. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  370. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  371. //
  372. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  373. // cell[@"count"]=[NSNumber numberWithInt:10];
  374. grid[@"cell0"]=dic;
  375. data[@"grid0"]=grid;
  376. return data;
  377. }
  378. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  379. {
  380. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  381. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  382. NSString *cache_folder=[paths objectAtIndex:0];
  383. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  384. BOOL bdir=NO;
  385. NSFileManager* fileManager = [NSFileManager defaultManager];
  386. if(PDF_DEBUG)
  387. {
  388. NSData *data = [NSData dataWithContentsOfFile:default_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  393. {
  394. NSError * error=nil;
  395. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  396. {
  397. return nil;
  398. }
  399. }
  400. NSData *data = [NSData dataWithContentsOfFile:template_path];
  401. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  402. return ret;
  403. }
  404. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  405. {
  406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  407. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  408. DebugLog(@"offline_login sql:%@",sqlQuery);
  409. sqlite3_stmt * statement;
  410. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  411. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(statement) == SQLITE_ROW)
  414. {
  415. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  416. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  417. // int can_show_price = sqlite3_column_int(statement, 0);
  418. // int can_see_price = sqlite3_column_int(statement, 1);
  419. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  420. // if(contact_id==nil)
  421. // contact_id="";
  422. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  423. // int user_type = sqlite3_column_int(statement, 3);
  424. // int can_cancel_order = sqlite3_column_int(statement, 4);
  425. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  426. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  427. // int can_delete_order = sqlite3_column_int(statement, 7);
  428. // int can_submit_order = sqlite3_column_int(statement, 8);
  429. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  430. // int can_create_order = sqlite3_column_int(statement, 10);
  431. // char *mode = (char*)sqlite3_column_text(statement, 11);
  432. // if(mode==nil)
  433. // mode="";
  434. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  435. // char *username = (char*)sqlite3_column_text(statement, 12);
  436. // if(username==nil)
  437. // username="";
  438. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  439. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  440. char *first_name = (char*)sqlite3_column_text(statement, 14);
  441. if(first_name==nil)
  442. first_name="";
  443. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  444. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  445. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  446. // [header setValue:nscontact_id forKey:@"contact_id"];
  447. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  448. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  450. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  451. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  452. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  454. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  455. //
  456. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  457. //
  458. // [header setValue:nsusername forKey:@"username"];
  459. //
  460. // NSError* error=nil;
  461. //
  462. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  463. // [header setValue:statusFilter forKey:@"statusFilter"];
  464. //
  465. //
  466. //
  467. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  468. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  469. //
  470. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  471. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  472. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  473. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  474. //
  475. // [ret setObject:header forKey:@"header"];
  476. [ret setValue:nsfirst_name forKey:@"first_name"];
  477. }
  478. sqlite3_finalize(statement);
  479. }
  480. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  481. return ret;
  482. }
  483. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  484. {
  485. assert(user!=nil);
  486. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  487. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  488. data[@"npd_url"]=COMPANY_WEB;
  489. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  490. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  491. NSString* date = [formatter stringFromDate:[NSDate date]];
  492. data[@"print_date"]=date;
  493. data[@"company_name"]=COMPANY_FULL_NAME;
  494. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  495. data[@"TOTAL_PAGE"]=0;
  496. data[@"CURRENT_PAGE"]=0;
  497. // [formatter setDateFormat:@"MM/dd/yyyy"];
  498. // date = [formatter stringFromDate:[NSDate date]];
  499. // data[@"creat_date"]=date;
  500. NSString* temp = NSTemporaryDirectory();
  501. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  502. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  503. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  504. // bool bwrite=
  505. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  506. data[@"img_barcode"]=qrpath;
  507. data[@"so_id"]=order_code;
  508. data[@"company"]=COMPANY_FULL_NAME;
  509. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  510. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  511. sqlite3 *db = [iSalesDB get_db];
  512. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  513. sqlite3_stmt * statement;
  514. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  515. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  516. row0[@"count"]=[NSNumber numberWithInt:1];
  517. datasource[@"row0"]=row0;
  518. NSString* print_status=@"";
  519. NSString* order_type=@"Offline Quote";
  520. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  521. {
  522. if (sqlite3_step(statement) == SQLITE_ROW)
  523. {
  524. int erpOrderStatus = sqlite3_column_int(statement, 0);
  525. int status = sqlite3_column_int(statement, 26);
  526. if(status==2)
  527. {
  528. switch (erpOrderStatus) {
  529. case 10:
  530. // order_type=@"Quoted Order";
  531. print_status=@"Quote";
  532. break;
  533. case 11:
  534. print_status=@"Booked";
  535. break;
  536. case 12:
  537. print_status=@"Picked";
  538. break;
  539. case 13:
  540. print_status=@"Shipped";
  541. break;
  542. case 14:
  543. print_status=@"Closed";
  544. break;
  545. case 15:
  546. print_status=@"Canceled";
  547. break;
  548. case 16:
  549. print_status=@"All";
  550. break;
  551. case 18:
  552. print_status=@"Processing";
  553. break;
  554. default:
  555. break;
  556. }
  557. }
  558. else{
  559. switch (status) {
  560. case 0:
  561. {
  562. print_status=@"Quote";
  563. break;
  564. }
  565. case 1:
  566. {
  567. print_status=@"Saved";
  568. break;
  569. }
  570. case 3:
  571. {
  572. //canceled
  573. print_status=@"Canceled";
  574. break;
  575. }
  576. default:
  577. break;
  578. }
  579. }
  580. NSString* sender_name = [self textAtColumn:1 statement:statement];
  581. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  582. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  583. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  584. NSString* sender_email = [self textAtColumn:5 statement:statement];
  585. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  586. NSString* billing_name = [self textAtColumn:7 statement:statement];
  587. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  588. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  589. if(billing_phone.length>0)
  590. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  591. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  592. if(billing_fax.length>0)
  593. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  594. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  595. [billto_arr0 addObject:billing_phone];
  596. [billto_arr0 addObject:billing_fax];
  597. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  598. NSString* billing_email = [self textAtColumn:11 statement:statement];
  599. if(billing_email.length>0)
  600. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  601. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  602. if(billing_contact.length>0)
  603. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  604. NSString* receive_name = [self textAtColumn:12 statement:statement];
  605. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  606. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  607. if(receive_phone.length>0)
  608. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  609. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  610. if(receive_fax.length>0)
  611. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  612. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  613. [receive_arr0 addObject:receive_phone];
  614. [receive_arr0 addObject:receive_fax];
  615. NSString* receive_email = [self textAtColumn:16 statement:statement];
  616. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  617. if(receive_email.length>0)
  618. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  619. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  620. if(receive_contact.length>0)
  621. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  622. NSString* poNumber = [self textAtColumn:17 statement:statement];
  623. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  624. NSString* carrier = [self textAtColumn:19 statement:statement];
  625. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  626. NSString* terms = [self textAtColumn:21 statement:statement];
  627. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  628. NSString* port_destination = [self textAtColumn:23 statement:statement];
  629. NSString* etd = [self textAtColumn:24 statement:statement];
  630. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  631. if(sign_picpath.length==0)
  632. sign_picpath=nil;
  633. NSString* create_date = [self textAtColumn:27 statement:statement];
  634. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  635. NSString* general_notes=[self textAtColumn:33 statement:statement];
  636. NSString *customerID = [self textAtColumn:34 statement:statement];
  637. if (customerID.length == 0) {
  638. customerID = nil;
  639. }
  640. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  641. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  642. if(general_notes.length>0)
  643. {
  644. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  645. row2[@"item_0"]=@{@"g_notes":general_notes };
  646. row2[@"count"]=[NSNumber numberWithInt:1];
  647. datasource[@"row2"]=row2;
  648. }
  649. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  650. [billto_arr addObject:billing_ext];
  651. [billto_arr addObject:billing_pf];
  652. [billto_arr addObject:billing_contact];
  653. [billto_arr addObject:billing_email];
  654. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  655. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  656. [receive_arr addObject:receive_ext];
  657. [receive_arr addObject:receive_pf];
  658. [receive_arr addObject:receive_contact];
  659. [receive_arr addObject:receive_email];
  660. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  661. float shipping = sqlite3_column_double(statement, 28);
  662. float lift_gate_value = sqlite3_column_double(statement, 29);
  663. float handling_fee_value = sqlite3_column_double(statement, 30);
  664. data[@"company"]=sender_name;
  665. data[@"shipfrom_addr"]=sender_ext;
  666. data[@"shipfrom_phone"]=sender_phone;
  667. data[@"shipfrom_fax"]=sender_fax;
  668. data[@"shipfrom_email"]=sender_email;
  669. data[@"warehouse_name"]=warehouse_name;
  670. data[@"bill_to_name"]=billing_name;
  671. data[@"bill_to_ext"]=billto;
  672. data[@"ship_to_name"]=receive_name;
  673. data[@"ship_to_ext"]=receive;
  674. data[@"po_no"]=poNumber;
  675. data[@"rep"]=sales_rep;
  676. data[@"e_schdule"]=etd;
  677. data[@"sales_terms"]=sales_terms;
  678. data[@"port_destination"]=port_destination;
  679. data[@"terms"]=terms;
  680. data[@"truck_carrier"]=carrier;
  681. data[@"vendor_no"]=vendor_no;
  682. data[@"customer_sign"]=sign_picpath;
  683. data[@"print_status"]=print_status;
  684. data[@"create_date"]=create_date;
  685. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  686. sqlite3_stmt * statement_cart;
  687. // NSDate *date1 = [NSDate date];
  688. int count=0;
  689. // int cart_count=0;
  690. float credits=0;
  691. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  692. if ( dbresult== SQLITE_OK)
  693. {
  694. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  695. int row1count=0;
  696. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  697. {
  698. // NSDate *row_date = [NSDate date];
  699. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  700. // int product_id = sqlite3_column_int(statement_cart, 0);
  701. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  702. int item_id = sqlite3_column_int(statement_cart, 7);
  703. double discount = sqlite3_column_double(statement_cart, 2);
  704. NSString* Price=nil;
  705. if(str_price==nil)
  706. {
  707. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  708. // NSDate *price_date = [NSDate date];
  709. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  710. // DebugLog(@"price time interval");
  711. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  712. if(price==nil)
  713. Price=@"No Price.";
  714. else
  715. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  716. }
  717. else
  718. {
  719. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  720. }
  721. float dunitprice=0;
  722. int item_count = sqlite3_column_int(statement_cart, 3);
  723. NSString* Amount=@"";
  724. if(![Price isEqualToString:@"No Price."])
  725. {
  726. dunitprice=[Price floatValue];
  727. dunitprice=dunitprice* (1-discount/100.0);
  728. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  729. credits+=dunitprice*item_count;
  730. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  731. }
  732. else
  733. {
  734. Price=@"";
  735. }
  736. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  737. // NSString *nsline_note=nil;
  738. // if(line_note!=nil)
  739. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  740. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  741. NSString *nsname=nil;
  742. if(name!=nil)
  743. nsname= [[NSString alloc]initWithUTF8String:name];
  744. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  745. NSString *nsdescription=nil;
  746. if(description!=nil)
  747. nsdescription= [[NSString alloc]initWithUTF8String:description];
  748. // int stockUom = sqlite3_column_int(statement_cart, 8);
  749. // int _id = sqlite3_column_int(statement_cart, 9);
  750. // NSDate *subtotal_date = [NSDate date];
  751. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  752. // DebugLog(@"subtotal_date time interval");
  753. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  754. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  755. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  756. // int carton=[bsubtotaljson[@"carton"] intValue];
  757. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  758. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  759. // NSDate *img_date = [NSDate date];
  760. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  761. // DebugLog(@"img_date time interval");
  762. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  763. itemjson[@"model"]=nsname;
  764. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  765. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  766. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  767. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  768. // itemjson[@"check"]=@"true";
  769. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  770. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  771. itemjson[@"unit_price"]=Price;
  772. itemjson[@"amount"]=Amount;
  773. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  774. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  775. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  776. // itemjson[@"note"]=nsline_note;
  777. // NSDate *date2 = [NSDate date];
  778. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  779. // DebugLog(@"model_bundle time interval");
  780. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  781. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  782. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  783. row1count++;
  784. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  785. for(int c=0;c<[combine[@"count"] intValue];c++)
  786. {
  787. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  788. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  789. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  790. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  791. credits+=combine_amount;
  792. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  793. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  794. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  795. row1count++;
  796. }
  797. count++;
  798. // DebugLog(@"row time interval");
  799. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  800. }
  801. row1[@"count"]=[NSNumber numberWithInt:row1count];
  802. datasource[@"row1"]=row1;
  803. data[@"table_model"]=datasource;
  804. sqlite3_finalize(statement_cart);
  805. }
  806. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  807. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  808. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  809. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  810. // if(credits>0)
  811. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  812. // else
  813. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  814. if(shipping>0)
  815. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  816. else
  817. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  818. if(lift_gate_value>0)
  819. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  820. else
  821. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  822. if(handling_fee_value>0)
  823. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  824. else
  825. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  826. float total = credits+shipping+lift_gate_value+handling_fee_value;
  827. if(total>0)
  828. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  829. else
  830. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  831. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  832. fee[@"row0"]=fee_row0;
  833. data[@"table_fee"]=fee;
  834. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  835. // ret[@"sign_url"] = sign_url;
  836. //
  837. // customer_contact = [self textAtColumn:52 statement:statement];
  838. // customer_email = [self textAtColumn:53 statement:statement];
  839. // customer_phone = [self textAtColumn:54 statement:statement];
  840. // customer_fax = [self textAtColumn:55 statement:statement];
  841. //
  842. //
  843. // int offline_edit=sqlite3_column_int(statement, 56);
  844. //
  845. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  846. //
  847. // char *soid = (char*)sqlite3_column_text(statement, 1);
  848. // if(soid==nil)
  849. // soid= "";
  850. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  851. // // so#
  852. // ret[@"so#"] = nssoid;
  853. //
  854. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  855. // if(poNumber==nil)
  856. // poNumber= "";
  857. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  858. //
  859. //
  860. //
  861. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  862. // if(create_time==nil)
  863. // create_time= "";
  864. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  865. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  866. //
  867. // int status = sqlite3_column_int(statement, 4);
  868. // int erpStatus = sqlite3_column_int(statement, 49);
  869. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  870. //
  871. // // status
  872. // if (status > 1 && status != 3) {
  873. // status = erpStatus;
  874. // } else if (status == 3) {
  875. // status = 15;
  876. // }
  877. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  878. // ret[@"order_status"] = nsstatus;
  879. //
  880. //
  881. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  882. // if(company_name==nil)
  883. // company_name= "";
  884. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  885. // // company name
  886. // ret[@"company_name"] = nscompany_name;
  887. //
  888. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  889. // if(customer_contact==nil)
  890. // customer_contact= "";
  891. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  892. //
  893. //
  894. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  895. // if(addr_1==nil)
  896. // addr_1="";
  897. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  898. //
  899. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  900. // if(addr_2==nil)
  901. // addr_2="";
  902. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  903. //
  904. //
  905. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  906. // if(addr_3==nil)
  907. // addr_3="";
  908. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  909. //
  910. //
  911. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  912. // if(addr_4==nil)
  913. // addr_4="";
  914. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  915. //
  916. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  917. // [arr_addr addObject:nsaddr_1];
  918. // [arr_addr addObject:nsaddr_2];
  919. // [arr_addr addObject:nsaddr_3];
  920. // [arr_addr addObject:nsaddr_4];
  921. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  922. //
  923. //
  924. // char *logist = (char*)sqlite3_column_text(statement, 11);
  925. // if(logist==nil)
  926. // logist= "";
  927. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  928. // if (status == -11 || status == 10 || status == 11) {
  929. // nslogist = [self textAtColumn:59 statement:statement];
  930. // };
  931. //
  932. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  933. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  934. // shipping = @"Shipping To Be Quoted";
  935. // } else {
  936. // shippingFee = sqlite3_column_double(statement, 12);
  937. // }
  938. //
  939. // // Shipping
  940. // ret[@"Shipping"] = shipping;
  941. //
  942. // int have_lift_gate = sqlite3_column_int(statement, 17);
  943. // lift_gate = sqlite3_column_double(statement, 13);
  944. // // Liftgate Fee(No loading dock)
  945. // if (!have_lift_gate) {
  946. // lift_gate = 0;
  947. // }
  948. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  949. // if (sqlite3_column_int(statement, 57)) {
  950. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  951. // }
  952. //
  953. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  954. // if(general_notes==nil)
  955. // general_notes= "";
  956. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  957. //
  958. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  959. // if(internal_notes==nil)
  960. // internal_notes= "";
  961. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  962. //
  963. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  964. // if(payment_type==nil)
  965. // payment_type= "";
  966. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  967. //
  968. //
  969. //
  970. // // order info
  971. // orderinfo = [self textFileName:@"order_info.html"];
  972. //
  973. //
  974. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  975. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  978. //
  979. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  987. //
  988. // NSString *payment = nil;
  989. // if([nspayment_type isEqualToString:@"Credit Card"])
  990. // {
  991. // payment = [self textFileName:@"creditcardpayment.html"];
  992. //
  993. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  994. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  995. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  996. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  997. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  998. // NSString *card_type = [self textAtColumn:42 statement:statement];
  999. // if (card_type.length > 0) { // 显示星号
  1000. // card_type = @"****";
  1001. // }
  1002. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1003. // if (card_number.length > 0 && card_number.length > 4) {
  1004. // for (int i = 0; i < card_number.length - 4; i++) {
  1005. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1006. // }
  1007. // } else {
  1008. // card_number = @"";
  1009. // }
  1010. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1011. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1012. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1013. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1014. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1015. // card_expiration = @"****";
  1016. // }
  1017. //
  1018. //
  1019. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1020. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1021. //
  1022. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1023. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1024. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1025. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1026. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1029. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1030. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1031. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1032. //
  1033. //
  1034. // }
  1035. // else
  1036. // {
  1037. // payment=[self textFileName:@"normalpayment.html"];
  1038. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1039. // }
  1040. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1041. //
  1042. // ret[@"result"]= [NSNumber numberWithInt:2];
  1043. //
  1044. // // more info
  1045. // moreInfo = [self textFileName:@"more_info.html"];
  1046. //
  1047. //
  1048. // // ShipToCompany_or_&nbsp
  1049. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1051. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1053. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1055. //
  1056. //
  1057. // // ShipFromCompany_or_&nbsp
  1058. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1060. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1062. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1064. //
  1065. //
  1066. // // FreightBillToCompany_or_&nbsp
  1067. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1069. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1071. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1073. //
  1074. //
  1075. // // MerchandiseBillToCompany_or_&nbsp
  1076. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1078. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1079. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1080. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1081. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1082. //
  1083. //
  1084. // // ReturnToCompany_or_&nbsp
  1085. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1086. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1087. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1088. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1089. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1090. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1091. // //
  1092. //
  1093. // // DebugLog(@"more info : %@",moreInfo);
  1094. //
  1095. // // handling fee
  1096. // handlingFee = sqlite3_column_double(statement, 33);
  1097. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1098. // if (sqlite3_column_int(statement, 58)) {
  1099. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1100. // }
  1101. // //
  1102. //
  1103. // // customer info
  1104. // customerID = [self textAtColumn:36 statement:statement];
  1105. //
  1106. // // mode
  1107. // ret[@"mode"] = appDelegate.mode;
  1108. //
  1109. // // model_count
  1110. // ret[@"model_count"] = @(0);
  1111. }
  1112. sqlite3_finalize(statement);
  1113. }
  1114. [iSalesDB close_db:db];
  1115. data[@"order_type"]=order_type;
  1116. /*
  1117. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1118. // "customer_email" = "Shui Hu";
  1119. // "customer_fax" = "";
  1120. // "customer_first_name" = F;
  1121. // "customer_last_name" = L;
  1122. // "customer_name" = ",da He Xiang Dong Liu A";
  1123. // "customer_phone" = "Hey Xuan Feng";
  1124. contactInfo[@"customer_phone"] = customer_phone;
  1125. contactInfo[@"customer_fax"] = customer_fax;
  1126. contactInfo[@"customer_email"] = customer_email;
  1127. NSString *first_name = @"";
  1128. NSString *last_name = @"";
  1129. if ([customer_contact isEqualToString:@""]) {
  1130. } else if ([customer_contact containsString:@" "]) {
  1131. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1132. first_name = [customer_contact substringToIndex:first_space_index];
  1133. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1134. }
  1135. contactInfo[@"customer_first_name"] = first_name;
  1136. contactInfo[@"customer_last_name"] = last_name;
  1137. ret[@"customerInfo"] = contactInfo;
  1138. // models
  1139. if (nssoid) {
  1140. __block double TotalCuft = 0;
  1141. __block double TotalWeight = 0;
  1142. __block int TotalCarton = 0;
  1143. __block double allItemPrice = 0;
  1144. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1145. sqlite3 *db1 = [iSalesDB get_db];
  1146. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1147. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1148. int product_id = sqlite3_column_int(stmt, 0);
  1149. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1150. int item_id = sqlite3_column_int(stmt, 7);
  1151. NSString* Price=nil;
  1152. if(str_price==nil)
  1153. {
  1154. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1155. if(price==nil)
  1156. Price=@"No Price.";
  1157. else
  1158. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1159. }
  1160. else
  1161. {
  1162. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1163. }
  1164. double discount = sqlite3_column_double(stmt, 2);
  1165. int item_count = sqlite3_column_int(stmt, 3);
  1166. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1167. NSString *nsline_note=nil;
  1168. if(line_note!=nil)
  1169. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1170. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1171. // NSString *nsname = nil;
  1172. // if(name!=nil)
  1173. // nsname= [[NSString alloc]initWithUTF8String:name];
  1174. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1175. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1176. // NSString *nsdescription=nil;
  1177. // if(description!=nil)
  1178. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1179. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1180. // int stockUom = sqlite3_column_int(stmt, 8);
  1181. // int _id = sqlite3_column_int(stmt, 9);
  1182. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1183. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1184. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1185. int carton=[bsubtotaljson[@"carton"] intValue];
  1186. TotalCuft += cuft;
  1187. TotalWeight += weight;
  1188. TotalCarton += carton;
  1189. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1190. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1191. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1192. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1193. itemjson[@"note"]=nsline_note;
  1194. itemjson[@"origin_price"] = Price;
  1195. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1196. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1198. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1199. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1200. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1201. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1202. if(itemjson[@"combine"] != nil)
  1203. {
  1204. // int citem=0;
  1205. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1206. for(int bc=0;bc<bcount;bc++)
  1207. {
  1208. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1209. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1210. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1211. subTotal += uprice * modulus * item_count;
  1212. }
  1213. }
  1214. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1215. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1216. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1217. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1218. allItemPrice += subTotal;
  1219. }];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1223. // payments/Credits
  1224. // payments_and_credist = sqlite3_column_double(statement, 34);
  1225. payments_and_credist = allItemPrice;
  1226. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1227. // // total
  1228. // totalPrice = sqlite3_column_double(statement, 35);
  1229. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1230. } else {
  1231. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1234. // payments/Credits
  1235. payments_and_credist = 0;
  1236. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1237. }
  1238. // total
  1239. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1240. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1241. ret[@"order_info"]= orderinfo;
  1242. ret[@"more_order_info"] = moreInfo;
  1243. return [RAConvertor dict2data:ret];
  1244. */
  1245. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1246. return data;
  1247. }
  1248. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1249. {
  1250. assert(params[@"user"]!=nil);
  1251. NSString* template_file=nil;
  1252. switch([params[@"thumb"] intValue])
  1253. {
  1254. case 0:
  1255. template_file= @"so.json";
  1256. break;
  1257. case 1:
  1258. template_file= @"so_thumb.json";
  1259. break;
  1260. default:
  1261. template_file= @"so.json";
  1262. }
  1263. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1264. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1265. if(data[@"table_model"][@"row2"]==nil)
  1266. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1267. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1268. DebugLog(@"%@",file);
  1269. // return nil;
  1270. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1271. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1272. if (file) {
  1273. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1274. [dic setObject:file forKey:@"pdf_path"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAConvertor dict2data:dic];
  1277. }
  1278. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1279. dic[@"isLocalFile"]=@"true";
  1280. return [RAConvertor dict2data:dic];
  1281. }
  1282. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1283. {
  1284. // assert(params[@"contact_id"]!=nil);
  1285. // assert(params[@"user"]!=nil);
  1286. if(params[@"user"]==nil)
  1287. {
  1288. AppDelegate *appDelegate = nil;
  1289. //some UI methods ej
  1290. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1291. if(appDelegate.user!=nil)
  1292. [params setValue:appDelegate.user forKey:@"user"];
  1293. }
  1294. NSString* template_file=nil;
  1295. switch([params[@"pdf_style"] intValue])
  1296. {
  1297. case 0:
  1298. template_file= @"portfolio_2x3.json";
  1299. break;
  1300. case 1:
  1301. template_file= @"portfolio_3x2.json";
  1302. break;
  1303. default:
  1304. template_file= @"portfolio_3x2.json";
  1305. }
  1306. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1307. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1308. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1309. DebugLog(@"%@",file);
  1310. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1311. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1312. if (file) {
  1313. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1314. [dic setObject:file forKey:@"pdf_path"];
  1315. dic[@"isLocalFile"]=@"true";
  1316. return [RAConvertor dict2data:dic];
  1317. }
  1318. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1319. dic[@"isLocalFile"]=@"true";
  1320. return [RAConvertor dict2data:dic];
  1321. }
  1322. +(NSString*) get_offline_soid:(sqlite3*)db
  1323. {
  1324. NSString* soid=nil;
  1325. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1326. [formatter setDateFormat:@"yyMMdd"];
  1327. NSString* date = [formatter stringFromDate:[NSDate date]];
  1328. for(int i=1;i<999;i++)
  1329. {
  1330. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1331. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1332. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1333. if(count==0)
  1334. return soid;
  1335. }
  1336. return nil;
  1337. }
  1338. +(NSArray*) enumOfflineOrder
  1339. {
  1340. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1341. sqlite3 *db = [iSalesDB get_db];
  1342. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1343. sqlite3_stmt * statement;
  1344. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1345. if ( dbresult== SQLITE_OK)
  1346. {
  1347. while (sqlite3_step(statement) == SQLITE_ROW)
  1348. {
  1349. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1350. NSString* nsso_id=nil;
  1351. if(so_id!=nil)
  1352. {
  1353. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1354. }
  1355. [ret addObject:nsso_id];
  1356. }
  1357. sqlite3_finalize(statement);
  1358. }
  1359. [iSalesDB close_db:db];
  1360. return ret;
  1361. }
  1362. //+(void) uploadFile:(NSString*) file
  1363. //{
  1364. // NSData* data = [NSData dataWithContentsOfFile: file];
  1365. // UIApplication * app = [UIApplication sharedApplication];
  1366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1367. //
  1368. //
  1369. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1370. //
  1371. //
  1372. //
  1373. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1374. //
  1375. // if(appDelegate.user!=nil)
  1376. // [params setValue:appDelegate.user forKey:@"user"];
  1377. // // if(appDelegate.contact_id!=nil)
  1378. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1379. // if(appDelegate.password!=nil)
  1380. // [params setValue:appDelegate.password forKey:@"password"];
  1381. //
  1382. //
  1383. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1384. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1385. // } error:nil];
  1386. //
  1387. //
  1388. //
  1389. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1390. //
  1391. // NSProgress *progress = nil;
  1392. //
  1393. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1394. //
  1395. //
  1396. //
  1397. // if (error) {
  1398. //
  1399. // NSString* err_msg = [error localizedDescription];
  1400. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1401. //
  1402. //
  1403. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1404. //
  1405. // DebugLog(@"data string: %@",str);
  1406. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1407. //
  1408. // } else {
  1409. // DebugLog(@"response ");
  1410. //
  1411. //
  1412. //
  1413. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1414. //
  1415. // // 再将NSData转为字符串
  1416. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1417. //
  1418. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1419. //
  1420. // DebugLog(@"data string: %@",jsonStr);
  1421. //
  1422. // NSDictionary* json = responseObject;
  1423. //
  1424. //
  1425. // if([[json valueForKey:@"result"] intValue]==2)
  1426. // {
  1427. //// NSString* img_url_down = json[@"img_url_aname"];
  1428. //// NSString* img_url_up = json[@"img_url"];
  1429. //
  1430. //
  1431. // }
  1432. // else
  1433. // {
  1434. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1435. // }
  1436. //
  1437. //
  1438. // }
  1439. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1440. // }];
  1441. //
  1442. //
  1443. //
  1444. //
  1445. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1446. // //
  1447. //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. // [uploadTask resume];
  1454. //
  1455. //}
  1456. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1457. //{
  1458. //
  1459. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1460. // NSString *documents = [paths objectAtIndex:0];
  1461. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1462. //
  1463. //
  1464. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1465. //
  1466. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1467. //
  1468. //
  1469. //
  1470. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1471. //
  1472. //
  1473. //
  1474. //
  1475. //
  1476. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1477. //
  1478. //
  1479. //
  1480. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1481. //
  1482. //
  1483. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1484. //
  1485. //
  1486. //
  1487. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1488. //
  1489. //
  1490. //
  1491. //
  1492. // NSMutableDictionary* orderlist = nil;
  1493. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1494. //
  1495. //
  1496. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1497. //
  1498. //
  1499. // orderlist = [NSMutableDictionary new];
  1500. //
  1501. //// [orderlist addObject:appDelegate.order_code];
  1502. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1503. //
  1504. // }
  1505. // else
  1506. // {
  1507. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1508. //
  1509. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1510. //
  1511. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1512. // }
  1513. //
  1514. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1515. // orderitem[@"create_by"] = appDelegate.user;
  1516. // orderitem[@"price"] = upparams[@"total_price"];
  1517. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1518. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1519. // orderitem[@"order_status"] = @"Saved Order";
  1520. // orderitem[@"order_code"] = appDelegate.order_code;
  1521. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1522. //
  1523. //
  1524. // int count = [orderlist[@"count"] intValue];
  1525. //
  1526. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1527. //
  1528. // newlist[@"item_0"]=orderitem;
  1529. // int newcount = 1;
  1530. // for(int i=0;i<count;i++)
  1531. // {
  1532. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1533. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1534. // continue;
  1535. // else
  1536. // {
  1537. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1538. // newcount++;
  1539. // }
  1540. // }
  1541. // newlist[@"count"] = @(newcount);
  1542. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1543. //
  1544. // return;
  1545. //}
  1546. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1547. {
  1548. if(submitlist==nil)
  1549. return;
  1550. NSString *userdir = [OLDataProvider getUserPath];
  1551. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1552. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1553. }
  1554. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1555. {
  1556. if(orderlist==nil)
  1557. return;
  1558. NSString *userdir = [OLDataProvider getUserPath];
  1559. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1560. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1561. }
  1562. +(NSMutableDictionary*) getScanSubmitList
  1563. {
  1564. NSString *userdir = [OLDataProvider getUserPath];
  1565. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1566. NSMutableDictionary* submitlist = nil;
  1567. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1568. submitlist = [NSMutableDictionary new];
  1569. }
  1570. else
  1571. {
  1572. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1573. }
  1574. return submitlist;
  1575. }
  1576. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1577. //// NSString *userdir = [self getUserPath:user];
  1578. // NSMutableDictionary* orderlist = nil;
  1579. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1580. //
  1581. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1582. //
  1583. //
  1584. // orderlist = [NSMutableDictionary new];
  1585. //
  1586. // // [orderlist addObject:appDelegate.order_code];
  1587. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1588. //
  1589. // }
  1590. // else
  1591. // {
  1592. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1593. //
  1594. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1595. //
  1596. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1597. // }
  1598. // return orderlist;
  1599. //}
  1600. +(NSMutableDictionary*) getScanOrderList{
  1601. NSString *userdir = [OLDataProvider getUserPath];
  1602. NSMutableDictionary* orderlist = nil;
  1603. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1604. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1605. orderlist = [NSMutableDictionary new];
  1606. // [orderlist addObject:appDelegate.order_code];
  1607. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1608. }
  1609. else
  1610. {
  1611. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1612. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1613. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1614. }
  1615. return orderlist;
  1616. }
  1617. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1618. {
  1619. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1620. // NSString *documents = [paths objectAtIndex:0];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. // NSString *userdir = [OLDataProvider getUserPath];
  1623. NSMutableDictionary* orderlist = [self getScanOrderList];
  1624. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1625. int count = [orderlist[@"count"] intValue];
  1626. if(count==0)
  1627. {
  1628. // order list 为空,不用维护
  1629. #ifdef RA_NOTIFICATION
  1630. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1631. #else
  1632. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1633. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1634. #endif
  1635. return;
  1636. }
  1637. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1638. //
  1639. // // order list 为空,不用维护
  1640. //
  1641. //
  1642. // // [orderlist addObject:appDelegate.order_code];
  1643. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1644. //
  1645. // }
  1646. // else
  1647. // {
  1648. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1649. //
  1650. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1651. //
  1652. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1653. // }
  1654. //==================== order list 删除
  1655. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1656. NSMutableDictionary* submititem =nil;
  1657. int newcount = 0;
  1658. for(int i=0;i<count;i++)
  1659. {
  1660. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1661. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1662. {
  1663. submititem = item;
  1664. continue;
  1665. }
  1666. else
  1667. {
  1668. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1669. newcount++;
  1670. }
  1671. }
  1672. if(submititem==nil)
  1673. {
  1674. //order list 里没有和当前打开的order code一致的order
  1675. //这种情况往往是因为重复点提交按钮,order已关闭。
  1676. return;
  1677. }
  1678. newlist[@"count"] = @(newcount);
  1679. [self saveScanOrderList:newlist];
  1680. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1681. //////////////////---=============================== submitlist 添加
  1682. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1683. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1684. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1685. //
  1686. // submitlist = [NSMutableDictionary new];
  1687. //
  1688. //
  1689. // }
  1690. // else
  1691. // {
  1692. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1693. //
  1694. //
  1695. // }
  1696. int submitcount =[submitlist[@"count"] intValue];
  1697. submititem[@"order_status"] = soid;//@"Submitted Order";
  1698. submititem[@"customer_name"] = company;//@"Submitted Order";
  1699. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1700. submitlist[@"count"] = @(submitcount+1);
  1701. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1702. [self saveScanSubmitList:submitlist];
  1703. #ifdef RA_NOTIFICATION
  1704. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1705. #else
  1706. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1707. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1708. #endif
  1709. }
  1710. +(void) save2submitScanOrder1
  1711. {
  1712. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1713. // NSString *documents = [paths objectAtIndex:0];
  1714. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1715. // NSString *userdir = [OLDataProvider getUserPath];
  1716. NSMutableDictionary* orderlist = [self getScanOrderList];
  1717. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1718. int count = [orderlist[@"count"] intValue];
  1719. if(count==0)
  1720. {
  1721. // order list 为空,不用维护
  1722. #ifdef RA_NOTIFICATION
  1723. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1724. #else
  1725. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1726. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1727. #endif
  1728. return;
  1729. return;
  1730. }
  1731. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1732. int newcount = 0;
  1733. for(int i=0;i<count;i++)
  1734. {
  1735. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1736. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1737. continue;
  1738. else
  1739. {
  1740. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1741. newcount++;
  1742. }
  1743. }
  1744. newlist[@"count"] = @(newcount);
  1745. [self saveScanOrderList:newlist];
  1746. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1747. #ifdef RA_NOTIFICATION
  1748. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1749. #else
  1750. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1751. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1752. #endif
  1753. }
  1754. //+(void) createScanTempFolder
  1755. //{
  1756. // return;
  1757. //// NSString* scanFilePath = [self getScanTempPath];
  1758. //// BOOL bdir=YES;
  1759. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1760. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1761. //// {
  1762. ////
  1763. //// NSError *error = nil;
  1764. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1765. ////
  1766. //// if(!bsuccess)
  1767. //// DebugLog(@"Create cache folder failed");
  1768. ////
  1769. //// // if(bsuccess)
  1770. //// // {
  1771. //// // sqlite3 *db = [self get_db];
  1772. //// //
  1773. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1774. //// // [iSalesDB close_db:db];
  1775. //// // }
  1776. //// }
  1777. //}
  1778. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1779. {
  1780. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1781. NSString *documents = [paths objectAtIndex:0];
  1782. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1783. NSString* servername = addressDic[@"name"];
  1784. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1785. NSString* user = appDelegate.user;
  1786. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1787. orderpath = [orderpath stringByAppendingPathComponent:user];
  1788. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1789. BOOL bdir=YES;
  1790. NSFileManager* fileManager = [NSFileManager defaultManager];
  1791. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1792. {
  1793. NSError *error = nil;
  1794. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1795. if(!bsuccess)
  1796. DebugLog(@"Create cache folder failed");
  1797. // if(bsuccess)
  1798. // {
  1799. // sqlite3 *db = [self get_db];
  1800. //
  1801. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1802. // [iSalesDB close_db:db];
  1803. // }
  1804. }
  1805. return orderpath;
  1806. }
  1807. +(NSString*) getScanOrderPath
  1808. {
  1809. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1810. NSString *documents = [paths objectAtIndex:0];
  1811. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1812. NSString* servername = addressDic[@"name"];
  1813. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1814. NSString* user = appDelegate.user;
  1815. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1816. orderpath = [orderpath stringByAppendingPathComponent:user];
  1817. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1818. BOOL bdir=YES;
  1819. NSFileManager* fileManager = [NSFileManager defaultManager];
  1820. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1821. {
  1822. NSError *error = nil;
  1823. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1824. if(!bsuccess)
  1825. DebugLog(@"Create cache folder failed");
  1826. // if(bsuccess)
  1827. // {
  1828. // sqlite3 *db = [self get_db];
  1829. //
  1830. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1831. // [iSalesDB close_db:db];
  1832. // }
  1833. }
  1834. return orderpath;
  1835. }
  1836. //+(NSString*) getScanTempPath
  1837. //{
  1838. // return nil;
  1839. ////
  1840. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1841. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1842. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1843. //}
  1844. //+(void) moveScanTemp2Order
  1845. //{
  1846. // return;
  1847. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1848. ////
  1849. //// NSError *error = nil;
  1850. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1851. ////
  1852. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1853. //// // for(int i=0;i<arr_files.count;i++)
  1854. //// // {
  1855. //// // NSString* file=arr_files[i];
  1856. //// //
  1857. //// //
  1858. //// // }
  1859. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1860. ////
  1861. //
  1862. //}
  1863. //+(NSString*) getUserPath: (NSString*) user
  1864. //{
  1865. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1866. // NSString *documents = [paths objectAtIndex:0];
  1867. //
  1868. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1869. //
  1870. // NSString* servername = addressDic[@"name"];
  1871. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1872. //// NSString* user = appDelegate.user;
  1873. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1874. // userpath = [userpath stringByAppendingPathComponent:user];
  1875. // return userpath;
  1876. //}
  1877. +(NSString*) getServerPath
  1878. {
  1879. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1880. NSString *documents = [paths objectAtIndex:0];
  1881. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1882. NSString* servername = addressDic[@"name"];
  1883. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1884. // NSString* user = appDelegate.user;
  1885. // NSString* user = RASingleton.sharedInstance.user;
  1886. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1887. // userpath = [userpath stringByAppendingPathComponent:user];
  1888. return serverpath;
  1889. }
  1890. +(NSString*) getUserPath
  1891. {
  1892. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1893. NSString *documents = [paths objectAtIndex:0];
  1894. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1895. NSString* servername = addressDic[@"name"];
  1896. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1897. // NSString* user = appDelegate.user;
  1898. NSString* user = RASingleton.sharedInstance.user;
  1899. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1900. userpath = [userpath stringByAppendingPathComponent:user];
  1901. return userpath;
  1902. }
  1903. +(bool) isSavedScanOrder:(NSString*) order_code
  1904. {
  1905. if(order_code == nil)
  1906. return false;
  1907. NSMutableDictionary * orderlist=[self getScanOrderList];
  1908. int count = [orderlist[@"count"] intValue];
  1909. for(int i=0;i<count;i++)
  1910. {
  1911. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1912. if([item[@"order_code"] isEqualToString: order_code ])
  1913. {
  1914. return true;
  1915. }
  1916. }
  1917. return false;
  1918. }
  1919. +(NSString*) getScanPath
  1920. {
  1921. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1922. if(appDelegate.order_code==nil)
  1923. {
  1924. appDelegate.order_code=[[NSUUID new] UUIDString];
  1925. }
  1926. return [self getScanOrderPath];
  1927. // else
  1928. // {
  1929. // [self createScanTempFolder];
  1930. // return [self getScanTempPath];
  1931. // }
  1932. }
  1933. +(void) scanCloseOrder
  1934. {
  1935. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1936. // if(appDelegate.user.length<=0)
  1937. // {
  1938. // [RAUtils message_alert:@"Please login first." title:@"Warring" controller:self];
  1939. // return;
  1940. // }
  1941. // if(appDelegate.order_code.length>0)
  1942. {
  1943. // 当前订单先关闭
  1944. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1945. appDelegate.order_code = nil;
  1946. // RASingleton.sharedInstance.scan_temp_code = nil;
  1947. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1948. { appDelegate.customerInfo = nil;
  1949. appDelegate.contact_id = nil;
  1950. }
  1951. RASingleton.sharedInstance.scan_cart = nil;
  1952. RASingleton.sharedInstance.price_type = 1;
  1953. // [appDelegate updateScanButton:false];
  1954. [appDelegate update_count_mark];
  1955. [appDelegate closeOrder];
  1956. }
  1957. }
  1958. +(NSString*) createScanTempCode
  1959. {
  1960. return [[NSUUID new] UUIDString];
  1961. }
  1962. +(NSString*) scanTemplatePath:(NSString*)file
  1963. {
  1964. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1965. NSString *documents = [paths objectAtIndex:0];
  1966. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1967. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1968. return templatefile;
  1969. }
  1970. +(void) realguestLogin
  1971. {
  1972. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1973. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1974. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1975. }
  1976. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1977. {
  1978. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1979. NSString *documents = [paths objectAtIndex:0];
  1980. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1981. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1982. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1983. return [dict mutableCopy];
  1984. }
  1985. +(void) deleteSubmittedOrder:(NSString*) order_code
  1986. {
  1987. NSString *userdir = [OLDataProvider getUserPath];
  1988. if(order_code.length==0)
  1989. return;
  1990. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1991. // NSString *documents = [paths objectAtIndex:0];
  1992. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1993. // if([appDelegate.order_code isEqualToString:order_code])
  1994. // {
  1995. // //要删除的订单是当前打开的订单;
  1996. // appDelegate.order_code = nil;
  1997. // appDelegate.customerInfo = nil;
  1998. // RASingleton.sharedInstance.scan_cart = nil;
  1999. // appDelegate.contact_id = nil;
  2000. // // [appDelegate updateScanButton:false];
  2001. //
  2002. // [appDelegate update_count_mark];
  2003. // }
  2004. //删除订单目录
  2005. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2006. //维护订单列表
  2007. NSMutableDictionary* submitlist = [self getScanSubmitList];
  2008. int count = [submitlist[@"count"] intValue];
  2009. if(count==0)
  2010. {
  2011. // order list 为空,不用维护
  2012. #ifdef RA_NOTIFICATION
  2013. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2014. #else
  2015. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2016. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2017. #endif
  2018. return;
  2019. }
  2020. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  2021. //
  2022. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2023. //
  2024. //
  2025. //
  2026. // // [orderlist addObject:appDelegate.order_code];
  2027. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2028. //
  2029. // }
  2030. // else
  2031. // {
  2032. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2033. //
  2034. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2035. //
  2036. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2037. // }
  2038. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2039. int newcount = 0;
  2040. for(int i=0;i<count;i++)
  2041. {
  2042. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  2043. if([item[@"order_code"] isEqualToString: order_code ])
  2044. continue;
  2045. else
  2046. {
  2047. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2048. newcount++;
  2049. }
  2050. }
  2051. newlist[@"count"] = @(newcount);
  2052. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2053. [self saveScanSubmitList:newlist];
  2054. #ifdef RA_NOTIFICATION
  2055. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2056. #else
  2057. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2058. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2059. #endif
  2060. }
  2061. +(void) updateScanOrder:(NSString*) order_code
  2062. {
  2063. #ifdef SCANNER_ORDER
  2064. NSString *userdir = [OLDataProvider getUserPath];
  2065. if(order_code.length==0)
  2066. return;
  2067. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2068. // NSString *documents = [paths objectAtIndex:0];
  2069. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2070. NSMutableDictionary* orderlist = [self getScanOrderList];
  2071. int count = [orderlist[@"count"] intValue];
  2072. if(count==0)
  2073. {
  2074. // order list 为空,不用维护
  2075. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2076. return;
  2077. }
  2078. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2079. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2080. //
  2081. //
  2082. //
  2083. // // [orderlist addObject:appDelegate.order_code];
  2084. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2085. //
  2086. // }
  2087. // else
  2088. // {
  2089. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2090. //
  2091. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2092. //
  2093. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2094. // }
  2095. //
  2096. //
  2097. // int count = [orderlist[@"count"] intValue];
  2098. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  2099. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2100. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2101. float p =0;
  2102. float s =0;
  2103. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  2104. {
  2105. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  2106. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  2107. int mcount = [cartitem[@"count"] intValue];
  2108. double msurcharge =0;
  2109. // remove net price
  2110. // if(RASingleton.sharedInstance.price_type==1)
  2111. // {
  2112. // if([cartitem[@"special_price"] boolValue])
  2113. // {
  2114. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2115. // }
  2116. // else
  2117. // {
  2118. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2119. // }
  2120. // }
  2121. s+=msurcharge;
  2122. }
  2123. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  2124. int order_qty=0;
  2125. for(int c=0;c<[section[@"count"] intValue];c++)
  2126. {
  2127. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  2128. order_qty+=[item[@"count"] intValue];
  2129. }
  2130. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  2131. // int newcount = 0;
  2132. for(int i=0;i<count;i++)
  2133. {
  2134. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2135. if([item[@"order_code"] isEqualToString: order_code ])
  2136. {
  2137. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  2138. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  2139. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  2140. }
  2141. }
  2142. // newlist[@"count"] = @(newcount);
  2143. [self saveScanOrderList:orderlist];
  2144. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2145. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2146. #endif
  2147. }
  2148. +(void) deleteScanOrder:(NSString*) order_code
  2149. {
  2150. NSString *userdir = [OLDataProvider getUserPath];
  2151. if(order_code.length==0)
  2152. return;
  2153. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2154. // NSString *documents = [paths objectAtIndex:0];
  2155. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2156. if([appDelegate.order_code isEqualToString:order_code])
  2157. {
  2158. //要删除的订单是当前打开的订单;
  2159. // appDelegate.order_code = nil;
  2160. // appDelegate.customerInfo = nil;
  2161. // RASingleton.sharedInstance.scan_cart = nil;
  2162. // appDelegate.contact_id = nil;
  2163. [self scanCloseOrder];
  2164. // [appDelegate updateScanButton:false];
  2165. [appDelegate update_count_mark];
  2166. }
  2167. //删除订单目录
  2168. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2169. //维护订单列表
  2170. NSMutableDictionary* orderlist = [self getScanOrderList];
  2171. int count = [orderlist[@"count"] intValue];
  2172. if(count==0)
  2173. {
  2174. // order list 为空,不用维护
  2175. #ifdef RA_NOTIFICATION
  2176. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2177. #else
  2178. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2179. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2180. #endif
  2181. return;
  2182. }
  2183. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2184. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2185. //
  2186. //
  2187. //
  2188. // // [orderlist addObject:appDelegate.order_code];
  2189. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2190. //
  2191. // }
  2192. // else
  2193. // {
  2194. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2195. //
  2196. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2197. //
  2198. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2199. // }
  2200. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2201. int newcount = 0;
  2202. for(int i=0;i<count;i++)
  2203. {
  2204. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2205. if([item[@"order_code"] isEqualToString: order_code ])
  2206. continue;
  2207. else
  2208. {
  2209. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2210. newcount++;
  2211. }
  2212. }
  2213. newlist[@"count"] = @(newcount);
  2214. [self saveScanOrderList:newlist];
  2215. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2216. #ifdef RA_NOTIFICATION
  2217. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2218. #else
  2219. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2220. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2221. #endif
  2222. }
  2223. #ifdef SCANNER_ORDER
  2224. +(void) scanRefreshCart
  2225. {
  2226. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2227. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2228. NSMutableDictionary * section = cart[@"section_0"];
  2229. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2230. int count = [section[@"count"] intValue];
  2231. int newcount = 0;
  2232. for(int i=0;i<count;i++)
  2233. {
  2234. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2235. NSString* model = item[@"model"];
  2236. NSDictionary* modeljson=appDelegate.scan_model[model];
  2237. if(modeljson==nil)
  2238. continue;
  2239. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2240. cartitem[@"note"]=item[@"note"];
  2241. cartitem[@"discount"]=item[@"discount"];
  2242. // [item removeObjectForKey:@"discount"];
  2243. // [item removeObjectForKey:@"note"];
  2244. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2245. newcount++;
  2246. }
  2247. newsection[@"count"]=@(newcount);
  2248. newsection[@"available"] = @(1);
  2249. newsection[@"title"] = @"Available List";
  2250. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2251. }
  2252. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2253. {
  2254. // _modelJson = modelJson;
  2255. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2256. // self.labelPrice0.text = appDelegate.price0_name;
  2257. // self.labelPrice1.text = appDelegate.price1_name;
  2258. // self.labelPrice2.text = appDelegate.price2_name;
  2259. // self.labelPrice3.text = appDelegate.price3_name;
  2260. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2261. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2262. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2263. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2264. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2265. NSLog( [RAConvertor dict2string:_modelJson]);
  2266. NSString * port=_modelJson[@"port"];
  2267. if(port.length==0)
  2268. port = @"N/A";
  2269. NSString * origin=_modelJson[@"origin"];
  2270. if(origin.length==0)
  2271. origin = @"N/A";
  2272. NSString * dimension=_modelJson[@"dimension"];
  2273. if(dimension.length==0)
  2274. dimension = @"N/A";
  2275. // _labelModel.text=_modelJson[@"model"];
  2276. // _labelDescription.text=_modelJson[@"description"];
  2277. // _labelDimension.text=dimension;
  2278. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2279. // _labelOrigin.text=origin;
  2280. // _labelPort.text=port;
  2281. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2282. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2283. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2284. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2285. if(![price0 isEqualToString: @"N/A"])
  2286. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2287. if(![price1 isEqualToString: @"N/A"])
  2288. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2289. if(![price2 isEqualToString: @"N/A"])
  2290. {
  2291. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2292. // if(RASingleton.sharedInstance.price_type==1)
  2293. _modelJson [@"special_price"] = @true;
  2294. }
  2295. if(![price3 isEqualToString: @"N/A"])
  2296. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2297. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2298. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2299. {
  2300. // if(RASingleton.sharedInstance.price_type==1)
  2301. _modelJson [@"net_price"] = @true;
  2302. // remove net price
  2303. _modelJson [@"net_price"] = @false;
  2304. }
  2305. //
  2306. // if(![price1 isEqualToString:@"N/A"])
  2307. // {
  2308. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2309. // _modelJson[@"price2"]= price2;
  2310. // }
  2311. // else
  2312. // {
  2313. // price2=@"N/A";
  2314. // }
  2315. //
  2316. // if([price0 isEqualToString:@"N/A"])
  2317. // _labelPriceCTNR.text= @"";//price0;
  2318. // else
  2319. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2320. //
  2321. // if([price3 isEqualToString:@"N/A"])
  2322. // _labelPriceNCA.text= price3;
  2323. // else
  2324. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2325. // _labelPrice25p.text=price2;
  2326. //
  2327. // if([_modelJson [@"special_price"] boolValue])
  2328. // {
  2329. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2330. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2331. // else
  2332. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2333. // }
  2334. // else
  2335. // {
  2336. // _labelPriceSpecial.text=@"";//@"N/A";
  2337. // }
  2338. //
  2339. // if([_modelJson [@"net_price"] boolValue])
  2340. // {
  2341. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2342. //// {
  2343. //// _labelPriceNet.text=_modelJson[@"price1"];
  2344. ////// _labelPriceNCA.text=@"N/A";
  2345. //// }
  2346. //// else
  2347. //// {
  2348. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2349. ////// _labelPriceNCA.text=@"N/A";
  2350. //// }
  2351. //
  2352. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2353. // {
  2354. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2355. //// _labelPriceNCA.text=@"N/A";
  2356. // }
  2357. // else
  2358. // {
  2359. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2360. //// _labelPriceNCA.text=@"N/A";
  2361. // }
  2362. //
  2363. // }
  2364. // else
  2365. // {
  2366. // _labelPriceNet.text=@"";//@"N/A";
  2367. // }
  2368. //
  2369. // {
  2370. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2371. // {
  2372. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2373. //// _labelPriceNCA.text=@"N/A";
  2374. // }
  2375. // else
  2376. // {
  2377. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2378. //// _labelPriceNet.text=@"N/A";
  2379. // }
  2380. // }
  2381. NSString * available=_modelJson[@"available"];
  2382. if(available.length==0)
  2383. available = @"N/A";
  2384. // _labelAvailable.text=available;
  2385. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2386. if(unit_cuft.length==0)
  2387. unit_cuft = @"N/A";
  2388. else
  2389. {
  2390. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2391. }
  2392. NSString * cell_price;
  2393. if(RASingleton.sharedInstance.price_type==1)
  2394. {
  2395. // if(_modelJson [@"net_price"])
  2396. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2397. // else if(_modelJson [@"special_price"])
  2398. // {
  2399. // unit_price= _modelJson[@"price2"];
  2400. // }
  2401. // else
  2402. // {
  2403. // unit_price= _modelJson[@"price1"];
  2404. // }
  2405. }
  2406. else
  2407. {
  2408. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2409. }
  2410. NSString * mpack=_modelJson[@"stockUom"];
  2411. if([mpack isEqualToString:@"N/A"])
  2412. mpack=@"1";
  2413. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2414. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2415. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2416. NSMutableDictionary* jitem = nil;
  2417. jitem = [_modelJson mutableCopy];
  2418. int stockUom =[jitem[@"stockUom"] intValue];
  2419. if(stockUom==0)
  2420. stockUom=1;
  2421. bool newitem = true;
  2422. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2423. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2424. // for(int i=0;i<count;i++)
  2425. // {
  2426. //
  2427. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2428. //
  2429. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2430. // {
  2431. //
  2432. //// int oldcount = [litem[@"stockUom"] intValue];
  2433. // newitem = false;
  2434. //
  2435. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2436. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2437. //
  2438. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2439. // break;
  2440. //
  2441. //
  2442. // }
  2443. //
  2444. //
  2445. //
  2446. // }
  2447. // if(true)
  2448. // {
  2449. jitem[@"count"]=@(stockUom);
  2450. jitem[@"check"]=@(true);
  2451. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2452. NSString * unit_price;
  2453. if(RASingleton.sharedInstance.price_type==0)
  2454. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2455. else if(_modelJson [@"special_price"])
  2456. {
  2457. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2458. }
  2459. else if(_modelJson [@"net_price"])
  2460. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2461. else
  2462. {
  2463. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2464. }
  2465. if([unit_price isEqualToString:@"N/A"])
  2466. unit_price = @"0";
  2467. else
  2468. {
  2469. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2470. }
  2471. jitem[@"unit_price"] = unit_price;
  2472. jitem[@"erp_unit_price"] = unit_price;
  2473. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2474. // section[@"count"]= @(count+1);
  2475. // count++;
  2476. // }
  2477. return jitem;
  2478. }
  2479. +(void) saveScanCart:(NSMutableDictionary*) cart
  2480. {
  2481. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2482. if(appDelegate.order_code.length<=0)
  2483. return;
  2484. if(cart==nil)
  2485. return;
  2486. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2487. // NSString *documents = [paths objectAtIndex:0];
  2488. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2489. if(RASingleton.sharedInstance.price_type==0)
  2490. {
  2491. NSMutableDictionary * section = cart[@"section_0"];
  2492. int count = [section[@"count"] intValue];
  2493. for(int i=0;i<count;i++)
  2494. {
  2495. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2496. [item removeObjectForKey:@"discount"];
  2497. [item removeObjectForKey:@"note"];
  2498. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2499. }
  2500. cart[@"section_0"]=section;
  2501. }
  2502. NSString *orderdir = [self getScanPath];
  2503. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2504. cart[@"price_group"] = appDelegate.price_group;
  2505. [RAUtils dicttofile:cartpath dict:cart];
  2506. }
  2507. #endif
  2508. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2509. {
  2510. assert(add_params[@"user"]!=nil);
  2511. // assert(add_params[@"contact_id"]!=nil);
  2512. // assert(add_params[@"password"]!=nil);
  2513. NSString* serial= [[NSUUID UUID] UUIDString];
  2514. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2515. NSString *cachefolder = [paths objectAtIndex:0];
  2516. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2517. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2518. NSFileManager* fileManager = [NSFileManager defaultManager];
  2519. BOOL bdir=YES;
  2520. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2521. {
  2522. NSError *error = nil;
  2523. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2524. if(!bsuccess)
  2525. DebugLog(@"Create UPLOAD folder failed");
  2526. }
  2527. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2528. ret[@"contact"]=[self prepareContact:serial];
  2529. ret[@"wishlist"]=[self prepareWishlist:serial];
  2530. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2531. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2532. ret[@"view_portfolio"] = [self preparePDF:serial];
  2533. NSString* str= [RAConvertor dict2string:ret];
  2534. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2535. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2536. NSError *error=nil;
  2537. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2538. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2539. ZipArchive* zip = [[ZipArchive alloc] init];
  2540. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2541. for(int i=0;i<arr_files.count;i++)
  2542. {
  2543. NSString* file=arr_files[i];
  2544. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2545. }
  2546. if( ![zip CloseZipFile2] )
  2547. {
  2548. zippath = @"";
  2549. }
  2550. ret[@"file"]=zippath;
  2551. return ret;
  2552. }
  2553. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2554. {
  2555. if(filename.length==0)
  2556. return false;
  2557. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2558. bool ret=false;
  2559. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2560. NSString *cachefolder = [paths objectAtIndex:0];
  2561. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2562. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2563. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2564. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2565. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2566. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2567. NSFileManager* fileManager = [NSFileManager defaultManager];
  2568. BOOL bdir=NO;
  2569. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2570. {
  2571. NSError *error = nil;
  2572. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2573. {
  2574. ret=false;
  2575. }
  2576. else
  2577. {
  2578. ret=true;
  2579. }
  2580. }
  2581. return ret;
  2582. }
  2583. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2584. {
  2585. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2586. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2587. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2588. NSString* where=@" 1=1";
  2589. if (ver!=nil) {
  2590. where=@"is_dirty=1";
  2591. }
  2592. sqlite3 *db = [iSalesDB get_db];
  2593. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2594. sqlite3_stmt * statement;
  2595. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2596. int count=0;
  2597. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2598. if ( dbresult== SQLITE_OK)
  2599. {
  2600. while (sqlite3_step(statement) == SQLITE_ROW)
  2601. {
  2602. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2603. int _id = sqlite3_column_int(statement, 0);
  2604. int product_id = sqlite3_column_int(statement, 1);
  2605. int item_id = sqlite3_column_int(statement, 2);
  2606. int qty = sqlite3_column_int(statement, 3);
  2607. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2608. NSString* nscreate_time=nil;
  2609. if(create_time!=nil)
  2610. {
  2611. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2612. }
  2613. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2614. NSString* nsmodify_time=nil;
  2615. if(modify_time!=nil)
  2616. {
  2617. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2618. }
  2619. int is_delete = sqlite3_column_int(statement, 6);
  2620. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2621. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2622. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2623. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2624. itemjson[@"create_time"]=nscreate_time;
  2625. itemjson[@"modify_time"]=nsmodify_time;
  2626. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2627. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2628. count++;
  2629. }
  2630. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2631. sqlite3_finalize(statement);
  2632. }
  2633. ret[@"count"]=[NSNumber numberWithInt:count ];
  2634. [iSalesDB close_db:db];
  2635. return ret;
  2636. }
  2637. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2638. {
  2639. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2640. // UIApplication * app = [UIApplication sharedApplication];
  2641. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2642. assert(add_params[@"user"]!=nil);
  2643. // assert(add_params[@"password"]!=nil);
  2644. 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 ];
  2645. // 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 ];
  2646. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2647. DebugLog(@"offline_login sql:%@",sqlQuery);
  2648. sqlite3_stmt * statement;
  2649. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2650. int count=0;
  2651. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2652. if ( dbresult== SQLITE_OK)
  2653. {
  2654. while (sqlite3_step(statement) == SQLITE_ROW)
  2655. {
  2656. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2657. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2658. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2659. int product_id = sqlite3_column_int(statement, 0);
  2660. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2661. int item_id = sqlite3_column_int(statement, 7);
  2662. NSString* Price=nil;
  2663. if(str_price==nil)
  2664. {
  2665. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2666. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2667. if(price==nil)
  2668. Price=@"No Price.";
  2669. else
  2670. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2671. }
  2672. else
  2673. {
  2674. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2675. }
  2676. double discount = sqlite3_column_double(statement, 2);
  2677. int item_count = sqlite3_column_int(statement, 3);
  2678. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2679. NSString *nsline_note=nil;
  2680. if(line_note!=nil)
  2681. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2682. char *name = (char*)sqlite3_column_text(statement, 5);
  2683. NSString *nsname=nil;
  2684. if(name!=nil)
  2685. nsname= [[NSString alloc]initWithUTF8String:name];
  2686. char *description = (char*)sqlite3_column_text(statement, 6);
  2687. NSString *nsdescription=nil;
  2688. if(description!=nil)
  2689. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2690. // int stockUom = sqlite3_column_int(statement, 8);
  2691. // int _id = sqlite3_column_int(statement, 9);
  2692. //
  2693. //
  2694. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2695. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2696. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2697. // int carton=[bsubtotaljson[@"carton"] intValue];
  2698. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2699. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2700. itemjson[@"model"]=nsname;
  2701. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2702. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2703. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2704. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2705. // itemjson[@"check"]=@"true";
  2706. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2707. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2708. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2709. itemjson[@"unit_price"]=Price;
  2710. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2711. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2712. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2713. itemjson[@"note"]=nsline_note;
  2714. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2715. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2716. count++;
  2717. }
  2718. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2719. sqlite3_finalize(statement);
  2720. }
  2721. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2722. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2723. return ret;
  2724. }
  2725. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2726. {
  2727. assert(add_params[@"user"]!=nil);
  2728. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2729. sqlite3 *db = [iSalesDB get_db];
  2730. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2731. // for(int i=0;i<arr_soid.count;i++)
  2732. sqlite3_stmt * statement;
  2733. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2734. //int count=0;
  2735. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2736. if ( dbresult== SQLITE_OK)
  2737. {
  2738. int count=0;
  2739. while (sqlite3_step(statement) == SQLITE_ROW)
  2740. {
  2741. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2742. int _id = sqlite3_column_int(statement, 0);
  2743. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2744. NSString* nssync_data=nil;
  2745. if(sync_data!=nil)
  2746. {
  2747. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2748. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2749. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2750. }
  2751. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2752. NSString* nsimg_1=nil;
  2753. if(img_1!=nil)
  2754. {
  2755. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2756. [self copy_upImg:serial file:nsimg_1];
  2757. }
  2758. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2759. NSString* nsso_no=nil;
  2760. if(so_no!=nil)
  2761. {
  2762. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2763. }
  2764. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2765. itemjson[@"order_type"]=@"submit order";
  2766. else
  2767. itemjson[@"order_type"]=@"archive order";
  2768. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2769. itemjson[@"json_data"]= nssync_data;
  2770. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2771. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2772. count++;
  2773. }
  2774. ret[@"count"]=[NSNumber numberWithInt:count ];
  2775. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2776. }
  2777. sqlite3_finalize(statement);
  2778. [iSalesDB close_db:db];
  2779. return ret;
  2780. }
  2781. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2782. {
  2783. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2784. sqlite3 *db = [iSalesDB get_db];
  2785. // UIApplication * app = [UIApplication sharedApplication];
  2786. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2787. 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";
  2788. sqlite3_stmt * statement;
  2789. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2790. int count=0;
  2791. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2792. if ( dbresult== SQLITE_OK)
  2793. {
  2794. while (sqlite3_step(statement) == SQLITE_ROW)
  2795. {
  2796. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2797. int _id = sqlite3_column_int(statement, 0);
  2798. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2799. NSString* nsimg_0=nil;
  2800. if(img_0!=nil)
  2801. {
  2802. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2803. [self copy_upImg:serial file:nsimg_0];
  2804. }
  2805. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2806. NSString* nsimg_1=nil;
  2807. if(img_1!=nil)
  2808. {
  2809. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2810. [self copy_upImg:serial file:nsimg_1];
  2811. }
  2812. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2813. NSString* nsimg_2=nil;
  2814. if(img_2!=nil)
  2815. {
  2816. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2817. [self copy_upImg:serial file:nsimg_2];
  2818. }
  2819. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2820. NSString* nssync_data=nil;
  2821. if(sync_data!=nil)
  2822. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2823. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2824. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2825. itemjson[@"json_data"]= nssync_data;
  2826. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2827. count++;
  2828. }
  2829. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2830. sqlite3_finalize(statement);
  2831. }
  2832. ret[@"count"]=[NSNumber numberWithInt:count ];
  2833. [iSalesDB close_db:db];
  2834. return ret;
  2835. }
  2836. +(bool) check_offlinedata
  2837. {
  2838. // UIApplication * app = [UIApplication sharedApplication];
  2839. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2840. sqlite3 *db = [iSalesDB get_db];
  2841. NSString * where=@"1=1";
  2842. // if(appDelegate.user!=nil)
  2843. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2844. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2845. [iSalesDB close_db:db];
  2846. if(count==0)
  2847. {
  2848. return false;
  2849. }
  2850. return true;
  2851. //
  2852. //[iSalesDB close_db:db];
  2853. }
  2854. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2855. {
  2856. // UIApplication * app = [UIApplication sharedApplication];
  2857. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2858. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2859. sqlite3 *db = [iSalesDB get_db];
  2860. NSString* collectId=params[@"collectId"];
  2861. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2862. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2863. [iSalesDB execSql:sqlQuery db:db];
  2864. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2865. [iSalesDB close_db:db];
  2866. // appDelegate.wish_count =count;
  2867. //
  2868. // [appDelegate update_count_mark];
  2869. ret[@"result"]= [NSNumber numberWithInt:2];
  2870. ret[@"wish_count"]=@(count);
  2871. return ret;
  2872. }
  2873. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2874. //{
  2875. //
  2876. // UIApplication * app = [UIApplication sharedApplication];
  2877. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2878. //
  2879. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2880. // sqlite3 *db = [iSalesDB get_db];
  2881. // NSString* product_id=params[@"product_id"];
  2882. //
  2883. //
  2884. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2885. //
  2886. //
  2887. // for(int i=0;i<arr.count;i++)
  2888. // {
  2889. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2890. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2891. // if(count==0)
  2892. // {
  2893. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2894. // [iSalesDB execSql:sqlQuery db:db];
  2895. // }
  2896. // }
  2897. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2898. // [iSalesDB close_db:db];
  2899. //
  2900. // appDelegate.wish_count =count;
  2901. //
  2902. // [appDelegate update_count_mark];
  2903. // ret[@"result"]= [NSNumber numberWithInt:2];
  2904. // return ret;
  2905. // //
  2906. // //return ret;
  2907. //}
  2908. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2909. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2910. DebugLog(@"time interval: %lf",interval);
  2911. }
  2912. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2913. //{
  2914. // UIApplication * app = [UIApplication sharedApplication];
  2915. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2916. //
  2917. // int sort = [[params objectForKey:@"sort"] intValue];
  2918. // NSString *sort_str = @"";
  2919. // switch (sort) {
  2920. // case 0:{
  2921. // sort_str = @"order by w.modify_time desc";
  2922. // }
  2923. // break;
  2924. // case 1:{
  2925. // sort_str = @"order by w.modify_time asc";
  2926. // }
  2927. // break;
  2928. // case 2:{
  2929. // sort_str = @"order by m.name asc";
  2930. // }
  2931. // break;
  2932. // case 3:{
  2933. // sort_str = @"order by m.name desc";
  2934. // }
  2935. // break;
  2936. // case 4:{
  2937. // sort_str = @"order by m.description asc";
  2938. // }
  2939. // break;
  2940. //
  2941. // default:
  2942. // break;
  2943. //
  2944. // }
  2945. //
  2946. //
  2947. //// NSString* user = appDelegate.user;
  2948. //
  2949. // sqlite3 *db = [iSalesDB get_db];
  2950. //
  2951. // // order by w.create_time
  2952. // 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];
  2953. //
  2954. //// 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];
  2955. //
  2956. //
  2957. // sqlite3_stmt * statement;
  2958. //
  2959. // NSDate *date1 = [NSDate date];
  2960. //// NSDate *date2 = nil;
  2961. //
  2962. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2963. // int count=0;
  2964. //
  2965. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2966. // {
  2967. //
  2968. //
  2969. // while (sqlite3_step(statement) == SQLITE_ROW)
  2970. // {
  2971. //
  2972. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2973. //
  2974. // int product_id = sqlite3_column_double(statement, 0);
  2975. //
  2976. //
  2977. //
  2978. //
  2979. //
  2980. // char *description = (char*)sqlite3_column_text(statement, 1);
  2981. // if(description==nil)
  2982. // description= "";
  2983. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2984. //
  2985. // int item_id = sqlite3_column_double(statement, 2);
  2986. //
  2987. // NSDate *date_image = [NSDate date];
  2988. //
  2989. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2990. //
  2991. // printf("image : ");
  2992. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2993. //
  2994. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2995. // // if(url==nil)
  2996. // // url="";
  2997. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2998. //
  2999. // int qty = sqlite3_column_int(statement, 3);
  3000. //
  3001. //
  3002. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  3003. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  3004. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  3005. // item[@"description"]= nsdescription;
  3006. // item[@"img"]= nsurl;
  3007. //
  3008. //
  3009. //
  3010. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3011. // count++;
  3012. //
  3013. // }
  3014. // printf("total time:");
  3015. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3016. //
  3017. // ret[@"count"]= [NSNumber numberWithInt:count];
  3018. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  3019. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  3020. // ret[@"result"]= [NSNumber numberWithInt:2];
  3021. //
  3022. //
  3023. // appDelegate.wish_count =count;
  3024. //
  3025. // [appDelegate update_count_mark];
  3026. // sqlite3_finalize(statement);
  3027. //
  3028. //
  3029. //
  3030. //
  3031. // }
  3032. //
  3033. // [iSalesDB close_db:db];
  3034. //
  3035. // return ret;
  3036. //}
  3037. +(NSDictionary*) offline_notimpl
  3038. {
  3039. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3040. ret[@"result"]=@"8";
  3041. ret[@"err_msg"]=@"offline mode does not support this function.";
  3042. return ret;
  3043. }
  3044. +(NSDictionary*) offline_home
  3045. {
  3046. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3047. NSString *cachefolder = [paths objectAtIndex:0];
  3048. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  3049. NSData* json =nil;
  3050. json=[NSData dataWithContentsOfFile:img_cache];
  3051. if(json==nil)
  3052. return nil;
  3053. NSError *error=nil;
  3054. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3055. return menu;
  3056. }
  3057. +(NSDictionary*) offline_category_menu
  3058. {
  3059. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3060. NSString *cachefolder = [paths objectAtIndex:0];
  3061. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  3062. NSData* json =nil;
  3063. json=[NSData dataWithContentsOfFile:img_cache];
  3064. if(json==nil)
  3065. return nil;
  3066. NSError *error=nil;
  3067. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3068. return menu;
  3069. }
  3070. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3071. {
  3072. NSString* offline_command=params[@"offline_Command"];
  3073. NSDictionary* ret=nil;
  3074. if([offline_command isEqualToString:@"model_NIYMAL"])
  3075. {
  3076. NSString* category = params[@"category"];
  3077. ret = [self refresh_model_NIYMAL:category];
  3078. }
  3079. return ret;
  3080. }
  3081. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3082. {
  3083. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3084. [ret setValue:@"2" forKey:@"result"];
  3085. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3086. sqlite3* db= [iSalesDB get_db];
  3087. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3088. [iSalesDB close_db:db];
  3089. [ret setObject:detail1_section forKey:@"detail_1"];
  3090. return ret;
  3091. }
  3092. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3093. {
  3094. //assert(user!=nil);
  3095. // UIApplication * app = [UIApplication sharedApplication];
  3096. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3097. // NSArray* arr1 = [self get_user_all_price_type];
  3098. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3099. // NSSet *set1 = [NSSet setWithArray:arr1];
  3100. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3101. // if(appDelegate.contact_id==nil)
  3102. // set2=[set1 mutableCopy];
  3103. // else
  3104. // [set2 intersectsSet:set1];
  3105. // NSArray *retarr = [set2 allObjects];
  3106. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3107. // sqlite3 *db = [iSalesDB get_db];
  3108. NSString* sqlQuery = nil;
  3109. if(contact_id==nil)
  3110. {
  3111. if(!blogin)
  3112. return nil;
  3113. 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];
  3114. }
  3115. else
  3116. 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];
  3117. sqlite3_stmt * statement;
  3118. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3119. int count=0;
  3120. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3121. {
  3122. while (sqlite3_step(statement) == SQLITE_ROW)
  3123. {
  3124. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3125. char *name = (char*)sqlite3_column_text(statement, 0);
  3126. if(name==nil)
  3127. name="";
  3128. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3129. // double price = sqlite3_column_double(statement, 1);
  3130. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3131. // if(isnull==nil)
  3132. // item[nsname]= @"No Price";
  3133. // else
  3134. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3135. char *price = (char*)sqlite3_column_text(statement, 1);
  3136. if(price!=nil)
  3137. {
  3138. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3139. nsprice=[AESCrypt fastdecrypt:nsprice];
  3140. if(nsprice.length>0)
  3141. {
  3142. double dp= [nsprice doubleValue];
  3143. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3144. }
  3145. }
  3146. else
  3147. {
  3148. item[nsname]= @"No Price";
  3149. }
  3150. // int type= sqlite3_column_int(statement, 2);
  3151. //item[@"type"]=@"price";
  3152. // item[nsname]= nsprice;
  3153. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3154. count++;
  3155. }
  3156. ret[@"count"]= [NSNumber numberWithInt:count];
  3157. sqlite3_finalize(statement);
  3158. }
  3159. // [iSalesDB close_db:db];
  3160. return ret;
  3161. }
  3162. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3163. //{
  3164. // NSArray* arr1 = [self get_user_all_price_type:db];
  3165. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3166. //
  3167. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3168. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3169. // // [set2 intersectsSet:set1];
  3170. // //
  3171. // //
  3172. // // NSArray *retarr = [set2 allObjects];
  3173. //
  3174. // NSString* whereprice=nil;
  3175. // if(contact_id==nil)
  3176. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3177. // else
  3178. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3179. //
  3180. //
  3181. // // sqlite3 *db = [iSalesDB get_db];
  3182. //
  3183. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3184. // sqlite3_stmt * statement;
  3185. //
  3186. //
  3187. // NSNumber* ret = nil;
  3188. // double dprice=DBL_MAX;
  3189. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3190. // {
  3191. //
  3192. //
  3193. // while (sqlite3_step(statement) == SQLITE_ROW)
  3194. // {
  3195. //
  3196. // // double val = sqlite3_column_double(statement, 0);
  3197. // char *price = (char*)sqlite3_column_text(statement, 0);
  3198. // if(price!=nil)
  3199. // {
  3200. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3201. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3202. // if(nsprice.length>0)
  3203. // {
  3204. // double dp= [nsprice doubleValue];
  3205. // if(dp<dprice)
  3206. // dprice=dp;
  3207. // }
  3208. // }
  3209. // }
  3210. //
  3211. //
  3212. //
  3213. //
  3214. // sqlite3_finalize(statement);
  3215. //
  3216. //
  3217. //
  3218. //
  3219. // }
  3220. //
  3221. // // [iSalesDB close_db:db];
  3222. //
  3223. // if(dprice==DBL_MAX)
  3224. // ret= nil;
  3225. // else
  3226. // ret= [NSNumber numberWithDouble:dprice];
  3227. // return ret;
  3228. //}
  3229. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3230. {
  3231. assert(user!=nil);
  3232. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3233. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3234. NSString* whereprice=nil;
  3235. if(contact_id==nil)
  3236. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3237. else
  3238. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3239. // sqlite3 *db = [iSalesDB get_db];
  3240. NSString *productIdCondition = @"1 = 1";
  3241. if (product_id) {
  3242. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3243. }
  3244. NSString *itemIdCondition = @"";
  3245. if (item_id) {
  3246. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3247. }
  3248. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3249. sqlite3_stmt * statement;
  3250. NSNumber* ret = nil;
  3251. double dprice=DBL_MAX;
  3252. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3253. {
  3254. while (sqlite3_step(statement) == SQLITE_ROW)
  3255. {
  3256. // double val = sqlite3_column_double(statement, 0);
  3257. char *price = (char*)sqlite3_column_text(statement, 0);
  3258. if(price!=nil)
  3259. {
  3260. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3261. nsprice=[AESCrypt fastdecrypt:nsprice];
  3262. if(nsprice.length>0)
  3263. {
  3264. double dp= [nsprice doubleValue];
  3265. if(dp<dprice)
  3266. dprice=dp;
  3267. }
  3268. }
  3269. }
  3270. sqlite3_finalize(statement);
  3271. }
  3272. // [iSalesDB close_db:db];
  3273. if(dprice==DBL_MAX)
  3274. ret= nil;
  3275. else
  3276. ret= [NSNumber numberWithDouble:dprice];
  3277. return ret;
  3278. }
  3279. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3280. {
  3281. assert(user!=nil);
  3282. NSArray* ret=nil;
  3283. // sqlite3 *db = [iSalesDB get_db];
  3284. // no customer assigned , use login user contact_id
  3285. // UIApplication * app = [UIApplication sharedApplication];
  3286. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3287. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3288. sqlite3_stmt * statement;
  3289. // int count=0;
  3290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3291. {
  3292. if (sqlite3_step(statement) == SQLITE_ROW)
  3293. {
  3294. char *val = (char*)sqlite3_column_text(statement, 0);
  3295. if(val==nil)
  3296. val="";
  3297. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3298. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3299. }
  3300. sqlite3_finalize(statement);
  3301. }
  3302. // [iSalesDB close_db:db];
  3303. return ret;
  3304. }
  3305. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3306. {
  3307. // sqlite3 *db = [iSalesDB get_db];
  3308. if(contact_id==nil)
  3309. {
  3310. // no customer assigned , use login user contact_id
  3311. // UIApplication * app = [UIApplication sharedApplication];
  3312. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3313. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3314. sqlite3_stmt * statement;
  3315. // int count=0;
  3316. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3317. {
  3318. if (sqlite3_step(statement) == SQLITE_ROW)
  3319. {
  3320. char *val = (char*)sqlite3_column_text(statement, 0);
  3321. if(val==nil)
  3322. val="";
  3323. contact_id = [[NSString alloc]initWithUTF8String:val];
  3324. }
  3325. sqlite3_finalize(statement);
  3326. }
  3327. if(contact_id.length<=0)
  3328. {
  3329. // [iSalesDB close_db:db];
  3330. return nil;
  3331. }
  3332. }
  3333. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3334. sqlite3_stmt * statement;
  3335. NSArray* ret=nil;
  3336. // int count=0;
  3337. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3338. {
  3339. if (sqlite3_step(statement) == SQLITE_ROW)
  3340. {
  3341. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3342. if(price_type==nil)
  3343. price_type="";
  3344. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3345. if(nsprice_type.length>0)
  3346. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3347. else
  3348. ret=nil;
  3349. }
  3350. sqlite3_finalize(statement);
  3351. }
  3352. // [iSalesDB close_db:db];
  3353. return ret;
  3354. }
  3355. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3356. {
  3357. NSString* ret= nil;
  3358. // sqlite3 *db = [iSalesDB get_db];
  3359. NSString *sqlQuery = nil;
  3360. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3361. // 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;
  3362. // 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
  3363. if(product_id==nil && model_name)
  3364. 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;
  3365. else if (product_id)
  3366. 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
  3367. sqlite3_stmt * statement;
  3368. // int count=0;
  3369. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3370. {
  3371. if (sqlite3_step(statement) == SQLITE_ROW)
  3372. {
  3373. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3374. if(imgurl==nil)
  3375. imgurl="";
  3376. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3377. ret=nsimgurl;
  3378. }
  3379. sqlite3_finalize(statement);
  3380. }
  3381. else
  3382. {
  3383. [ret setValue:@"8" forKey:@"result"];
  3384. }
  3385. // [iSalesDB close_db:db];
  3386. DebugLog(@"data string: %@",ret );
  3387. return ret;
  3388. }
  3389. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3390. {
  3391. NSString* ret= nil;
  3392. sqlite3 *db = [iSalesDB get_db];
  3393. NSString *sqlQuery = nil;
  3394. if(product_id==nil)
  3395. if(upc_code==nil)
  3396. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3397. else
  3398. sqlQuery = [NSString stringWithFormat:@"select default_category from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upc_code,upc_code,upc_code,upc_code];// select default_category from model where name='%@';
  3399. else
  3400. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3401. sqlite3_stmt * statement;
  3402. // int count=0;
  3403. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3404. {
  3405. if (sqlite3_step(statement) == SQLITE_ROW)
  3406. {
  3407. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3408. if(default_category==nil)
  3409. default_category="";
  3410. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3411. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3412. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3413. ret=nsdefault_category;
  3414. }
  3415. sqlite3_finalize(statement);
  3416. }
  3417. else
  3418. {
  3419. [ret setValue:@"8" forKey:@"result"];
  3420. }
  3421. [iSalesDB close_db:db];
  3422. DebugLog(@"data string: %@",ret );
  3423. return ret;
  3424. }
  3425. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3426. NSString *sql = nil;
  3427. if (product_id != nil) {
  3428. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3429. } else {
  3430. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3431. }
  3432. NSString *result_set = [iSalesDB jk_queryText:sql];
  3433. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3434. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3435. return result_set;
  3436. }
  3437. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3438. //{
  3439. //
  3440. //
  3441. // assert(params[@"user"]!=nil);
  3442. //
  3443. //
  3444. //
  3445. // NSString* model_name = [params valueForKey:@"product_name"];
  3446. //
  3447. // NSString* product_id = [params valueForKey:@"product_id"];
  3448. //
  3449. // NSString* category = [params valueForKey:@"category"];
  3450. //
  3451. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3452. // if(category==nil) {
  3453. // category = default_category_id;
  3454. // } else {
  3455. //
  3456. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3457. // // 参数重有多个category id
  3458. // if (arr_0.count > 1) {
  3459. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3460. //
  3461. // // 取交集
  3462. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3463. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3464. // [set_0 intersectSet:set_1];
  3465. //
  3466. // if (set_0.count == 1) {
  3467. // category = (NSString *)[set_0 anyObject];
  3468. // } else {
  3469. // if ([set_0 containsObject:default_category_id]) {
  3470. // category = default_category_id;
  3471. // } else {
  3472. // category = (NSString *)[set_0 anyObject];
  3473. // }
  3474. // }
  3475. // }
  3476. // }
  3477. //
  3478. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3479. //
  3480. //
  3481. //
  3482. //
  3483. //
  3484. //
  3485. // sqlite3 *db = [iSalesDB get_db];
  3486. //
  3487. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3488. //
  3489. //
  3490. // NSString *sqlQuery = nil;
  3491. //
  3492. // if(product_id==nil)
  3493. // 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='%@';
  3494. // else
  3495. //
  3496. // 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=%@;
  3497. //
  3498. //
  3499. // sqlite3_stmt * statement;
  3500. // [ret setValue:@"2" forKey:@"result"];
  3501. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3502. //
  3503. // // int count=0;
  3504. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3505. // {
  3506. //
  3507. //
  3508. // if (sqlite3_step(statement) == SQLITE_ROW)
  3509. // {
  3510. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3511. //
  3512. //
  3513. // char *name = (char*)sqlite3_column_text(statement, 0);
  3514. // if(name==nil)
  3515. // name="";
  3516. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3517. //
  3518. // char *description = (char*)sqlite3_column_text(statement, 1);
  3519. // if(description==nil)
  3520. // description="";
  3521. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3522. //
  3523. //
  3524. // int product_id = sqlite3_column_int(statement, 2);
  3525. //
  3526. //
  3527. // char *color = (char*)sqlite3_column_text(statement, 3);
  3528. // if(color==nil)
  3529. // color="";
  3530. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3531. // //
  3532. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3533. // // if(legcolor==nil)
  3534. // // legcolor="";
  3535. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3536. // //
  3537. // //
  3538. // int availability = sqlite3_column_int(statement, 5);
  3539. // //
  3540. // int incoming_stock = sqlite3_column_int(statement, 6);
  3541. //
  3542. //
  3543. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3544. // if(demension==nil)
  3545. // demension="";
  3546. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3547. //
  3548. //
  3549. //
  3550. // // ,,,,,,,,,
  3551. //
  3552. //
  3553. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3554. // if(seat_height==nil)
  3555. // seat_height="";
  3556. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3557. //
  3558. //
  3559. // char *material = (char*)sqlite3_column_text(statement, 9);
  3560. // if(material==nil)
  3561. // material="";
  3562. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3563. //
  3564. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3565. // if(box_dim==nil)
  3566. // box_dim="";
  3567. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3568. //
  3569. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3570. // if(volume==nil)
  3571. // volume="";
  3572. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3573. //
  3574. // double weight = sqlite3_column_double(statement, 12);
  3575. //
  3576. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3577. // if(model_set==nil)
  3578. // model_set="";
  3579. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3580. //
  3581. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3582. // if(load_ability==nil)
  3583. // load_ability="";
  3584. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3585. //
  3586. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3587. //// if(default_category==nil)
  3588. //// default_category="";
  3589. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3590. //
  3591. //
  3592. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3593. // if(fabric_content==nil)
  3594. // fabric_content="";
  3595. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3596. //
  3597. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3598. // if(assembling==nil)
  3599. // assembling="";
  3600. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3601. //
  3602. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3603. // if(made_in==nil)
  3604. // made_in="";
  3605. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3606. //
  3607. //
  3608. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3609. // if(special_remarks==nil)
  3610. // special_remarks="";
  3611. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3612. //
  3613. //
  3614. // int stockUcom = sqlite3_column_double(statement, 20);
  3615. //
  3616. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3617. // if(product_group==nil)
  3618. // product_group="";
  3619. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3620. //
  3621. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3622. // // if(fashion_selector==nil)
  3623. // // fashion_selector="";
  3624. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3625. //
  3626. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3627. // if(selector_field==nil)
  3628. // selector_field="";
  3629. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3630. //
  3631. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3632. // if(property_field==nil)
  3633. // property_field="";
  3634. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3635. //
  3636. //
  3637. //
  3638. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3639. // if(packaging==nil)
  3640. // packaging="";
  3641. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3642. //
  3643. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3644. //
  3645. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3646. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3647. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3648. //
  3649. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3650. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3651. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3652. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3653. //
  3654. //
  3655. //
  3656. //
  3657. //
  3658. // NSString* Availability=nil;
  3659. // if(availability>0)
  3660. // Availability=[NSString stringWithFormat:@"%d",availability];
  3661. // else
  3662. // Availability = @"Out of Stock";
  3663. //
  3664. // [img_section setValue:Availability forKey:@"Availability"];
  3665. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3666. //
  3667. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3668. //
  3669. //
  3670. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3671. // if(eta==nil)
  3672. // eta="";
  3673. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3674. // if ([nseta isEqualToString:@"null"]) {
  3675. // nseta = @"";
  3676. // }
  3677. // if (availability <= 0) {
  3678. // [img_section setValue:nseta forKey:@"ETA"];
  3679. // }
  3680. //
  3681. //
  3682. // int item_id = sqlite3_column_int(statement, 26);
  3683. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3684. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3685. //
  3686. // NSString* Price=nil;
  3687. // if(appDelegate.bLogin==false)
  3688. // Price=@"Must Sign in.";
  3689. // else
  3690. // {
  3691. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3692. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3693. // if(price==nil)
  3694. // Price=@"No Price.";
  3695. // else
  3696. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3697. // }
  3698. //
  3699. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3700. // [img_section setValue:Price forKey:@"price"];
  3701. // [img_section setValue:nsname forKey:@"model_name"];
  3702. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3703. //
  3704. // if (appDelegate.order_code) { // 离线order code即so#
  3705. //
  3706. // 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];
  3707. // __block int cartQTY = 0;
  3708. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3709. //
  3710. // cartQTY = sqlite3_column_int(stmt, 0);
  3711. //
  3712. // }];
  3713. //
  3714. // if (cartQTY > 0) {
  3715. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3716. // }
  3717. // }
  3718. //
  3719. // [ret setObject:img_section forKey:@"img_section"];
  3720. //
  3721. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3722. // int detail0_item_count=0;
  3723. //
  3724. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3725. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3726. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3727. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3728. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3729. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3730. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3731. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3732. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3733. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3734. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3735. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3736. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3737. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3738. //
  3739. //
  3740. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3741. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3742. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3743. // {
  3744. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3745. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3746. // }
  3747. //
  3748. //
  3749. //
  3750. //
  3751. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3752. // [detail0_section setValue:@"kv" forKey:@"type"];
  3753. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3754. //
  3755. // [ret setObject:detail0_section forKey:@"detail_0"];
  3756. //
  3757. //
  3758. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3759. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3760. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3761. // // [detail1_section setValue:@"content" forKey:@"type"];
  3762. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3763. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3764. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3765. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3766. // [ret setObject:detail1_section forKey:@"detail_1"];
  3767. //
  3768. //
  3769. //
  3770. //
  3771. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3772. // [detail2_section setValue:@"detail" forKey:@"target"];
  3773. // [detail2_section setValue:@"popup" forKey:@"action"];
  3774. // [detail2_section setValue:@"content" forKey:@"type"];
  3775. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3776. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3777. // [detail2_section setValue:@"local" forKey:@"data"];
  3778. // [ret setObject:detail2_section forKey:@"detail_2"];
  3779. // }
  3780. //
  3781. //
  3782. //
  3783. //
  3784. // sqlite3_finalize(statement);
  3785. // }
  3786. // else
  3787. // {
  3788. // [ret setValue:@"8" forKey:@"result"];
  3789. // }
  3790. //// DebugLog(@"count:%d",count);
  3791. //
  3792. //
  3793. // [iSalesDB close_db:db];
  3794. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3795. //
  3796. // return ret;
  3797. //}
  3798. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3799. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3800. NSString* keyword = [params valueForKey:@"keyword"];
  3801. keyword=keyword.lowercaseString;
  3802. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3803. int limit = [[params valueForKey:@"limit"] intValue];
  3804. int offset = [[params valueForKey:@"offset"] intValue];
  3805. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3806. NSString *limit_str = @"";
  3807. if (limited) {
  3808. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3809. }
  3810. sqlite3 *db = [iSalesDB get_db];
  3811. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3812. // UIApplication * app = [UIApplication sharedApplication];
  3813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3814. NSString *sqlQuery = nil;
  3815. if(exactMatch )
  3816. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",params[@"orderCode"], keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  3817. else
  3818. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",params[@"orderCode"], keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  3819. DebugLog(@"offline_search sql:%@",sqlQuery);
  3820. sqlite3_stmt * statement;
  3821. [ret setValue:@"2" forKey:@"result"];
  3822. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3823. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3824. // int count=0;
  3825. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3826. {
  3827. int i=0;
  3828. while (sqlite3_step(statement) == SQLITE_ROW)
  3829. {
  3830. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3831. // char *name = (char*)sqlite3_column_text(statement, 1);
  3832. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3833. char *name = (char*)sqlite3_column_text(statement, 0);
  3834. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3835. char *description = (char*)sqlite3_column_text(statement, 1);
  3836. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3837. int product_id = sqlite3_column_int(statement, 2);
  3838. // char *url = (char*)sqlite3_column_text(statement, 3);
  3839. // if(url==nil)
  3840. // url="";
  3841. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3842. int wid = sqlite3_column_int(statement, 3);
  3843. int closeout = sqlite3_column_int(statement, 4);
  3844. int cid = sqlite3_column_int(statement, 5);
  3845. int wisdelete = sqlite3_column_int(statement, 6);
  3846. int more_color = sqlite3_column_int(statement, 7);
  3847. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3848. if(wid !=0 && wisdelete != 1)
  3849. [item setValue:@"true" forKey:@"wish_exists"];
  3850. else
  3851. [item setValue:@"false" forKey:@"wish_exists"];
  3852. if(closeout==0)
  3853. [item setValue:@"false" forKey:@"is_closeout"];
  3854. else
  3855. [item setValue:@"true" forKey:@"is_closeout"];
  3856. if(cid==0)
  3857. [item setValue:@"false" forKey:@"cart_exists"];
  3858. else
  3859. [item setValue:@"true" forKey:@"cart_exists"];
  3860. if (more_color == 0) {
  3861. [item setObject:@(false) forKey:@"more_color"];
  3862. } else if (more_color == 1) {
  3863. [item setObject:@(true) forKey:@"more_color"];
  3864. }
  3865. [item addEntriesFromDictionary:imgjson];
  3866. // [item setValue:nsurl forKey:@"img"];
  3867. [item setValue:nsname forKey:@"fash_name"];
  3868. [item setValue:nsdescription forKey:@"description"];
  3869. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3870. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3871. i++;
  3872. }
  3873. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3874. [ret setObject:items forKey:@"items"];
  3875. sqlite3_finalize(statement);
  3876. }
  3877. DebugLog(@"count:%d",count);
  3878. [iSalesDB close_db:db];
  3879. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3880. return ret;
  3881. }
  3882. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3883. {
  3884. return [self search:params limited:YES];
  3885. }
  3886. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3887. //{
  3888. // UIApplication * app = [UIApplication sharedApplication];
  3889. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3890. //
  3891. //// [iSalesDB disable_trigger]
  3892. // [iSalesDB disable_trigger];
  3893. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3894. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3895. //
  3896. // [iSalesDB enable_trigger];
  3897. //// [iSalesDB enable_trigger]
  3898. // //
  3899. // // NSString* user = [params valueForKey:@"user"];
  3900. // //
  3901. // // NSString* password = [params valueForKey:@"password"];
  3902. //
  3903. //
  3904. //
  3905. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3906. //
  3907. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3908. //
  3909. //
  3910. //
  3911. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3912. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3913. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3914. //
  3915. // [appDelegate update_count_mark];
  3916. //
  3917. //
  3918. // appDelegate.can_show_price =false;
  3919. // appDelegate.can_see_price =false;
  3920. // appDelegate.can_create_portfolio =false;
  3921. // appDelegate.can_create_order =false;
  3922. //
  3923. //
  3924. // appDelegate.can_cancel_order =false;
  3925. // appDelegate.can_set_cart_price =false;
  3926. // appDelegate.can_delete_order =false;
  3927. // appDelegate.can_submit_order =false;
  3928. // appDelegate.can_set_tearsheet_price =false;
  3929. // appDelegate.can_update_contact_info = false;
  3930. //
  3931. // appDelegate.save_order_logout = false;
  3932. // appDelegate.submit_order_logout = false;
  3933. // appDelegate.alert_sold_in_quantities = false;
  3934. //
  3935. // appDelegate.ipad_perm =nil ;
  3936. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3937. // appDelegate.OrderFilter= nil;
  3938. // [appDelegate SetSo:nil];
  3939. // [appDelegate set_main_button_panel];
  3940. //
  3941. //
  3942. // // sqlite3 *db = [iSalesDB get_db];
  3943. // //
  3944. // //
  3945. // //
  3946. // //
  3947. // //
  3948. // // 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"]];
  3949. // //
  3950. // //
  3951. // //
  3952. // //
  3953. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3954. // // sqlite3_stmt * statement;
  3955. // //
  3956. // //
  3957. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3958. // //
  3959. // //
  3960. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3961. // // {
  3962. // //
  3963. // //
  3964. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3965. // // {
  3966. // //
  3967. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3968. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3969. // //
  3970. // //
  3971. // //
  3972. // // int can_show_price = sqlite3_column_int(statement, 0);
  3973. // // int can_see_price = sqlite3_column_int(statement, 1);
  3974. // //
  3975. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3976. // // if(contact_id==nil)
  3977. // // contact_id="";
  3978. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3979. // //
  3980. // // int user_type = sqlite3_column_int(statement, 3);
  3981. // //
  3982. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3983. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3984. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3985. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3986. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3987. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3988. // // int can_create_order = sqlite3_column_int(statement, 10);
  3989. // //
  3990. // //
  3991. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3992. // // if(mode==nil)
  3993. // // mode="";
  3994. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3995. // //
  3996. // //
  3997. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3998. // // if(username==nil)
  3999. // // username="";
  4000. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4001. // //
  4002. // //
  4003. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4004. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4005. // // [header setValue:nscontact_id forKey:@"contact_id"];
  4006. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4007. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4008. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4009. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4010. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4011. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4012. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4013. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4014. // //
  4015. // // [header setValue:nsusername forKey:@"username"];
  4016. // //
  4017. // //
  4018. // // [ret setObject:header forKey:@"header"];
  4019. // // [ret setValue:nsmode forKey:@"mode"];
  4020. // //
  4021. // //
  4022. // // }
  4023. // //
  4024. // //
  4025. // //
  4026. // // sqlite3_finalize(statement);
  4027. // // }
  4028. // //
  4029. // //
  4030. // //
  4031. // // [iSalesDB close_db:db];
  4032. // //
  4033. // //
  4034. // //
  4035. // //
  4036. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4037. //
  4038. // return ret;
  4039. //}
  4040. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  4041. //{
  4042. //
  4043. // UIApplication * app = [UIApplication sharedApplication];
  4044. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4045. //
  4046. // [iSalesDB disable_trigger];
  4047. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4048. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4049. // [iSalesDB enable_trigger];
  4050. //
  4051. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4052. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4053. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  4054. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  4055. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  4056. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  4057. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  4058. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  4059. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  4060. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  4061. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  4062. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  4063. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  4064. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4065. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4066. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4067. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4068. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4069. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4070. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4071. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4072. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4073. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4074. //
  4075. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4076. //
  4077. // [arr_name addObject:customer_first_name];
  4078. // [arr_name addObject:customer_last_name];
  4079. //
  4080. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4081. //
  4082. // // default ship from
  4083. // 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';";
  4084. //
  4085. // __block NSString *cid = @"";
  4086. // __block NSString *name = @"";
  4087. // __block NSString *ext = @"";
  4088. // __block NSString *contact = @"";
  4089. // __block NSString *email = @"";
  4090. // __block NSString *fax = @"";
  4091. // __block NSString *phone = @"";
  4092. //
  4093. // sqlite3 *db = [iSalesDB get_db];
  4094. //
  4095. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4096. //
  4097. // cid = [self textAtColumn:0 statement:statment];
  4098. // name = [self textAtColumn:1 statement:statment];
  4099. // ext = [self textAtColumn:2 statement:statment];
  4100. // contact = [self textAtColumn:3 statement:statment];
  4101. // email = [self textAtColumn:4 statement:statment];
  4102. // fax = [self textAtColumn:5 statement:statment];
  4103. // phone = [self textAtColumn:6 statement:statment];
  4104. //
  4105. // }];
  4106. //
  4107. // NSString* so_id = [self get_offline_soid:db];
  4108. // if(so_id==nil)
  4109. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4110. //
  4111. // 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];
  4112. //
  4113. //
  4114. //
  4115. // int result =[iSalesDB execSql:sql_neworder db:db];
  4116. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4117. //
  4118. //
  4119. //
  4120. // //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'
  4121. // //soId
  4122. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4123. //
  4124. //
  4125. //
  4126. //
  4127. //
  4128. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4129. // sqlite3_stmt * statement;
  4130. //
  4131. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4132. // {
  4133. // if (sqlite3_step(statement) == SQLITE_ROW)
  4134. // {
  4135. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4136. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4137. //
  4138. // //ret = sqlite3_column_int(statement, 0);
  4139. //
  4140. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4141. // if(soId==nil)
  4142. // soId="";
  4143. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4144. // [ret setValue:nssoId forKey:@"soId"];
  4145. // [ret setValue:nssoId forKey:@"orderCode"];
  4146. //
  4147. // }
  4148. // sqlite3_finalize(statement);
  4149. // }
  4150. //
  4151. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4152. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4153. //
  4154. //
  4155. // [iSalesDB close_db:db];
  4156. //
  4157. // return [RAConvertor dict2data:ret];
  4158. //
  4159. //}
  4160. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4161. {
  4162. //assert(params[@"order_code"]);
  4163. // assert(params[@"order_code"]);
  4164. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4165. NSString* orderCode = [params valueForKey:@"orderCode"];
  4166. NSString* app_order_code= params[@"appDelegate.order_code"];
  4167. // UIApplication * app = [UIApplication sharedApplication];
  4168. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4169. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4170. {
  4171. [iSalesDB disable_trigger];
  4172. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4173. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4174. [iSalesDB enable_trigger];
  4175. }
  4176. sqlite3 *db = [iSalesDB get_db];
  4177. int cart_count=[self query_ordercartcount:orderCode db:db];
  4178. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4179. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4180. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4181. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4182. [iSalesDB close_db:db];
  4183. return [RAConvertor dict2data:ret];
  4184. }
  4185. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4186. {
  4187. assert(params[@"can_create_backorder"]!=nil);
  4188. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4189. sqlite3 *db = [iSalesDB get_db];
  4190. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4191. int count =0;
  4192. if(params[@"count"]!=nil)
  4193. {
  4194. count = [params[@"count"] intValue];
  4195. }
  4196. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4197. NSString* product_id=params[@"product_id"];
  4198. NSString* orderCode=params[@"orderCode"];
  4199. NSString *qty = params[@"qty"];
  4200. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4201. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4202. __block int number_of_outOfStock = 0;
  4203. for(int i=0;i<arr_id.count;i++)
  4204. {
  4205. NSInteger item_qty= count;
  4206. if (qty) {
  4207. item_qty = [qty_arr[i] integerValue];
  4208. }
  4209. if(item_qty==0)
  4210. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4211. // 检查新加Model数量是否大于库存
  4212. if (![params[@"can_create_backorder"] boolValue]) {
  4213. __block BOOL needContinue = NO;
  4214. [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) {
  4215. int availability = sqlite3_column_int(stmt, 0);
  4216. // 库存小于购买量为缺货
  4217. if (availability < item_qty || availability <= 0) {
  4218. number_of_outOfStock++;
  4219. needContinue = YES;
  4220. }
  4221. }];
  4222. if (needContinue) {
  4223. continue;
  4224. }
  4225. }
  4226. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4227. NSString* sql=nil;
  4228. sqlite3_stmt *stmt = nil;
  4229. BOOL shouldStep = NO;
  4230. if(_id<0)
  4231. {
  4232. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4233. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4234. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4235. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4236. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4237. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4238. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4239. shouldStep = YES;
  4240. }
  4241. else
  4242. {
  4243. if (qty) { // wish list move to cart
  4244. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4245. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4246. sqlite3_bind_int(stmt, 1, _id);
  4247. shouldStep = YES;
  4248. } else {
  4249. 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];
  4250. __block BOOL update = YES;
  4251. if (![params[@"can_create_backorder"] boolValue]) {
  4252. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4253. int newQTY = sqlite3_column_int(stmt, 0);
  4254. int availability = sqlite3_column_int(stmt, 1);
  4255. if (newQTY > availability) { // 库存不够
  4256. update = NO;
  4257. number_of_outOfStock++;
  4258. }
  4259. }];
  4260. }
  4261. if (update) {
  4262. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4263. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4264. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4265. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4266. shouldStep = YES;
  4267. }
  4268. }
  4269. }
  4270. if (shouldStep) {
  4271. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4272. [iSalesDB execSql:@"ROLLBACK" db:db];
  4273. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4274. [iSalesDB close_db:db];
  4275. DebugLog(@"add to cart error");
  4276. return [RAConvertor dict2data:ret];
  4277. }
  4278. }
  4279. }
  4280. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4281. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4282. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4283. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4284. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4285. if (![params[@"can_create_backorder"] boolValue]) {
  4286. if (number_of_outOfStock > 0) {
  4287. ret[@"result"]=[NSNumber numberWithInt:8];
  4288. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4289. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4290. }
  4291. }
  4292. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4293. [iSalesDB close_db:db];
  4294. return [RAConvertor dict2data:ret];
  4295. }
  4296. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4297. {
  4298. // UIApplication * app = [UIApplication sharedApplication];
  4299. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4300. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4301. 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];
  4302. sqlite3_stmt * statement;
  4303. int count=0;
  4304. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4305. {
  4306. while (sqlite3_step(statement) == SQLITE_ROW)
  4307. {
  4308. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4309. int bitem_id = sqlite3_column_int(statement, 0);
  4310. int bitem_qty = sqlite3_column_int(statement, 1);
  4311. char *name = (char*)sqlite3_column_text(statement, 2);
  4312. if(name==nil)
  4313. name="";
  4314. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4315. char *description = (char*)sqlite3_column_text(statement, 3);
  4316. if(description==nil)
  4317. description="";
  4318. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4319. double unit_price = sqlite3_column_double(statement, 4);
  4320. int use_unitprice = sqlite3_column_int(statement, 5);
  4321. if(use_unitprice!=1)
  4322. {
  4323. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4324. }
  4325. itemjson[@"model"]=nsname;
  4326. itemjson[@"description"]=nsdescription;
  4327. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4328. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4329. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4330. if(compute)
  4331. {
  4332. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4333. }
  4334. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4335. count++;
  4336. }
  4337. sqlite3_finalize(statement);
  4338. }
  4339. ret[@"count"]=@(count);
  4340. if(count==0)
  4341. return nil;
  4342. else
  4343. return ret;
  4344. }
  4345. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4346. {
  4347. //compute: add part to subtotal;
  4348. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4349. dict_item[@(item_id)]=@"1";
  4350. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4351. double cuft=0;
  4352. double weight=0;
  4353. int carton=0;
  4354. int impack=0;
  4355. 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];
  4356. sqlite3_stmt * statement;
  4357. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4358. {
  4359. if (sqlite3_step(statement) == SQLITE_ROW)
  4360. {
  4361. double ulength = sqlite3_column_double(statement, 0);
  4362. double uwidth = sqlite3_column_double(statement, 1);
  4363. double uheight = sqlite3_column_double(statement, 2);
  4364. double uweight = sqlite3_column_double(statement, 3);
  4365. double mlength = sqlite3_column_double(statement, 4);
  4366. double mwidth = sqlite3_column_double(statement, 5);
  4367. double mheight = sqlite3_column_double(statement, 6);
  4368. double mweight = sqlite3_column_double(statement, 7);
  4369. double ilength = sqlite3_column_double(statement, 8);
  4370. double iwidth = sqlite3_column_double(statement, 9);
  4371. double iheight = sqlite3_column_double(statement, 10);
  4372. double iweight = sqlite3_column_double(statement, 11);
  4373. // int pcs = sqlite3_column_int(statement,12);
  4374. int mpack = sqlite3_column_int(statement, 13);
  4375. impack = mpack;
  4376. int ipack = sqlite3_column_int(statement, 14);
  4377. double ucbf = sqlite3_column_double(statement, 15);
  4378. // double icbf = sqlite3_column_double(statement, 16);
  4379. // double mcbf = sqlite3_column_double(statement, 17);
  4380. if(ipack==0)
  4381. {
  4382. carton= count/mpack ;
  4383. weight = mweight*carton;
  4384. cuft= carton*(mlength*mwidth*mheight);
  4385. int remain=count%mpack;
  4386. if(remain==0)
  4387. {
  4388. //do nothing;
  4389. }
  4390. else
  4391. {
  4392. carton++;
  4393. weight += uweight*remain;
  4394. cuft += (ulength*uwidth*uheight)*remain;
  4395. }
  4396. }
  4397. else
  4398. {
  4399. carton = count/(mpack*ipack);
  4400. weight = mweight*carton;
  4401. cuft= carton*(mlength*mwidth*mheight);
  4402. int remain=count%(mpack*ipack);
  4403. if(remain==0)
  4404. {
  4405. // do nothing;
  4406. }
  4407. else
  4408. {
  4409. carton++;
  4410. int icarton =remain/ipack;
  4411. int iremain=remain%ipack;
  4412. weight += iweight*icarton;
  4413. cuft += (ilength*iwidth*iheight)*icarton;
  4414. if(iremain==0)
  4415. {
  4416. //do nothing;
  4417. }
  4418. else
  4419. {
  4420. weight += uweight*iremain;
  4421. cuft += (ulength*uwidth*uheight)*iremain;
  4422. }
  4423. }
  4424. }
  4425. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4426. cuft=ucbf*count;
  4427. weight= uweight*count;
  4428. #endif
  4429. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4430. cuft=ucbf*count;
  4431. weight= uweight*count;
  4432. #endif
  4433. }
  4434. sqlite3_finalize(statement);
  4435. }
  4436. if(compute)
  4437. {
  4438. NSArray * arr_count=nil;
  4439. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4440. for(int i=0;i<arr_bundle.count;i++)
  4441. {
  4442. dict_item[arr_bundle[i]]=@"1";
  4443. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4444. cuft+=[bundlejson[@"cuft"] doubleValue];
  4445. weight+=[bundlejson[@"weight"] doubleValue];
  4446. carton+=[bundlejson[@"carton"] intValue];
  4447. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4448. }
  4449. }
  4450. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4451. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4452. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4453. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4454. ret[@"items"]=dict_item;
  4455. return ret;
  4456. }
  4457. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4458. {
  4459. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4460. // 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 ];
  4461. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4462. DebugLog(@"offline_login sql:%@",sqlQuery);
  4463. sqlite3_stmt * statement;
  4464. int cart_count=0;
  4465. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4466. if ( dbresult== SQLITE_OK)
  4467. {
  4468. while (sqlite3_step(statement) == SQLITE_ROW)
  4469. {
  4470. int item_id = sqlite3_column_int(statement, 0);
  4471. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4472. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4473. }
  4474. sqlite3_finalize(statement);
  4475. }
  4476. return cart_count;
  4477. }
  4478. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4479. //{
  4480. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4481. // sqlite3 *db = [iSalesDB get_db];
  4482. // UIApplication * app = [UIApplication sharedApplication];
  4483. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4484. //
  4485. // NSString* orderCode=params[@"orderCode"];
  4486. //
  4487. // int sort = [[params objectForKey:@"sort"] intValue];
  4488. // NSString *sort_str = @"";
  4489. // switch (sort) {
  4490. // case 0:{
  4491. // sort_str = @"order by c.modify_time desc";
  4492. // }
  4493. // break;
  4494. // case 1:{
  4495. // sort_str = @"order by c.modify_time asc";
  4496. // }
  4497. // break;
  4498. // case 2:{
  4499. // sort_str = @"order by m.name asc";
  4500. // }
  4501. // break;
  4502. // case 3:{
  4503. // sort_str = @"order by m.name desc";
  4504. // }
  4505. // break;
  4506. // case 4:{
  4507. // sort_str = @"order by m.description asc";
  4508. // }
  4509. // break;
  4510. //
  4511. // default:
  4512. // break;
  4513. //
  4514. // }
  4515. //
  4516. //
  4517. //
  4518. // 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 ];
  4519. //
  4520. //
  4521. //// 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 ];
  4522. //
  4523. //
  4524. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4525. //
  4526. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4527. // sqlite3_stmt * statement;
  4528. //
  4529. //
  4530. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4531. //
  4532. // NSDate *date1 = [NSDate date];
  4533. //
  4534. // int count=0;
  4535. // int cart_count=0;
  4536. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4537. // if ( dbresult== SQLITE_OK)
  4538. // {
  4539. //
  4540. //
  4541. // while (sqlite3_step(statement) == SQLITE_ROW)
  4542. // {
  4543. //// NSDate *row_date = [NSDate date];
  4544. //
  4545. //
  4546. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4547. //
  4548. //
  4549. //
  4550. //
  4551. // int product_id = sqlite3_column_int(statement, 0);
  4552. //
  4553. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4554. //
  4555. // int item_id = sqlite3_column_int(statement, 7);
  4556. //
  4557. // NSString* Price=nil;
  4558. // if(str_price==nil)
  4559. // {
  4560. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4561. //// NSDate *price_date = [NSDate date];
  4562. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4563. //// DebugLog(@"price time interval");
  4564. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4565. //
  4566. // if(price==nil)
  4567. // Price=@"No Price.";
  4568. // else
  4569. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4570. // }
  4571. // else
  4572. // {
  4573. //
  4574. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4575. // }
  4576. //
  4577. //
  4578. // double discount = sqlite3_column_double(statement, 2);
  4579. // int item_count = sqlite3_column_int(statement, 3);
  4580. //
  4581. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4582. // NSString *nsline_note=nil;
  4583. // if(line_note!=nil)
  4584. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4585. //
  4586. //
  4587. // char *name = (char*)sqlite3_column_text(statement, 5);
  4588. // NSString *nsname=nil;
  4589. // if(name!=nil)
  4590. // nsname= [[NSString alloc]initWithUTF8String:name];
  4591. //
  4592. // char *description = (char*)sqlite3_column_text(statement, 6);
  4593. // NSString *nsdescription=nil;
  4594. // if(description!=nil)
  4595. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4596. //
  4597. //
  4598. //
  4599. // int stockUom = sqlite3_column_int(statement, 8);
  4600. // int _id = sqlite3_column_int(statement, 9);
  4601. // int availability = sqlite3_column_int(statement, 10);
  4602. //
  4603. //// NSDate *subtotal_date = [NSDate date];
  4604. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4605. //// DebugLog(@"subtotal_date time interval");
  4606. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4607. //
  4608. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4609. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4610. // int carton=[bsubtotaljson[@"carton"] intValue];
  4611. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4612. //
  4613. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4614. //// NSDate *img_date = [NSDate date];
  4615. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4616. //// DebugLog(@"img_date time interval");
  4617. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4618. //
  4619. // itemjson[@"model"]=nsname;
  4620. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4621. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4622. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4623. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4624. // itemjson[@"check"]=@"true";
  4625. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4626. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4627. // itemjson[@"unit_price"]=Price;
  4628. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4629. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4630. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4631. // itemjson[@"note"]=nsline_note;
  4632. // if (!appDelegate.can_create_backorder) {
  4633. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4634. // }
  4635. //// NSDate *date2 = [NSDate date];
  4636. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4637. //// DebugLog(@"model_bundle time interval");
  4638. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4639. //
  4640. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4641. // count++;
  4642. //
  4643. //// DebugLog(@"row time interval");
  4644. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4645. // }
  4646. //
  4647. //
  4648. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4649. // sqlite3_finalize(statement);
  4650. // }
  4651. //
  4652. //
  4653. // DebugLog(@"request cart total time interval");
  4654. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4655. //
  4656. //
  4657. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4658. //
  4659. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4660. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4661. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4662. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4663. //
  4664. // ret[@"mode"]=@"Regular Mode";
  4665. //
  4666. // [iSalesDB close_db:db];
  4667. //
  4668. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4669. //
  4670. // DebugLog(@"general notes :%@",general_note);
  4671. //
  4672. // ret[@"general_note"]= general_note;
  4673. //
  4674. // return [RAConvertor dict2data:ret];
  4675. //}
  4676. +(NSData*) offline_login :(NSMutableDictionary *) params
  4677. {
  4678. NSString* user = [params valueForKey:@"user"];
  4679. NSString* password = [params valueForKey:@"password"];
  4680. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4681. sqlite3 *db = [iSalesDB get_db];
  4682. 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"]];
  4683. DebugLog(@"offline_login sql:%@",sqlQuery);
  4684. sqlite3_stmt * statement;
  4685. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4686. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4687. {
  4688. if (sqlite3_step(statement) == SQLITE_ROW)
  4689. {
  4690. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4691. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4692. int can_show_price = sqlite3_column_int(statement, 0);
  4693. int can_see_price = sqlite3_column_int(statement, 1);
  4694. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4695. if(contact_id==nil)
  4696. contact_id="";
  4697. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4698. int user_type = sqlite3_column_int(statement, 3);
  4699. int can_cancel_order = sqlite3_column_int(statement, 4);
  4700. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4701. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4702. int can_delete_order = sqlite3_column_int(statement, 7);
  4703. int can_submit_order = sqlite3_column_int(statement, 8);
  4704. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4705. int can_create_order = sqlite3_column_int(statement, 10);
  4706. char *mode = (char*)sqlite3_column_text(statement, 11);
  4707. if(mode==nil)
  4708. mode="";
  4709. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4710. char *username = (char*)sqlite3_column_text(statement, 12);
  4711. if(username==nil)
  4712. username="";
  4713. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4714. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4715. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4716. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4717. [header setValue:nscontact_id forKey:@"contact_id"];
  4718. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4719. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4720. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4721. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4722. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4723. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4724. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4725. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4726. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4727. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4728. [header setValue:nsusername forKey:@"username"];
  4729. NSError* error=nil;
  4730. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4731. [header setValue:statusFilter forKey:@"statusFilter"];
  4732. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4733. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4734. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4735. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4736. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4737. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4738. [ret setObject:header forKey:@"header"];
  4739. [ret setValue:nsmode forKey:@"mode"];
  4740. }
  4741. sqlite3_finalize(statement);
  4742. }
  4743. [iSalesDB close_db:db];
  4744. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4745. return [RAConvertor dict2data:ret];
  4746. }
  4747. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4748. {
  4749. assert(params[@"mode"]!=nil);
  4750. NSString* contactId = [params valueForKey:@"contactId"];
  4751. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4752. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4753. sqlite3 *db = [iSalesDB get_db];
  4754. NSString *sqlQuery = nil;
  4755. {
  4756. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type,carrier from offline_contact where contact_id='%@'",contactId];
  4757. }
  4758. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4759. sqlite3_stmt * statement;
  4760. [ret setValue:@"2" forKey:@"result"];
  4761. int carrier = -1;
  4762. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4763. {
  4764. //int i = 0;
  4765. if (sqlite3_step(statement) == SQLITE_ROW)
  4766. {
  4767. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4768. // int editable = sqlite3_column_int(statement, 0);
  4769. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4770. NSString *nscompany_name =nil;
  4771. if(company_name==nil)
  4772. nscompany_name=@"";
  4773. else
  4774. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4775. char *country = (char*)sqlite3_column_text(statement, 2);
  4776. if(country==nil)
  4777. country="";
  4778. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4779. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4780. // if(addr==nil)
  4781. // addr="";
  4782. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4783. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4784. if(zipcode==nil)
  4785. zipcode="";
  4786. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4787. char *state = (char*)sqlite3_column_text(statement, 5);
  4788. if(state==nil)
  4789. state="";
  4790. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4791. char *city = (char*)sqlite3_column_text(statement, 6);
  4792. if(city==nil)
  4793. city="";
  4794. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4795. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4796. // NSString *nscontact_name = nil;
  4797. // if(contact_name==nil)
  4798. // nscontact_name=@"";
  4799. // else
  4800. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4801. char *phone = (char*)sqlite3_column_text(statement, 8);
  4802. NSString *nsphone = nil;
  4803. if(phone==nil)
  4804. nsphone=@"";
  4805. else
  4806. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4807. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4808. // if(contact_id==nil)
  4809. // contact_id="";
  4810. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4811. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4812. if(addr_1==nil)
  4813. addr_1="";
  4814. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4815. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4816. if(addr_2==nil)
  4817. addr_2="";
  4818. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4819. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4820. if(addr_3==nil)
  4821. addr_3="";
  4822. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4823. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4824. if(addr_4==nil)
  4825. addr_4="";
  4826. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4827. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4828. if(first_name==nil)
  4829. first_name="";
  4830. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4831. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4832. if(last_name==nil)
  4833. last_name="";
  4834. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4835. char *fax = (char*)sqlite3_column_text(statement, 16);
  4836. NSString *nsfax = nil;
  4837. if(fax==nil)
  4838. nsfax=@"";
  4839. else
  4840. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4841. char *email = (char*)sqlite3_column_text(statement, 17);
  4842. NSString *nsemail = nil;
  4843. if(email==nil)
  4844. nsemail=@"";
  4845. else
  4846. nsemail= [[NSString alloc]initWithUTF8String:email];
  4847. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4848. NSString *nsimg_0 = nil;
  4849. if(img_0==nil)
  4850. nsimg_0=@"";
  4851. else
  4852. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4853. [self copy_bcardImg:nsimg_0];
  4854. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4855. NSString *nsimg_1 = nil;
  4856. if(img_1==nil)
  4857. nsimg_1=@"";
  4858. else
  4859. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4860. [self copy_bcardImg:nsimg_1];
  4861. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4862. NSString *nsimg_2 = nil;
  4863. if(img_2==nil)
  4864. nsimg_2=@"";
  4865. else
  4866. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4867. [self copy_bcardImg:nsimg_2];
  4868. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4869. NSString *nsprice_type = nil;
  4870. if(price_type==nil)
  4871. nsprice_type=@"";
  4872. else
  4873. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4874. char *notes = (char*)sqlite3_column_text(statement, 22);
  4875. NSString *nsnotes = nil;
  4876. if(notes==nil)
  4877. nsnotes=@"";
  4878. else
  4879. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4880. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4881. NSString *nssalesrep = nil;
  4882. if(salesrep==nil)
  4883. nssalesrep=@"";
  4884. else
  4885. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4886. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4887. carrier = sqlite3_column_int(statement, 25);
  4888. {
  4889. // decrypt
  4890. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4891. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4892. nsphone=[AESCrypt fastdecrypt:nsphone];
  4893. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4894. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4895. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4896. }
  4897. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4898. [arr_name addObject:nsfirst_name];
  4899. [arr_name addObject:nslast_name];
  4900. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4901. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4902. [arr_ext addObject:nsaddr_1];
  4903. [arr_ext addObject:nsaddr_2];
  4904. [arr_ext addObject:nsaddr_3];
  4905. [arr_ext addObject:nsaddr_4];
  4906. [arr_ext addObject:@"\r\n"];
  4907. [arr_ext addObject:nscity];
  4908. [arr_ext addObject:nsstate];
  4909. [arr_ext addObject:nszipcode];
  4910. [arr_ext addObject:nscountry];
  4911. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4912. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4913. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4914. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4915. [item setValue:nscountry forKey:@"customer_country"];
  4916. [item setValue:nsphone forKey:@"customer_phone"];
  4917. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4918. [item setValue:nscompany_name forKey:@"customer_name"];
  4919. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4920. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4921. [item setValue:nsext forKey:@"customer_contact_ext"];
  4922. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4923. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4924. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4925. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4926. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4927. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4928. [item setValue:nslast_name forKey:@"customer_last_name"];
  4929. [item setValue:nscity forKey:@"customer_city"];
  4930. [item setValue:nsstate forKey:@"customer_state"];
  4931. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4932. [item setValue:contactId forKey:@"customer_cid"];
  4933. [item setValue:nscontact_name forKey:@"customer_contact"];
  4934. [item setValue:nsfax forKey:@"customer_fax"];
  4935. [item setValue:nsemail forKey:@"customer_email"];
  4936. [item setValue:contact_type forKey:@"customer_contact_type"];
  4937. // i++;
  4938. }
  4939. // UIApplication * app = [UIApplication sharedApplication];
  4940. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4941. [ret setValue:params[@"mode"] forKey:@"mode"];
  4942. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4943. sqlite3_finalize(statement);
  4944. }
  4945. [iSalesDB close_db:db];
  4946. NSString* scarrier = [self offline_getCarrier:carrier];
  4947. item[@"customer_truck_carrier"] = scarrier;
  4948. [ret setObject:item forKey:@"customerInfo"];
  4949. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4950. return ret;
  4951. }
  4952. + (bool) copy_bcardImg:(NSString*) filename
  4953. {
  4954. if(filename.length==0)
  4955. return false;
  4956. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4957. bool ret=false;
  4958. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4959. NSString *cachefolder = [paths objectAtIndex:0];
  4960. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4961. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4962. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4963. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4964. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4965. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4966. //
  4967. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4968. NSFileManager* fileManager = [NSFileManager defaultManager];
  4969. BOOL bdir=NO;
  4970. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4971. {
  4972. NSError *error = nil;
  4973. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4974. {
  4975. ret=false;
  4976. }
  4977. else
  4978. {
  4979. ret=true;
  4980. }
  4981. // NSError *error = nil;
  4982. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4983. //
  4984. // if(!bsuccess)
  4985. // {
  4986. // DebugLog(@"Create offline_createimg folder failed");
  4987. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4988. // return [RAConvertor dict2data:ret];
  4989. // }
  4990. // if(bsuccess)
  4991. // {
  4992. // sqlite3 *db = [self get_db];
  4993. //
  4994. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4995. // [iSalesDB close_db:db];
  4996. // }
  4997. }
  4998. return ret;
  4999. //
  5000. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  5001. // if(bsuccess)
  5002. // {
  5003. // NSError *error = nil;
  5004. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5005. // {
  5006. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5007. // }
  5008. // else
  5009. // {
  5010. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5011. //
  5012. // ret[@"img_url_aname"]=filename;
  5013. // ret[@"img_url"]=savedImagePath;
  5014. // }
  5015. // }
  5016. }
  5017. +(NSData *) offline_saveBusinesscard:(NSData *) image
  5018. {
  5019. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5020. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  5021. NSString *cachefolder = [paths objectAtIndex:0];
  5022. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  5023. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  5024. NSFileManager* fileManager = [NSFileManager defaultManager];
  5025. BOOL bdir=YES;
  5026. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  5027. {
  5028. NSError *error = nil;
  5029. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  5030. if(!bsuccess)
  5031. {
  5032. DebugLog(@"Create offline_createimg folder failed");
  5033. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5034. return [RAConvertor dict2data:ret];
  5035. }
  5036. // if(bsuccess)
  5037. // {
  5038. // sqlite3 *db = [self get_db];
  5039. //
  5040. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5041. // [iSalesDB close_db:db];
  5042. // }
  5043. }
  5044. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  5045. {
  5046. NSError *error = nil;
  5047. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  5048. if(!bsuccess)
  5049. {
  5050. DebugLog(@"Create img_cache folder failed");
  5051. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5052. return [RAConvertor dict2data:ret];
  5053. }
  5054. // if(bsuccess)
  5055. // {
  5056. // sqlite3 *db = [self get_db];
  5057. //
  5058. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  5059. // [iSalesDB close_db:db];
  5060. // }
  5061. }
  5062. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  5063. //JEPG格式
  5064. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  5065. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  5066. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  5067. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  5068. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  5069. if(bsuccess)
  5070. {
  5071. NSError *error = nil;
  5072. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5073. {
  5074. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5075. }
  5076. else
  5077. {
  5078. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5079. ret[@"img_url_aname"]=filename;
  5080. ret[@"img_url"]=filename;
  5081. }
  5082. }
  5083. else
  5084. {
  5085. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5086. }
  5087. return [RAConvertor dict2data:ret];
  5088. }
  5089. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5090. {
  5091. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5092. category = [category substringToIndex:3];
  5093. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5094. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5095. params[@"category"]= category;
  5096. ret[@"params"]= params;
  5097. [ret setValue:@"detail" forKey:@"target"];
  5098. [ret setValue:@"popup" forKey:@"action"];
  5099. [ret setValue:@"content" forKey:@"type"];
  5100. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5101. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5102. [ret setValue:@"true" forKey:@"single_row"];
  5103. [ret setValue:@"true" forKey:@"partial_refresh"];
  5104. // sqlite3 *db = [iSalesDB get_db];
  5105. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5106. NSString *sqlQuery =nil;
  5107. #ifdef BUILD_NPD
  5108. 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 ;
  5109. #else
  5110. sqlQuery=[NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY];
  5111. #endif
  5112. sqlite3_stmt * statement;
  5113. int count = 0;
  5114. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5115. // int count=0;
  5116. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5117. {
  5118. int i=0;
  5119. while (sqlite3_step(statement) == SQLITE_ROW)
  5120. {
  5121. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5122. // char *name = (char*)sqlite3_column_text(statement, 1);
  5123. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5124. char *name = (char*)sqlite3_column_text(statement, 0);
  5125. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5126. int product_id = sqlite3_column_int(statement, 1);
  5127. char *url = (char*)sqlite3_column_text(statement, 2);
  5128. if(url==nil)
  5129. url="";
  5130. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5131. [item setValue:nsurl forKey:@"picture_path"];
  5132. [item setValue:nsname forKey:@"fash_name"];
  5133. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5134. [item setValue:category forKey:@"category"];
  5135. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5136. i++;
  5137. }
  5138. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5139. sqlite3_finalize(statement);
  5140. }
  5141. DebugLog(@"count:%d",count);
  5142. // [iSalesDB close_db:db];
  5143. return ret;
  5144. }
  5145. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5146. {
  5147. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5148. [ret setValue:key forKey:@"key"];
  5149. [ret setValue:value forKey:@"val"];
  5150. [ret setValue:@"price" forKey:@"type"];
  5151. return ret;
  5152. }
  5153. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5154. {
  5155. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5156. [ret setValue:key forKey:@"key"];
  5157. [ret setValue:value forKey:@"val"];
  5158. return ret;
  5159. }
  5160. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5161. {
  5162. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5163. [ret setValue:@"0" forKey:@"img_count"];
  5164. // sqlite3 *db = [iSalesDB get_db];
  5165. 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 ;
  5166. sqlite3_stmt * statement;
  5167. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5168. {
  5169. int i=0;
  5170. if (sqlite3_step(statement) == SQLITE_ROW)
  5171. {
  5172. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5173. // char *name = (char*)sqlite3_column_text(statement, 1);
  5174. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5175. char *value = (char*)sqlite3_column_text(statement, 0);
  5176. if(value==nil)
  5177. value="";
  5178. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5179. char *key = (char*)sqlite3_column_text(statement, 1);
  5180. if(key==nil)
  5181. key="";
  5182. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5183. [item setValue:nsvalue forKey:@"val"];
  5184. [item setValue:nskey forKey:@"key"];
  5185. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5186. [ret setValue:@"1" forKey:@"count"];
  5187. i++;
  5188. }
  5189. sqlite3_finalize(statement);
  5190. }
  5191. // [iSalesDB close_db:db];
  5192. return ret;
  5193. }
  5194. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5195. {
  5196. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5197. [ret setValue:@"0" forKey:@"count"];
  5198. // sqlite3 *db = [iSalesDB get_db];
  5199. 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;
  5200. sqlite3_stmt * statement;
  5201. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5202. {
  5203. int i=0;
  5204. while (sqlite3_step(statement) == SQLITE_ROW)
  5205. {
  5206. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5207. // char *name = (char*)sqlite3_column_text(statement, 1);
  5208. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5209. char *value = (char*)sqlite3_column_text(statement, 0);
  5210. if(value==nil)
  5211. value="";
  5212. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5213. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5214. if(selector_display==nil)
  5215. selector_display="";
  5216. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5217. int product_id = sqlite3_column_int(statement, 2);
  5218. char *category = (char*)sqlite3_column_text(statement, 3);
  5219. if(category==nil)
  5220. category="";
  5221. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5222. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5223. [item setValue:nsvalue forKey:@"title"];
  5224. [item setValue:url forKey:@"pic_url"];
  5225. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5226. [params setValue:@"2" forKey:@"count"];
  5227. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5228. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5229. [param0 setValue:@"product_id" forKey:@"name"];
  5230. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5231. [param1 setValue:nscategory forKey:@"val"];
  5232. [param1 setValue:@"category" forKey:@"name"];
  5233. [params setObject:param0 forKey:@"param_0"];
  5234. [params setObject:param1 forKey:@"param_1"];
  5235. [item setObject:params forKey:@"params"];
  5236. [ret setValue:nsselector_display forKey:@"name"];
  5237. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5238. i++;
  5239. }
  5240. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5241. [ret setValue:@"switch" forKey:@"action"];
  5242. sqlite3_finalize(statement);
  5243. }
  5244. // [iSalesDB close_db:db];
  5245. return ret;
  5246. }
  5247. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5248. {
  5249. // model 在 category search 显示的图片。
  5250. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5251. // sqlite3 *db = [iSalesDB get_db];
  5252. 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];
  5253. sqlite3_stmt * statement;
  5254. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5255. {
  5256. while (sqlite3_step(statement) == SQLITE_ROW)
  5257. {
  5258. char *url = (char*)sqlite3_column_text(statement, 0);
  5259. if(url==nil)
  5260. url="";
  5261. int type = sqlite3_column_int(statement, 1);
  5262. if(type==0)
  5263. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5264. else
  5265. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5266. }
  5267. sqlite3_finalize(statement);
  5268. }
  5269. // [iSalesDB close_db:db];
  5270. return ret;
  5271. }
  5272. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5273. {
  5274. int item_id=-1;
  5275. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5276. sqlite3_stmt * statement;
  5277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5278. {
  5279. if (sqlite3_step(statement) == SQLITE_ROW)
  5280. {
  5281. item_id = sqlite3_column_int(statement, 0);
  5282. }
  5283. sqlite3_finalize(statement);
  5284. }
  5285. return item_id;
  5286. }
  5287. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5288. {
  5289. // NSString* ret = @"";
  5290. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5291. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5292. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5293. sqlite3_stmt * statement;
  5294. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5295. {
  5296. while (sqlite3_step(statement) == SQLITE_ROW)
  5297. {
  5298. int bitem_id = sqlite3_column_int(statement, 0);
  5299. int bitem_qty = sqlite3_column_int(statement, 1);
  5300. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5301. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5302. }
  5303. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5304. sqlite3_finalize(statement);
  5305. }
  5306. // if(ret==nil)
  5307. // ret=@"";
  5308. *count=arr_count;
  5309. return arr_bundle;
  5310. }
  5311. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5312. {
  5313. // get default sold qty, return -1 if model not found;
  5314. int ret = -1;
  5315. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5316. sqlite3_stmt * statement;
  5317. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5318. {
  5319. if (sqlite3_step(statement) == SQLITE_ROW)
  5320. {
  5321. ret = sqlite3_column_int(statement, 0);
  5322. }
  5323. sqlite3_finalize(statement);
  5324. }
  5325. return ret;
  5326. }
  5327. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5328. {
  5329. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5330. [ret setValue:@"0" forKey:@"img_count"];
  5331. // sqlite3 *db = [iSalesDB get_db];
  5332. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5333. sqlite3_stmt * statement;
  5334. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5335. {
  5336. int i=0;
  5337. while (sqlite3_step(statement) == SQLITE_ROW)
  5338. {
  5339. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5340. // char *name = (char*)sqlite3_column_text(statement, 1);
  5341. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5342. char *url = (char*)sqlite3_column_text(statement, 0);
  5343. if(url==nil)
  5344. url="";
  5345. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5346. [item setValue:nsurl forKey:@"s"];
  5347. [item setValue:nsurl forKey:@"l"];
  5348. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5349. i++;
  5350. }
  5351. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5352. sqlite3_finalize(statement);
  5353. }
  5354. // [iSalesDB close_db:db];
  5355. return ret;
  5356. }
  5357. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5358. // UIApplication * app = [UIApplication sharedApplication];
  5359. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5360. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5361. sqlite3 *db = [iSalesDB get_db];
  5362. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5363. NSString* product_id=params[@"product_id"];
  5364. NSString *item_count_str = params[@"item_count"];
  5365. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5366. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5367. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5368. // NSString *sql = @"";
  5369. for(int i=0;i<arr.count;i++)
  5370. {
  5371. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5372. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5373. __block int cart_count = 0;
  5374. if (!item_count_str) {
  5375. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5376. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5377. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5378. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5379. cart_count = [[model_set_components lastObject] intValue];
  5380. }];
  5381. }
  5382. if(count==0)
  5383. {
  5384. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5385. sqlite3_stmt *stmt;
  5386. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5387. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5388. if (item_count_arr) {
  5389. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5390. } else {
  5391. sqlite3_bind_int(stmt,2,cart_count);
  5392. }
  5393. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5394. [iSalesDB execSql:@"ROLLBACK" db:db];
  5395. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5396. [iSalesDB close_db:db];
  5397. DebugLog(@"add to wishlist error");
  5398. return ret;
  5399. }
  5400. } else {
  5401. int qty = 0;
  5402. if (item_count_arr) {
  5403. qty = [item_count_arr[i] intValue];
  5404. } else {
  5405. qty = cart_count;
  5406. }
  5407. 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]];
  5408. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5409. [iSalesDB execSql:@"ROLLBACK" db:db];
  5410. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5411. [iSalesDB close_db:db];
  5412. DebugLog(@"add to wishlist error");
  5413. return ret;
  5414. }
  5415. }
  5416. }
  5417. // [iSalesDB execSql:sql db:db];
  5418. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5419. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5420. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5421. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5422. [iSalesDB close_db:db];
  5423. ret[@"wish_count"]=@(count);
  5424. ret[@"result"]= [NSNumber numberWithInt:2];
  5425. return ret;
  5426. }
  5427. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5428. // 0 category
  5429. // 1 search
  5430. // 2 itemsearch
  5431. NSData *ret = nil;
  5432. NSDictionary *items = nil;
  5433. switch (from) {
  5434. case 0:{
  5435. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5436. }
  5437. break;
  5438. case 1:{
  5439. items = [[self search:params limited:NO] objectForKey:@"items"];
  5440. }
  5441. break;
  5442. case 2:{
  5443. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5444. }
  5445. break;
  5446. default:
  5447. break;
  5448. }
  5449. if (!items) {
  5450. return ret;
  5451. }
  5452. int count = [[items objectForKey:@"count"] intValue];
  5453. NSMutableString *product_id_str = [@"" mutableCopy];
  5454. for (int i = 0; i < count; i++) {
  5455. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5456. NSDictionary *item = [items objectForKey:key];
  5457. NSString *product_id = [item objectForKey:@"product_id"];
  5458. if (i == 0) {
  5459. [product_id_str appendString:product_id];
  5460. } else {
  5461. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5462. }
  5463. }
  5464. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5465. if ([add_to isEqualToString:@"cart"]) {
  5466. NSString *order_code = [params objectForKey:@"orderCode"];
  5467. if (order_code.length) {
  5468. NSDictionary *newParams = @{
  5469. @"product_id" : product_id_str,
  5470. @"orderCode" : order_code,
  5471. @"can_create_backorder":params[@"can_create_backorder"]
  5472. };
  5473. ret = [self offline_add2cart:newParams.mutableCopy];
  5474. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5475. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5476. ret = [RAConvertor dict2data:retDic];
  5477. }
  5478. } else if([add_to isEqualToString:@"wishlist"]) {
  5479. NSDictionary *newParams = @{
  5480. @"product_id" : product_id_str
  5481. };
  5482. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5483. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5484. ret = [RAConvertor dict2data:retDic];
  5485. } else if([add_to isEqualToString:@"portfolio"]) {
  5486. NSDictionary *newParams = @{
  5487. @"product_id" : product_id_str
  5488. };
  5489. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5490. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5491. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5492. ret = [RAConvertor dict2data:retDic];
  5493. }
  5494. return ret;
  5495. }
  5496. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5497. {
  5498. return [self addAll:params from:0];
  5499. }
  5500. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5501. {
  5502. return [self addAll:params from:1];
  5503. }
  5504. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5505. {
  5506. return [self addAll:params from:2];
  5507. }
  5508. #pragma mark - Jack
  5509. #warning 做SQL操作时转义!!
  5510. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5511. // "val_227" : {
  5512. // "check" : 1,
  5513. // "value" : "US United States",
  5514. // "value_id" : "228"
  5515. // },
  5516. if (!countryCode) {
  5517. countryCode = @"US";
  5518. }
  5519. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5520. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5521. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5522. int code_id = sqlite3_column_int(stmt, 3); // id
  5523. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5524. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5525. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5526. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5527. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5528. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5529. }
  5530. long n = *count;
  5531. *count = n + 1;
  5532. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5533. [container setValue:countryDic forKey:key];
  5534. }] mutableCopy];
  5535. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5536. return ret;
  5537. }
  5538. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5539. // "val_227" : {
  5540. // "check" : 1,
  5541. // "value" : "US United States",
  5542. // "value_id" : "228"
  5543. // },
  5544. __block NSString* OrderCarrier =@"";
  5545. // sqlite3 *db = [iSalesDB get_db];
  5546. // sqlite3_stmt * statement;
  5547. [iSalesDB jk_query:[NSString stringWithFormat:@"select carrier from offline_order where so_id='%@';",order_code] db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5548. OrderCarrier = [self textAtColumn:0 statement:statment];
  5549. // sqlite3_finalize(statement);
  5550. }];
  5551. // [iSalesDB close_db:db];
  5552. __block int c=0;
  5553. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5554. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5555. int code_id = sqlite3_column_int(stmt, 2); // id
  5556. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5557. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5558. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5559. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5560. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5561. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5562. }
  5563. c++;
  5564. long n = *count;
  5565. *count = n + 1;
  5566. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5567. [container setValue:carrierDic forKey:key];
  5568. }] mutableCopy];
  5569. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5570. return ret;
  5571. }
  5572. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5573. // "val_227" : {
  5574. // "check" : 1,
  5575. // "value" : "US United States",
  5576. // "value_id" : "228"
  5577. // },
  5578. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5579. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5580. int code_id = sqlite3_column_int(stmt, 2); // id
  5581. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5582. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5583. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5584. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5585. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5586. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5587. }
  5588. long n = *count;
  5589. *count = n + 1;
  5590. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5591. [container setValue:carrierDic forKey:key];
  5592. }] mutableCopy];
  5593. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5594. return ret;
  5595. }
  5596. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5597. // // "val_227" : {
  5598. // // "check" : 1,
  5599. // // "value" : "US United States",
  5600. // // "value_id" : "228"
  5601. // // },
  5602. //
  5603. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5604. // NSString * code = [iSalesDB jk_queryText:Sql];
  5605. //
  5606. // return [self offline_getCarrier:code];
  5607. //
  5608. //}
  5609. + (NSString *)offline_getCarrier:(int)code {
  5610. // "val_227" : {
  5611. // "check" : 1,
  5612. // "value" : "US United States",
  5613. // "value_id" : "228"
  5614. // },
  5615. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5616. NSString * ret = [iSalesDB jk_queryText:Sql];
  5617. return ret;
  5618. }
  5619. + (int )offline_getCarrierCode:(NSString*)name {
  5620. // "val_227" : {
  5621. // "check" : 1,
  5622. // "value" : "US United States",
  5623. // "value_id" : "228"
  5624. // },
  5625. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5626. NSString * ret = [iSalesDB jk_queryText:Sql];
  5627. return [ret intValue];
  5628. }
  5629. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5630. countryCode = [self translateSingleQuote:countryCode];
  5631. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5632. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5633. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5634. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5635. if (name == NULL) {
  5636. name = "";
  5637. }
  5638. if (code == NULL) {
  5639. code = "";
  5640. }
  5641. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5642. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5643. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5644. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5645. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5646. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5647. }
  5648. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5649. [container setValue:stateDic forKey:key];
  5650. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5651. DebugLog(@"query all state error: %@",err_msg);
  5652. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5653. // [stateDic setValue:@"Other" forKey:@"value"];
  5654. // [stateDic setValue:@"" forKey:@"value_id"];
  5655. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5656. //
  5657. // if (state_code && [@"" isEqualToString:state_code]) {
  5658. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5659. // }
  5660. //
  5661. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5662. // [container setValue:stateDic forKey:key];
  5663. }] mutableCopy];
  5664. [ret removeObjectForKey:@"result"];
  5665. // failure 可以不用了,一样的
  5666. if (ret.allKeys.count == 0) {
  5667. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5668. [stateDic setValue:@"Other" forKey:@"value"];
  5669. [stateDic setValue:@"" forKey:@"value_id"];
  5670. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5671. if (state_code && [@"" isEqualToString:state_code]) {
  5672. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5673. }
  5674. NSString *key = [NSString stringWithFormat:@"val_0"];
  5675. [ret setValue:stateDic forKey:key];
  5676. }
  5677. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5678. return ret;
  5679. }
  5680. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5681. codeId = [self translateSingleQuote:codeId];
  5682. 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];
  5683. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5684. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5685. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5686. if (name == NULL) {
  5687. name = "";
  5688. }
  5689. if (code == NULL) {
  5690. code = "";
  5691. }
  5692. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5693. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5694. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5695. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5696. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5697. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5698. }
  5699. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5700. [container setValue:stateDic forKey:key];
  5701. }] mutableCopy];
  5702. [ret removeObjectForKey:@"result"];
  5703. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5704. return ret;
  5705. }
  5706. + (NSDictionary *)offline_getPrice {
  5707. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5708. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5709. char *name = (char *) sqlite3_column_text(stmt, 1);
  5710. int type = sqlite3_column_int(stmt, 2);
  5711. int orderBy = sqlite3_column_int(stmt, 3);
  5712. if (name == NULL) {
  5713. name = "";
  5714. }
  5715. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5716. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5717. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5718. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5719. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5720. if (orderBy == 0) {
  5721. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5722. }
  5723. [container setValue:priceDic forKey:key];
  5724. }] mutableCopy];
  5725. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5726. return ret;
  5727. }
  5728. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5729. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5730. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5731. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5732. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5733. // int _id = sqlite3_column_int(stmt, 0);
  5734. NSString *name = [self textAtColumn:1 statement:stmt];
  5735. NSDictionary *typeDic = @{
  5736. @"value_id" : name,
  5737. @"value" : name,
  5738. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5739. };
  5740. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5741. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5742. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5743. }];
  5744. return ret;
  5745. }
  5746. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5747. // 首先从offline_login表中取出sales_code
  5748. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5749. // NSString *user = app.user;
  5750. user = [self translateSingleQuote:user];
  5751. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5752. __block NSString *user_code = @"";
  5753. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5754. char *code = (char *)sqlite3_column_text(stmt, 0);
  5755. if (code == NULL) {
  5756. code = "";
  5757. }
  5758. user_code = [NSString stringWithUTF8String:code];
  5759. }];
  5760. // 再取所有salesRep
  5761. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5762. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5763. // 1 name 2 code 3 salesrep_id
  5764. char *name = (char *)sqlite3_column_text(stmt, 1);
  5765. char *code = (char *)sqlite3_column_text(stmt, 2);
  5766. int salesrep_id = sqlite3_column_int(stmt, 3);
  5767. if (name == NULL) {
  5768. name = "";
  5769. }
  5770. if (code == NULL) {
  5771. code = "";
  5772. }
  5773. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5774. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5775. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5776. // 比较code 相等则check
  5777. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5778. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5779. }
  5780. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5781. }] mutableCopy];
  5782. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5783. return ret;
  5784. }
  5785. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5786. zipcode = [self translateSingleQuote:zipcode];
  5787. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5788. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5789. char *country = (char *)sqlite3_column_text(stmt, 0);
  5790. char *state = (char *)sqlite3_column_text(stmt, 1);
  5791. char *city = (char *)sqlite3_column_text(stmt, 2);
  5792. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5793. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5794. if (country == NULL) {
  5795. country = "";
  5796. }
  5797. if (state == NULL) {
  5798. state = "";
  5799. }
  5800. if (city == NULL) {
  5801. city = "";
  5802. }
  5803. if (country_code == NULL) {
  5804. country_code = "";
  5805. }
  5806. if (state_code == NULL) {
  5807. state_code = "";
  5808. }
  5809. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5810. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5811. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5812. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5813. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5814. }] mutableCopy];
  5815. return ret;
  5816. }
  5817. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5818. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5819. [item setValue:value forKey:valueKey];
  5820. [dic setValue:item forKey:itemKey];
  5821. }
  5822. + (NSString *)countryCodeByid:(NSString *)code_id {
  5823. NSString *ret = nil;
  5824. code_id = [self translateSingleQuote:code_id];
  5825. sqlite3 *db = [iSalesDB get_db];
  5826. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5827. sqlite3_stmt * statement;
  5828. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5829. while (sqlite3_step(statement) == SQLITE_ROW) {
  5830. char *code = (char *)sqlite3_column_text(statement, 0);
  5831. if (code == NULL) {
  5832. code = "";
  5833. }
  5834. ret = [NSString stringWithUTF8String:code];
  5835. }
  5836. sqlite3_finalize(statement);
  5837. }
  5838. [iSalesDB close_db:db];
  5839. return ret;
  5840. }
  5841. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5842. NSString *ret = nil;
  5843. code = [self translateSingleQuote:code];
  5844. sqlite3 *db = [iSalesDB get_db];
  5845. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5846. sqlite3_stmt * statement;
  5847. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5848. while (sqlite3_step(statement) == SQLITE_ROW) {
  5849. char *_id = (char *)sqlite3_column_text(statement, 0);
  5850. if (_id == NULL) {
  5851. _id = "";
  5852. }
  5853. ret = [NSString stringWithFormat:@"%s",_id];
  5854. }
  5855. sqlite3_finalize(statement);
  5856. }
  5857. [iSalesDB close_db:db];
  5858. return ret;
  5859. }
  5860. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5861. NSString *name = nil;
  5862. codeId = [self translateSingleQuote:codeId];
  5863. sqlite3 *db = [iSalesDB get_db];
  5864. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5865. sqlite3_stmt * statement;
  5866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5867. while (sqlite3_step(statement) == SQLITE_ROW) {
  5868. char *value = (char *)sqlite3_column_text(statement, 0);
  5869. if (value == NULL) {
  5870. value = "";
  5871. }
  5872. name = [NSString stringWithUTF8String:value];
  5873. }
  5874. sqlite3_finalize(statement);
  5875. }
  5876. [iSalesDB close_db:db];
  5877. return name;
  5878. }
  5879. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5880. NSString *ret = nil;
  5881. sqlite3 *db = [iSalesDB get_db];
  5882. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5883. sqlite3_stmt * statement;
  5884. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5885. while (sqlite3_step(statement) == SQLITE_ROW) {
  5886. char *name = (char *)sqlite3_column_text(statement, 0);
  5887. if (name == NULL) {
  5888. name = "";
  5889. }
  5890. ret = [NSString stringWithUTF8String:name];
  5891. }
  5892. sqlite3_finalize(statement);
  5893. }
  5894. [iSalesDB close_db:db];
  5895. return ret;
  5896. }
  5897. + (NSString *)salesRepCodeById:(NSString *)_id {
  5898. NSString *ret = nil;
  5899. _id = [self translateSingleQuote:_id];
  5900. sqlite3 *db = [iSalesDB get_db];
  5901. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5902. sqlite3_stmt * statement;
  5903. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5904. while (sqlite3_step(statement) == SQLITE_ROW) {
  5905. char *rep = (char *)sqlite3_column_text(statement, 0);
  5906. if (rep == NULL) {
  5907. rep = "";
  5908. }
  5909. ret = [NSString stringWithUTF8String:rep];
  5910. }
  5911. sqlite3_finalize(statement);
  5912. }
  5913. [iSalesDB close_db:db];
  5914. return ret;
  5915. }
  5916. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5917. char *tx = (char *)sqlite3_column_text(stmt, col);
  5918. if (tx == NULL) {
  5919. tx = "";
  5920. }
  5921. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5922. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5923. if (!text) {
  5924. text = @"";
  5925. }
  5926. // 将字符全部为' '的字符串干掉
  5927. int spaceCount = 0;
  5928. for (int i = 0; i < text.length; i++) {
  5929. if ([text characterAtIndex:i] == ' ') {
  5930. spaceCount++;
  5931. }
  5932. }
  5933. if (spaceCount == text.length) {
  5934. text = @"";
  5935. }
  5936. return text;
  5937. }
  5938. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5939. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5940. NSData *data = [NSData dataWithContentsOfFile:path];
  5941. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5942. return ret;
  5943. }
  5944. + (NSString *)textFileName:(NSString *)name {
  5945. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5946. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5947. if (!text) {
  5948. text = @"";
  5949. }
  5950. return text;
  5951. }
  5952. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5953. return [[dic objectForKey:key] mutableCopy];
  5954. }
  5955. + (id)translateSingleQuote:(NSString *)string {
  5956. if ([string isKindOfClass:[NSString class]])
  5957. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5958. return string;
  5959. }
  5960. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5961. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5962. }
  5963. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5964. NSString* ret= nil;
  5965. NSString *sqlQuery = nil;
  5966. // 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
  5967. 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];
  5968. sqlite3_stmt * statement;
  5969. // int count=0;
  5970. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5971. {
  5972. if (sqlite3_step(statement) == SQLITE_ROW)
  5973. {
  5974. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5975. if(imgurl==nil)
  5976. imgurl="";
  5977. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5978. ret=nsimgurl;
  5979. }
  5980. sqlite3_finalize(statement);
  5981. }
  5982. else
  5983. {
  5984. [ret setValue:@"8" forKey:@"result"];
  5985. }
  5986. // [iSalesDB close_db:db];
  5987. // DebugLog(@"data string: %@",ret );
  5988. return ret;
  5989. }
  5990. #pragma mark contact Advanced search
  5991. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5992. {
  5993. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5994. return [RAConvertor dict2data:contactAdvanceDic];
  5995. }
  5996. #pragma mark create new contact
  5997. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5998. assert(params[@"user"]!=nil);
  5999. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  6000. NSData *data = [NSData dataWithContentsOfFile:path];
  6001. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6002. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6003. NSString *countryCode = nil;
  6004. NSString *countryCode_id = nil;
  6005. NSString *stateCode = nil;
  6006. NSString *city = nil;
  6007. NSString *zipCode = nil;
  6008. NSString *carrierCode = nil;
  6009. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6010. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  6011. NSString *code_id = params[@"country"];
  6012. countryCode_id = code_id;
  6013. countryCode = [self countryCodeByid:code_id];
  6014. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  6015. NSString *zip_code = params[@"zipcode"];
  6016. // 剔除全部为空格
  6017. int spaceCount = 0;
  6018. for (int i = 0; i < zip_code.length; i++) {
  6019. if ([zip_code characterAtIndex:i] == ' ') {
  6020. spaceCount++;
  6021. }
  6022. }
  6023. if (spaceCount == zip_code.length) {
  6024. zip_code = @"";
  6025. }
  6026. zipCode = zip_code;
  6027. if (zipCode.length > 0) {
  6028. countryCode_id = params[@"country"];
  6029. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6030. countryCode = [dic valueForKey:@"country_code"];
  6031. if (!countryCode) {
  6032. // countryCode = @"US";
  6033. NSString *code_id = params[@"country"];
  6034. countryCode = [self countryCodeByid:code_id];
  6035. }
  6036. stateCode = [dic valueForKey:@"state_code"];
  6037. if (!stateCode.length) {
  6038. stateCode = params[@"state"];
  6039. }
  6040. city = [dic valueForKey:@"city"];
  6041. if (!city.length) {
  6042. city = params[@"city"];
  6043. }
  6044. // zip code
  6045. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  6046. [zipDic setValue:zipCode forKey:@"value"];
  6047. [section_0 setValue:zipDic forKey:@"item_11"];
  6048. } else {
  6049. NSString *code_id = params[@"country"];
  6050. countryCode = [self countryCodeByid:code_id];
  6051. stateCode = params[@"state"];
  6052. city = params[@"city"];
  6053. }
  6054. }
  6055. } else {
  6056. // default: US United States
  6057. countryCode = @"US";
  6058. countryCode_id = @"228";
  6059. }
  6060. // country
  6061. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6062. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  6063. // state
  6064. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6065. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  6066. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  6067. // city
  6068. if (city) {
  6069. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  6070. [cityDic setValue:city forKey:@"value"];
  6071. [section_0 setValue:cityDic forKey:@"item_13"];
  6072. }
  6073. // price type
  6074. NSDictionary *priceDic = [self offline_getPrice];
  6075. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  6076. // contact type
  6077. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  6078. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  6079. // Sales Rep
  6080. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  6081. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6082. #ifdef BUILD_CONTRAST
  6083. //carrier
  6084. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  6085. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6086. #endif
  6087. [ret setValue:section_0 forKey:@"section_0"];
  6088. return [RAConvertor dict2data:ret];
  6089. }
  6090. #pragma mark save
  6091. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  6092. NSString *addr = nil;
  6093. NSString *contact_name = nil;
  6094. // int carrier = [params[@"carrier"] intValue];
  6095. NSString *companyName = [params objectForKey:@"company"];
  6096. if (companyName) {
  6097. companyName = [AESCrypt fastencrypt:companyName];
  6098. } else {
  6099. companyName = @"";
  6100. }
  6101. DebugLog(@"company");
  6102. companyName = [self translateSingleQuote:companyName];
  6103. NSString *addr1 = [params objectForKey:@"address"];
  6104. NSString *addr2 = [params objectForKey:@"address2"];
  6105. NSString *addr3 = [params objectForKey:@"address_3"];
  6106. NSString *addr4 = [params objectForKey:@"address_4"];
  6107. if (!addr2) {
  6108. addr2 = @"";
  6109. }
  6110. if (!addr3) {
  6111. addr3 = @"";
  6112. }
  6113. if (!addr4) {
  6114. addr4 = @"";
  6115. }
  6116. if (!addr1) {
  6117. addr1 = @"";
  6118. }
  6119. DebugLog(@"addr");
  6120. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  6121. addr = [AESCrypt fastencrypt:addr];
  6122. addr = [self translateSingleQuote:addr];
  6123. if (addr1 && ![addr1 isEqualToString:@""]) {
  6124. addr1 = [AESCrypt fastencrypt:addr1];
  6125. }
  6126. addr1 = [self translateSingleQuote:addr1];
  6127. addr2 = [self translateSingleQuote:addr2];
  6128. addr3 = [self translateSingleQuote:addr3];
  6129. addr4 = [self translateSingleQuote:addr4];
  6130. NSString *country = [params objectForKey:@"country"];
  6131. if (country) {
  6132. country = [self countryNameByCountryCodeId:country];
  6133. } else {
  6134. country = @"";
  6135. }
  6136. int carrier = [params[@"carrier"] intValue];
  6137. DebugLog(@"country");
  6138. country = [self translateSingleQuote:country];
  6139. NSString *state = [params objectForKey:@"state"];
  6140. if (!state) {
  6141. state = @"";
  6142. }
  6143. DebugLog(@"state");
  6144. state = [self translateSingleQuote:state];
  6145. NSString *city = [params objectForKey:@"city"];
  6146. if (!city) {
  6147. city = @"";
  6148. }
  6149. city = [self translateSingleQuote:city];
  6150. NSString *zipcode = [params objectForKey:@"zipcode"];
  6151. if (!zipcode) {
  6152. zipcode = @"";
  6153. }
  6154. DebugLog(@"zip");
  6155. zipcode = [self translateSingleQuote:zipcode];
  6156. NSString *fistName = [params objectForKey:@"firstname"];
  6157. if (!fistName) {
  6158. fistName = @"";
  6159. }
  6160. NSString *lastName = [params objectForKey:@"lastname"];
  6161. if (!lastName) {
  6162. lastName = @"";
  6163. }
  6164. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  6165. DebugLog(@"contact_name");
  6166. contact_name = [self translateSingleQuote:contact_name];
  6167. fistName = [self translateSingleQuote:fistName];
  6168. lastName = [self translateSingleQuote:lastName];
  6169. NSString *phone = [params objectForKey:@"phone"];
  6170. if (phone) {
  6171. phone = [AESCrypt fastencrypt:phone];
  6172. } else {
  6173. phone = @"";
  6174. }
  6175. DebugLog(@"PHONE");
  6176. phone = [self translateSingleQuote:phone];
  6177. NSString *fax = [params objectForKey:@"fax"];
  6178. if (!fax) {
  6179. fax = @"";
  6180. }
  6181. DebugLog(@"FAX");
  6182. fax = [self translateSingleQuote:fax];
  6183. NSString *email = [params objectForKey:@"email"];
  6184. if (!email) {
  6185. email = @"";
  6186. }
  6187. DebugLog(@"EMAIL:%@",email);
  6188. email = [self translateSingleQuote:email];
  6189. NSString *notes = [params objectForKey:@"contact_notes"];
  6190. if (!notes) {
  6191. notes = @"";
  6192. }
  6193. DebugLog(@"NOTE:%@",notes);
  6194. notes = [self translateSingleQuote:notes];
  6195. NSString *price = [params objectForKey:@"price_name"];
  6196. if (price) {
  6197. price = [self priceNameByPriceId:price];
  6198. } else {
  6199. price = @"";
  6200. }
  6201. DebugLog(@"PRICE");
  6202. price = [self translateSingleQuote:price];
  6203. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6204. if (salesRep) {
  6205. salesRep = [self salesRepCodeById:salesRep];
  6206. } else {
  6207. salesRep = @"";
  6208. }
  6209. salesRep = [self translateSingleQuote:salesRep];
  6210. NSString *img = [params objectForKey:@"business_card"];
  6211. NSArray *array = [img componentsSeparatedByString:@","];
  6212. NSString *img_0 = array[0];
  6213. if (!img_0) {
  6214. img_0 = @"";
  6215. }
  6216. img_0 = [self translateSingleQuote:img_0];
  6217. NSString *img_1 = array[1];
  6218. if (!img_1) {
  6219. img_1 = @"";
  6220. }
  6221. img_1 = [self translateSingleQuote:img_1];
  6222. NSString *img_2 = array[2];
  6223. if (!img_2) {
  6224. img_2 = @"";
  6225. }
  6226. img_2 = [self translateSingleQuote:img_2];
  6227. NSString *contact_id = [NSUUID UUID].UUIDString;
  6228. NSString *contact_type = [params objectForKey:@"type_name"];
  6229. if (!contact_type) {
  6230. contact_type = @"";
  6231. }
  6232. contact_type = [self translateSingleQuote:contact_type];
  6233. // 判断更新时是否为customer
  6234. if (update) {
  6235. contact_id = [params objectForKey:@"contact_id"];
  6236. if (!contact_id) {
  6237. contact_id = @"";
  6238. }
  6239. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6240. __block int customer = 0;
  6241. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6242. customer = sqlite3_column_int(stmt, 0);
  6243. }];
  6244. isCustomer = customer ? YES : NO;
  6245. }
  6246. NSMutableDictionary *sync_dic = [params mutableCopy];
  6247. if (isCustomer) {
  6248. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6249. } else {
  6250. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6251. }
  6252. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  6253. NSString *sync_data = nil;
  6254. NSString *sql = nil;
  6255. if (update){
  6256. contact_id = [params objectForKey:@"contact_id"];
  6257. if (!contact_id) {
  6258. contact_id = @"";
  6259. }
  6260. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6261. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6262. sync_data = [RAConvertor dict2string:sync_dic];
  6263. sync_data = [self translateSingleQuote:sync_data];
  6264. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' ,carrier = %d where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,carrier,contact_id];
  6265. } else {
  6266. contact_id = [self translateSingleQuote:contact_id];
  6267. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6268. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6269. sync_data = [RAConvertor dict2string:sync_dic];
  6270. sync_data = [self translateSingleQuote:sync_data];
  6271. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type,carrier) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@',%d)",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type,carrier];
  6272. }
  6273. int result = [iSalesDB execSql:sql];
  6274. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6275. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6276. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6277. }
  6278. #pragma mark save new contact
  6279. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6280. {
  6281. return [self offline_saveContact:params update:NO isCustomer:YES];
  6282. }
  6283. #pragma mark edit contact
  6284. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6285. {
  6286. assert(params[@"user"]!=nil);
  6287. // {
  6288. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6289. // password = 123456;
  6290. // user = EvanK;
  6291. // }
  6292. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6293. NSData *data = [NSData dataWithContentsOfFile:path];
  6294. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6295. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6296. NSString *countryCode = nil;
  6297. NSString *countryCode_id = nil;
  6298. NSString *stateCode = nil;
  6299. __block int carrierCode = -1;
  6300. /*------contact infor------*/
  6301. __block NSString *country = nil;
  6302. __block NSString *company_name = nil;
  6303. __block NSString *contact_id = params[@"contact_id"];
  6304. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6305. __block NSString *zipcode = nil;
  6306. __block NSString *state = nil; // state_code
  6307. __block NSString *city = nil; //
  6308. __block NSString *firt_name,*last_name;
  6309. __block NSString *phone,*fax,*email;
  6310. __block NSString *notes,*price_type,*sales_rep;
  6311. __block NSString *img_0,*img_1,*img_2;
  6312. __block NSString *contact_type;
  6313. contact_id = [self translateSingleQuote:contact_id];
  6314. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type,carrier from offline_contact where contact_id = '%@';",contact_id];
  6315. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6316. country = [self textAtColumn:0 statement:stmt]; // country name
  6317. company_name = [self textAtColumn:1 statement:stmt];
  6318. addr_1 = [self textAtColumn:2 statement:stmt];
  6319. addr_2 = [self textAtColumn:3 statement:stmt];
  6320. addr_3 = [self textAtColumn:4 statement:stmt];
  6321. addr_4 = [self textAtColumn:5 statement:stmt];
  6322. zipcode = [self textAtColumn:6 statement:stmt];
  6323. state = [self textAtColumn:7 statement:stmt]; // state code
  6324. city = [self textAtColumn:8 statement:stmt];
  6325. firt_name = [self textAtColumn:9 statement:stmt];
  6326. last_name = [self textAtColumn:10 statement:stmt];
  6327. phone = [self textAtColumn:11 statement:stmt];
  6328. fax = [self textAtColumn:12 statement:stmt];
  6329. email = [self textAtColumn:13 statement:stmt];
  6330. notes = [self textAtColumn:14 statement:stmt];
  6331. price_type = [self textAtColumn:15 statement:stmt]; // name
  6332. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6333. img_0 = [self textAtColumn:17 statement:stmt];
  6334. img_1 = [self textAtColumn:18 statement:stmt];
  6335. img_2 = [self textAtColumn:19 statement:stmt];
  6336. contact_type = [self textAtColumn:20 statement:stmt];
  6337. carrierCode = sqlite3_column_int(stmt, 21);
  6338. }];
  6339. // decrypt
  6340. if (company_name) {
  6341. company_name = [AESCrypt fastdecrypt:company_name];
  6342. }
  6343. if (addr_1) {
  6344. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6345. }
  6346. if (phone) {
  6347. phone = [AESCrypt fastdecrypt:phone];
  6348. }
  6349. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6350. countryCode = [iSalesDB jk_queryText:countrySql];
  6351. stateCode = state;
  6352. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6353. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6354. NSString *code_id = params[@"country"];
  6355. countryCode_id = code_id;
  6356. countryCode = [self countryCodeByid:code_id];
  6357. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6358. NSString *zip_code = params[@"zipcode"];
  6359. // 剔除全部为空格
  6360. int spaceCount = 0;
  6361. for (int i = 0; i < zip_code.length; i++) {
  6362. if ([zip_code characterAtIndex:i] == ' ') {
  6363. spaceCount++;
  6364. }
  6365. }
  6366. if (spaceCount == zip_code.length) {
  6367. zip_code = @"";
  6368. }
  6369. if (zipcode.length > 0) {
  6370. zipcode = zip_code;
  6371. countryCode_id = params[@"country"];
  6372. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6373. countryCode = [dic valueForKey:@"country_code"];
  6374. if (!countryCode) {
  6375. NSString *code_id = params[@"country"];
  6376. countryCode = [self countryCodeByid:code_id];
  6377. }
  6378. stateCode = [dic valueForKey:@"state_code"];
  6379. if (!stateCode.length) {
  6380. stateCode = params[@"state"];
  6381. }
  6382. city = [dic valueForKey:@"city"];
  6383. if (!city.length) {
  6384. city = params[@"city"];
  6385. }
  6386. // zip code
  6387. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6388. [zipDic setValue:zipcode forKey:@"value"];
  6389. [section_0 setValue:zipDic forKey:@"item_8"];
  6390. } else {
  6391. NSString *code_id = params[@"country"];
  6392. countryCode = [self countryCodeByid:code_id];
  6393. stateCode = params[@"state"];
  6394. city = params[@"city"];
  6395. }
  6396. }
  6397. }
  6398. // 0 Country
  6399. // 1 Company Name
  6400. // 2 Contact ID
  6401. // 3 Picture
  6402. // 4 Address 1
  6403. // 5 Address 2
  6404. // 6 Address 3
  6405. // 7 Address 4
  6406. // 8 Zip Code
  6407. // 9 State/Province
  6408. // 10 City
  6409. // 11 Contact First Name
  6410. // 12 Contact Last Name
  6411. // 13 Phone
  6412. // 14 Fax
  6413. // 15 Email
  6414. // 16 Contact Notes
  6415. // 17 Price Type
  6416. // 18 Contact Type
  6417. // 19 Sales Rep
  6418. // country
  6419. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6420. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6421. // company
  6422. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6423. // contact_id
  6424. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6425. // picture
  6426. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6427. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6428. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6429. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6430. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6431. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6432. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6433. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6434. // addr 1 2 3 4
  6435. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6436. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6437. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6438. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6439. // zip code
  6440. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6441. // state
  6442. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6443. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6444. // city
  6445. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6446. // first last
  6447. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6448. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6449. // phone fax email
  6450. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6451. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6452. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6453. // notes
  6454. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6455. // price
  6456. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6457. for (NSString *key in priceDic.allKeys) {
  6458. if ([key containsString:@"val_"]) {
  6459. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6460. if ([dic[@"value"] isEqualToString:price_type]) {
  6461. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6462. [priceDic setValue:dic forKey:key];
  6463. }
  6464. }
  6465. }
  6466. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6467. // Contact Rep
  6468. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6469. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6470. // Sales Rep
  6471. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6472. for (NSString *key in repDic.allKeys) {
  6473. if ([key containsString:@"val_"]) {
  6474. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6475. NSString *value = dic[@"value"];
  6476. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6477. if (code && [code isEqualToString:sales_rep]) {
  6478. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6479. [repDic setValue:dic forKey:key];
  6480. }
  6481. }
  6482. }
  6483. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6484. #ifdef BUILD_CONTRAST
  6485. //carrier
  6486. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6487. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6488. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6489. #endif
  6490. [ret setValue:section_0 forKey:@"section_0"];
  6491. return [RAConvertor dict2data:ret];
  6492. }
  6493. #pragma mark save contact
  6494. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6495. {
  6496. return [self offline_saveContact:params update:YES isCustomer:YES];
  6497. }
  6498. #pragma mark category
  6499. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6500. if (ck) {
  6501. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6502. for (NSString *key in res.allKeys) {
  6503. if (![key isEqualToString:@"count"]) {
  6504. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6505. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6506. if ([val[@"value"] isEqualToString:ck]) {
  6507. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6508. }
  6509. [res setValue:val forKey:key];
  6510. }
  6511. }
  6512. [dic setValue:res forKey:valueKey];
  6513. }
  6514. }
  6515. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6516. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6517. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6518. NSString* category = [params valueForKey:@"category"];
  6519. if (!category || [category isEqualToString:@""]) {
  6520. category = @"%";
  6521. }
  6522. category = [self translateSingleQuote:category];
  6523. int limit = [[params valueForKey:@"limit"] intValue];
  6524. int offset = [[params valueForKey:@"offset"] intValue];
  6525. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6526. NSString *limit_str = @"";
  6527. if (limited) {
  6528. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6529. }
  6530. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6531. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6532. sqlite3 *db = [iSalesDB get_db];
  6533. // [iSalesDB AddExFunction:db];
  6534. int count;
  6535. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6536. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",params[@"orderCode"], category,limit_str];
  6537. double price_min = 0;
  6538. double price_max = 0;
  6539. if ([params.allKeys containsObject:@"alert"]) {
  6540. // alert
  6541. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6542. NSString *alert = params[@"alert"];
  6543. if ([alert isEqualToString:@"Display All"]) {
  6544. alert = [NSString stringWithFormat:@""];
  6545. } else {
  6546. alert = [self translateSingleQuote:alert];
  6547. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6548. }
  6549. // available
  6550. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6551. NSString *available = params[@"available"];
  6552. NSString *available_condition;
  6553. if ([available isEqualToString:@"Display All"]) {
  6554. available_condition = @"";
  6555. } else if ([available isEqualToString:@"Available Now"]) {
  6556. available_condition = @"and availability > 0";
  6557. } else {
  6558. available_condition = @"and availability == 0";
  6559. }
  6560. // best seller
  6561. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6562. NSString *best_seller = @"";
  6563. NSString *order_best_seller = @"m.name asc";
  6564. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6565. best_seller = @"and best_seller > 0";
  6566. order_best_seller = @"m.best_seller desc,m.name asc";
  6567. }
  6568. // price
  6569. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6570. NSString *price = params[@"price"];
  6571. price_min = 0;
  6572. price_max = MAXFLOAT;
  6573. if (params[@"user"] && price != nil) {
  6574. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6575. NSMutableString *priceName = [NSMutableString string];
  6576. for (int i = 0; i < priceTypeArray.count; i++) {
  6577. NSString *pricetype = priceTypeArray[i];
  6578. pricetype = [self translateSingleQuote:pricetype];
  6579. if (i == 0) {
  6580. [priceName appendFormat:@"'%@'",pricetype];
  6581. } else {
  6582. [priceName appendFormat:@",'%@'",pricetype];
  6583. }
  6584. }
  6585. if ([price isEqualToString:@"Display All"]) {
  6586. price = [NSString stringWithFormat:@""];
  6587. } else if([price containsString:@"+"]){
  6588. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6589. price_min = [price doubleValue];
  6590. 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];
  6591. } else {
  6592. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6593. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6594. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6595. 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];
  6596. }
  6597. } else {
  6598. price = @"";
  6599. }
  6600. // sold_by_qty : Sold in quantities of %@
  6601. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6602. NSString *qty = params[@"sold_by_qty"];
  6603. if ([qty isEqualToString:@"Display All"]) {
  6604. qty = @"";
  6605. } else {
  6606. qty = [self translateSingleQuote:qty];
  6607. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6608. }
  6609. // model name;
  6610. NSString* modelname =params[@"modelName"];
  6611. if(modelname.length==0)
  6612. {
  6613. modelname=@"";
  6614. }
  6615. else
  6616. {
  6617. modelname = modelname.lowercaseString;
  6618. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6619. }
  6620. //modelDescription
  6621. NSString* modelDescription =params[@"modelDescription"];
  6622. if(modelDescription.length==0)
  6623. {
  6624. modelDescription=@"";
  6625. }
  6626. else
  6627. {
  6628. modelDescription = modelDescription.lowercaseString;
  6629. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6630. }
  6631. // cate
  6632. // category = [self translateSingleQuote:category];
  6633. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6634. // cate mutiple selection
  6635. NSString *category_id = params[@"category"];
  6636. NSMutableArray *cate_id_array = nil;
  6637. NSMutableString *cateWhere = [NSMutableString string];
  6638. if ([category_id isEqualToString:@""] || !category_id) {
  6639. [cateWhere appendString:@"1 = 1"];
  6640. } else {
  6641. if ([category_id containsString:@","]) {
  6642. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6643. } else {
  6644. cate_id_array = [@[category_id] mutableCopy];
  6645. }
  6646. [cateWhere appendString:@"("];
  6647. for (int i = 0; i < cate_id_array.count; i++) {
  6648. if (i == 0) {
  6649. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6650. } else {
  6651. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6652. }
  6653. }
  6654. [cateWhere appendString:@")"];
  6655. }
  6656. // where bestseller > 0 order by bestseller desc
  6657. // sql query: alert availability(int) best_seller(int) price qty
  6658. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color from product where %@ %@ %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  6659. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6660. }
  6661. DebugLog(@"offline category where: %@",where);
  6662. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6663. if (!params[@"user"]) {
  6664. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6665. }
  6666. [ret setValue:filter forKey:@"filter"];
  6667. DebugLog(@"offline_category sql:%@",sqlQuery);
  6668. sqlite3_stmt * statement;
  6669. [ret setValue:@"2" forKey:@"result"];
  6670. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6671. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6672. // int count=0;
  6673. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6674. {
  6675. int i=0;
  6676. while (sqlite3_step(statement) == SQLITE_ROW)
  6677. {
  6678. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6679. char *name = (char*)sqlite3_column_text(statement, 0);
  6680. if(name==nil)
  6681. name="";
  6682. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6683. char *description = (char*)sqlite3_column_text(statement, 1);
  6684. if(description==nil)
  6685. description="";
  6686. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6687. int product_id = sqlite3_column_int(statement, 2);
  6688. int wid = sqlite3_column_int(statement, 3);
  6689. int closeout = sqlite3_column_int(statement, 4);
  6690. int cid = sqlite3_column_int(statement, 5);
  6691. int wisdelete = sqlite3_column_int(statement, 6);
  6692. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6693. int more_color = sqlite3_column_int(statement, 8);
  6694. // Defaul Category ID
  6695. __block NSString *categoryID = nil;
  6696. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6697. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6698. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6699. if(default_category==nil)
  6700. default_category="";
  6701. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6702. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6703. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6704. categoryID = nsdefault_category;
  6705. }];
  6706. if (!categoryID.length) {
  6707. NSString *cateIDs = params[@"category"];
  6708. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6709. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6710. for (NSString *cateID in requestCategoryArr) {
  6711. BOOL needBreak = NO;
  6712. for (NSString *itemCateIDBox in itemCategoryArr) {
  6713. if (itemCateIDBox.length > 4) {
  6714. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6715. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6716. if ([itemCategoryID isEqualToString:cateID]) {
  6717. needBreak = YES;
  6718. categoryID = itemCategoryID;
  6719. break;
  6720. }
  6721. }
  6722. }
  6723. if (needBreak) {
  6724. break;
  6725. }
  6726. }
  6727. }
  6728. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6729. if(wid !=0 && wisdelete != 1)
  6730. [item setValue:@"true" forKey:@"wish_exists"];
  6731. else
  6732. [item setValue:@"false" forKey:@"wish_exists"];
  6733. if(closeout==0)
  6734. [item setValue:@"false" forKey:@"is_closeout"];
  6735. else
  6736. [item setValue:@"true" forKey:@"is_closeout"];
  6737. if(cid==0)
  6738. [item setValue:@"false" forKey:@"cart_exists"];
  6739. else
  6740. [item setValue:@"true" forKey:@"cart_exists"];
  6741. if (more_color == 0) {
  6742. [item setObject:@(false) forKey:@"more_color"];
  6743. } else if (more_color == 1) {
  6744. [item setObject:@(true) forKey:@"more_color"];
  6745. }
  6746. [item addEntriesFromDictionary:imgjson];
  6747. // [item setValue:nsurl forKey:@"img"];
  6748. [item setValue:nsname forKey:@"name"];
  6749. [item setValue:nsdescription forKey:@"description"];
  6750. if (categoryID) {
  6751. [item setValue:categoryID forKey:@"item_category_id"];
  6752. }
  6753. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6754. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6755. i++;
  6756. }
  6757. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6758. [ret setObject:items forKey:@"items"];
  6759. sqlite3_finalize(statement);
  6760. } else {
  6761. DebugLog(@"nothing...");
  6762. }
  6763. DebugLog(@"count:%d",count);
  6764. [iSalesDB close_db:db];
  6765. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6766. return ret;
  6767. }
  6768. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6769. {
  6770. return [self categoryList:params limited:YES];
  6771. }
  6772. # pragma mark item search
  6773. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6774. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6775. // assert(params[@"order_code"]);
  6776. // params[@"user"]
  6777. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6778. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6779. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6780. // category
  6781. NSDictionary *category_menu = [self offline_category_menu];
  6782. [filter setValue:category_menu forKey:@"category"];
  6783. NSString* where= nil;
  6784. NSString* orderby= @"m.name";
  6785. if (!filterSearch) {
  6786. int covertype = [[params valueForKey:@"covertype"] intValue];
  6787. switch (covertype) {
  6788. case 0:
  6789. {
  6790. where=@"m.category like'%%#005#%%'";
  6791. break;
  6792. }
  6793. case 1:
  6794. {
  6795. where=@"m.alert like '%QS%'";
  6796. break;
  6797. }
  6798. case 2:
  6799. {
  6800. where=@"m.availability>0";
  6801. break;
  6802. }
  6803. case 3:
  6804. {
  6805. where=@"m.best_seller>0";
  6806. orderby=@"m.best_seller desc,m.name asc";
  6807. break;
  6808. }
  6809. default:
  6810. where=@"1=1";
  6811. break;
  6812. }
  6813. }
  6814. int limit = [[params valueForKey:@"limit"] intValue];
  6815. int offset = [[params valueForKey:@"offset"] intValue];
  6816. NSString *limit_str = @"";
  6817. if (limited) {
  6818. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6819. }
  6820. sqlite3 *db = [iSalesDB get_db];
  6821. // [iSalesDB AddExFunction:db];
  6822. int count;
  6823. NSString *sqlQuery = nil;
  6824. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6825. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",params[@"orderCode"], where,orderby,limit_str];
  6826. double price_min = 0;
  6827. double price_max = 0;
  6828. if (filterSearch) {
  6829. // alert
  6830. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6831. NSString *alert = params[@"alert"];
  6832. if ([alert isEqualToString:@"Display All"]) {
  6833. alert = [NSString stringWithFormat:@""];
  6834. } else {
  6835. alert = [self translateSingleQuote:alert];
  6836. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6837. }
  6838. // available
  6839. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6840. NSString *available = params[@"available"];
  6841. NSString *available_condition;
  6842. if ([available isEqualToString:@"Display All"]) {
  6843. available_condition = @"";
  6844. } else if ([available isEqualToString:@"Available Now"]) {
  6845. available_condition = @"and availability > 0";
  6846. } else {
  6847. available_condition = @"and availability == 0";
  6848. }
  6849. // best seller
  6850. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6851. NSString *best_seller = @"";
  6852. NSString *order_best_seller = @"m.name asc";
  6853. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6854. best_seller = @"and best_seller > 0";
  6855. order_best_seller = @"m.best_seller desc,m.name asc";
  6856. }
  6857. // price
  6858. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6859. NSString *price = params[@"price"];
  6860. price_min = 0;
  6861. price_max = MAXFLOAT;
  6862. if (params[@"user"]) {
  6863. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6864. NSMutableString *priceName = [NSMutableString string];
  6865. for (int i = 0; i < priceTypeArray.count; i++) {
  6866. NSString *pricetype = priceTypeArray[i];
  6867. pricetype = [self translateSingleQuote:pricetype];
  6868. if (i == 0) {
  6869. [priceName appendFormat:@"'%@'",pricetype];
  6870. } else {
  6871. [priceName appendFormat:@",'%@'",pricetype];
  6872. }
  6873. }
  6874. if ([price isEqualToString:@"Display All"]) {
  6875. price = [NSString stringWithFormat:@""];
  6876. } else if([price containsString:@"+"]){
  6877. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6878. price_min = [price doubleValue];
  6879. 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];
  6880. } else {
  6881. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6882. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6883. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6884. 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];
  6885. }
  6886. } else {
  6887. price = @"";
  6888. }
  6889. // sold_by_qty : Sold in quantities of %@
  6890. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6891. NSString *qty = params[@"sold_by_qty"];
  6892. if ([qty isEqualToString:@"Display All"]) {
  6893. qty = @"";
  6894. } else {
  6895. qty = [self translateSingleQuote:qty];
  6896. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6897. }
  6898. // model name;
  6899. NSString* modelname =params[@"modelName"];
  6900. if(modelname.length==0)
  6901. {
  6902. modelname=@"";
  6903. }
  6904. else
  6905. {
  6906. modelname = modelname.lowercaseString;
  6907. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6908. }
  6909. //modelDescription
  6910. NSString* modelDescription =params[@"modelDescription"];
  6911. if(modelDescription.length==0)
  6912. {
  6913. modelDescription=@"";
  6914. }
  6915. else
  6916. {
  6917. modelDescription = modelDescription.lowercaseString;
  6918. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6919. }
  6920. //lower(description) like'%%%@%%'
  6921. // category
  6922. NSString *category_id = params[@"ctgId"];
  6923. NSMutableArray *cate_id_array = nil;
  6924. NSMutableString *cateWhere = [NSMutableString string];
  6925. if ([category_id isEqualToString:@""] || !category_id) {
  6926. [cateWhere appendString:@"1 = 1"];
  6927. } else {
  6928. if ([category_id containsString:@","]) {
  6929. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6930. } else {
  6931. cate_id_array = [@[category_id] mutableCopy];
  6932. }
  6933. /*-----------*/
  6934. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6935. [cateWhere appendString:@"("];
  6936. for (int i = 0; i < cate_id_array.count; i++) {
  6937. for (NSString *key0 in cateDic.allKeys) {
  6938. if ([key0 containsString:@"category_"]) {
  6939. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6940. for (NSString *key1 in category0.allKeys) {
  6941. if ([key1 containsString:@"category_"]) {
  6942. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6943. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6944. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6945. cate_id_array[i] = [category1 objectForKey:@"id"];
  6946. if (i == 0) {
  6947. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6948. } else {
  6949. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6950. }
  6951. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6952. [category0 setValue:category1 forKey:key1];
  6953. [cateDic setValue:category0 forKey:key0];
  6954. }
  6955. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6956. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6957. cate_id_array[i] = [category0 objectForKey:@"id"];
  6958. if (i == 0) {
  6959. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6960. } else {
  6961. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6962. }
  6963. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6964. [cateDic setValue:category0 forKey:key0];
  6965. }
  6966. }
  6967. }
  6968. }
  6969. }
  6970. }
  6971. [cateWhere appendString:@")"];
  6972. [filter setValue:cateDic forKey:@"category"];
  6973. }
  6974. // where bestseller > 0 order by bestseller desc
  6975. // sql query: alert availability(int) best_seller(int) price qty
  6976. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color from product where %@ %@ %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  6977. // count
  6978. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6979. }
  6980. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6981. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6982. if (!params[@"user"]) {
  6983. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6984. }
  6985. [ret setValue:filter forKey:@"filter"];
  6986. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6987. sqlite3_stmt * statement;
  6988. [ret setValue:@"2" forKey:@"result"];
  6989. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6990. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6991. // int count=0;
  6992. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6993. {
  6994. int i=0;
  6995. while (sqlite3_step(statement) == SQLITE_ROW)
  6996. {
  6997. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6998. char *name = (char*)sqlite3_column_text(statement, 0);
  6999. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  7000. char *description = (char*)sqlite3_column_text(statement, 1);
  7001. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  7002. int product_id = sqlite3_column_int(statement, 2);
  7003. int wid = sqlite3_column_int(statement, 3);
  7004. int closeout = sqlite3_column_int(statement, 4);
  7005. int cid = sqlite3_column_int(statement, 5);
  7006. int wisdelete = sqlite3_column_int(statement, 6);
  7007. int more_color = sqlite3_column_int(statement, 7);
  7008. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  7009. if(wid !=0 && wisdelete != 1)
  7010. [item setValue:@"true" forKey:@"wish_exists"];
  7011. else
  7012. [item setValue:@"false" forKey:@"wish_exists"];
  7013. if(closeout==0)
  7014. [item setValue:@"false" forKey:@"is_closeout"];
  7015. else
  7016. [item setValue:@"true" forKey:@"is_closeout"];
  7017. if(cid==0)
  7018. [item setValue:@"false" forKey:@"cart_exists"];
  7019. else
  7020. [item setValue:@"true" forKey:@"cart_exists"];
  7021. if (more_color == 0) {
  7022. [item setObject:@(false) forKey:@"more_color"];
  7023. } else if (more_color == 1) {
  7024. [item setObject:@(true) forKey:@"more_color"];
  7025. }
  7026. [item addEntriesFromDictionary:imgjson];
  7027. // [item setValue:nsurl forKey:@"img"];
  7028. [item setValue:nsname forKey:@"fash_name"];
  7029. [item setValue:nsdescription forKey:@"description"];
  7030. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  7031. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  7032. i++;
  7033. }
  7034. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  7035. [ret setObject:items forKey:@"items"];
  7036. sqlite3_finalize(statement);
  7037. }
  7038. [iSalesDB close_db:db];
  7039. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  7040. return ret;
  7041. }
  7042. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  7043. {
  7044. return [self itemsearch:params limited:YES];
  7045. }
  7046. #pragma mark order detail
  7047. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  7048. if (str1.length == 0) {
  7049. str1 = @"&nbsp";
  7050. }
  7051. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  7052. return str;
  7053. }
  7054. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  7055. {
  7056. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  7057. [fromformatter setDateFormat:from];
  7058. NSDate *date = [fromformatter dateFromString:datetime];
  7059. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  7060. [toformatter setDateFormat:to];
  7061. NSString * ret = [toformatter stringFromDate:date];
  7062. return ret;
  7063. }
  7064. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  7065. // 把毫秒去掉
  7066. if ([dateTime containsString:@"."]) {
  7067. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  7068. }
  7069. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  7070. formatter.dateFormat = formate;
  7071. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  7072. NSDate *date = [formatter dateFromString:dateTime];
  7073. formatter.dateFormat = newFormate;
  7074. NSString *result = [formatter stringFromDate:date];
  7075. return result ? result : @"";
  7076. }
  7077. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  7078. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  7079. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7080. }
  7081. + (NSString *)changeDateFormate:(NSString *)dateTime {
  7082. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  7083. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7084. }
  7085. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  7086. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  7087. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  7088. }
  7089. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  7090. {
  7091. assert(params[@"mode"]!=nil);
  7092. assert(params[@"user"]!=nil);
  7093. DebugLog(@"offline oderdetail params: %@",params);
  7094. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7095. int orderId = [params[@"orderId"] intValue];
  7096. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  7097. // decrypt card number and card security code
  7098. // 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 ];
  7099. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country,o.must_call from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  7100. sqlite3 *db = [iSalesDB get_db];
  7101. sqlite3_stmt * statement;
  7102. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  7103. NSString *nssoid = @"";
  7104. NSString* orderinfo = @"";
  7105. NSString *moreInfo = @"";
  7106. double handlingFee = 0;
  7107. double payments_and_credist = 0;
  7108. double totalPrice = 0;
  7109. double shippingFee = 0;
  7110. double lift_gate = 0;
  7111. NSString *customer_contact = @"";
  7112. NSString *customer_email = @"";
  7113. NSString *customer_fax = @"";
  7114. NSString *customer_phone = @"";
  7115. NSString *customer_city = @"";
  7116. NSString *customer_state = @"";
  7117. NSString *customer_zipcode = @"";
  7118. NSString *customer_country = @"";
  7119. BOOL must_call = NO;
  7120. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  7121. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  7122. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  7123. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  7124. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  7125. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  7126. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  7127. {
  7128. if (sqlite3_step(statement) == SQLITE_ROW)
  7129. {
  7130. // int order_id = sqlite3_column_int(statement, 0);
  7131. NSString *sign_url = [self textAtColumn:51 statement:statement];
  7132. // ret[@"sign_url"] = sign_url;
  7133. section_1 = @{
  7134. @"data":sign_url,
  7135. @"title":@"Signature",
  7136. @"type":@"sign_url"
  7137. }.mutableCopy;
  7138. [ret setObject:section_1 forKey:@"section_1"];
  7139. customer_contact = [self textAtColumn:52 statement:statement];
  7140. customer_email = [self textAtColumn:53 statement:statement];
  7141. customer_phone = [self textAtColumn:54 statement:statement];
  7142. customer_fax = [self textAtColumn:55 statement:statement];
  7143. customer_city = [self textAtColumn:60 statement:statement];
  7144. customer_state = [self textAtColumn:61 statement:statement];
  7145. customer_zipcode = [self textAtColumn:62 statement:statement];
  7146. customer_country = [self textAtColumn:63 statement:statement];
  7147. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  7148. int offline_edit=sqlite3_column_int(statement, 56);
  7149. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  7150. char *soid = (char*)sqlite3_column_text(statement, 1);
  7151. if(soid==nil)
  7152. soid= "";
  7153. nssoid= [[NSString alloc]initWithUTF8String:soid];
  7154. // so#
  7155. ret[@"so#"] = nssoid;
  7156. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  7157. if(poNumber==nil)
  7158. poNumber= "";
  7159. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  7160. char *create_time = (char*)sqlite3_column_text(statement, 3);
  7161. if(create_time==nil)
  7162. create_time= "";
  7163. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7164. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  7165. int status = sqlite3_column_int(statement, 4);
  7166. int erpStatus = sqlite3_column_int(statement, 49);
  7167. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7168. // status
  7169. if (status > 1 && status != 3) {
  7170. status = erpStatus;
  7171. } else if (status == 3) {
  7172. status = 15;
  7173. }
  7174. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  7175. ret[@"order_status"] = nsstatus;
  7176. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7177. if(company_name==nil)
  7178. company_name= "";
  7179. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7180. // company name
  7181. ret[@"company_name"] = nscompany_name;
  7182. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7183. if(customer_contact==nil)
  7184. customer_contact= "";
  7185. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7186. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7187. if(addr_1==nil)
  7188. addr_1="";
  7189. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7190. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7191. if(addr_2==nil)
  7192. addr_2="";
  7193. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7194. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7195. if(addr_3==nil)
  7196. addr_3="";
  7197. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7198. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7199. if(addr_4==nil)
  7200. addr_4="";
  7201. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7202. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7203. [arr_addr addObject:nsaddr_1];
  7204. [arr_addr addObject:nsaddr_2];
  7205. [arr_addr addObject:nsaddr_3];
  7206. [arr_addr addObject:nsaddr_4];
  7207. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7208. [arr_addr addObject:customer_state];
  7209. [arr_addr addObject:customer_zipcode];
  7210. [arr_addr addObject:customer_country];
  7211. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7212. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7213. char *logist = (char*)sqlite3_column_text(statement, 11);
  7214. if(logist==nil)
  7215. logist= "";
  7216. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7217. if (/*status == -11 || */status == 10 || status == 11) {
  7218. nslogist = [self textAtColumn:59 statement:statement];
  7219. };
  7220. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7221. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7222. shipping = @"Shipping To Be Quoted";
  7223. } else {
  7224. shippingFee = sqlite3_column_double(statement, 12);
  7225. }
  7226. // Shipping
  7227. // ret[@"Shipping"] = shipping;
  7228. NSDictionary *shipping_item = @{
  7229. @"title":@"Shipping",
  7230. @"value":shipping
  7231. };
  7232. [price_data setObject:shipping_item forKey:@"item_1"];
  7233. int have_lift_gate = sqlite3_column_int(statement, 17);
  7234. lift_gate = sqlite3_column_double(statement, 13);
  7235. // Liftgate Fee(No loading dock)
  7236. if (!have_lift_gate) {
  7237. lift_gate = 0;
  7238. }
  7239. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7240. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7241. if (sqlite3_column_int(statement, 57)) {
  7242. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7243. liftgate_value = @"Shipping To Be Quoted";
  7244. }
  7245. NSDictionary *liftgate_item = @{
  7246. @"title":@"Liftgate Fee(No loading dock)",
  7247. @"value":liftgate_value
  7248. };
  7249. [price_data setObject:liftgate_item forKey:@"item_2"];
  7250. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7251. // [ret removeObjectForKey:@"Shipping"];
  7252. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7253. // }
  7254. //
  7255. // if (have_lift_gate) {
  7256. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7257. // }
  7258. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7259. if(general_notes==nil)
  7260. general_notes= "";
  7261. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7262. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7263. if(internal_notes==nil)
  7264. internal_notes= "";
  7265. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7266. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7267. if(payment_type==nil)
  7268. payment_type= "";
  7269. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7270. // order info
  7271. orderinfo = [self textFileName:@"order_info.html"];
  7272. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7273. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7274. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7275. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7276. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7277. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7278. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7279. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7280. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7281. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7282. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7283. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7284. NSString *payment = nil;
  7285. // id -> show
  7286. __block NSString *show_pay_type = nspayment_type;
  7287. [iSalesDB jk_query:[NSString stringWithFormat:@"select pay_type from payment_type where type_id = '%@';",nspayment_type] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7288. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7289. if (show_typ_ch != NULL) {
  7290. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7291. }
  7292. }];
  7293. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7294. {
  7295. payment = [self textFileName:@"creditcardpayment.html"];
  7296. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7297. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7298. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7299. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7300. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7301. NSString *card_type = [self textAtColumn:42 statement:statement];
  7302. if (card_type.length > 0) { // 显示星号
  7303. card_type = @"****";
  7304. }
  7305. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7306. if (card_number.length > 0 && card_number.length > 4) {
  7307. for (int i = 0; i < card_number.length - 4; i++) {
  7308. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7309. }
  7310. } else {
  7311. card_number = @"";
  7312. }
  7313. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7314. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7315. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7316. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7317. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7318. card_expiration = @"****";
  7319. }
  7320. NSString *card_city = [self textAtColumn:46 statement:statement];
  7321. NSString *card_state = [self textAtColumn:47 statement:statement];
  7322. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7323. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7324. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7325. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7326. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7327. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7328. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7329. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7330. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7331. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7332. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7333. }
  7334. else
  7335. {
  7336. payment=[self textFileName:@"normalpayment.html"];
  7337. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7338. }
  7339. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7340. ret[@"result"]= [NSNumber numberWithInt:2];
  7341. // more info
  7342. moreInfo = [self textFileName:@"more_info.html"];
  7343. /*****ship to******/
  7344. // ShipToCompany_or_&nbsp
  7345. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7346. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7347. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7348. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7349. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7350. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7351. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7352. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7353. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7354. /*****ship from******/
  7355. // ShipFromCompany_or_&nbsp
  7356. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7357. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7358. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7359. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7360. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7361. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7362. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7363. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7364. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7365. /*****freight to******/
  7366. // FreightBillToCompany_or_&nbsp
  7367. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7368. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7369. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7370. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7371. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7372. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7373. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7374. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7375. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7376. /*****merchandise to******/
  7377. // MerchandiseBillToCompany_or_&nbsp
  7378. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7379. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7380. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7381. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7382. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7383. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7384. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7385. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7386. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7387. /*****return to******/
  7388. // ReturnToCompany_or_&nbsp
  7389. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7390. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7391. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7392. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7393. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7394. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7395. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7396. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7397. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7398. //
  7399. // DebugLog(@"more info : %@",moreInfo);
  7400. // handling fee
  7401. handlingFee = sqlite3_column_double(statement, 33);
  7402. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7403. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7404. if (sqlite3_column_int(statement, 58)) {
  7405. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7406. handling_fee_value = @"Shipping To Be Quoted";
  7407. }
  7408. NSDictionary *handling_fee_item = @{
  7409. @"title":@"Handling Fee",
  7410. @"value":handling_fee_value
  7411. };
  7412. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7413. //
  7414. // customer info
  7415. customerID = [self textAtColumn:36 statement:statement];
  7416. // mode
  7417. ret[@"mode"] = params[@"mode"];
  7418. // model_count
  7419. ret[@"model_count"] = @(0);
  7420. }
  7421. sqlite3_finalize(statement);
  7422. }
  7423. [iSalesDB close_db:db];
  7424. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7425. // "customer_email" = "Shui Hu";
  7426. // "customer_fax" = "";
  7427. // "customer_first_name" = F;
  7428. // "customer_last_name" = L;
  7429. // "customer_name" = ",da He Xiang Dong Liu A";
  7430. // "customer_phone" = "Hey Xuan Feng";
  7431. contactInfo[@"customer_phone"] = customer_phone;
  7432. contactInfo[@"customer_fax"] = customer_fax;
  7433. contactInfo[@"customer_email"] = customer_email;
  7434. NSString *first_name = @"";
  7435. NSString *last_name = @"";
  7436. if ([customer_contact isEqualToString:@""]) {
  7437. } else if ([customer_contact containsString:@" "]) {
  7438. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7439. first_name = [customer_contact substringToIndex:first_space_index];
  7440. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7441. }
  7442. contactInfo[@"customer_first_name"] = first_name;
  7443. contactInfo[@"customer_last_name"] = last_name;
  7444. ret[@"customerInfo"] = contactInfo;
  7445. // models
  7446. if (nssoid) {
  7447. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7448. __block double TotalCuft = 0;
  7449. __block double TotalWeight = 0;
  7450. __block int TotalCarton = 0;
  7451. __block double allItemPrice = 0;
  7452. 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];
  7453. sqlite3 *db1 = [iSalesDB get_db];
  7454. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7455. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7456. int product_id = sqlite3_column_int(stmt, 0);
  7457. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7458. int item_id = sqlite3_column_int(stmt, 7);
  7459. NSString* Price=nil;
  7460. if(str_price==nil)
  7461. {
  7462. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7463. if(price==nil)
  7464. Price=@"No Price.";
  7465. else
  7466. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7467. }
  7468. else
  7469. {
  7470. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7471. }
  7472. double discount = sqlite3_column_double(stmt, 2);
  7473. int item_count = sqlite3_column_int(stmt, 3);
  7474. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7475. NSString *nsline_note=nil;
  7476. if(line_note!=nil)
  7477. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7478. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7479. // NSString *nsname = nil;
  7480. // if(name!=nil)
  7481. // nsname= [[NSString alloc]initWithUTF8String:name];
  7482. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7483. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7484. // NSString *nsdescription=nil;
  7485. // if(description!=nil)
  7486. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7487. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7488. // int stockUom = sqlite3_column_int(stmt, 8);
  7489. // int _id = sqlite3_column_int(stmt, 9);
  7490. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7491. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7492. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7493. int carton=[bsubtotaljson[@"carton"] intValue];
  7494. TotalCuft += cuft;
  7495. TotalWeight += weight;
  7496. TotalCarton += carton;
  7497. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7498. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7499. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7500. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7501. itemjson[@"note"]=nsline_note;
  7502. itemjson[@"origin_price"] = Price;
  7503. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7504. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7505. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7506. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7507. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7508. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7509. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7510. if(itemjson[@"combine"] != nil)
  7511. {
  7512. // int citem=0;
  7513. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7514. for(int bc=0;bc<bcount;bc++)
  7515. {
  7516. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7517. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7518. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7519. subTotal += uprice * modulus * item_count;
  7520. }
  7521. }
  7522. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7523. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7524. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7525. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7526. itemjson[@"type"] = @"order_item";
  7527. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7528. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7529. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7530. allItemPrice += subTotal;
  7531. }];
  7532. section_3[@"data"] = model_data;
  7533. section_3[@"type"] = @"sub_order";
  7534. section_3[@"title"] = @"Models";
  7535. section_3[@"switch"] = @(false);
  7536. ret[@"section_3"] = section_3;
  7537. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7538. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7539. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7540. // payments/Credits
  7541. // payments_and_credist = sqlite3_column_double(statement, 34);
  7542. payments_and_credist = allItemPrice;
  7543. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7544. NSDictionary *sub_total_item = @{
  7545. @"title":@"Sub-Total",
  7546. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7547. };
  7548. [price_data setObject:sub_total_item forKey:@"item_0"];
  7549. // // total
  7550. // totalPrice = sqlite3_column_double(statement, 35);
  7551. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7552. } else {
  7553. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7554. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7555. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7556. // payments/Credits
  7557. payments_and_credist = 0;
  7558. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7559. NSDictionary *sub_total_item = @{
  7560. @"title":@"Sub-Total",
  7561. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7562. };
  7563. [price_data setObject:sub_total_item forKey:@"item_0"];
  7564. }
  7565. // total
  7566. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7567. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7568. NSDictionary *total_item = @{
  7569. @"title":@"Total",
  7570. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7571. };
  7572. [price_data setObject:total_item forKey:@"item_4"];
  7573. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7574. section_4 = @{
  7575. @"data":price_data,
  7576. @"title":@"Price Info",
  7577. @"type":@"price_info"
  7578. }.mutableCopy;
  7579. ret[@"section_4"] = section_4;
  7580. // ret[@"order_info"]= orderinfo;
  7581. section_0 = @{
  7582. @"data":orderinfo,
  7583. @"title":@"Order Info",
  7584. @"type":@"order_info"
  7585. }.mutableCopy;
  7586. ret[@"section_0"] = section_0;
  7587. // ret[@"more_order_info"] = moreInfo;
  7588. section_2 = @{
  7589. @"data":moreInfo,
  7590. @"title":@"More Info",
  7591. @"type":@"more_order_info"
  7592. }.mutableCopy;
  7593. ret[@"section_2"] = section_2;
  7594. ret[@"count"] = @(5);
  7595. return [RAConvertor dict2data:ret];
  7596. }
  7597. #pragma mark order list
  7598. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7599. NSString *nsstatus = @"";
  7600. switch (status) {
  7601. case 0:
  7602. {
  7603. nsstatus=@"Temp Order";
  7604. break;
  7605. }
  7606. case 1:
  7607. {
  7608. nsstatus=@"Saved Order";
  7609. break;
  7610. }
  7611. case 2: {
  7612. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7613. break;
  7614. }
  7615. case 3:
  7616. case 15:
  7617. {
  7618. nsstatus=@"Cancelled";
  7619. break;
  7620. }
  7621. case 10:
  7622. {
  7623. nsstatus=@"Quote Submitted";
  7624. break;
  7625. }
  7626. case 11:
  7627. {
  7628. nsstatus=@"Sales Order Submitted";
  7629. break;
  7630. }
  7631. case 12:
  7632. {
  7633. nsstatus=@"Processing";
  7634. break;
  7635. }
  7636. case 13:
  7637. {
  7638. nsstatus=@"Shipped";
  7639. break;
  7640. }
  7641. case 14:
  7642. {
  7643. nsstatus=@"Closed";
  7644. break;
  7645. }
  7646. case -11:
  7647. {
  7648. nsstatus = @"Ready For Submit";
  7649. break;
  7650. }
  7651. default:
  7652. break;
  7653. }
  7654. return nsstatus;
  7655. }
  7656. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7657. {
  7658. double total = 0;
  7659. __block double payments_and_credist = 0;
  7660. __block double allItemPrice = 0;
  7661. // sqlite3 *db1 = [iSalesDB get_db];
  7662. if (so_id) {
  7663. // 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];
  7664. 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];
  7665. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7666. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7667. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7668. int product_id = sqlite3_column_int(stmt, 0);
  7669. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7670. int discount = sqlite3_column_int(stmt, 2);
  7671. int item_count = sqlite3_column_int(stmt, 3);
  7672. // int item_id = sqlite3_column_int(stmt, 7);
  7673. int item_id = sqlite3_column_int(stmt, 4);
  7674. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7675. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7676. NSString* Price=nil;
  7677. if(str_price==nil)
  7678. {
  7679. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7680. if(price==nil)
  7681. Price=@"No Price.";
  7682. else
  7683. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7684. }
  7685. else
  7686. {
  7687. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7688. }
  7689. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7690. itemjson[@"The unit price"]=Price;
  7691. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7692. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7693. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7694. if(itemjson[@"combine"] != nil)
  7695. {
  7696. // int citem=0;
  7697. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7698. for(int bc=0;bc<bcount;bc++)
  7699. {
  7700. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7701. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7702. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7703. subTotal += uprice * modulus * item_count;
  7704. }
  7705. }
  7706. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7707. allItemPrice += subTotal;
  7708. }];
  7709. payments_and_credist = allItemPrice;
  7710. } else {
  7711. // payments/Credits
  7712. payments_and_credist = 0;
  7713. }
  7714. // lift_gate handlingFee shippingFee
  7715. __block double lift_gate = 0;
  7716. __block double handlingFee = 0;
  7717. __block double shippingFee = 0;
  7718. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7719. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7720. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7721. if (have_lift_gate) {
  7722. lift_gate = sqlite3_column_double(stmt, 1);
  7723. }
  7724. handlingFee = sqlite3_column_double(stmt, 2);
  7725. shippingFee = sqlite3_column_double(stmt, 3);
  7726. }];
  7727. // total
  7728. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7729. if (close) {
  7730. [iSalesDB close_db:db1];
  7731. }
  7732. return total;
  7733. }
  7734. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7735. {
  7736. assert(params[@"user"]!=nil);
  7737. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7738. int limit = [[params valueForKey:@"limit"] intValue];
  7739. int offset = [[params valueForKey:@"offset"] intValue];
  7740. NSString* keyword = [params valueForKey:@"keyWord"];
  7741. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7742. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7743. NSString* where=@"1 = 1";
  7744. if(keyword.length>0)
  7745. 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]];
  7746. if (orderStatus.length > 0) {
  7747. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7748. if (order_status_array.count == 1) {
  7749. int status_value = [[order_status_array firstObject] intValue];
  7750. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7751. if (status_value == 15 || status_value == 3) {
  7752. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7753. } else {
  7754. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7755. }
  7756. } else {
  7757. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7758. }
  7759. } else if (order_status_array.count > 1) {
  7760. for (int i = 0; i < order_status_array.count;i++) {
  7761. NSString *status = order_status_array[i];
  7762. NSString *condition = @" or";
  7763. if (i == 0) {
  7764. condition = @" and (";
  7765. }
  7766. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7767. int status_value = [status intValue];
  7768. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7769. if (status_value == 15 || status_value == 3) {
  7770. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7771. } else {
  7772. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7773. }
  7774. } else {
  7775. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7776. }
  7777. }
  7778. where = [where stringByAppendingString:@" )"];
  7779. }
  7780. }
  7781. 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];
  7782. // DebugLog(@"order list sql: %@",sqlQuery);
  7783. sqlite3 *db = [iSalesDB get_db];
  7784. sqlite3_stmt * statement;
  7785. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7786. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7787. {
  7788. int count=0;
  7789. while (sqlite3_step(statement) == SQLITE_ROW)
  7790. {
  7791. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7792. int order_id = sqlite3_column_double(statement, 0);
  7793. char *soid = (char*)sqlite3_column_text(statement, 1);
  7794. if(soid==nil)
  7795. soid= "";
  7796. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7797. int status = sqlite3_column_double(statement, 2);
  7798. int erpStatus = sqlite3_column_double(statement, 9);
  7799. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7800. if(sales_rep==nil)
  7801. sales_rep= "";
  7802. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7803. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7804. if(create_by==nil)
  7805. create_by= "";
  7806. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7807. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7808. if(company_name==nil)
  7809. company_name= "";
  7810. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7811. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7812. if(create_time==nil)
  7813. create_time= "";
  7814. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7815. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7816. // double total_price = sqlite3_column_double(statement, 7);
  7817. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7818. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7819. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7820. int offline_edit = sqlite3_column_int(statement, 10);
  7821. // ": "JH",
  7822. // "": "$8307.00",
  7823. // "": "MOB1608050001",
  7824. // "": "ArpithaT",
  7825. // "": "1st Stage Property Transformations",
  7826. // "": "JANICE SUTTON",
  7827. // "": 2255,
  7828. // "": "08/02/2016 09:49:18",
  7829. // "": 1,
  7830. // "": "Saved Order"
  7831. // "": "1470384050483",
  7832. // "model_count": "6 / 28"
  7833. item[@"sales_rep"]= nssales_rep;
  7834. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7835. item[@"so#"]= nssoid;
  7836. item[@"create_by"]= nscreate_by;
  7837. item[@"customer_name"]= nscompany_name;
  7838. item[@"customer_contact"] = customer_contact;
  7839. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7840. item[@"purchase_time"]= nscreate_time;
  7841. int statusCode = status;
  7842. if (statusCode == 2) {
  7843. statusCode = erpStatus;
  7844. } else if (statusCode == 3) {
  7845. statusCode = 15;
  7846. }
  7847. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7848. item[@"order_status"]= nsstatus;
  7849. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7850. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7851. // item[@"model_count"]
  7852. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7853. count++;
  7854. }
  7855. ret[@"count"]= [NSNumber numberWithInt:count];
  7856. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7857. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7858. ret[@"result"]= [NSNumber numberWithInt:2];
  7859. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7860. ret[@"time_zone"] = @"PST";
  7861. sqlite3_finalize(statement);
  7862. }
  7863. 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];
  7864. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7865. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7866. [iSalesDB close_db:db];
  7867. return [RAConvertor dict2data:ret];
  7868. }
  7869. #pragma mark update gnotes
  7870. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7871. {
  7872. DebugLog(@"params: %@",params);
  7873. // comments = Meyoyoyoyoyoyoy;
  7874. // orderCode = MOB1608110001;
  7875. // password = 123456;
  7876. // user = EvanK;
  7877. 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]];
  7878. int ret = [iSalesDB execSql:sql];
  7879. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7880. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7881. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7882. // [dic setValue:@"160409" forKey:@"min_ver"];
  7883. return [RAConvertor dict2data:dic];
  7884. }
  7885. #pragma mark move to wishlist
  7886. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7887. {
  7888. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7889. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7890. _id = [NSString stringWithFormat:@"(%@)",_id];
  7891. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7892. sqlite3 *db = [iSalesDB get_db];
  7893. __block NSString *product_id = @"";
  7894. __block NSString *item_count_str = @"";
  7895. // __block NSString *item_id = nil;
  7896. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7897. // product_id = [self textAtColumn:0 statement:stmt];
  7898. int item_count = sqlite3_column_int(stmt, 1);
  7899. // item_id = [self textAtColumn:2 statement:stmt];
  7900. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7901. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7902. if (p_id.length) {
  7903. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7904. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7905. }
  7906. }];
  7907. [iSalesDB close_db:db];
  7908. // 去除第一个,
  7909. if (product_id.length > 1) {
  7910. product_id = [product_id substringFromIndex:1];
  7911. }
  7912. if (item_count_str.length > 1) {
  7913. item_count_str = [item_count_str substringFromIndex:1];
  7914. }
  7915. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7916. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7917. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7918. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7919. sqlite3 *db1 = [iSalesDB get_db];
  7920. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7921. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7922. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7923. // 删除
  7924. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7925. int ret = [iSalesDB execSql:deleteSql db:db1];
  7926. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7927. [iSalesDB close_db:db1];
  7928. return [RAConvertor dict2data:dic];
  7929. }
  7930. #pragma mark cart delete
  7931. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7932. {
  7933. // cartItemId = 548;
  7934. // orderCode = MOB1608110001;
  7935. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7936. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7937. _id = [NSString stringWithFormat:@"(%@)",_id];
  7938. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7939. int ret = [iSalesDB execSql:sql];
  7940. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7941. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7942. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7943. // [dic setValue:@"160409" forKey:@"min_ver"];
  7944. return [RAConvertor dict2data:dic];
  7945. }
  7946. #pragma mark set order discount
  7947. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7948. //{
  7949. // DebugLog(@"cart set price params: %@",params);
  7950. // // "cartitem_id" = 1;
  7951. // // discount = "0.000000";
  7952. // // "item_note" = "";
  7953. // // price = "269.000000";
  7954. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7955. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7956. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7957. //// NSString *price = [self valueInParams:params key:@"price"];
  7958. //
  7959. // // bool badd_price_changed=false;
  7960. // // sqlite3* db=[iSalesDB get_db];
  7961. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7962. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7963. // // NSRange range;
  7964. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7965. // //
  7966. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7967. // // badd_price_changed=true;
  7968. // // [iSalesDB close_db:db];
  7969. // //
  7970. // // if(badd_price_changed)
  7971. // // {
  7972. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7973. // // }
  7974. // //
  7975. //
  7976. //
  7977. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7978. //
  7979. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7980. //
  7981. // int ret = [iSalesDB execSql:sql];
  7982. //
  7983. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7984. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7985. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7986. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7987. //
  7988. //// return [RAConvertor dict2data:dic];
  7989. // return dic;
  7990. //
  7991. //}
  7992. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7993. {
  7994. DebugLog(@"order set discount params: %@",params);
  7995. // "cartitem_id" = 1;
  7996. // discount = "0.000000";
  7997. // "item_note" = "";
  7998. // price = "269.000000";
  7999. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  8000. //NSString *notes = [self valueInParams:params key:@"item_note"];
  8001. NSString *discount = [self valueInParams:params key:@"app_discount"];
  8002. // NSString *price = [self valueInParams:params key:@"price"];
  8003. // bool badd_price_changed=false;
  8004. // sqlite3* db=[iSalesDB get_db];
  8005. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8006. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  8007. // NSRange range;
  8008. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  8009. //
  8010. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  8011. // badd_price_changed=true;
  8012. // [iSalesDB close_db:db];
  8013. //
  8014. // if(badd_price_changed)
  8015. // {
  8016. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  8017. // }
  8018. //
  8019. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  8020. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  8021. int ret = [iSalesDB execSql:sql];
  8022. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8023. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8024. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8025. // [dic setValue:@"160409" forKey:@"min_ver"];
  8026. // return [RAConvertor dict2data:dic];
  8027. return dic;
  8028. }
  8029. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  8030. {
  8031. DebugLog(@"cart set price params: %@",params);
  8032. // "cartitem_id" = 1;
  8033. // discount = "0.000000";
  8034. // "item_note" = "";
  8035. // price = "269.000000";
  8036. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8037. NSString *notes = [self valueInParams:params key:@"item_note"];
  8038. NSString *discount = [self valueInParams:params key:@"discount"];
  8039. NSString *price = [self valueInParams:params key:@"price"];
  8040. // bool badd_price_changed=false;
  8041. // sqlite3* db=[iSalesDB get_db];
  8042. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8043. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  8044. // NSRange range;
  8045. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  8046. //
  8047. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  8048. // badd_price_changed=true;
  8049. // [iSalesDB close_db:db];
  8050. //
  8051. // if(badd_price_changed)
  8052. // {
  8053. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  8054. // }
  8055. //
  8056. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  8057. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  8058. int ret = [iSalesDB execSql:sql];
  8059. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8060. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8061. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8062. // [dic setValue:@"160409" forKey:@"min_ver"];
  8063. return [RAConvertor dict2data:dic];
  8064. }
  8065. #pragma mark set line notes
  8066. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  8067. {
  8068. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8069. NSString *notes = [self valueInParams:params key:@"notes"];
  8070. notes = [self translateSingleQuote:notes];
  8071. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  8072. int ret = [iSalesDB execSql:sql];
  8073. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8074. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8075. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8076. // [dic setValue:@"160409" forKey:@"min_ver"];
  8077. return [RAConvertor dict2data:dic];
  8078. }
  8079. #pragma mark set qty
  8080. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  8081. {
  8082. assert(params[@"can_create_backorder"]!=nil);
  8083. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8084. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  8085. int item_count = [params[@"inputInt"] intValue];
  8086. // 购买检查数量大于库存
  8087. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8088. if (!params[@"can_create_backorder"]) {
  8089. 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];
  8090. __block BOOL out_of_stock = NO;
  8091. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8092. int availability = sqlite3_column_int(stmt, 0);
  8093. if (availability < item_count) {
  8094. out_of_stock = YES;
  8095. }
  8096. }];
  8097. if (out_of_stock) { // 缺货
  8098. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  8099. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  8100. return [RAConvertor dict2data:dic];
  8101. }
  8102. }
  8103. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  8104. int ret = [iSalesDB execSql:sql];
  8105. __block int item_id = 0;
  8106. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8107. item_id = sqlite3_column_int(stmt, 0);
  8108. }];
  8109. sqlite3 *db = [iSalesDB get_db];
  8110. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8111. [iSalesDB close_db:db];
  8112. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8113. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8114. // [dic setValue:@"160409" forKey:@"min_ver"];
  8115. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  8116. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  8117. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  8118. return [RAConvertor dict2data:dic];
  8119. }
  8120. #pragma mark place order
  8121. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  8122. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8123. if (aname) {
  8124. [dic setValue:aname forKey:@"aname"];
  8125. }
  8126. if (control) {
  8127. [dic setValue:control forKey:@"control"];
  8128. }
  8129. if (keyboard) {
  8130. [dic setValue:keyboard forKey:@"keyboard"];
  8131. }
  8132. if (name) {
  8133. [dic setValue:name forKey:@"name"];
  8134. }
  8135. if (value) {
  8136. [dic setValue:value forKey:@"value"];
  8137. }
  8138. return dic;
  8139. }
  8140. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  8141. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8142. orderCode = [self translateSingleQuote:orderCode];
  8143. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  8144. 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];
  8145. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8146. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  8147. NSString *img0 = [self textAtColumn:1 statement:stmt];
  8148. NSString *img1 = [self textAtColumn:2 statement:stmt];
  8149. NSString *img2 = [self textAtColumn:3 statement:stmt];
  8150. NSString *company_name = [self textAtColumn:4 statement:stmt];
  8151. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  8152. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  8153. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  8154. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  8155. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  8156. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  8157. NSString *email = [self textAtColumn:11 statement:stmt];
  8158. NSString *phone = [self textAtColumn:12 statement:stmt];
  8159. NSString *fax = [self textAtColumn:13 statement:stmt];
  8160. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  8161. NSString *city = [self textAtColumn:15 statement:stmt];
  8162. NSString *state = [self textAtColumn:16 statement:stmt];
  8163. NSString *country = [self textAtColumn:17 statement:stmt];
  8164. NSString *name = [self textAtColumn:18 statement:stmt];
  8165. // contact id
  8166. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  8167. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  8168. [contact_id_dic setValue:@"text" forKey:@"control"];
  8169. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  8170. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  8171. [contact_id_dic setValue:@"true" forKey:@"required"];
  8172. [contact_id_dic setValue:contact_id forKey:@"value"];
  8173. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  8174. // business card
  8175. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  8176. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  8177. [business_card_dic setValue:@"img" forKey:@"control"];
  8178. [business_card_dic setValue:@"1" forKey:@"disable"];
  8179. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  8180. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  8181. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  8182. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  8183. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  8184. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  8185. [business_card_dic setValue:@"business_card" forKey:@"name"];
  8186. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  8187. // fax
  8188. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8189. [customer_dic setValue:fax_dic forKey:@"item_10"];
  8190. // zipcode
  8191. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  8192. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  8193. // city
  8194. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  8195. [customer_dic setValue:city_dic forKey:@"item_12"];
  8196. // state
  8197. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  8198. [customer_dic setValue:state_dic forKey:@"item_13"];
  8199. // country
  8200. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  8201. [customer_dic setValue:country_dic forKey:@"item_14"];
  8202. // company name
  8203. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  8204. [customer_dic setValue:company_dic forKey:@"item_2"];
  8205. // addr_1
  8206. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  8207. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  8208. // addr_2
  8209. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  8210. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  8211. // addr_3
  8212. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  8213. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  8214. // addr_4
  8215. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  8216. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  8217. // Contact
  8218. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  8219. [customer_dic setValue:contact_dic forKey:@"item_7"];
  8220. // email
  8221. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8222. [customer_dic setValue:email_dic forKey:@"item_8"];
  8223. // phone
  8224. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8225. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8226. // title
  8227. [customer_dic setValue:@"Customer" forKey:@"title"];
  8228. // count
  8229. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8230. }];
  8231. // setting
  8232. NSDictionary *setting = params[@"setting"];
  8233. NSNumber *hide = setting[@"CustomerHide"];
  8234. [customer_dic setValue:hide forKey:@"hide"];
  8235. return customer_dic;
  8236. }
  8237. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8238. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8239. // setting
  8240. NSDictionary *setting = params[@"setting"];
  8241. NSNumber *hide = setting[@"ShipToHide"];
  8242. [dic setValue:hide forKey:@"hide"];
  8243. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8244. orderCode = [self translateSingleQuote:orderCode];
  8245. 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];
  8246. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8247. NSString *cid = [self textAtColumn:0 statement:stmt];
  8248. NSString *name = [self textAtColumn:1 statement:stmt];
  8249. NSString *ext = [self textAtColumn:2 statement:stmt];
  8250. NSString *contact = [self textAtColumn:3 statement:stmt];
  8251. NSString *email = [self textAtColumn:4 statement:stmt];
  8252. NSString *fax = [self textAtColumn:5 statement:stmt];
  8253. NSString *phone = [self textAtColumn:6 statement:stmt];
  8254. // count
  8255. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8256. // title
  8257. [dic setValue:@"Ship To" forKey:@"title"];
  8258. // choose
  8259. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8260. [choose_dic setValue:@"choose" forKey:@"aname"];
  8261. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8262. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8263. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8264. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8265. @"receive_contact" : @"customer_contact",
  8266. @"receive_email" : @"customer_email",
  8267. @"receive_ext" : @"customer_contact_ext",
  8268. @"receive_fax" : @"customer_fax",
  8269. @"receive_name" : @"customer_name",
  8270. @"receive_phone" : @"customer_phone"},
  8271. @"refresh" : [NSNumber numberWithInteger:1],
  8272. @"type" : @"pull"};
  8273. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8274. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8275. @"name" : @"Add new address",
  8276. @"refresh" : [NSNumber numberWithInteger:1],
  8277. @"value" : @"new_addr"
  8278. };
  8279. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8280. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8281. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8282. @"receive_contact" : @"customer_contact",
  8283. @"receive_email" : @"customer_email",
  8284. @"receive_ext" : @"customer_contact_ext",
  8285. @"receive_fax" : @"customer_fax",
  8286. @"receive_name" : @"customer_name",
  8287. @"receive_phone" : @"customer_phone"},
  8288. @"name" : @"select_ship_to",
  8289. @"refresh" : [NSNumber numberWithInteger:1],
  8290. @"value" : @"Sales_Order_Ship_To"};
  8291. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8292. [dic setValue:choose_dic forKey:@"item_0"];
  8293. // contact id
  8294. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8295. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8296. [contact_id_dic setValue:@"true" forKey:@"required"];
  8297. [dic setValue:contact_id_dic forKey:@"item_1"];
  8298. // company name
  8299. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8300. [dic setValue:company_name_dic forKey:@"item_2"];
  8301. // address
  8302. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8303. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8304. if ([required integerValue]) {
  8305. [ext_dic setValue:@"true" forKey:@"required"];
  8306. }
  8307. [dic setValue:ext_dic forKey:@"item_3"];
  8308. // contact
  8309. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8310. [dic setValue:contact_dic forKey:@"item_4"];
  8311. // phone
  8312. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8313. [dic setValue:phone_dic forKey:@"item_5"];
  8314. // fax
  8315. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8316. [dic setValue:fax_dic forKey:@"item_6"];
  8317. // email
  8318. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8319. [dic setValue:email_dic forKey:@"item_7"];
  8320. }];
  8321. return dic;
  8322. }
  8323. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8324. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8325. // setting
  8326. NSDictionary *setting = params[@"setting"];
  8327. NSNumber *hide = setting[@"ShipFromHide"];
  8328. [dic setValue:hide forKey:@"hide"];
  8329. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8330. orderCode = [self translateSingleQuote:orderCode];
  8331. 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];
  8332. 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';";
  8333. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8334. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8335. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8336. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8337. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8338. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8339. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8340. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8341. if (!cid.length) {
  8342. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8343. cid = [self textAtColumn:0 statement:statment];
  8344. name = [self textAtColumn:1 statement:statment];
  8345. ext = [self textAtColumn:2 statement:statment];
  8346. contact = [self textAtColumn:3 statement:statment];
  8347. email = [self textAtColumn:4 statement:statment];
  8348. fax = [self textAtColumn:5 statement:statment];
  8349. phone = [self textAtColumn:6 statement:statment];
  8350. }];
  8351. }
  8352. // count
  8353. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8354. // title
  8355. [dic setValue:@"Ship From" forKey:@"title"];
  8356. // hide
  8357. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8358. // choose
  8359. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8360. [choose_dic setValue:@"choose" forKey:@"aname"];
  8361. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8362. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8363. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8364. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8365. @"sender_contact" : @"customer_contact",
  8366. @"sender_email" : @"customer_email",
  8367. @"sender_ext" : @"customer_contact_ext",
  8368. @"sender_fax" : @"customer_fax",
  8369. @"sender_name" : @"customer_name",
  8370. @"sender_phone" : @"customer_phone"},
  8371. @"name" : @"select_cid",
  8372. @"refresh" : [NSNumber numberWithInteger:0],
  8373. @"value" : @"Sales_Order_Ship_From"};
  8374. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8375. [dic setValue:choose_dic forKey:@"item_0"];
  8376. // contact id
  8377. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8378. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8379. [contact_id_dic setValue:@"true" forKey:@"required"];
  8380. [dic setValue:contact_id_dic forKey:@"item_1"];
  8381. // company name
  8382. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8383. [dic setValue:company_name_dic forKey:@"item_2"];
  8384. // address
  8385. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8386. [dic setValue:ext_dic forKey:@"item_3"];
  8387. // contact
  8388. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8389. [dic setValue:contact_dic forKey:@"item_4"];
  8390. // phone
  8391. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8392. [dic setValue:phone_dic forKey:@"item_5"];
  8393. // fax
  8394. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8395. [dic setValue:fax_dic forKey:@"item_6"];
  8396. // email
  8397. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8398. [dic setValue:email_dic forKey:@"item_7"];
  8399. }];
  8400. return dic;
  8401. }
  8402. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8403. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8404. // setting
  8405. NSDictionary *setting = params[@"setting"];
  8406. NSNumber *hide = setting[@"FreightBillToHide"];
  8407. [dic setValue:hide forKey:@"hide"];
  8408. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8409. orderCode = [self translateSingleQuote:orderCode];
  8410. 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];
  8411. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8412. NSString *cid = [self textAtColumn:0 statement:stmt];
  8413. NSString *name = [self textAtColumn:1 statement:stmt];
  8414. NSString *ext = [self textAtColumn:2 statement:stmt];
  8415. NSString *contact = [self textAtColumn:3 statement:stmt];
  8416. NSString *email = [self textAtColumn:4 statement:stmt];
  8417. NSString *fax = [self textAtColumn:5 statement:stmt];
  8418. NSString *phone = [self textAtColumn:6 statement:stmt];
  8419. // count
  8420. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8421. // title
  8422. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8423. // hide
  8424. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8425. // choose
  8426. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8427. [choose_dic setValue:@"choose" forKey:@"aname"];
  8428. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8429. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8430. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8431. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8432. @"shipping_billto_contact" : @"receive_contact",
  8433. @"shipping_billto_email" : @"receive_email",
  8434. @"shipping_billto_ext" : @"receive_ext",
  8435. @"shipping_billto_fax" : @"receive_fax",
  8436. @"shipping_billto_name" : @"receive_name",
  8437. @"shipping_billto_phone" : @"receive_phone"},
  8438. @"type" : @"pull"};
  8439. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8440. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8441. @"type" : @"pull",
  8442. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8443. @"shipping_billto_contact" : @"customer_contact",
  8444. @"shipping_billto_email" : @"customer_email",
  8445. @"shipping_billto_ext" : @"customer_contact_ext",
  8446. @"shipping_billto_fax" : @"customer_fax",
  8447. @"shipping_billto_name" : @"customer_name",
  8448. @"shipping_billto_phone" : @"customer_phone"}
  8449. };
  8450. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8451. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8452. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8453. @"shipping_billto_contact" : @"sender_contact",
  8454. @"shipping_billto_email" : @"sender_email",
  8455. @"shipping_billto_ext" : @"sender_ext",
  8456. @"shipping_billto_fax" : @"sender_fax",
  8457. @"shipping_billto_name" : @"sender_name",
  8458. @"shipping_billto_phone" : @"sender_phone"},
  8459. @"type" : @"pull"
  8460. };
  8461. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8462. NSDictionary *select_freight_bill_to_dic = @{
  8463. @"aname" : @"Select freight bill to",
  8464. @"name" : @"select_cid",
  8465. @"refresh" : [NSNumber numberWithInteger:0],
  8466. @"value" : @"Sales_Order_Freight_Bill_To",
  8467. @"key_map" : @{
  8468. @"shipping_billto_cid" : @"customer_cid",
  8469. @"shipping_billto_contact" : @"customer_contact",
  8470. @"shipping_billto_email" : @"customer_email",
  8471. @"shipping_billto_ext" : @"customer_contact_ext",
  8472. @"shipping_billto_fax" : @"customer_fax",
  8473. @"shipping_billto_name" : @"customer_name",
  8474. @"shipping_billto_phone" : @"customer_phone"
  8475. }
  8476. };
  8477. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8478. [dic setValue:choose_dic forKey:@"item_0"];
  8479. // contact id
  8480. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8481. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8482. [contact_id_dic setValue:@"true" forKey:@"required"];
  8483. [dic setValue:contact_id_dic forKey:@"item_1"];
  8484. // company name
  8485. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8486. [dic setValue:company_name_dic forKey:@"item_2"];
  8487. // address
  8488. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8489. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8490. if ([ext_required integerValue]) {
  8491. [ext_dic setValue:@"true" forKey:@"required"];
  8492. }
  8493. [dic setValue:ext_dic forKey:@"item_3"];
  8494. // contact
  8495. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8496. [dic setValue:contact_dic forKey:@"item_4"];
  8497. // phone
  8498. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8499. [dic setValue:phone_dic forKey:@"item_5"];
  8500. // fax
  8501. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8502. [dic setValue:fax_dic forKey:@"item_6"];
  8503. // email
  8504. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8505. [dic setValue:email_dic forKey:@"item_7"];
  8506. }];
  8507. return dic;
  8508. }
  8509. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8510. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8511. // setting
  8512. NSDictionary *setting = params[@"setting"];
  8513. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8514. [dic setValue:hide forKey:@"hide"];
  8515. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8516. orderCode = [self translateSingleQuote:orderCode];
  8517. 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];
  8518. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8519. NSString *cid = [self textAtColumn:0 statement:stmt];
  8520. NSString *name = [self textAtColumn:1 statement:stmt];
  8521. NSString *ext = [self textAtColumn:2 statement:stmt];
  8522. NSString *contact = [self textAtColumn:3 statement:stmt];
  8523. NSString *email = [self textAtColumn:4 statement:stmt];
  8524. NSString *fax = [self textAtColumn:5 statement:stmt];
  8525. NSString *phone = [self textAtColumn:6 statement:stmt];
  8526. // count
  8527. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8528. // title
  8529. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8530. // hide
  8531. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8532. // choose
  8533. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8534. [choose_dic setValue:@"choose" forKey:@"aname"];
  8535. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8536. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8537. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8538. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8539. @"billing_contact" : @"receive_contact",
  8540. @"billing_email" : @"receive_email",
  8541. @"billing_ext" : @"receive_ext",
  8542. @"billing_fax" : @"receive_fax",
  8543. @"billing_name" : @"receive_name",
  8544. @"billing_phone" : @"receive_phone"},
  8545. @"type" : @"pull"};
  8546. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8547. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8548. @"type" : @"pull",
  8549. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8550. @"billing_contact" : @"customer_contact",
  8551. @"billing_email" : @"customer_email",
  8552. @"billing_ext" : @"customer_contact_ext",
  8553. @"billing_fax" : @"customer_fax",
  8554. @"billing_name" : @"customer_name",
  8555. @"billing_phone" : @"customer_phone"}
  8556. };
  8557. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8558. NSDictionary *select_bill_to_dic = @{
  8559. @"aname" : @"Select bill to",
  8560. @"name" : @"select_cid",
  8561. @"refresh" : [NSNumber numberWithInteger:0],
  8562. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8563. @"key_map" : @{
  8564. @"billing_cid" : @"customer_cid",
  8565. @"billing_contact" : @"customer_contact",
  8566. @"billing_email" : @"customer_email",
  8567. @"billing_ext" : @"customer_contact_ext",
  8568. @"billing_fax" : @"customer_fax",
  8569. @"billing_name" : @"customer_name",
  8570. @"billing_phone" : @"customer_phone"
  8571. }
  8572. };
  8573. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8574. [dic setValue:choose_dic forKey:@"item_0"];
  8575. // contact id
  8576. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8577. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8578. [contact_id_dic setValue:@"true" forKey:@"required"];
  8579. [dic setValue:contact_id_dic forKey:@"item_1"];
  8580. // company name
  8581. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8582. [dic setValue:company_name_dic forKey:@"item_2"];
  8583. // address
  8584. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8585. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8586. if ([ext_required integerValue]) {
  8587. [ext_dic setValue:@"true" forKey:@"required"];
  8588. }
  8589. [dic setValue:ext_dic forKey:@"item_3"];
  8590. // contact
  8591. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8592. [dic setValue:contact_dic forKey:@"item_4"];
  8593. // phone
  8594. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8595. [dic setValue:phone_dic forKey:@"item_5"];
  8596. // fax
  8597. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8598. [dic setValue:fax_dic forKey:@"item_6"];
  8599. // email
  8600. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8601. [dic setValue:email_dic forKey:@"item_7"];
  8602. }];
  8603. return dic;
  8604. }
  8605. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8606. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8607. // setting
  8608. NSDictionary *setting = params[@"setting"];
  8609. NSNumber *hide = setting[@"ReturnToHide"];
  8610. [dic setValue:hide forKey:@"hide"];
  8611. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8612. orderCode = [self translateSingleQuote:orderCode];
  8613. 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];
  8614. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8615. NSString *cid = [self textAtColumn:0 statement:stmt];
  8616. NSString *name = [self textAtColumn:1 statement:stmt];
  8617. NSString *ext = [self textAtColumn:2 statement:stmt];
  8618. NSString *contact = [self textAtColumn:3 statement:stmt];
  8619. NSString *email = [self textAtColumn:4 statement:stmt];
  8620. NSString *fax = [self textAtColumn:5 statement:stmt];
  8621. NSString *phone = [self textAtColumn:6 statement:stmt];
  8622. // count
  8623. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8624. // title
  8625. [dic setValue:@"Return To" forKey:@"title"];
  8626. // hide
  8627. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8628. // choose
  8629. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8630. [choose_dic setValue:@"choose" forKey:@"aname"];
  8631. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8632. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8633. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8634. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8635. @"returnto_contact" : @"sender_contact",
  8636. @"returnto_email" : @"sender_email",
  8637. @"returnto_ext" : @"sender_ext",
  8638. @"returnto_fax" : @"sender_fax",
  8639. @"returnto_name" : @"sender_name",
  8640. @"returnto_phone" : @"sender_phone"},
  8641. @"type" : @"pull"};
  8642. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8643. NSDictionary *select_return_to_dic = @{
  8644. @"aname" : @"Select return to",
  8645. @"name" : @"select_cid",
  8646. @"refresh" : [NSNumber numberWithInteger:0],
  8647. @"value" : @"Contact_Return_To",
  8648. @"key_map" : @{
  8649. @"returnto_cid" : @"customer_cid",
  8650. @"returnto_contact" : @"customer_contact",
  8651. @"returnto_email" : @"customer_email",
  8652. @"returnto_ext" : @"customer_contact_ext",
  8653. @"returnto_fax" : @"customer_fax",
  8654. @"returnto_name" : @"customer_name",
  8655. @"returnto_phone" : @"customer_phone"
  8656. }
  8657. };
  8658. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8659. [dic setValue:choose_dic forKey:@"item_0"];
  8660. // contact id
  8661. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8662. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8663. [contact_id_dic setValue:@"true" forKey:@"required"];
  8664. [dic setValue:contact_id_dic forKey:@"item_1"];
  8665. // company name
  8666. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8667. [dic setValue:company_name_dic forKey:@"item_2"];
  8668. // address
  8669. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8670. [dic setValue:ext_dic forKey:@"item_3"];
  8671. // contact
  8672. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8673. [dic setValue:contact_dic forKey:@"item_4"];
  8674. // phone
  8675. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8676. [dic setValue:phone_dic forKey:@"item_5"];
  8677. // fax
  8678. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8679. [dic setValue:fax_dic forKey:@"item_6"];
  8680. // email
  8681. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8682. [dic setValue:email_dic forKey:@"item_7"];
  8683. }];
  8684. return dic;
  8685. }
  8686. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8687. {
  8688. assert(params[@"user"]!=nil);
  8689. assert(params[@"contact_id"]!=nil);
  8690. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8691. orderCode = [self translateSingleQuote:orderCode];
  8692. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8693. __block double TotalCuft = 0;
  8694. __block double TotalWeight = 0;
  8695. __block int TotalCarton = 0;
  8696. __block double payments = 0;
  8697. // setting
  8698. NSDictionary *setting = params[@"setting"];
  8699. NSNumber *hide = setting[@"ModelInformationHide"];
  8700. [dic setValue:hide forKey:@"hide"];
  8701. 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];
  8702. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8703. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8704. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8705. // item id
  8706. int item_id = sqlite3_column_int(stmt, 0);
  8707. // count
  8708. int item_count = sqlite3_column_int(stmt, 1);
  8709. // stockUom
  8710. int stockUom = sqlite3_column_int(stmt, 2);
  8711. // unit price
  8712. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8713. NSString* Price=nil;
  8714. if([str_price isEqualToString:@""])
  8715. {
  8716. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8717. if(price==nil)
  8718. Price=@"No Price.";
  8719. else
  8720. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8721. }
  8722. else
  8723. {
  8724. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8725. }
  8726. // discount
  8727. double discount = sqlite3_column_double(stmt, 4);
  8728. // name
  8729. NSString *name = [self textAtColumn:5 statement:stmt];
  8730. // description
  8731. NSString *description = [self textAtColumn:6 statement:stmt];
  8732. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8733. // line note
  8734. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8735. int avaulability = sqlite3_column_int(stmt, 8);
  8736. // img
  8737. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8738. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8739. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8740. if(combine != nil)
  8741. {
  8742. // int citem=0;
  8743. int bcount=[[combine valueForKey:@"count"] intValue];
  8744. for(int bc=0;bc<bcount;bc++)
  8745. {
  8746. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8747. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8748. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8749. subTotal += uprice * modulus * item_count;
  8750. }
  8751. }
  8752. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8753. [model_dic setValue:@"model" forKey:@"control"];
  8754. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8755. [model_dic setValue:description forKey:@"description"];
  8756. [model_dic setValue:line_note forKey:@"note"];
  8757. [model_dic setValue:img forKey:@"img_url"];
  8758. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8759. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8760. [model_dic setValue:Price forKey:@"unit_price"];
  8761. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8762. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8763. if (combine) {
  8764. [model_dic setValue:combine forKey:@"combine"];
  8765. }
  8766. // well,what under the row is the info for total
  8767. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8768. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8769. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8770. int carton=[bsubtotaljson[@"carton"] intValue];
  8771. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8772. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8773. TotalCuft += cuft;
  8774. TotalWeight += weight;
  8775. TotalCarton += carton;
  8776. payments += subTotal;
  8777. //---------------------------
  8778. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8779. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8780. }];
  8781. [dic setValue:@"Model Information" forKey:@"title"];
  8782. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8783. return dic;
  8784. }
  8785. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8786. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8787. [dic setValue:@"Remarks Content" forKey:@"title"];
  8788. // setting
  8789. NSDictionary *setting = params[@"setting"];
  8790. NSNumber *hide = setting[@"RemarksContentHide"];
  8791. [dic setValue:hide forKey:@"hide"];
  8792. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8793. orderCode = [self translateSingleQuote:orderCode];
  8794. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8795. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8796. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8797. int mustCall = sqlite3_column_int(stmt, 1);
  8798. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8799. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8800. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8801. NSDictionary *po_dic = @{
  8802. @"aname" : @"PO#",
  8803. @"control" : @"edit",
  8804. @"keyboard" : @"text",
  8805. @"name" : @"poNumber",
  8806. @"value" : poNumber
  8807. };
  8808. #ifdef BUILD_CONTRAST
  8809. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8810. NSDictionary *schedule_dict = @{
  8811. @"aname" : @"Schdule Date",
  8812. @"control" : @"datepicker",
  8813. @"type": @"date",
  8814. @"required": @"true",
  8815. @"name" : @"schedule_date_str",
  8816. @"value" : nsdate
  8817. };
  8818. #endif
  8819. NSDictionary *must_call_dic = @{
  8820. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8821. @"control" : @"switch",
  8822. @"name" : @"must_call",
  8823. @"value" : mustCall ? @"true" : @"false"
  8824. };
  8825. NSDictionary *general_notes_dic = @{
  8826. @"aname" : @"General notes",
  8827. @"control" : @"text_view",
  8828. @"keyboard" : @"text",
  8829. @"name" : @"comments",
  8830. @"value" : generalNotes
  8831. };
  8832. // NSDictionary *internal_notes_dic = @{
  8833. // @"aname" : @"Internal notes",
  8834. // @"control" : @"text_view",
  8835. // @"keyboard" : @"text",
  8836. // @"name" : @"internal_notes",
  8837. // @"value" : internalNotes
  8838. // };
  8839. #ifdef BUILD_CONTRAST
  8840. [dic setValue:po_dic forKey:@"item_0"];
  8841. [dic setValue:schedule_dict forKey:@"item_1"];
  8842. [dic setValue:must_call_dic forKey:@"item_2"];
  8843. [dic setValue:general_notes_dic forKey:@"item_3"];
  8844. #else
  8845. [dic setValue:po_dic forKey:@"item_0"];
  8846. [dic setValue:must_call_dic forKey:@"item_1"];
  8847. [dic setValue:general_notes_dic forKey:@"item_2"];
  8848. #endif
  8849. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8850. }];
  8851. #ifdef BUILD_CONTRAST
  8852. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8853. #else
  8854. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8855. #endif
  8856. return dic;
  8857. }
  8858. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8859. // params
  8860. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8861. orderCode = [self translateSingleQuote:orderCode];
  8862. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8863. // setting
  8864. NSDictionary *setting = params[@"setting"];
  8865. NSNumber *hide = setting[@"OrderTotalHide"];
  8866. [dic setValue:hide forKey:@"hide"];
  8867. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8868. __block double lift_gate_value = 0;
  8869. __block double handling_fee = 0;
  8870. __block double shipping = 0;
  8871. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8872. int lift_gate = sqlite3_column_int(stmt, 0);
  8873. lift_gate_value = sqlite3_column_double(stmt, 1);
  8874. shipping = sqlite3_column_double(stmt, 2);
  8875. handling_fee = sqlite3_column_double(stmt, 3);
  8876. if (!lift_gate) {
  8877. lift_gate_value = 0;
  8878. }
  8879. }];
  8880. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8881. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8882. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8883. double payments = [[total valueForKey:@"payments"] doubleValue];
  8884. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8885. double totalPrice = payments;
  8886. [dic setValue:@"Order Total" forKey:@"title"];
  8887. NSDictionary *payments_dic = @{
  8888. @"align" : @"right",
  8889. @"aname" : @"Payments/Credits",
  8890. @"control" : @"text",
  8891. @"name" : @"paymentsAndCredits",
  8892. @"type" : @"price",
  8893. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8894. };
  8895. [dic setValue:payments_dic forKey:@"item_0"];
  8896. // version 1.71 remove
  8897. // NSDictionary *handling_fee_dic = @{
  8898. // @"align" : @"right",
  8899. // @"aname" : @"Handling Fee",
  8900. // @"control" : @"text",
  8901. // @"name" : @"handling_fee_value",
  8902. // @"required" : @"true",
  8903. // @"type" : @"price",
  8904. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8905. // };
  8906. NSDictionary *shipping_dic = @{
  8907. @"align" : @"right",
  8908. @"aname" : @"Shipping*",
  8909. @"control" : @"text",
  8910. @"name" : @"shipping",
  8911. @"required" : @"true",
  8912. @"type" : @"price",
  8913. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8914. };
  8915. NSDictionary *lift_gate_dic = @{
  8916. @"align" : @"right",
  8917. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8918. @"control" : @"text",
  8919. @"name" : @"lift_gate_value",
  8920. @"required" : @"true",
  8921. @"type" : @"price",
  8922. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8923. };
  8924. int item_count = 1;
  8925. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8926. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8927. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8928. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8929. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8930. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8931. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8932. } else {
  8933. }
  8934. }
  8935. // version 1.71 remove
  8936. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8937. NSDictionary *total_price_dic = @{
  8938. @"align" : @"right",
  8939. @"aname" : @"Total",
  8940. @"control" : @"text",
  8941. @"name" : @"totalPrice",
  8942. @"type" : @"price",
  8943. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8944. };
  8945. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8946. NSDictionary *total_cuft_dic = @{
  8947. @"align" : @"right",
  8948. @"aname" : @"Total Cuft",
  8949. @"control" : @"text",
  8950. @"name" : @"",
  8951. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8952. };
  8953. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8954. NSDictionary *total_weight_dic = @{
  8955. @"align" : @"right",
  8956. @"aname" : @"Total Weight",
  8957. @"control" : @"text",
  8958. @"name" : @"",
  8959. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8960. };
  8961. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8962. NSDictionary *total_carton_dic = @{
  8963. @"align" : @"right",
  8964. @"aname" : @"Total Carton",
  8965. @"control" : @"text",
  8966. @"name" : @"",
  8967. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8968. };
  8969. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8970. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8971. return dic;
  8972. }
  8973. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8974. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8975. orderCode = [self translateSingleQuote:orderCode];
  8976. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8977. // setting
  8978. NSDictionary *setting = params[@"setting"];
  8979. NSNumber *hide = setting[@"SignatureHide"];
  8980. [dic setValue:hide forKey:@"hide"];
  8981. [dic setValue:@"Signature" forKey:@"title"];
  8982. __block NSString *pic_path = @"";
  8983. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8984. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8985. pic_path = [self textAtColumn:0 statement:stmt];
  8986. }];
  8987. NSDictionary *pic_dic = @{
  8988. @"aname" : @"Signature",
  8989. @"avalue" :pic_path,
  8990. @"control" : @"signature",
  8991. @"name" : @"sign_picpath",
  8992. @"value" : pic_path
  8993. };
  8994. [dic setValue:pic_dic forKey:@"item_0"];
  8995. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8996. return dic;
  8997. }
  8998. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8999. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9000. // setting
  9001. NSDictionary *setting = params[@"setting"];
  9002. NSNumber *hide = setting[@"ShippingMethodHide"];
  9003. [dic setValue:hide forKey:@"hide"];
  9004. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9005. orderCode = [self translateSingleQuote:orderCode];
  9006. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  9007. __block NSString *logist = @"";
  9008. __block NSString *carrier = @"";
  9009. __block NSString *lift_gate = @"";
  9010. __block int lift_gate_integer = 0;
  9011. __block NSString *logistic_note = @"";
  9012. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9013. logist = [self textAtColumn:0 statement:stmt];
  9014. lift_gate_integer = sqlite3_column_int(stmt, 1);
  9015. logistic_note = [self textAtColumn:2 statement:stmt];
  9016. carrier = [self textAtColumn:3 statement:stmt];
  9017. }];
  9018. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  9019. [dic setValue:@"Shipping Method" forKey:@"title"];
  9020. // val_0
  9021. int PERSONAL_PICK_UP_check = 0;
  9022. int USE_MY_CARRIER_check = 0;
  9023. NSString *logistic_note_text = @"";
  9024. int will_call_check = 0;
  9025. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  9026. will_call_check = 1;
  9027. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  9028. PERSONAL_PICK_UP_check = 1;
  9029. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  9030. USE_MY_CARRIER_check = 1;
  9031. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  9032. logistic_note = [logistic_note_array firstObject];
  9033. if (logistic_note_array.count > 1) {
  9034. logistic_note_text = [logistic_note_array lastObject];
  9035. }
  9036. }
  9037. }
  9038. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  9039. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  9040. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  9041. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  9042. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  9043. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  9044. NSDictionary *val0_subItem_item0 = @{
  9045. @"aname" : @"Option",
  9046. @"cadedate" : @{
  9047. @"count" : [NSNumber numberWithInteger:2],
  9048. @"val_0" : @{
  9049. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  9050. @"refresh" : [NSNumber numberWithInteger:0],
  9051. @"value" : @"PERSONAL PICK UP",
  9052. @"value_id" : @"PERSONAL PICK UP"
  9053. },
  9054. @"val_1" : @{
  9055. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  9056. @"refresh" : [NSNumber numberWithInteger:0],
  9057. @"sub_item" : @{
  9058. @"count" : [NSNumber numberWithInteger:1],
  9059. @"item_0" : @{
  9060. @"aname" : @"BOL",
  9061. @"control" : @"edit",
  9062. @"keyboard" : @"text",
  9063. @"name" : @"logist_note_text",
  9064. @"refresh" : [NSNumber numberWithInteger:0],
  9065. @"required" : @"false",
  9066. @"value" : logistic_note_text
  9067. }
  9068. },
  9069. @"value" : @"USE MY CARRIER",
  9070. @"value_id" : @"USE MY CARRIER"
  9071. }
  9072. },
  9073. @"control" : @"enum",
  9074. @"name" : @"logistic_note",
  9075. @"required" : @"true",
  9076. @"single_select" : @"true"
  9077. };
  9078. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  9079. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  9080. // val_1
  9081. lift_gate = [self valueInParams:params key:@"lift_gate"];
  9082. if([lift_gate isEqualToString:@""]) {
  9083. if (lift_gate_integer == 1) {
  9084. lift_gate = @"true";
  9085. } else {
  9086. lift_gate = @"false";
  9087. }
  9088. }
  9089. // carrier = [self valueInParams:params key:@"carrier"];
  9090. // if([carrier isEqualToString:@""]) {
  9091. //
  9092. // if (lift_gate_integer == 1) {
  9093. // lift_gate = @"true";
  9094. // } else {
  9095. // lift_gate = @"false";
  9096. // }
  9097. //
  9098. // }
  9099. int common_carrier_check = 0;
  9100. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  9101. common_carrier_check = 1;
  9102. [params setValue:lift_gate forKey:@"lift_gate"];
  9103. #ifdef BUILD_CONTRAST
  9104. [params setValue:carrier forKey:@"carrier"];
  9105. #endif
  9106. }
  9107. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  9108. // NSString * code = [iSalesDB jk_queryText:Sql];
  9109. // [self offline_getCarrierCode:offline_getCarrierCode];
  9110. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  9111. NSDictionary *val_1 = @{
  9112. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  9113. @"sub_item" : @{
  9114. @"item_0" : @{
  9115. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  9116. @"control" : @"switch",
  9117. @"name" : @"lift_gate",
  9118. @"refresh" : [NSNumber numberWithInteger:1],
  9119. @"required" : @"true",
  9120. @"value" : lift_gate
  9121. },
  9122. #ifdef BUILD_CONTRAST
  9123. @"count" : [NSNumber numberWithInteger:2],
  9124. @"item_1" : @{
  9125. @"aname" : @"Carrier",
  9126. @"cadedate" :carrierDic,
  9127. @"control" : @"enum",
  9128. @"name" : @"carrier",
  9129. @"required" : @"false",
  9130. @"value" : @"",
  9131. @"single_select" : @"true"
  9132. }
  9133. #else
  9134. @"count" : [NSNumber numberWithInteger:1]
  9135. #endif
  9136. },
  9137. @"value" : @"COMMON CARRIER",
  9138. @"value_id" : @"COMMON CARRIER"
  9139. };
  9140. // cadedate
  9141. NSDictionary *cadedate = @{
  9142. @"count" : [NSNumber numberWithInteger:2],
  9143. @"val_0" : val_0,
  9144. @"val_1" : val_1
  9145. };
  9146. // item_0
  9147. NSMutableDictionary *item_0 = @{
  9148. @"aname" : @"Shipping",
  9149. @"cadedate" : cadedate,
  9150. @"control" : @"enum",
  9151. @"name" : @"logist",
  9152. @"refresh" : [NSNumber numberWithInteger:1],
  9153. @"single_select" : @"true",
  9154. }.mutableCopy;
  9155. NSNumber *required = setting[@"ShippingMethodRequire"];
  9156. if ([required integerValue]) {
  9157. [item_0 setValue:@"true" forKey:@"required"];
  9158. }
  9159. [dic setValue:item_0 forKey:@"item_0"];
  9160. if ([logist isEqualToString:@"WILL CALL"]) {
  9161. [dic removeObjectForKey:@"lift_gate"];
  9162. }
  9163. return dic;
  9164. }
  9165. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  9166. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9167. orderCode = [self translateSingleQuote:orderCode];
  9168. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  9169. __block int submit_as_integer = 0;
  9170. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9171. submit_as_integer = sqlite3_column_int(stmt, 0);
  9172. }];
  9173. int check11 = 0;
  9174. int check10 = 0;
  9175. if (submit_as_integer == 11) {
  9176. check11 = 1;
  9177. }
  9178. if (submit_as_integer == 10) {
  9179. check10 = 1;
  9180. }
  9181. // section_0
  9182. NSMutableDictionary *dic = @{
  9183. @"count" : @(1),
  9184. @"item_0" : @{
  9185. @"aname" : @"Submit Order As",
  9186. @"cadedate" : @{
  9187. @"count" : @(2),
  9188. @"val_0" : @{
  9189. @"check" : [NSNumber numberWithInteger:check11],
  9190. @"value" : @"Sales Order",
  9191. @"value_id" : @(11)
  9192. },
  9193. @"val_1" : @{
  9194. @"check" : [NSNumber numberWithInteger:check10],
  9195. @"value" : @"Quote",
  9196. @"value_id" : @(10)
  9197. }
  9198. },
  9199. @"control" : @"enum",
  9200. @"name" : @"erpOrderStatus",
  9201. @"required" : @"true",
  9202. @"single_select" : @"true"
  9203. },
  9204. @"title" : @"Order Type"
  9205. }.mutableCopy;
  9206. // setting
  9207. NSDictionary *setting = params[@"setting"];
  9208. NSNumber *hide = setting[@"OrderTypeHide"];
  9209. [dic setValue:hide forKey:@"hide"];
  9210. return dic;
  9211. }
  9212. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  9213. countryCode = [self translateSingleQuote:countryCode];
  9214. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  9215. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9216. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  9217. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  9218. if (name == NULL) {
  9219. name = "";
  9220. }
  9221. if (code == NULL) {
  9222. code = "";
  9223. }
  9224. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9225. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  9226. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  9227. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9228. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  9229. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9230. }
  9231. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  9232. [container setValue:stateDic forKey:key];
  9233. }] mutableCopy];
  9234. [ret removeObjectForKey:@"result"];
  9235. // failure 可以不用了,一样的
  9236. if (ret.allKeys.count == 0) {
  9237. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9238. [stateDic setValue:@"Other" forKey:@"value"];
  9239. [stateDic setValue:@"" forKey:@"value_id"];
  9240. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9241. if (state_code && [@"" isEqualToString:state_code]) {
  9242. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9243. }
  9244. NSString *key = [NSString stringWithFormat:@"val_0"];
  9245. [ret setValue:stateDic forKey:key];
  9246. }
  9247. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  9248. return ret;
  9249. }
  9250. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  9251. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9252. orderCode = [self translateSingleQuote:orderCode];
  9253. 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];
  9254. __block NSString *payType = @"";
  9255. __block NSString *firstName = @"";
  9256. __block NSString *lastName = @"";
  9257. __block NSString *addr1 = @"";
  9258. __block NSString *addr2 = @"";
  9259. __block NSString *zipcode = @"";
  9260. __block NSString *cardType = @"";
  9261. __block NSString *cardNumber = @"";
  9262. __block NSString *securityCode = @"";
  9263. __block NSString *month = @"";
  9264. __block NSString *year = @"";
  9265. __block NSString *city = @"";
  9266. __block NSString *state = @"";
  9267. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9268. payType = [self textAtColumn:0 statement:stmt];
  9269. firstName = [self textAtColumn:1 statement:stmt];
  9270. lastName = [self textAtColumn:2 statement:stmt];
  9271. addr1 = [self textAtColumn:3 statement:stmt];
  9272. addr2 = [self textAtColumn:4 statement:stmt];
  9273. zipcode = [self textAtColumn:5 statement:stmt];
  9274. cardType = [self textAtColumn:6 statement:stmt];
  9275. cardNumber = [self textAtColumn:7 statement:stmt];
  9276. securityCode = [self textAtColumn:8 statement:stmt];
  9277. month = [self textAtColumn:9 statement:stmt];
  9278. year = [self textAtColumn:10 statement:stmt];
  9279. city = [self textAtColumn:11 statement:stmt];
  9280. state = [self textAtColumn:12 statement:stmt];
  9281. }];
  9282. NSString *required = @"true";
  9283. // setting
  9284. NSDictionary *setting = params[@"setting"];
  9285. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9286. if ([requiredNumber integerValue]) {
  9287. required = @"true";
  9288. } else {
  9289. required = @"false";
  9290. }
  9291. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9292. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9293. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9294. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9295. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9296. NSString *type = [self textAtColumn:1 statement:stmt];
  9297. NSMutableDictionary *val = @{
  9298. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9299. @"value" : type,
  9300. @"value_id" : type_id
  9301. }.mutableCopy;
  9302. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9303. NSDictionary *sub_item =nil;
  9304. #ifdef BUILD_CONTRAST
  9305. sub_item = @{
  9306. @"count" : @(3),
  9307. @"item_0" : @{
  9308. @"aname" : @"Number",
  9309. @"control" : @"edit",
  9310. @"keyboard" : @"int",
  9311. @"length" : @"16",
  9312. @"name" : @"credit_card_number",
  9313. @"required" : @"false",
  9314. @"value" : cardNumber
  9315. },
  9316. @"item_1" : @{
  9317. @"aname" : @"Expiration Date",
  9318. @"control" : @"monthpicker",
  9319. @"name" : @"credit_card_expiration",
  9320. @"required" : @"false",
  9321. @"type" : @"date",
  9322. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9323. },
  9324. @"item_2" : @{
  9325. @"aname" : @"Security Code",
  9326. @"control" : @"edit",
  9327. @"keyboard" : @"int",
  9328. @"length" : @"4",
  9329. @"name" : @"credit_card_security_code",
  9330. @"required" : @"false",
  9331. @"value" : securityCode
  9332. },
  9333. };
  9334. #else
  9335. sub_item= @{
  9336. @"count" : @(3),
  9337. @"item_0" : @{
  9338. @"aname" : @"choose",
  9339. @"control" : @"multi_action",
  9340. @"count" : @(1),
  9341. @"item_0" : @{
  9342. @"aname" : @"Same as customer",
  9343. @"key_map" : @{
  9344. @"credit_card_address1" : @"customer_address1",
  9345. @"credit_card_address2" : @"customer_address2",
  9346. @"credit_card_city" : @"customer_city",
  9347. @"credit_card_first_name" : @"customer_first_name",
  9348. @"credit_card_last_name" : @"customer_last_name",
  9349. @"credit_card_state" : @"customer_state",
  9350. @"credit_card_zipcode" : @"customer_zipcode"
  9351. },
  9352. @"type" : @"pull"
  9353. }
  9354. },
  9355. @"item_1" : @{
  9356. @"aname" : @"",
  9357. @"color" : @"red",
  9358. @"control" : @"text",
  9359. @"name" : @"",
  9360. @"value" : @"USA Credit cards only"
  9361. },
  9362. @"item_2" : @{
  9363. @"aname" : @"Fill",
  9364. @"cadedate" : @{
  9365. @"count" : @(2),
  9366. @"val_0" : @{
  9367. @"check" : @(1),
  9368. @"sub_item" : @{
  9369. @"count" : @(11),
  9370. @"item_0" : @{
  9371. @"aname" : @"Type",
  9372. @"cadedate" : @{
  9373. @"count" : @(2),
  9374. @"val_0" : @{
  9375. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9376. @"value" : @"VISA",
  9377. @"value_id" : @"VISA"/*@(0)*/
  9378. },
  9379. @"val_1" : @{
  9380. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9381. @"value" : @"MASTER CARD",
  9382. @"value_id" : @"MASTER CARD"/*@(1)*/
  9383. }
  9384. },
  9385. @"control" : @"enum",
  9386. @"name" : @"credit_card_type",
  9387. @"required" : @"true",
  9388. @"single_select" : @"true"
  9389. },
  9390. @"item_1" : @{
  9391. @"aname" : @"Number",
  9392. @"control" : @"edit",
  9393. @"keyboard" : @"int",
  9394. @"length" : @"16",
  9395. @"name" : @"credit_card_number",
  9396. @"required" : @"true",
  9397. @"value" : cardNumber
  9398. },
  9399. @"item_10" : @{
  9400. @"aname" : @"State",
  9401. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9402. @"control" : @"enum",
  9403. @"enum" : @"true",
  9404. @"name" : @"credit_card_state",
  9405. @"required" : @"true",
  9406. @"single_select" : @"true"
  9407. },
  9408. @"item_2" : @{
  9409. @"aname" : @"Expiration Date",
  9410. @"control" : @"monthpicker",
  9411. @"name" : @"credit_card_expiration",
  9412. @"required" : @"true",
  9413. @"type" : @"date",
  9414. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9415. },
  9416. @"item_3" : @{
  9417. @"aname" : @"Security Code",
  9418. @"control" : @"edit",
  9419. @"keyboard" : @"int",
  9420. @"length" : @"3",
  9421. @"name" : @"credit_card_security_code",
  9422. @"required" : @"true",
  9423. @"value" : securityCode
  9424. },
  9425. @"item_4" : @{
  9426. @"aname" : @"First Name",
  9427. @"control" : @"edit",
  9428. @"keyboard" : @"text",
  9429. @"name" : @"credit_card_first_name",
  9430. @"required" : @"true",
  9431. @"value" : firstName
  9432. },
  9433. @"item_5" : @{
  9434. @"aname" : @"Last Name",
  9435. @"control" : @"edit",
  9436. @"keyboard" : @"text",
  9437. @"name" : @"credit_card_last_name",
  9438. @"required" : @"true",
  9439. @"value" : lastName
  9440. },
  9441. @"item_6" : @{
  9442. @"aname" : @"Address 1",
  9443. @"control" : @"edit",
  9444. @"keyboard" : @"text",
  9445. @"name" : @"credit_card_address1",
  9446. @"required" : @"true",
  9447. @"value" : addr1
  9448. },
  9449. @"item_7" : @{
  9450. @"aname" : @"Address 2",
  9451. @"control" : @"edit",
  9452. @"keyboard" : @"text",
  9453. @"name" : @"credit_card_address2",
  9454. @"value" : addr2
  9455. },
  9456. @"item_8" : @{
  9457. @"aname" : @"zip code",
  9458. @"control" : @"edit",
  9459. @"keyboard" : @"text",
  9460. @"name" : @"credit_card_zipcode",
  9461. @"required" : @"true",
  9462. @"value" : zipcode
  9463. },
  9464. @"item_9" : @{
  9465. @"aname" : @"City",
  9466. @"control" : @"edit",
  9467. @"keyboard" : @"text",
  9468. @"name" : @"credit_card_city",
  9469. @"required" : @"true",
  9470. @"value" : city
  9471. }
  9472. },
  9473. @"value" : @"Fill Now",
  9474. @"value_id" : @""
  9475. },
  9476. @"val_1" : @{
  9477. @"check" : @(0),
  9478. @"value" : @"Fill Later",
  9479. @"value_id" : @""
  9480. }
  9481. },
  9482. @"control" : @"enum",
  9483. @"name" : @"",
  9484. @"single_select" : @"true"
  9485. }
  9486. };
  9487. #endif;
  9488. [val setObject:sub_item forKey:@"sub_item"];
  9489. }
  9490. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9491. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9492. }];
  9493. // "section_2"
  9494. NSMutableDictionary *dic = @{
  9495. @"count" : @(1),
  9496. @"item_0" : @{
  9497. @"aname" : @"Payment",
  9498. @"required" : required,
  9499. @"cadedate" : cadedate,
  9500. // @{
  9501. // @"count" : @(6),
  9502. // @"val_3" : @{
  9503. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9504. // @"value" : @"Check",
  9505. // @"value_id" : @"Check"
  9506. // },
  9507. // @"val_2" : @{
  9508. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9509. // @"value" : @"Cash",
  9510. // @"value_id" : @"Cash"
  9511. // },
  9512. // @"val_1" : @{
  9513. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9514. // @"value" : @"NET 60",
  9515. // @"value_id" : @"NET 30"
  9516. // },
  9517. // @"val_4" : @{
  9518. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9519. // @"value" : @"Wire Transfer",
  9520. // @"value_id" : @"Wire Transfer"
  9521. // },
  9522. // @"val_0" : @{
  9523. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9524. // @"sub_item" : @{
  9525. // @"count" : @(3),
  9526. // @"item_0" : @{
  9527. // @"aname" : @"choose",
  9528. // @"control" : @"multi_action",
  9529. // @"count" : @(1),
  9530. // @"item_0" : @{
  9531. // @"aname" : @"Same as customer",
  9532. // @"key_map" : @{
  9533. // @"credit_card_address1" : @"customer_address1",
  9534. // @"credit_card_address2" : @"customer_address2",
  9535. // @"credit_card_city" : @"customer_city",
  9536. // @"credit_card_first_name" : @"customer_first_name",
  9537. // @"credit_card_last_name" : @"customer_last_name",
  9538. // @"credit_card_state" : @"customer_state",
  9539. // @"credit_card_zipcode" : @"customer_zipcode"
  9540. // },
  9541. // @"type" : @"pull"
  9542. // }
  9543. // },
  9544. // @"item_1" : @{
  9545. // @"aname" : @"",
  9546. // @"color" : @"red",
  9547. // @"control" : @"text",
  9548. // @"name" : @"",
  9549. // @"value" : @"USA Credit cards only"
  9550. // },
  9551. // @"item_2" : @{
  9552. // @"aname" : @"Fill",
  9553. // @"cadedate" : @{
  9554. // @"count" : @(2),
  9555. // @"val_0" : @{
  9556. // @"check" : @(1),
  9557. // @"sub_item" : @{
  9558. // @"count" : @(11),
  9559. // @"item_0" : @{
  9560. // @"aname" : @"Type",
  9561. // @"cadedate" : @{
  9562. // @"count" : @(2),
  9563. // @"val_0" : @{
  9564. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9565. // @"value" : @"VISA",
  9566. // @"value_id" : @(0)
  9567. // },
  9568. // @"val_1" : @{
  9569. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9570. // @"value" : @"MASTER CARD",
  9571. // @"value_id" : @(1)
  9572. // }
  9573. // },
  9574. // @"control" : @"enum",
  9575. // @"name" : @"credit_card_type",
  9576. // @"required" : @"true",
  9577. // @"single_select" : @"true"
  9578. // },
  9579. // @"item_1" : @{
  9580. // @"aname" : @"Number",
  9581. // @"control" : @"edit",
  9582. // @"keyboard" : @"int",
  9583. // @"length" : @"16",
  9584. // @"name" : @"credit_card_number",
  9585. // @"required" : @"true",
  9586. // @"value" : cardNumber
  9587. // },
  9588. // @"item_10" : @{
  9589. // @"aname" : @"State",
  9590. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9591. // @"control" : @"enum",
  9592. // @"enum" : @"true",
  9593. // @"name" : @"credit_card_state",
  9594. // @"required" : @"true",
  9595. // @"single_select" : @"true"
  9596. // },
  9597. // @"item_2" : @{
  9598. // @"aname" : @"Expiration Date",
  9599. // @"control" : @"monthpicker",
  9600. // @"name" : @"credit_card_expiration",
  9601. // @"required" : @"true",
  9602. // @"type" : @"date",
  9603. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9604. // },
  9605. // @"item_3" : @{
  9606. // @"aname" : @"Security Code",
  9607. // @"control" : @"edit",
  9608. // @"keyboard" : @"int",
  9609. // @"length" : @"3",
  9610. // @"name" : @"credit_card_security_code",
  9611. // @"required" : @"true",
  9612. // @"value" : securityCode
  9613. // },
  9614. // @"item_4" : @{
  9615. // @"aname" : @"First Name",
  9616. // @"control" : @"edit",
  9617. // @"keyboard" : @"text",
  9618. // @"name" : @"credit_card_first_name",
  9619. // @"required" : @"true",
  9620. // @"value" : firstName
  9621. // },
  9622. // @"item_5" : @{
  9623. // @"aname" : @"Last Name",
  9624. // @"control" : @"edit",
  9625. // @"keyboard" : @"text",
  9626. // @"name" : @"credit_card_last_name",
  9627. // @"required" : @"true",
  9628. // @"value" : lastName
  9629. // },
  9630. // @"item_6" : @{
  9631. // @"aname" : @"Address 1",
  9632. // @"control" : @"edit",
  9633. // @"keyboard" : @"text",
  9634. // @"name" : @"credit_card_address1",
  9635. // @"required" : @"true",
  9636. // @"value" : addr1
  9637. // },
  9638. // @"item_7" : @{
  9639. // @"aname" : @"Address 2",
  9640. // @"control" : @"edit",
  9641. // @"keyboard" : @"text",
  9642. // @"name" : @"credit_card_address2",
  9643. // @"value" : addr2
  9644. // },
  9645. // @"item_8" : @{
  9646. // @"aname" : @"zip code",
  9647. // @"control" : @"edit",
  9648. // @"keyboard" : @"text",
  9649. // @"name" : @"credit_card_zipcode",
  9650. // @"required" : @"true",
  9651. // @"value" : zipcode
  9652. // },
  9653. // @"item_9" : @{
  9654. // @"aname" : @"City",
  9655. // @"control" : @"edit",
  9656. // @"keyboard" : @"text",
  9657. // @"name" : @"credit_card_city",
  9658. // @"required" : @"true",
  9659. // @"value" : city
  9660. // }
  9661. // },
  9662. // @"value" : @"Fill Now",
  9663. // @"value_id" : @""
  9664. // },
  9665. // @"val_1" : @{
  9666. // @"check" : @(0),
  9667. // @"value" : @"Fill Later",
  9668. // @"value_id" : @""
  9669. // }
  9670. // },
  9671. // @"control" : @"enum",
  9672. // @"name" : @"",
  9673. // @"single_select" : @"true"
  9674. // }
  9675. // },
  9676. // @"value" : @"Visa/Master",
  9677. // @"value_id" : @"Credit Card"
  9678. // },
  9679. // @"val_5" : @{
  9680. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9681. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9682. // @"value_id" : @"FOLLOW EXISTING"
  9683. // }
  9684. // },
  9685. @"control" : @"enum",
  9686. @"name" : @"paymentType",
  9687. @"single_select" : @"true"
  9688. },
  9689. @"title" : @"Payment Information"
  9690. }.mutableCopy;
  9691. NSNumber *hide = setting[@"PaymentInformationHide"];
  9692. [dic setValue:hide forKey:@"hide"];
  9693. return dic;
  9694. }
  9695. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9696. // params
  9697. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9698. orderCode = [self translateSingleQuote:orderCode];
  9699. // 缺货检查
  9700. 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];
  9701. __block BOOL outOfStock = NO;
  9702. sqlite3 *database = db;
  9703. if (!db) {
  9704. database = [iSalesDB get_db];
  9705. }
  9706. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9707. int availability = sqlite3_column_int(stmt, 0);
  9708. int item_qty = sqlite3_column_int(stmt, 1);
  9709. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9710. }];
  9711. if (!db) {
  9712. [iSalesDB close_db:database];
  9713. }
  9714. return outOfStock;
  9715. }
  9716. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9717. {
  9718. assert(params[@"user"]!=nil);
  9719. assert(params[@"contact_id"]!=nil);
  9720. assert(params[@"can_create_backorder"]!=nil);
  9721. sqlite3 *db = [iSalesDB get_db];
  9722. // params
  9723. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9724. orderCode = [self translateSingleQuote:orderCode];
  9725. // 缺货检查
  9726. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9727. if (![params[@"can_create_backorder"] boolValue]) {
  9728. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9729. if (out_of_stock) {
  9730. [iSalesDB close_db:db];
  9731. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9732. [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"];
  9733. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9734. return [RAConvertor dict2data:resultDic];
  9735. }
  9736. }
  9737. // UISetting
  9738. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9739. NSString *cachefolder = [paths objectAtIndex:0];
  9740. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9741. NSData* json =nil;
  9742. json=[NSData dataWithContentsOfFile:img_cache];
  9743. NSError *error=nil;
  9744. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9745. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9746. [params setObject:setting forKey:@"setting"];
  9747. int section_count = 0;
  9748. // 0 Order Type 1 Shipping Method 2 Payment Information
  9749. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9750. // 0 Order Type
  9751. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9752. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9753. [ret setValue:order_type_dic forKey:key0];
  9754. // 1 Shipping Method
  9755. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9756. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9757. [ret setValue:shipping_method_dic forKey:key1];
  9758. // 2 Payment Information
  9759. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9760. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9761. [ret setValue:payment_info_dic forKey:key2];
  9762. // 3 Customer
  9763. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9764. NSDictionary *customer_dic = [self customerDic:params db:db];
  9765. [ret setValue:customer_dic forKey:key3];
  9766. // 4 Ship To
  9767. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9768. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9769. [ret setValue:ship_to_dic forKey:key4];
  9770. // 5 Ship From
  9771. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9772. if (![shipFromDisable integerValue]) {
  9773. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9774. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9775. [ret setValue:ship_from_dic forKey:key5];
  9776. }
  9777. // 6 Freight Bill To
  9778. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9779. if (![freightBillToDisable integerValue]) {
  9780. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9781. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9782. [ret setValue:freight_bill_to forKey:key6];
  9783. }
  9784. // 7 Merchandise Bill To
  9785. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9786. if (![merchandiseBillToDisable integerValue]) {
  9787. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9788. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9789. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9790. }
  9791. // 8 Return To
  9792. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9793. if (![returnToDisable integerValue]) {
  9794. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9795. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9796. [ret setValue:return_to_dic forKey:key8];
  9797. }
  9798. // 9 Model Information
  9799. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9800. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9801. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9802. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9803. [ret setValue:model_info_dic forKey:key9];
  9804. // 10 Remarks Content
  9805. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9806. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9807. [ret setValue:remarks_content_dic forKey:key10];
  9808. // 11 Order Total
  9809. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9810. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9811. [ret setValue:order_total_dic forKey:key11];
  9812. // 12 Signature
  9813. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9814. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9815. [ret setValue:sign_dic forKey:key12];
  9816. [ret setValue:@(section_count) forKey:@"section_count"];
  9817. [iSalesDB close_db:db];
  9818. return [RAConvertor dict2data:ret];
  9819. // return nil;
  9820. }
  9821. #pragma mark addr editor
  9822. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9823. NSMutableDictionary *new_item = [item mutableCopy];
  9824. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9825. return new_item;
  9826. }
  9827. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9828. {
  9829. // "is_subaction" = true;
  9830. // orderCode = MOB1608240002;
  9831. // password = 123456;
  9832. // "subaction_tag" = 1;
  9833. // user = EvanK;
  9834. // {
  9835. // "is_subaction" = true;
  9836. // orderCode = MOB1608240002;
  9837. // password = 123456;
  9838. // "refresh_trigger" = zipcode;
  9839. // "subaction_tag" = 1;
  9840. // user = EvanK;
  9841. // }
  9842. NSString *country_code = nil;
  9843. NSString *zipCode = nil;
  9844. NSString *stateCode = nil;
  9845. NSString *city = nil;
  9846. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9847. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9848. NSString *code_id = params[@"country"];
  9849. country_code = [self countryCodeByid:code_id];
  9850. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9851. NSString *zip_code = params[@"zipcode"];
  9852. // 剔除全部为空格
  9853. int spaceCount = 0;
  9854. for (int i = 0; i < zip_code.length; i++) {
  9855. if ([zip_code characterAtIndex:i] == ' ') {
  9856. spaceCount++;
  9857. }
  9858. }
  9859. if (spaceCount == zip_code.length) {
  9860. zip_code = @"";
  9861. }
  9862. zipCode = zip_code;
  9863. if (zipCode.length > 0) {
  9864. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9865. country_code = [dic valueForKey:@"country_code"];
  9866. if (!country_code) {
  9867. // country_code = @"US";
  9868. NSString *code_id = params[@"country"];
  9869. country_code = [self countryCodeByid:code_id];
  9870. }
  9871. stateCode = [dic valueForKey:@"state_code"];
  9872. if(!stateCode.length) {
  9873. stateCode = params[@"state"];
  9874. }
  9875. city = [dic valueForKey:@"city"];
  9876. if (!city.length) {
  9877. city = params[@"city"];
  9878. }
  9879. // zip code
  9880. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9881. // [zipDic setValue:zipCode forKey:@"value"];
  9882. // [section_0 setValue:zipDic forKey:@"item_11"];
  9883. } else {
  9884. NSString *code_id = params[@"country"];
  9885. country_code = [self countryCodeByid:code_id];
  9886. stateCode = params[@"state"];
  9887. city = params[@"city"];
  9888. }
  9889. }
  9890. }
  9891. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9892. // [ret removeObjectForKey:@"up_params"];
  9893. [ret setObject:@"New Address" forKey:@"title"];
  9894. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9895. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9896. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9897. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9898. // [country_dic removeObjectForKey:@"refresh"];
  9899. // [country_dic removeObjectForKey:@"restore"];
  9900. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9901. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9902. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9903. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9904. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9905. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9906. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9907. // [zip_code_dic removeObjectForKey:@"refresh"];
  9908. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9909. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9910. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9911. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9912. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9913. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9914. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9915. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9916. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9917. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9918. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9919. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9920. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9921. // country
  9922. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9923. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9924. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9925. // state
  9926. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9927. // NSDictionary *tmpDic = @{
  9928. // @"value" : @"Other",
  9929. // @"value_id" : @"",
  9930. // @"check" : [NSNumber numberWithInteger:0]
  9931. // };
  9932. //
  9933. // for (int i = 0; i < allState.allKeys.count; i++) {
  9934. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9935. //
  9936. // NSDictionary *tmp = allState[key];
  9937. // [allState setValue:tmpDic forKey:key];
  9938. // tmpDic = tmp;
  9939. // }
  9940. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9941. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9942. [ret setValue:new_section_0 forKey:@"section_0"];
  9943. return [RAConvertor dict2data:ret];
  9944. }
  9945. #pragma mark save addr
  9946. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9947. {
  9948. // NSString *companyName = [self valueInParams:params key:@"company"];
  9949. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9950. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9951. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9952. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9953. // NSString *countryId = [self valueInParams:params key:@"country"];
  9954. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9955. // NSString *city = [self valueInParams:params key:@"city"];
  9956. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9957. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9958. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9959. // NSString *phone = [self valueInParams:params key:@"phone"];
  9960. // NSString *fax = [self valueInParams:params key:@"fax"];
  9961. // NSString *email = [self valueInParams:params key:@"email"];
  9962. return [self offline_saveContact:params update:NO isCustomer:NO];
  9963. }
  9964. #pragma mark cancel order
  9965. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9966. {
  9967. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9968. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9969. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9970. if (order_id.length) {
  9971. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9972. }
  9973. int ret = [iSalesDB execSql:sql];
  9974. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9975. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9976. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9977. // [dic setValue:@"160409" forKey:@"min_ver"];
  9978. return [RAConvertor dict2data:dic];
  9979. }
  9980. #pragma mark sign order
  9981. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9982. {
  9983. //参考 offline_saveBusinesscard
  9984. DebugLog(@"sign order params: %@",params);
  9985. // orderCode = MOB1608240002;
  9986. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9987. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9988. orderCode = [self translateSingleQuote:orderCode];
  9989. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9990. picPath = [self translateSingleQuote:picPath];
  9991. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9992. int ret = [iSalesDB execSql:sql];
  9993. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9994. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9995. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9996. // [dic setValue:@"160409" forKey:@"min_ver"];
  9997. return [RAConvertor dict2data:dic];
  9998. }
  9999. #pragma mark save order
  10000. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  10001. NSString *ret = [self valueInParams:params key:key];
  10002. if (translate) {
  10003. ret = [self translateSingleQuote:ret];
  10004. }
  10005. return ret;
  10006. }
  10007. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  10008. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  10009. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  10010. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  10011. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  10012. if (schedule_date.length==0 ) {
  10013. schedule_date = @"";
  10014. } else {
  10015. schedule_date = [self rchangeDateFormate:schedule_date];
  10016. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  10017. }
  10018. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  10019. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  10020. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  10021. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  10022. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  10023. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  10024. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  10025. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  10026. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  10027. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  10028. if (![total_price isEqualToString:@""]) {
  10029. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  10030. } else {
  10031. total_price = @"";
  10032. }
  10033. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  10034. if ([paymentsAndCredits isEqualToString:@""]) {
  10035. paymentsAndCredits = @"";
  10036. } else {
  10037. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  10038. }
  10039. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  10040. if ([handling_fee_value isEqualToString:@""]) {
  10041. handling_fee_value = @"";
  10042. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  10043. handling_fee_value = @"";
  10044. } else {
  10045. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  10046. }
  10047. NSString *handling_fee_placeholder = handling_fee_value;
  10048. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  10049. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  10050. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  10051. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  10052. NSString *lift_gate_placeholder = @"";
  10053. if ([lift_gate_value isEqualToString:@""]) {
  10054. lift_gate_placeholder = @"";
  10055. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  10056. lift_gate_placeholder = @"";
  10057. } else {
  10058. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  10059. }
  10060. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  10061. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  10062. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  10063. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  10064. 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];
  10065. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  10066. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  10067. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  10068. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  10069. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  10070. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  10071. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  10072. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  10073. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  10074. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  10075. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  10076. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  10077. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  10078. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  10079. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  10080. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  10081. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  10082. BOOL number_nil = [credit_card_number isEqualToString:@""];
  10083. if (!number_nil) {
  10084. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  10085. }
  10086. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  10087. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  10088. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  10089. if (!security_code_nil) {
  10090. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  10091. }
  10092. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  10093. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  10094. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  10095. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  10096. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  10097. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  10098. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  10099. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  10100. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  10101. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  10102. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  10103. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  10104. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  10105. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  10106. NSString *contact_id = customer_cid;
  10107. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  10108. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  10109. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  10110. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  10111. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  10112. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  10113. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  10114. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  10115. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  10116. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  10117. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  10118. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  10119. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  10120. //
  10121. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  10122. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  10123. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  10124. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  10125. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  10126. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  10127. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  10128. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  10129. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  10130. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  10131. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  10132. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  10133. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  10134. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  10135. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  10136. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  10137. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  10138. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  10139. 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];
  10140. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  10141. if (receive_cid.length) {
  10142. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  10143. }
  10144. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  10145. if (receive_name.length) {
  10146. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  10147. }
  10148. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  10149. if (receive_ext.length) {
  10150. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  10151. }
  10152. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  10153. if (receive_contact.length) {
  10154. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  10155. }
  10156. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  10157. if (receive_phone.length) {
  10158. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  10159. }
  10160. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  10161. if (receive_email.length) {
  10162. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  10163. }
  10164. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  10165. if (receive_fax.length) {
  10166. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  10167. }
  10168. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  10169. if (sender_cid.length) {
  10170. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  10171. }
  10172. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  10173. if (sender_name.length) {
  10174. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  10175. }
  10176. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  10177. if (sender_ext.length) {
  10178. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  10179. }
  10180. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  10181. if(sender_contact.length) {
  10182. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  10183. }
  10184. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  10185. if (sender_phone.length) {
  10186. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  10187. }
  10188. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  10189. if (sender_fax.length) {
  10190. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  10191. }
  10192. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  10193. if (sender_email.length) {
  10194. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  10195. }
  10196. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  10197. if (shipping_billto_cid.length) {
  10198. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  10199. }
  10200. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  10201. if (shipping_billto_name.length) {
  10202. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  10203. }
  10204. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  10205. if (shipping_billto_ext.length) {
  10206. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  10207. }
  10208. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  10209. if (shipping_billto_contact.length) {
  10210. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  10211. }
  10212. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  10213. if (shipping_billto_phone.length) {
  10214. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  10215. }
  10216. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  10217. if (shipping_billto_fax.length) {
  10218. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  10219. }
  10220. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  10221. if (shipping_billto_email.length) {
  10222. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  10223. }
  10224. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  10225. if (billing_cid.length) {
  10226. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  10227. }
  10228. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  10229. if (billing_name.length) {
  10230. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  10231. }
  10232. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  10233. if (billing_ext.length) {
  10234. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  10235. }
  10236. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  10237. if (billing_contact.length) {
  10238. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  10239. }
  10240. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  10241. if (billing_phone.length) {
  10242. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  10243. }
  10244. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  10245. if (billing_fax.length) {
  10246. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  10247. }
  10248. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  10249. if (billing_email.length) {
  10250. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  10251. }
  10252. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  10253. if (returnto_cid.length) {
  10254. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  10255. }
  10256. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10257. if (returnto_name.length) {
  10258. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10259. }
  10260. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10261. if (returnto_ext.length) {
  10262. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10263. }
  10264. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10265. if (returnto_contact.length) {
  10266. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10267. }
  10268. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10269. if (returnto_phone.length) {
  10270. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10271. }
  10272. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10273. if (returnto_fax.length) {
  10274. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10275. }
  10276. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10277. if (returnto_email.length) {
  10278. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10279. }
  10280. NSString *order_status = @"status = 1,";
  10281. if (submit) {
  10282. order_status = @"status = -11,";
  10283. }
  10284. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10285. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10286. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10287. NSString *sync_sql = @"";
  10288. if (submit) {
  10289. param[@"truck_carrier"]=carrier;
  10290. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10291. [param setValue:sales_rep forKey:@"sales_rep"];
  10292. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10293. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10294. }
  10295. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",schedule_date, order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,scarrier,so_id,sync_sql,so_id];
  10296. DebugLog(@"save order contactSql: %@",contactSql);
  10297. DebugLog(@"save order orderSql: %@",orderSql);
  10298. // int contact_ret = [iSalesDB execSql:contactSql];
  10299. int order_ret = [iSalesDB execSql:orderSql];
  10300. int ret = order_ret;
  10301. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10302. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10303. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10304. // [dic setValue:@"160409" forKey:@"min_ver"];
  10305. [dic setObject:so_id forKey:@"so#"];
  10306. return [RAConvertor dict2data:dic];
  10307. }
  10308. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10309. {
  10310. // id foo = nil;
  10311. // NSMutableArray *a = @[].mutableCopy;
  10312. // [a addObject:foo];
  10313. return [self saveorder:param submit:NO];
  10314. }
  10315. #pragma mark add to cart by name
  10316. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10317. {
  10318. NSString *orderCode = [params objectForKey:@"orderCode"];
  10319. NSDictionary *newParams = @{
  10320. @"product_id" : params[@"product_id_string"],
  10321. @"orderCode" : orderCode,
  10322. @"can_create_backorder":params[@"can_create_backorder"]
  10323. };
  10324. return [self offline_add2cart:[newParams mutableCopy]];
  10325. }
  10326. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10327. {
  10328. NSString *orderCode = [params objectForKey:@"orderCode"];
  10329. NSString *upccode = [params objectForKey:@"upc_code"];
  10330. // product_name = [self translateSingleQuote:product_name];
  10331. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10332. // bool search_upc = [params[@"search_upc"] boolValue];
  10333. sqlite3 *db = [iSalesDB get_db];
  10334. __block NSMutableString *product_id_string = [NSMutableString string];
  10335. // NSString *name = [product_name_array objectAtIndex:i];
  10336. NSString *sql =nil;
  10337. sql= [NSString stringWithFormat:@"select product_id from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upccode,upccode,upccode,upccode];
  10338. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10339. int product_id = sqlite3_column_int(stmt, 0);
  10340. [product_id_string appendFormat:@"%d",product_id];
  10341. }];
  10342. if (!orderCode) {
  10343. orderCode = @"";
  10344. }
  10345. NSDictionary *newParams = @{
  10346. @"product_id" : product_id_string,
  10347. @"orderCode" : orderCode,
  10348. @"can_create_backorder":params[@"can_create_backorder"]
  10349. };
  10350. [iSalesDB close_db:db];
  10351. return [self offline_add2cart:[newParams mutableCopy]];
  10352. }
  10353. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10354. {
  10355. NSString *orderCode = [params objectForKey:@"orderCode"];
  10356. NSString *product_name = [params objectForKey:@"product_name"];
  10357. product_name = [self translateSingleQuote:product_name];
  10358. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10359. bool search_upc = [params[@"search_upc"] boolValue];
  10360. sqlite3 *db = [iSalesDB get_db];
  10361. __block NSMutableString *product_id_string = [NSMutableString string];
  10362. for (int i = 0; i < product_name_array.count; i++) {
  10363. NSString *name = [product_name_array objectAtIndex:i];
  10364. NSString *sql =nil;
  10365. if(search_upc)
  10366. sql= [NSString stringWithFormat:@"select product_id from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",name,name,name,name];
  10367. else
  10368. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10369. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10370. int product_id = sqlite3_column_int(stmt, 0);
  10371. if (i == product_name_array.count - 1) {
  10372. [product_id_string appendFormat:@"%d",product_id];
  10373. } else {
  10374. [product_id_string appendFormat:@"%d,",product_id];
  10375. }
  10376. }];
  10377. }
  10378. if (!orderCode) {
  10379. orderCode = @"";
  10380. }
  10381. NSDictionary *newParams = @{
  10382. @"product_id" : product_id_string,
  10383. @"orderCode" : orderCode,
  10384. @"can_create_backorder":params[@"can_create_backorder"]
  10385. };
  10386. [iSalesDB close_db:db];
  10387. return [self offline_add2cart:[newParams mutableCopy]];
  10388. }
  10389. #pragma mark reset order
  10390. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10391. {
  10392. // UIApplication * app = [UIApplication sharedApplication];
  10393. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10394. [iSalesDB disable_trigger];
  10395. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10396. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10397. [iSalesDB enable_trigger];
  10398. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10399. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10400. return [RAConvertor dict2data:dic];
  10401. }
  10402. #pragma mark submit order
  10403. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10404. {
  10405. return [self saveorder:params submit:YES];
  10406. }
  10407. #pragma mark copy order
  10408. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10409. {
  10410. NSMutableDictionary *ret = @{}.mutableCopy;
  10411. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10412. sqlite3 *db = [iSalesDB get_db];
  10413. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10414. // 首先查看联系人是否active
  10415. 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];
  10416. __block int customer_is_active = 1;
  10417. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10418. customer_is_active = sqlite3_column_int(stmt, 0);
  10419. }];
  10420. if (!customer_is_active) {
  10421. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10422. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10423. [iSalesDB close_db:db];
  10424. return [RAConvertor dict2data:ret];
  10425. }
  10426. // new order
  10427. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10428. NSString *new_order_code = [self get_offline_soid:db];
  10429. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10430. user = [self translateSingleQuote:user];
  10431. 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
  10432. __block int result = 1;
  10433. result = [iSalesDB execSql:insert_order_sql db:db];
  10434. if (!result) {
  10435. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10436. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10437. [iSalesDB close_db:db];
  10438. return [RAConvertor dict2data:ret];
  10439. }
  10440. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10441. // __block NSString *product_id = @"";
  10442. __weak typeof(self) weakSelf = self;
  10443. 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];
  10444. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10445. int is_active = sqlite3_column_int(stmt, 1);
  10446. if (is_active) {
  10447. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10448. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10449. 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];
  10450. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10451. }
  10452. }];
  10453. // product_id = [product_id substringFromIndex:1];
  10454. //
  10455. // [self offline_add2cart:@{}.mutableCopy];
  10456. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10457. [iSalesDB close_db:db];
  10458. if (result) {
  10459. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10460. } else {
  10461. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10462. }
  10463. [ret setObject:new_order_code forKey:@"so_id"];
  10464. return [RAConvertor dict2data:ret];
  10465. }
  10466. #pragma mark move wish list to cart
  10467. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10468. assert(params[@"can_create_backorder"]!=nil);
  10469. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10470. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10471. NSString *collectId = params[@"collectId"];
  10472. 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];
  10473. __block NSString *product_id = @"";
  10474. __block NSString *qty = @"";
  10475. __block int number_of_outOfStock = 0;
  10476. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10477. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10478. int productId = sqlite3_column_int(stmt, 0);
  10479. int item_qty = sqlite3_column_int(stmt, 1);
  10480. int availability = sqlite3_column_int(stmt, 2);
  10481. int _id = sqlite3_column_int(stmt, 3);
  10482. if (![params[@"can_create_backorder"] boolValue]) {
  10483. // 库存小于购买量为缺货
  10484. if (availability >= item_qty) {
  10485. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10486. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10487. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10488. } else {
  10489. number_of_outOfStock++;
  10490. }
  10491. } else {
  10492. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10493. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10494. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10495. }
  10496. }];
  10497. NSMutableDictionary *retDic = nil;
  10498. if (![params[@"can_create_backorder"] boolValue]) {
  10499. if (delete_collectId.count == 0) {
  10500. retDic = [NSMutableDictionary dictionary];
  10501. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10502. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10503. return [RAConvertor dict2data:retDic];
  10504. }
  10505. }
  10506. if (product_id.length > 1) {
  10507. product_id = [product_id substringFromIndex:1];
  10508. }
  10509. if (qty.length > 1) {
  10510. qty = [qty substringFromIndex:1];
  10511. }
  10512. NSString *orderCode = params[@"orderCode"];
  10513. if (!orderCode) {
  10514. orderCode = @"";
  10515. }
  10516. NSDictionary *newParams = @{
  10517. @"product_id" : product_id,
  10518. @"orderCode" : orderCode,
  10519. @"qty" : qty,
  10520. @"can_create_backorder":params[@"can_create_backorder"]
  10521. };
  10522. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10523. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10524. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10525. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10526. NSInteger ret = [wish_return[@"result"] intValue];
  10527. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10528. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10529. }
  10530. if (![params[@"can_create_backorder"] boolValue]) {
  10531. if (number_of_outOfStock > 0) {
  10532. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10533. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10534. }
  10535. }
  10536. return [RAConvertor dict2data:retDic];
  10537. }
  10538. #pragma mark - portfolio
  10539. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10540. // assert(params[@"contact_id"]!=nil);
  10541. assert(params[@"user"]!=nil);
  10542. assert(params[@"can_see_price"]!=nil);
  10543. int sort = [[params valueForKey:@"sort"] intValue];
  10544. int offset = [[params valueForKey:@"offset"] intValue];
  10545. int limit = [[params valueForKey:@"limit"] intValue];
  10546. NSString *orderBy = @"";
  10547. switch (sort) {
  10548. case 0:{
  10549. orderBy = @"p.modify_time desc";
  10550. }
  10551. break;
  10552. case 1:{
  10553. orderBy = @"modify_time asc";
  10554. }
  10555. break;
  10556. case 2:{
  10557. orderBy = @"p.name asc";
  10558. }
  10559. break;
  10560. case 3:{
  10561. orderBy = @"p.name desc";
  10562. }
  10563. break;
  10564. case 4:{
  10565. orderBy = @"p.description asc";
  10566. }
  10567. break;
  10568. case 5: {
  10569. orderBy = @"m.default_category asc";
  10570. }
  10571. default:
  10572. break;
  10573. }
  10574. // 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];
  10575. 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];
  10576. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10577. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10578. sqlite3 *db = [iSalesDB get_db];
  10579. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10580. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10581. int product_id = sqlite3_column_int(stmt, 0);
  10582. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10583. NSString *name = [self textAtColumn:1 statement:stmt];
  10584. NSString *description = [self textAtColumn:2 statement:stmt];
  10585. double price = sqlite3_column_double(stmt, 3);
  10586. double discount = sqlite3_column_double(stmt,4);
  10587. int qty = sqlite3_column_int(stmt, 5);
  10588. int percentage = sqlite3_column_int(stmt, 6);
  10589. int item_id = sqlite3_column_int(stmt, 7);
  10590. // int fashion_id = sqlite3_column_int(stmt, 8);
  10591. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10592. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10593. double percent = sqlite3_column_double(stmt, 11);
  10594. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10595. if ([price_null isEqualToString:@"null"]) {
  10596. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10597. }
  10598. NSMutableDictionary *item = @{
  10599. @"linenotes": line_note,
  10600. @"check": @(1),
  10601. @"product_id": product_id_string,
  10602. @"available_qty": @(qty),
  10603. @"available_percent" : @(percent),
  10604. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10605. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10606. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10607. @"img": img_path,
  10608. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10609. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10610. }.mutableCopy;
  10611. if (percentage) {
  10612. [item removeObjectForKey:@"available_qty"];
  10613. } else {
  10614. [item removeObjectForKey:@"available_percent"];
  10615. }
  10616. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10617. if ([qty_null isEqualToString:@"null"]) {
  10618. [item removeObjectForKey:@"available_qty"];
  10619. }
  10620. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10621. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10622. }];
  10623. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10624. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10625. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10626. }
  10627. [iSalesDB close_db:db];
  10628. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10629. [dic setValue:@"" forKey:@"email_content"];
  10630. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10631. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10632. return [RAConvertor dict2data:dic];
  10633. }
  10634. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10635. __block int qty = 0;
  10636. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10637. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10638. qty = sqlite3_column_int(stmt, 0);
  10639. }];
  10640. return qty;
  10641. }
  10642. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10643. {
  10644. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10645. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10646. // NSMutableDictionary * values = params[@"replaceValue"];
  10647. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10648. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10649. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10650. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10651. NSString *pdf_path = @"";
  10652. if (direct) {
  10653. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10654. } else {
  10655. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10656. }
  10657. NSString *create_user = [self valueInParams:params key:@"user"];
  10658. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10659. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10660. // model info
  10661. // 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];
  10662. // V1.90 more color
  10663. 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];
  10664. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10665. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10666. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10667. sqlite3 *db = [iSalesDB get_db];
  10668. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10669. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10670. int product_id = sqlite3_column_int(stmt, 0);
  10671. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10672. double price = sqlite3_column_double(stmt, 1);
  10673. double discount = sqlite3_column_double(stmt,2);
  10674. int qty = sqlite3_column_int(stmt, 3);
  10675. int percentage = sqlite3_column_int(stmt, 4);
  10676. int item_id = sqlite3_column_int(stmt, 5);
  10677. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10678. double percent = sqlite3_column_double(stmt, 7);
  10679. int more_color = sqlite3_column_int(stmt, 8);
  10680. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10681. /* if ([price_null isEqualToString:@"null"]) {
  10682. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10683. }
  10684. */
  10685. [product_ids_string appendFormat:@"%@,",product_id_string];
  10686. // Regular Price
  10687. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10688. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10689. // QTY
  10690. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10691. if ([qty_null isEqualToString:@"null"]) {
  10692. // 查available
  10693. qty = [self model_QTY:product_id_string db:db];
  10694. }
  10695. if (percentage) {
  10696. qty = qty * percent / 100;
  10697. }
  10698. // Special Price
  10699. if ([price_null isEqualToString:@"null"]) {
  10700. // price = regular price
  10701. price = [regular_price_str doubleValue];
  10702. }
  10703. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10704. if (![discount_null isEqualToString:@"null"]) {
  10705. price = price * (1 - discount / 100.0);
  10706. }
  10707. NSMutableDictionary *item = @{
  10708. @"line_note": line_note,
  10709. @"product_id": product_id_string,
  10710. @"available_qty": @(qty),
  10711. @"more_color":@(more_color),
  10712. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10713. @"regular_price" : regular_price_str,
  10714. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10715. }.mutableCopy;
  10716. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10717. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10718. }];
  10719. [iSalesDB close_db:db];
  10720. if (product_ids_string.length > 0) {
  10721. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10722. }
  10723. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10724. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10725. return [RAConvertor dict2data:resultDictionary];
  10726. }
  10727. NSString *model_info = [RAConvertor dict2string:dic];
  10728. // 创建PDF
  10729. // 在preview情况下保存,则不需要新建了
  10730. if (direct) {
  10731. NSMutableDictionary *tear_sheet_params = params;
  10732. // if (tear_sheet_id) {
  10733. // tear_sheet_params = values;
  10734. // }
  10735. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10736. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10737. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10738. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10739. resultDictionary = pdfInfo.mutableCopy;
  10740. } else { // 创建PDF失败
  10741. return pdfData;
  10742. }
  10743. } else {
  10744. // pdf_path 就是本地路径
  10745. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10746. }
  10747. // 将文件移动到PDF Cache文件夹
  10748. NSString *newPath = [pdf_path lastPathComponent];
  10749. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10750. NSString *cachefolder = [paths objectAtIndex:0];
  10751. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10752. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10753. NSFileManager *fileManager = [NSFileManager defaultManager];
  10754. NSError *error = nil;
  10755. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10756. if (error) { // 移动文件失败
  10757. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10758. return [RAConvertor dict2data:resultDictionary];
  10759. }
  10760. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10761. pdf_path = [newPath lastPathComponent];
  10762. // 保存信息
  10763. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10764. NSString *off_params = [RAConvertor dict2string:params];
  10765. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10766. pdf_path = [self translateSingleQuote:pdf_path];
  10767. create_user = [self translateSingleQuote:create_user];
  10768. tear_note = [self translateSingleQuote:tear_note];
  10769. tear_name = [self translateSingleQuote:tear_name];
  10770. model_info = [self translateSingleQuote:model_info];
  10771. configureParams = [self translateSingleQuote:configureParams];
  10772. off_params = [self translateSingleQuote:off_params];
  10773. 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];
  10774. if (tear_sheet_id) {
  10775. int _id = [tear_sheet_id intValue];
  10776. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10777. }
  10778. int result = [iSalesDB execSql:save_pdf_sql];
  10779. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10780. //
  10781. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10782. if (remove_Item) {
  10783. // portfolioId
  10784. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10785. }
  10786. return [RAConvertor dict2data:resultDictionary];
  10787. }
  10788. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10789. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10790. }
  10791. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10792. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10793. }
  10794. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10795. int offset = [[params valueForKey:@"offset"] intValue];
  10796. int limit = [[params valueForKey:@"limit"] intValue];
  10797. NSString *keyword = [params valueForKey:@"keyWord"];
  10798. NSString *where = @"where is_delete is null or is_delete = 0";
  10799. if (keyword.length) {
  10800. keyword = [self translateSingleQuote:keyword];
  10801. 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];
  10802. }
  10803. 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
  10804. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10805. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10806. NSString *cachefolder = [paths objectAtIndex:0];
  10807. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10808. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10809. NSString *name = [self textAtColumn:0 statement:stmt];
  10810. NSString *note = [self textAtColumn:1 statement:stmt];
  10811. NSString *time = [self textAtColumn:2 statement:stmt];
  10812. NSString *user = [self textAtColumn:3 statement:stmt];
  10813. NSString *path = [self textAtColumn:4 statement:stmt];
  10814. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10815. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10816. path = [pdfFolder stringByAppendingPathComponent:path];
  10817. BOOL bdir=NO;
  10818. NSFileManager* fileManager = [NSFileManager defaultManager];
  10819. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10820. {
  10821. //pdf文件不存在
  10822. path=nil;
  10823. }
  10824. time = [self changeDateTimeFormate:time];
  10825. time = [time stringByAppendingString:@" PST"];
  10826. int sheet_id = sqlite3_column_int(stmt, 5);
  10827. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10828. item[@"tearsheetsId"]=@(sheet_id);
  10829. item[@"pdf_path"]=path;
  10830. item[@"create_time"]=time;
  10831. item[@"create_user"]=user;
  10832. item[@"tear_note"]=note;
  10833. item[@"tear_name"]=name;
  10834. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10835. item[@"model_info"]=model_info;
  10836. item[@"off_params"]=off_params;
  10837. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10838. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10839. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10840. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10841. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10842. }];
  10843. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10844. return [RAConvertor dict2data:dic];
  10845. }
  10846. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10847. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10848. // NSString *user = [params objectForKey:@"user"];
  10849. // 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];
  10850. 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];
  10851. int result = [iSalesDB execSql:sql];
  10852. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10853. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10854. if (result == RESULT_TRUE) {
  10855. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10856. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10857. sqlite3 *db = [iSalesDB get_db];
  10858. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10859. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10860. [iSalesDB close_db:db];
  10861. }
  10862. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10863. return [RAConvertor dict2data:dic];
  10864. }
  10865. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10866. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10867. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10868. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10869. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10870. NSDictionary *company_item = @{
  10871. @"control": @"text",
  10872. @"keyboard": @"text",
  10873. @"name": @"company_name",
  10874. @"value": COMPANY_FULL_NAME,
  10875. @"aname": @"Company Name"
  10876. };
  10877. [section_0 setObject:company_item forKey:@"item_0"];
  10878. [dic setObject:section_0 forKey:@"section_0"];
  10879. // Regurlar Price
  10880. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10881. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10882. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10883. __block long val_count = 0;
  10884. NSString *sql = @"select name,type,order_by from price order by order_by";
  10885. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10886. NSString *name = [self textAtColumn:0 statement:stmt];
  10887. int type = sqlite3_column_int(stmt, 1);
  10888. int order_by = sqlite3_column_int(stmt, 2);
  10889. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10890. NSDictionary *price_dic = @{
  10891. @"value" : name,
  10892. @"value_id" : [NSNumber numberWithInteger:type],
  10893. @"check" : order_by == 0 ? @(1) : @(0)
  10894. };
  10895. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10896. val_count = ++(*count);
  10897. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10898. val_count = 0;
  10899. }];
  10900. [cadedate setObject:@{@"value" : @"None",
  10901. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10902. val_count++;
  10903. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10904. [price setObject:cadedate forKey:@"cadedate"];
  10905. [section1 setObject:price forKey:@"item_2"];
  10906. [dic setObject:section1 forKey:@"section_1"];
  10907. return [RAConvertor dict2data:dic];
  10908. }
  10909. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10910. NSString *product_id = [params objectForKey:@"fashionId"];
  10911. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10912. __block int result = RESULT_TRUE;
  10913. __block int qty = 0;
  10914. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10915. qty = sqlite3_column_int(stmt, 0);
  10916. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10917. result = RESULT_FALSE;
  10918. }];
  10919. NSMutableDictionary *dic = @{
  10920. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10921. @"mode" : @"Regular Mode",
  10922. @"quantity_available" : @(qty),
  10923. @"result" : @(result),
  10924. }.mutableCopy;
  10925. return [RAConvertor dict2data:dic];
  10926. }
  10927. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10928. NSString *item_ids = [params objectForKey:@"item_id"];
  10929. NSString *line_notes = [params objectForKey:@"notes"];
  10930. NSString *price_str = [params objectForKey:@"price"];
  10931. NSString *discount_str = [params objectForKey:@"discount"];
  10932. NSString *percent = [params objectForKey:@"available_percent"];
  10933. NSString *qty = [params objectForKey:@"available_qty"];
  10934. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10935. int dot = 0;
  10936. if (line_notes) {
  10937. line_notes = [self translateSingleQuote:line_notes];
  10938. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10939. sql = [sql stringByAppendingString:line_notes];
  10940. dot = 1;
  10941. } else {
  10942. line_notes = @"";
  10943. }
  10944. if (price_str) {
  10945. if (dot) {
  10946. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10947. } else {
  10948. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10949. dot = 1;
  10950. }
  10951. sql = [sql stringByAppendingString:price_str];
  10952. } else {
  10953. price_str = @"";
  10954. }
  10955. if (discount_str) {
  10956. if (dot) {
  10957. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10958. } else {
  10959. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10960. dot = 1;
  10961. }
  10962. sql = [sql stringByAppendingString:discount_str];
  10963. } else {
  10964. discount_str = @"";
  10965. }
  10966. if (percent) {
  10967. if (dot) {
  10968. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10969. } else {
  10970. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10971. dot = 1;
  10972. }
  10973. sql = [sql stringByAppendingString:percent];
  10974. } else {
  10975. percent = @"";
  10976. }
  10977. if (qty) {
  10978. if (dot) {
  10979. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10980. } else {
  10981. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10982. dot = 1;
  10983. }
  10984. sql = [sql stringByAppendingString:qty];
  10985. } else {
  10986. qty = @"";
  10987. }
  10988. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10989. sql = [sql stringByAppendingString:where];
  10990. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10991. int result = [iSalesDB execSql:sql];
  10992. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10993. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10994. return [RAConvertor dict2data:dic];
  10995. }
  10996. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10997. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10998. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10999. sqlite3 *db = [iSalesDB get_db];
  11000. 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];
  11001. int result = [iSalesDB execSql:sql db:db];
  11002. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  11003. [dic setObject:@"Regular Mode" forKey:@"mode"];
  11004. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  11005. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  11006. [iSalesDB close_db:db];
  11007. return [RAConvertor dict2data:dic];
  11008. }
  11009. + (void)offline_removePDFWithName:(NSString *)name {
  11010. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  11011. NSString *cachefolder = [paths objectAtIndex:0];
  11012. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  11013. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  11014. NSFileManager *fileManager = [NSFileManager defaultManager];
  11015. [fileManager removeItemAtPath:path error:nil];
  11016. }
  11017. + (void)offline_clear_PDFCache {
  11018. NSFileManager *fileManager = [NSFileManager defaultManager];
  11019. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  11020. NSString *cachefolder = [paths objectAtIndex:0];
  11021. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  11022. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  11023. for (NSString *path in pdf_files) {
  11024. [self offline_removePDFWithName:[path lastPathComponent]];
  11025. }
  11026. }
  11027. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  11028. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  11029. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  11030. NSString *user = [params objectForKey:@"user"];
  11031. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  11032. if (![create_user isEqualToString:user]) {
  11033. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  11034. [dic setObject:@"Regular Mode" forKey:@"mode"];
  11035. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  11036. return [RAConvertor dict2data:dic];
  11037. }
  11038. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  11039. __block int is_local = 0;
  11040. __block NSString *path = @"";
  11041. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11042. is_local = sqlite3_column_int(stmt, 0);
  11043. path = [self textAtColumn:1 statement:stmt];
  11044. }];
  11045. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  11046. if (is_local == 1) {
  11047. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  11048. // 删除文件
  11049. [self offline_removePDFWithName:path];
  11050. }
  11051. int result = [iSalesDB execSql:sql];
  11052. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  11053. [dic setObject:@"Regular Mode" forKey:@"mode"];
  11054. return [RAConvertor dict2data:dic];
  11055. }
  11056. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  11057. {
  11058. // assert(add_params[@"contact_id"]!=nil);
  11059. assert(add_params[@"user"]!=nil);
  11060. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11061. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11062. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11063. NSString* where=@"1=1";
  11064. if (ver!=nil) {
  11065. where=@"is_dirty=1";
  11066. }
  11067. 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];
  11068. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11069. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11070. sqlite3 *db = [iSalesDB get_db];
  11071. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11072. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11073. NSInteger _id = sqlite3_column_int(stmt, 0);
  11074. NSInteger product_id = sqlite3_column_int(stmt, 1);
  11075. NSString *name = [self textAtColumn:2 statement:stmt];
  11076. NSString *desc = [self textAtColumn:3 statement:stmt];
  11077. NSInteger item_id = sqlite3_column_int(stmt, 4);
  11078. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  11079. NSInteger qty = sqlite3_column_int(stmt, 6);
  11080. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  11081. double percent = sqlite3_column_double(stmt, 8);
  11082. double price = sqlite3_column_double(stmt, 9);
  11083. double discount = sqlite3_column_double(stmt, 10);
  11084. NSString *img = [self textAtColumn:11 statement:stmt];
  11085. NSString *line_note = [self textAtColumn:12 statement:stmt];
  11086. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  11087. NSString *create_time = [self textAtColumn:14 statement:stmt];
  11088. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  11089. NSString *price_null = [self textAtColumn:9 statement:stmt];
  11090. if ([price_null isEqualToString:@"null"]) {
  11091. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  11092. }
  11093. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  11094. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  11095. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  11096. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11097. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  11098. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  11099. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  11100. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  11101. if ([qty_null isEqualToString:@"null"]) {
  11102. // [item setValue:@"null" forKey:@"available_qty"];
  11103. } else {
  11104. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  11105. }
  11106. if ([is_percent_null isEqualToString:@"null"]) {
  11107. // [item setValue:@"null" forKey:@"percentage"];
  11108. } else {
  11109. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  11110. }
  11111. if ([percent_null isEqualToString:@"null"]) {
  11112. // [item setValue:@"null" forKey:@"percent"];
  11113. } else {
  11114. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  11115. }
  11116. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  11117. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  11118. [item setValue:name forKey:@"name"];
  11119. [item setValue:desc forKey:@"description"];
  11120. [item setValue:img forKey:@"img"];
  11121. [item setValue:line_note forKey:@"line_note"];
  11122. [item setValue:create_time forKey:@"createtime"];
  11123. [item setValue:modify_time forKey:@"modifytime"];
  11124. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11125. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11126. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11127. }];
  11128. [iSalesDB close_db:db];
  11129. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  11130. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11131. }
  11132. return ret;
  11133. }
  11134. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  11135. {
  11136. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11137. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11138. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11139. NSString* where=@"1=1";
  11140. if (ver!=nil) {
  11141. where=@"is_dirty=1";
  11142. }
  11143. 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];
  11144. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11145. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11146. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11147. NSInteger _id = sqlite3_column_int(stmt, 0);
  11148. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  11149. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  11150. NSString *create_user = [self textAtColumn:3 statement:stmt];
  11151. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  11152. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  11153. NSString *model_info = [self textAtColumn:6 statement:stmt];
  11154. NSString *createtime = [self textAtColumn:7 statement:stmt];
  11155. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  11156. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  11157. NSString *off_params = [self textAtColumn:10 statement:stmt];
  11158. NSString *uuid = [NSUUID UUID].UUIDString;
  11159. int is_delete = sqlite3_column_int(stmt, 11);
  11160. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  11161. int is_local = sqlite3_column_int(stmt, 12);
  11162. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11163. if (![tearsheet_id_null isEqualToString:@"null"]) {
  11164. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  11165. }
  11166. [item setObject:pdf_path forKey:@"pdf_path"];
  11167. [item setObject:create_user forKey:@"create_user"];
  11168. [item setObject:tear_note forKey:@"tear_note"];
  11169. [item setObject:tear_name forKey:@"tear_name"];
  11170. [item setObject:model_info forKey:@"model_info"];
  11171. [item setObject:createtime forKey:@"createtime"];
  11172. [item setObject:modifytime forKey:@"modifytime"];
  11173. [item setObject:urlParams forKey:@"urlParams"];
  11174. [item setObject:off_params forKey:@"off_params"];
  11175. [item setObject:uuid forKey:@"pdf_token"];
  11176. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  11177. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  11178. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  11179. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  11180. } else {
  11181. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11182. }
  11183. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11184. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11185. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  11186. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11187. }];
  11188. return ret;
  11189. }
  11190. #pragma mark 2020
  11191. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11192. {
  11193. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11194. sqlite3 *db = [iSalesDB get_db];
  11195. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  11196. if (contactType) {
  11197. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  11198. } else {
  11199. contactType = @"1 = 1";
  11200. }
  11201. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  11202. DebugLog(@"offline contact list keyword: %@",keyword);
  11203. // advanced search
  11204. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  11205. if (contact_name) {
  11206. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11207. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  11208. } else {
  11209. contact_name = @"";
  11210. }
  11211. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  11212. if (customer_phone) {
  11213. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11214. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  11215. } else {
  11216. customer_phone = @"";
  11217. }
  11218. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  11219. if (customer_fax) {
  11220. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11221. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  11222. } else {
  11223. customer_fax = @"";
  11224. }
  11225. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  11226. if (customer_zipcode) {
  11227. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11228. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  11229. } else {
  11230. customer_zipcode = @"";
  11231. }
  11232. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  11233. if (customer_sales_rep) {
  11234. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11235. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  11236. } else {
  11237. customer_sales_rep = @"";
  11238. }
  11239. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  11240. if (customer_state) {
  11241. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11242. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  11243. } else {
  11244. customer_state = @"";
  11245. }
  11246. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  11247. if (customer_name) {
  11248. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11249. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  11250. } else {
  11251. customer_name = @"";
  11252. }
  11253. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  11254. if (customer_country) {
  11255. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11256. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  11257. } else {
  11258. customer_country = @"";
  11259. }
  11260. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11261. if (customer_cid) {
  11262. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11263. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11264. } else {
  11265. customer_cid = @"";
  11266. }
  11267. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11268. if (customer_city) {
  11269. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11270. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11271. } else {
  11272. customer_city = @"";
  11273. }
  11274. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11275. if (customer_address) {
  11276. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11277. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11278. } else {
  11279. customer_address = @"";
  11280. }
  11281. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11282. if (customer_email) {
  11283. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11284. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11285. } else {
  11286. customer_email = @"";
  11287. }
  11288. NSString *price_name = [params valueForKey:@"price_name"];
  11289. if (price_name) {
  11290. if ([price_name containsString:@","]) {
  11291. // 首先从 price表中查处name
  11292. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11293. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11294. for (int i = 1;i < pArray.count;i++) {
  11295. NSString *p = pArray[i];
  11296. [mutablePStr appendFormat:@" or type = %@ ",p];
  11297. }
  11298. [mutablePStr appendString:@";"];
  11299. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11300. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11301. char *name = (char *)sqlite3_column_text(stmt, 0);
  11302. if (!name)
  11303. name = "";
  11304. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11305. }];
  11306. // 再根据name 拼sql
  11307. NSMutableString *mutable_price_name = [NSMutableString string];
  11308. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11309. for (int i = 1; i < price_name_array.count; i++) {
  11310. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11311. }
  11312. [mutable_price_name appendString:@")"];
  11313. price_name = mutable_price_name;
  11314. } else {
  11315. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11316. if ([price_name isEqualToString:@""]) {
  11317. price_name = @"";
  11318. } else {
  11319. __block NSString *price;
  11320. price_name = [self translateSingleQuote:price_name];
  11321. [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) {
  11322. char *p = (char *)sqlite3_column_text(stmt, 0);
  11323. if (p == NULL) {
  11324. p = "";
  11325. }
  11326. price = [NSString stringWithUTF8String:p];
  11327. }];
  11328. if ([price isEqualToString:@""]) {
  11329. price_name = @"";
  11330. } else {
  11331. price = [self translateSingleQuote:price];
  11332. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11333. }
  11334. }
  11335. }
  11336. } else {
  11337. price_name = @"";
  11338. }
  11339. int limit = [[params valueForKey:@"limit"] intValue];
  11340. int offset = [[params valueForKey:@"offset"] intValue];
  11341. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11342. 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];
  11343. 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];
  11344. // int result= [iSalesDB AddExFunction:db];
  11345. int count =0;
  11346. NSString *sqlQuery = nil;
  11347. if(keyword.length==0)
  11348. {
  11349. // 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];
  11350. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11351. sqlQuery = sql;
  11352. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11353. }
  11354. else
  11355. {
  11356. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11357. keyword = keyword.lowercaseString;
  11358. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11359. 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];
  11360. 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]];
  11361. }
  11362. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11363. sqlite3_stmt * statement;
  11364. [ret setValue:@"2" forKey:@"result"];
  11365. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11366. // 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";
  11367. int i = 0;
  11368. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11369. {
  11370. while (sqlite3_step(statement) == SQLITE_ROW)
  11371. {
  11372. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11373. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11374. int editable = sqlite3_column_int(statement, 0);
  11375. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11376. NSString *nscompany_name =nil;
  11377. if(company_name==nil)
  11378. nscompany_name=@"";
  11379. else
  11380. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11381. char *country = (char*)sqlite3_column_text(statement, 2);
  11382. if(country==nil)
  11383. country="";
  11384. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11385. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11386. // if(addr==nil)
  11387. // addr="";
  11388. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11389. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11390. if(zipcode==nil)
  11391. zipcode="";
  11392. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11393. char *state = (char*)sqlite3_column_text(statement, 5);
  11394. if(state==nil)
  11395. state="";
  11396. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11397. char *city = (char*)sqlite3_column_text(statement, 6);
  11398. if(city==nil)
  11399. city="";
  11400. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11401. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11402. // NSString *nscontact_name = nil;
  11403. // if(contact_name==nil)
  11404. // nscontact_name=@"";
  11405. // else
  11406. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11407. char *phone = (char*)sqlite3_column_text(statement, 8);
  11408. NSString *nsphone = nil;
  11409. if(phone==nil)
  11410. nsphone=@"";
  11411. else
  11412. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11413. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11414. if(contact_id==nil)
  11415. contact_id="";
  11416. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11417. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11418. if(addr_1==nil)
  11419. addr_1="";
  11420. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11421. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11422. if(addr_2==nil)
  11423. addr_2="";
  11424. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11425. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11426. if(addr_3==nil)
  11427. addr_3="";
  11428. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11429. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11430. if(addr_4==nil)
  11431. addr_4="";
  11432. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11433. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11434. if(first_name==nil)
  11435. first_name="";
  11436. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11437. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11438. if(last_name==nil)
  11439. last_name="";
  11440. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11441. char *fax = (char*)sqlite3_column_text(statement, 16);
  11442. NSString *nsfax = nil;
  11443. if(fax==nil)
  11444. nsfax=@"";
  11445. else
  11446. {
  11447. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11448. if(nsfax.length>0)
  11449. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11450. }
  11451. char *email = (char*)sqlite3_column_text(statement, 17);
  11452. NSString *nsemail = nil;
  11453. if(email==nil)
  11454. nsemail=@"";
  11455. else
  11456. {
  11457. nsemail= [[NSString alloc]initWithUTF8String:email];
  11458. if(nsemail.length>0)
  11459. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11460. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11461. }
  11462. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11463. [arr_name addObject:nsfirst_name];
  11464. [arr_name addObject:nslast_name];
  11465. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11466. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11467. {
  11468. // decrypt
  11469. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11470. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11471. nsphone=[AESCrypt fastdecrypt:nsphone];
  11472. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11473. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11474. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11475. }
  11476. [arr_addr addObject:nscompany_name];
  11477. [arr_addr addObject:nscontact_name];
  11478. [arr_addr addObject:@"<br>"];
  11479. [arr_addr addObject:nsaddr_1];
  11480. [arr_addr addObject:nsaddr_2];
  11481. [arr_addr addObject:nsaddr_3];
  11482. [arr_addr addObject:nsaddr_4];
  11483. //[arr_addr addObject:nsaddr];
  11484. [arr_addr addObject:nszipcode];
  11485. [arr_addr addObject:nscity];
  11486. [arr_addr addObject:nsstate];
  11487. [arr_addr addObject:nscountry];
  11488. [arr_addr addObject:@"<br>"];
  11489. [arr_addr addObject:nsphone];
  11490. [arr_addr addObject:nsfax];
  11491. [arr_addr addObject:nsemail];
  11492. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11493. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11494. [item setValue:name forKey:@"name"];
  11495. [item setValue:nscontact_id forKey:@"contact_id"];
  11496. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11497. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11498. i++;
  11499. }
  11500. sqlite3_finalize(statement);
  11501. }
  11502. [iSalesDB close_db:db];
  11503. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11504. dispatch_async(dispatch_get_main_queue(), ^{
  11505. UIApplication * app = [UIApplication sharedApplication];
  11506. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11507. [ret setValue:appDelegate.mode forKey:@"mode"];
  11508. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11509. result(ret);
  11510. });
  11511. return ;
  11512. });
  11513. }
  11514. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11515. //{
  11516. //
  11517. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11518. //
  11519. // NSString* contactId = [params valueForKey:@"contactId"];
  11520. //
  11521. //
  11522. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11523. //
  11524. //
  11525. //
  11526. //
  11527. //
  11528. //
  11529. // sqlite3 *db = [iSalesDB get_db];
  11530. //
  11531. //
  11532. //
  11533. //
  11534. //
  11535. // NSString *sqlQuery = nil;
  11536. //
  11537. //
  11538. // {
  11539. // 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];
  11540. //
  11541. // }
  11542. //
  11543. //
  11544. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11545. // sqlite3_stmt * statement;
  11546. //
  11547. //
  11548. // [ret setValue:@"2" forKey:@"result"];
  11549. //
  11550. //
  11551. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11552. // {
  11553. //
  11554. // //int i = 0;
  11555. // if (sqlite3_step(statement) == SQLITE_ROW)
  11556. // {
  11557. //
  11558. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11559. //
  11560. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11561. //
  11562. //
  11563. // // int editable = sqlite3_column_int(statement, 0);
  11564. //
  11565. //
  11566. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11567. // NSString *nscompany_name =nil;
  11568. // if(company_name==nil)
  11569. // nscompany_name=@"";
  11570. // else
  11571. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11572. //
  11573. //
  11574. // char *country = (char*)sqlite3_column_text(statement, 2);
  11575. // if(country==nil)
  11576. // country="";
  11577. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11578. //
  11579. //
  11580. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11581. // // if(addr==nil)
  11582. // // addr="";
  11583. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11584. //
  11585. //
  11586. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11587. // if(zipcode==nil)
  11588. // zipcode="";
  11589. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11590. //
  11591. //
  11592. // char *state = (char*)sqlite3_column_text(statement, 5);
  11593. // if(state==nil)
  11594. // state="";
  11595. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11596. //
  11597. // char *city = (char*)sqlite3_column_text(statement, 6);
  11598. // if(city==nil)
  11599. // city="";
  11600. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11601. //
  11602. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11603. // // NSString *nscontact_name = nil;
  11604. // // if(contact_name==nil)
  11605. // // nscontact_name=@"";
  11606. // // else
  11607. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11608. //
  11609. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11610. // NSString *nsphone = nil;
  11611. // if(phone==nil)
  11612. // nsphone=@"";
  11613. // else
  11614. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11615. //
  11616. //
  11617. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11618. // // if(contact_id==nil)
  11619. // // contact_id="";
  11620. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11621. //
  11622. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11623. // if(addr_1==nil)
  11624. // addr_1="";
  11625. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11626. //
  11627. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11628. // if(addr_2==nil)
  11629. // addr_2="";
  11630. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11631. //
  11632. //
  11633. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11634. // if(addr_3==nil)
  11635. // addr_3="";
  11636. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11637. //
  11638. //
  11639. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11640. // if(addr_4==nil)
  11641. // addr_4="";
  11642. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11643. //
  11644. //
  11645. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11646. // if(first_name==nil)
  11647. // first_name="";
  11648. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11649. //
  11650. //
  11651. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11652. // if(last_name==nil)
  11653. // last_name="";
  11654. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11655. //
  11656. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11657. // NSString *nsfax = nil;
  11658. // if(fax==nil)
  11659. // nsfax=@"";
  11660. // else
  11661. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11662. //
  11663. // char *email = (char*)sqlite3_column_text(statement, 17);
  11664. // NSString *nsemail = nil;
  11665. // if(email==nil)
  11666. // nsemail=@"";
  11667. // else
  11668. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11669. //
  11670. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11671. // NSString *nsimg_0 = nil;
  11672. // if(img_0==nil)
  11673. // nsimg_0=@"";
  11674. // else
  11675. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11676. //
  11677. // [self copy_bcardImg:nsimg_0];
  11678. //
  11679. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11680. // NSString *nsimg_1 = nil;
  11681. // if(img_1==nil)
  11682. // nsimg_1=@"";
  11683. // else
  11684. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11685. // [self copy_bcardImg:nsimg_1];
  11686. //
  11687. //
  11688. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11689. // NSString *nsimg_2 = nil;
  11690. // if(img_2==nil)
  11691. // nsimg_2=@"";
  11692. // else
  11693. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11694. // [self copy_bcardImg:nsimg_2];
  11695. //
  11696. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11697. // NSString *nsprice_type = nil;
  11698. // if(price_type==nil)
  11699. // nsprice_type=@"";
  11700. // else
  11701. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11702. //
  11703. //
  11704. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11705. // NSString *nsnotes = nil;
  11706. // if(notes==nil)
  11707. // nsnotes=@"";
  11708. // else
  11709. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11710. //
  11711. //
  11712. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11713. // NSString *nssalesrep = nil;
  11714. // if(salesrep==nil)
  11715. // nssalesrep=@"";
  11716. // else
  11717. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11718. //
  11719. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11720. //
  11721. //
  11722. // {
  11723. // // decrypt
  11724. //
  11725. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11726. //
  11727. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11728. //
  11729. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11730. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11731. //
  11732. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11733. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11734. //
  11735. // }
  11736. //
  11737. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11738. //
  11739. // [arr_name addObject:nsfirst_name];
  11740. // [arr_name addObject:nslast_name];
  11741. //
  11742. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11743. //
  11744. //
  11745. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11746. //
  11747. // [arr_ext addObject:nsaddr_1];
  11748. // [arr_ext addObject:nsaddr_2];
  11749. // [arr_ext addObject:nsaddr_3];
  11750. // [arr_ext addObject:nsaddr_4];
  11751. // [arr_ext addObject:@"\r\n"];
  11752. //
  11753. // [arr_ext addObject:nscity];
  11754. // [arr_ext addObject:nsstate];
  11755. // [arr_ext addObject:nszipcode];
  11756. // [arr_ext addObject:nscountry];
  11757. //
  11758. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11759. //
  11760. //
  11761. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11762. //
  11763. //
  11764. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11765. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11766. // [item setValue:nscountry forKey:@"customer_country"];
  11767. // [item setValue:nsphone forKey:@"customer_phone"];
  11768. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11769. // [item setValue:nscompany_name forKey:@"customer_name"];
  11770. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11771. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11772. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11773. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11774. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11775. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11776. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11777. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11778. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11779. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11780. // [item setValue:nscity forKey:@"customer_city"];
  11781. // [item setValue:nsstate forKey:@"customer_state"];
  11782. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11783. // [item setValue:contactId forKey:@"customer_cid"];
  11784. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11785. // [item setValue:nsfax forKey:@"customer_fax"];
  11786. // [item setValue:nsemail forKey:@"customer_email"];
  11787. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11788. //
  11789. //
  11790. // [ret setObject:item forKey:@"customerInfo"];
  11791. // // i++;
  11792. //
  11793. //
  11794. //
  11795. // }
  11796. //
  11797. //
  11798. //
  11799. //
  11800. // sqlite3_finalize(statement);
  11801. // }
  11802. //
  11803. //
  11804. //
  11805. // [iSalesDB close_db:db];
  11806. //
  11807. //
  11808. //
  11809. //
  11810. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11811. // dispatch_async(dispatch_get_main_queue(), ^{
  11812. // UIApplication * app = [UIApplication sharedApplication];
  11813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11814. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11815. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11816. // result(ret);
  11817. // });
  11818. //
  11819. // return ;
  11820. // });
  11821. //}
  11822. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11823. {
  11824. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11825. NSString* order_code= appDelegate.order_code;
  11826. NSString* user = appDelegate.user;
  11827. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11828. NSString* category = [params valueForKey:@"category"];
  11829. if (!category || [category isEqualToString:@""]) {
  11830. category = @"%";
  11831. }
  11832. category = [self translateSingleQuote:category];
  11833. int limit = [[params valueForKey:@"limit"] intValue];
  11834. int offset = [[params valueForKey:@"offset"] intValue];
  11835. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11836. NSString *limit_str = @"";
  11837. if (limited) {
  11838. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11839. }
  11840. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11841. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11842. sqlite3 *db = [iSalesDB get_db];
  11843. // [iSalesDB AddExFunction:db];
  11844. int count;
  11845. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11846. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",order_code, category,limit_str];
  11847. double price_min = 0;
  11848. double price_max = 0;
  11849. if ([params.allKeys containsObject:@"alert"]) {
  11850. // alert
  11851. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11852. NSString *alert = params[@"alert"];
  11853. if ([alert isEqualToString:@"Display All"]) {
  11854. alert = [NSString stringWithFormat:@""];
  11855. } else {
  11856. alert = [self translateSingleQuote:alert];
  11857. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11858. }
  11859. // available
  11860. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11861. NSString *available = params[@"available"];
  11862. NSString *available_condition;
  11863. if ([available isEqualToString:@"Display All"]) {
  11864. available_condition = @"";
  11865. } else if ([available isEqualToString:@"Available Now"]) {
  11866. available_condition = @"and availability > 0";
  11867. } else {
  11868. available_condition = @"and availability == 0";
  11869. }
  11870. // best seller
  11871. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11872. NSString *best_seller = @"";
  11873. NSString *order_best_seller = @"m.name asc";
  11874. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11875. best_seller = @"and best_seller > 0";
  11876. order_best_seller = @"m.best_seller desc,m.name asc";
  11877. }
  11878. // price
  11879. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11880. NSString *price = params[@"price"];
  11881. price_min = 0;
  11882. price_max = MAXFLOAT;
  11883. if (user && price != nil) {
  11884. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11885. NSMutableString *priceName = [NSMutableString string];
  11886. for (int i = 0; i < priceTypeArray.count; i++) {
  11887. NSString *pricetype = priceTypeArray[i];
  11888. pricetype = [self translateSingleQuote:pricetype];
  11889. if (i == 0) {
  11890. [priceName appendFormat:@"'%@'",pricetype];
  11891. } else {
  11892. [priceName appendFormat:@",'%@'",pricetype];
  11893. }
  11894. }
  11895. if ([price isEqualToString:@"Display All"]) {
  11896. price = [NSString stringWithFormat:@""];
  11897. } else if([price containsString:@"+"]){
  11898. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11899. price_min = [price doubleValue];
  11900. 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];
  11901. } else {
  11902. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11903. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11904. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11905. 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];
  11906. }
  11907. } else {
  11908. price = @"";
  11909. }
  11910. // sold_by_qty : Sold in quantities of %@
  11911. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11912. NSString *qty = params[@"sold_by_qty"];
  11913. if ([qty isEqualToString:@"Display All"]) {
  11914. qty = @"";
  11915. } else {
  11916. qty = [self translateSingleQuote:qty];
  11917. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11918. }
  11919. // cate
  11920. // category = [self translateSingleQuote:category];
  11921. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11922. // cate mutiple selection
  11923. NSString *category_id = params[@"category"];
  11924. NSMutableArray *cate_id_array = nil;
  11925. NSMutableString *cateWhere = [NSMutableString string];
  11926. if ([category_id isEqualToString:@""] || !category_id) {
  11927. [cateWhere appendString:@"1 = 1"];
  11928. } else {
  11929. if ([category_id containsString:@","]) {
  11930. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11931. } else {
  11932. cate_id_array = [@[category_id] mutableCopy];
  11933. }
  11934. [cateWhere appendString:@"("];
  11935. for (int i = 0; i < cate_id_array.count; i++) {
  11936. if (i == 0) {
  11937. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11938. } else {
  11939. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11940. }
  11941. }
  11942. [cateWhere appendString:@")"];
  11943. }
  11944. // where bestseller > 0 order by bestseller desc
  11945. // sql query: alert availability(int) best_seller(int) price qty
  11946. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,order_code,order_best_seller,limit_str];
  11947. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11948. }
  11949. DebugLog(@"offline category where: %@",where);
  11950. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11951. if (!user) {
  11952. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11953. }
  11954. [ret setValue:filter forKey:@"filter"];
  11955. DebugLog(@"offline_category sql:%@",sqlQuery);
  11956. sqlite3_stmt * statement;
  11957. [ret setValue:@"2" forKey:@"result"];
  11958. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11959. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11960. // int count=0;
  11961. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11962. {
  11963. int i=0;
  11964. while (sqlite3_step(statement) == SQLITE_ROW)
  11965. {
  11966. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11967. char *name = (char*)sqlite3_column_text(statement, 0);
  11968. if(name==nil)
  11969. name="";
  11970. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11971. char *description = (char*)sqlite3_column_text(statement, 1);
  11972. if(description==nil)
  11973. description="";
  11974. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11975. int product_id = sqlite3_column_int(statement, 2);
  11976. int wid = sqlite3_column_int(statement, 3);
  11977. int closeout = sqlite3_column_int(statement, 4);
  11978. int cid = sqlite3_column_int(statement, 5);
  11979. int wisdelete = sqlite3_column_int(statement, 6);
  11980. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11981. int more_color = sqlite3_column_int(statement, 8);
  11982. // Defaul Category ID
  11983. __block NSString *categoryID = nil;
  11984. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11985. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11986. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11987. if(default_category==nil)
  11988. default_category="";
  11989. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11990. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11991. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11992. categoryID = nsdefault_category;
  11993. }];
  11994. if (!categoryID.length) {
  11995. NSString *cateIDs = params[@"category"];
  11996. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11997. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11998. for (NSString *cateID in requestCategoryArr) {
  11999. BOOL needBreak = NO;
  12000. for (NSString *itemCateIDBox in itemCategoryArr) {
  12001. if (itemCateIDBox.length > 4) {
  12002. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  12003. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  12004. if ([itemCategoryID isEqualToString:cateID]) {
  12005. needBreak = YES;
  12006. categoryID = itemCategoryID;
  12007. break;
  12008. }
  12009. }
  12010. }
  12011. if (needBreak) {
  12012. break;
  12013. }
  12014. }
  12015. }
  12016. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12017. if(wid !=0 && wisdelete != 1)
  12018. [item setValue:@"true" forKey:@"wish_exists"];
  12019. else
  12020. [item setValue:@"false" forKey:@"wish_exists"];
  12021. if(closeout==0)
  12022. [item setValue:@"false" forKey:@"is_closeout"];
  12023. else
  12024. [item setValue:@"true" forKey:@"is_closeout"];
  12025. if(cid==0)
  12026. [item setValue:@"false" forKey:@"cart_exists"];
  12027. else
  12028. [item setValue:@"true" forKey:@"cart_exists"];
  12029. if (more_color == 0) {
  12030. [item setObject:@(false) forKey:@"more_color"];
  12031. } else if (more_color == 1) {
  12032. [item setObject:@(true) forKey:@"more_color"];
  12033. }
  12034. [item addEntriesFromDictionary:imgjson];
  12035. // [item setValue:nsurl forKey:@"img"];
  12036. [item setValue:nsname forKey:@"name"];
  12037. [item setValue:nsdescription forKey:@"description"];
  12038. if (categoryID) {
  12039. [item setValue:categoryID forKey:@"item_category_id"];
  12040. }
  12041. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12042. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12043. i++;
  12044. }
  12045. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12046. [ret setObject:items forKey:@"items"];
  12047. sqlite3_finalize(statement);
  12048. } else {
  12049. DebugLog(@"nothing...");
  12050. }
  12051. DebugLog(@"count:%d",count);
  12052. [iSalesDB close_db:db];
  12053. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12054. dispatch_async(dispatch_get_main_queue(), ^{
  12055. // UIApplication * app = [UIApplication sharedApplication];
  12056. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12057. // [ret setValue:appDelegate.mode forKey:@"mode"];
  12058. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  12059. result(ret);
  12060. });
  12061. });
  12062. // NSString* orderCode = [params valueForKey:@"orderCode"];
  12063. }
  12064. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12065. {
  12066. [self categoryList:params limited:YES completionHandler:result];
  12067. }
  12068. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12069. {
  12070. UIApplication * app = [UIApplication sharedApplication];
  12071. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12072. NSString* order_code = appDelegate.order_code;
  12073. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12074. NSString* keyword = [params valueForKey:@"keyword"];
  12075. keyword=keyword.lowercaseString;
  12076. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  12077. int limit = [[params valueForKey:@"limit"] intValue];
  12078. int offset = [[params valueForKey:@"offset"] intValue];
  12079. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12080. NSString *limit_str = @"";
  12081. if (limited) {
  12082. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  12083. }
  12084. sqlite3 *db = [iSalesDB get_db];
  12085. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  12086. NSString *sqlQuery = nil;
  12087. if(exactMatch )
  12088. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  12089. else
  12090. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",order_code, keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  12091. DebugLog(@"offline_search sql:%@",sqlQuery);
  12092. sqlite3_stmt * statement;
  12093. [ret setValue:@"2" forKey:@"result"];
  12094. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12095. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12096. // int count=0;
  12097. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12098. {
  12099. int i=0;
  12100. while (sqlite3_step(statement) == SQLITE_ROW)
  12101. {
  12102. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12103. // char *name = (char*)sqlite3_column_text(statement, 1);
  12104. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12105. char *name = (char*)sqlite3_column_text(statement, 0);
  12106. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12107. char *description = (char*)sqlite3_column_text(statement, 1);
  12108. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12109. int product_id = sqlite3_column_int(statement, 2);
  12110. // char *url = (char*)sqlite3_column_text(statement, 3);
  12111. // if(url==nil)
  12112. // url="";
  12113. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12114. int wid = sqlite3_column_int(statement, 3);
  12115. int closeout = sqlite3_column_int(statement, 4);
  12116. int cid = sqlite3_column_int(statement, 5);
  12117. int wisdelete = sqlite3_column_int(statement, 6);
  12118. int more_color = sqlite3_column_int(statement, 7);
  12119. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12120. if(wid !=0 && wisdelete != 1)
  12121. [item setValue:@"true" forKey:@"wish_exists"];
  12122. else
  12123. [item setValue:@"false" forKey:@"wish_exists"];
  12124. if(closeout==0)
  12125. [item setValue:@"false" forKey:@"is_closeout"];
  12126. else
  12127. [item setValue:@"true" forKey:@"is_closeout"];
  12128. if(cid==0)
  12129. [item setValue:@"false" forKey:@"cart_exists"];
  12130. else
  12131. [item setValue:@"true" forKey:@"cart_exists"];
  12132. if (more_color == 0) {
  12133. [item setObject:@(false) forKey:@"more_color"];
  12134. } else if (more_color == 1) {
  12135. [item setObject:@(true) forKey:@"more_color"];
  12136. }
  12137. [item addEntriesFromDictionary:imgjson];
  12138. // [item setValue:nsurl forKey:@"img"];
  12139. [item setValue:nsname forKey:@"fash_name"];
  12140. [item setValue:nsdescription forKey:@"description"];
  12141. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12142. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12143. i++;
  12144. }
  12145. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12146. [ret setObject:items forKey:@"items"];
  12147. sqlite3_finalize(statement);
  12148. }
  12149. DebugLog(@"count:%d",count);
  12150. [iSalesDB close_db:db];
  12151. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12152. dispatch_async(dispatch_get_main_queue(), ^{
  12153. result(ret);
  12154. });
  12155. });
  12156. }
  12157. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12158. {
  12159. [self search:params limited:YES completionHandler:result];
  12160. }
  12161. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12162. //{
  12163. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  12164. // NSString* order_code = appDelegate.order_code;
  12165. // NSString* user = appDelegate.user;
  12166. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12167. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  12168. //
  12169. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  12170. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  12171. //
  12172. // // category
  12173. // NSDictionary *category_menu = [self offline_category_menu];
  12174. // [filter setValue:category_menu forKey:@"category"];
  12175. //
  12176. // NSString* where= nil;
  12177. // NSString* orderby= @"m.name";
  12178. // if (!filterSearch) {
  12179. // int covertype = [[params valueForKey:@"covertype"] intValue];
  12180. //
  12181. // switch (covertype) {
  12182. // case 0:
  12183. // {
  12184. // where=@"m.category like'%%#005#%%'";
  12185. // break;
  12186. // }
  12187. // case 1:
  12188. // {
  12189. // where=@"m.alert like '%QS%'";
  12190. // break;
  12191. // }
  12192. // case 2:
  12193. // {
  12194. // where=@"m.availability>0";
  12195. // break;
  12196. // }
  12197. // case 3:
  12198. // {
  12199. // where=@"m.best_seller>0";
  12200. // orderby=@"m.best_seller desc,m.name asc";
  12201. // break;
  12202. // }
  12203. // default:
  12204. // where=@"1=1";
  12205. // break;
  12206. // }
  12207. //
  12208. // }
  12209. //
  12210. //
  12211. // int limit = [[params valueForKey:@"limit"] intValue];
  12212. // int offset = [[params valueForKey:@"offset"] intValue];
  12213. //
  12214. // NSString *limit_str = @"";
  12215. // if (limited) {
  12216. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  12217. // }
  12218. //
  12219. //
  12220. // sqlite3 *db = [iSalesDB get_db];
  12221. // // [iSalesDB AddExFunction:db];
  12222. //
  12223. // int count;
  12224. //
  12225. // NSString *sqlQuery = nil;
  12226. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  12227. // sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",order_code, where,orderby,limit_str];
  12228. //
  12229. //
  12230. // double price_min = 0;
  12231. // double price_max = 0;
  12232. // if (filterSearch) {
  12233. // // alert
  12234. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  12235. // NSString *alert = params[@"alert"];
  12236. // if ([alert isEqualToString:@"Display All"]) {
  12237. // alert = [NSString stringWithFormat:@""];
  12238. // } else {
  12239. // alert = [self translateSingleQuote:alert];
  12240. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  12241. // }
  12242. //
  12243. // // available
  12244. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  12245. // NSString *available = params[@"available"];
  12246. // NSString *available_condition;
  12247. // if ([available isEqualToString:@"Display All"]) {
  12248. // available_condition = @"";
  12249. // } else if ([available isEqualToString:@"Available Now"]) {
  12250. // available_condition = @"and availability > 0";
  12251. // } else {
  12252. // available_condition = @"and availability == 0";
  12253. // }
  12254. //
  12255. // // best seller
  12256. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  12257. // NSString *best_seller = @"";
  12258. // NSString *order_best_seller = @"m.name asc";
  12259. //
  12260. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12261. // best_seller = @"and best_seller > 0";
  12262. // order_best_seller = @"m.best_seller desc,m.name asc";
  12263. // }
  12264. //
  12265. // // price
  12266. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12267. // NSString *price = params[@"price"];
  12268. // price_min = 0;
  12269. // price_max = MAXFLOAT;
  12270. // if (user) {
  12271. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12272. // NSMutableString *priceName = [NSMutableString string];
  12273. // for (int i = 0; i < priceTypeArray.count; i++) {
  12274. // NSString *pricetype = priceTypeArray[i];
  12275. // pricetype = [self translateSingleQuote:pricetype];
  12276. // if (i == 0) {
  12277. // [priceName appendFormat:@"'%@'",pricetype];
  12278. // } else {
  12279. // [priceName appendFormat:@",'%@'",pricetype];
  12280. // }
  12281. // }
  12282. //
  12283. // if ([price isEqualToString:@"Display All"]) {
  12284. // price = [NSString stringWithFormat:@""];
  12285. // } else if([price containsString:@"+"]){
  12286. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12287. // price_min = [price doubleValue];
  12288. // 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];
  12289. // } else {
  12290. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12291. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12292. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12293. // 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];
  12294. // }
  12295. // } else {
  12296. // price = @"";
  12297. // }
  12298. //
  12299. // // sold_by_qty : Sold in quantities of %@
  12300. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12301. // NSString *qty = params[@"sold_by_qty"];
  12302. // if ([qty isEqualToString:@"Display All"]) {
  12303. // qty = @"";
  12304. // } else {
  12305. // qty = [self translateSingleQuote:qty];
  12306. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12307. // }
  12308. //
  12309. // // category
  12310. // NSString *category_id = params[@"ctgId"];
  12311. // NSMutableArray *cate_id_array = nil;
  12312. // NSMutableString *cateWhere = [NSMutableString string];
  12313. //
  12314. // if ([category_id isEqualToString:@""] || !category_id) {
  12315. // [cateWhere appendString:@"1 = 1"];
  12316. // } else {
  12317. //
  12318. // if ([category_id containsString:@","]) {
  12319. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12320. // } else {
  12321. // cate_id_array = [@[category_id] mutableCopy];
  12322. // }
  12323. // /*-----------*/
  12324. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12325. //
  12326. // [cateWhere appendString:@"("];
  12327. // for (int i = 0; i < cate_id_array.count; i++) {
  12328. //
  12329. // for (NSString *key0 in cateDic.allKeys) {
  12330. //
  12331. // if ([key0 containsString:@"category_"]) {
  12332. //
  12333. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12334. //
  12335. // for (NSString *key1 in category0.allKeys) {
  12336. //
  12337. // if ([key1 containsString:@"category_"]) {
  12338. //
  12339. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12340. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12341. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12342. //
  12343. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12344. // if (i == 0) {
  12345. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12346. // } else {
  12347. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12348. // }
  12349. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12350. // [category0 setValue:category1 forKey:key1];
  12351. // [cateDic setValue:category0 forKey:key0];
  12352. //
  12353. // }
  12354. //
  12355. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12356. //
  12357. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12358. //
  12359. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12360. // if (i == 0) {
  12361. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12362. // } else {
  12363. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12364. // }
  12365. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12366. // [cateDic setValue:category0 forKey:key0];
  12367. //
  12368. // }
  12369. //
  12370. // }
  12371. //
  12372. // }
  12373. //
  12374. // }
  12375. //
  12376. // }
  12377. //
  12378. // }
  12379. // [cateWhere appendString:@")"];
  12380. //
  12381. // [filter setValue:cateDic forKey:@"category"];
  12382. // }
  12383. //
  12384. // // where bestseller > 0 order by bestseller desc
  12385. // // sql query: alert availability(int) best_seller(int) price qty
  12386. //
  12387. // sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,order_code,order_best_seller,limit_str];
  12388. //
  12389. //
  12390. // // count
  12391. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12392. //
  12393. // }
  12394. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12395. //
  12396. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12397. //
  12398. //
  12399. //
  12400. //
  12401. // if (!user) {
  12402. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12403. // }
  12404. //
  12405. // [ret setValue:filter forKey:@"filter"];
  12406. //
  12407. //
  12408. //
  12409. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12410. //
  12411. // sqlite3_stmt * statement;
  12412. // [ret setValue:@"2" forKey:@"result"];
  12413. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12414. //
  12415. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12416. // // int count=0;
  12417. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12418. // {
  12419. //
  12420. // int i=0;
  12421. // while (sqlite3_step(statement) == SQLITE_ROW)
  12422. // {
  12423. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12424. //
  12425. //
  12426. // char *name = (char*)sqlite3_column_text(statement, 0);
  12427. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12428. //
  12429. // char *description = (char*)sqlite3_column_text(statement, 1);
  12430. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12431. //
  12432. //
  12433. //
  12434. // int product_id = sqlite3_column_int(statement, 2);
  12435. //
  12436. //
  12437. // int wid = sqlite3_column_int(statement, 3);
  12438. // int closeout = sqlite3_column_int(statement, 4);
  12439. // int cid = sqlite3_column_int(statement, 5);
  12440. // int wisdelete = sqlite3_column_int(statement, 6);
  12441. // int more_color = sqlite3_column_int(statement, 7);
  12442. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12443. //
  12444. // if(wid !=0 && wisdelete != 1)
  12445. // [item setValue:@"true" forKey:@"wish_exists"];
  12446. // else
  12447. // [item setValue:@"false" forKey:@"wish_exists"];
  12448. //
  12449. // if(closeout==0)
  12450. // [item setValue:@"false" forKey:@"is_closeout"];
  12451. // else
  12452. // [item setValue:@"true" forKey:@"is_closeout"];
  12453. //
  12454. // if(cid==0)
  12455. // [item setValue:@"false" forKey:@"cart_exists"];
  12456. // else
  12457. // [item setValue:@"true" forKey:@"cart_exists"];
  12458. //
  12459. // if (more_color == 0) {
  12460. // [item setObject:@(false) forKey:@"more_color"];
  12461. // } else if (more_color == 1) {
  12462. // [item setObject:@(true) forKey:@"more_color"];
  12463. // }
  12464. //
  12465. // [item addEntriesFromDictionary:imgjson];
  12466. //
  12467. //
  12468. // // [item setValue:nsurl forKey:@"img"];
  12469. // [item setValue:nsname forKey:@"fash_name"];
  12470. // [item setValue:nsdescription forKey:@"description"];
  12471. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12472. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12473. // i++;
  12474. // }
  12475. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12476. // [ret setObject:items forKey:@"items"];
  12477. // sqlite3_finalize(statement);
  12478. // }
  12479. //
  12480. // [iSalesDB close_db:db];
  12481. //
  12482. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12483. //
  12484. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12485. // dispatch_async(dispatch_get_main_queue(), ^{
  12486. //
  12487. // result(ret);
  12488. // });
  12489. //
  12490. // });
  12491. //
  12492. //
  12493. //
  12494. //}
  12495. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12496. //{
  12497. // [self itemsearch:params limited:YES completionHandler:result];
  12498. //}
  12499. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12500. {
  12501. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12502. NSString* offline_command=params[@"offline_Command"];
  12503. NSMutableDictionary* ret=nil;
  12504. if([offline_command isEqualToString:@"model_NIYMAL"])
  12505. {
  12506. NSString* category = params[@"category"];
  12507. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12508. }
  12509. dispatch_async(dispatch_get_main_queue(), ^{
  12510. result(ret);
  12511. });
  12512. });
  12513. }
  12514. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12515. {
  12516. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12517. int sort = [[params objectForKey:@"sort"] intValue];
  12518. NSString *sort_str = @"";
  12519. switch (sort) {
  12520. case 0:{
  12521. sort_str = @"order by w.modify_time desc";
  12522. }
  12523. break;
  12524. case 1:{
  12525. sort_str = @"order by w.modify_time asc";
  12526. }
  12527. break;
  12528. case 2:{
  12529. sort_str = @"order by m.name asc";
  12530. }
  12531. break;
  12532. case 3:{
  12533. sort_str = @"order by m.name desc";
  12534. }
  12535. break;
  12536. case 4:{
  12537. sort_str = @"order by m.description asc";
  12538. }
  12539. break;
  12540. default:
  12541. break;
  12542. }
  12543. // NSString* user = appDelegate.user;
  12544. sqlite3 *db = [iSalesDB get_db];
  12545. // order by w.create_time
  12546. 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];
  12547. // 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];
  12548. sqlite3_stmt * statement;
  12549. NSDate *date1 = [NSDate date];
  12550. // NSDate *date2 = nil;
  12551. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12552. int count=0;
  12553. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12554. {
  12555. while (sqlite3_step(statement) == SQLITE_ROW)
  12556. {
  12557. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12558. int product_id = sqlite3_column_double(statement, 0);
  12559. char *description = (char*)sqlite3_column_text(statement, 1);
  12560. if(description==nil)
  12561. description= "";
  12562. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12563. int item_id = sqlite3_column_double(statement, 2);
  12564. NSDate *date_image = [NSDate date];
  12565. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12566. printf("image : ");
  12567. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12568. // char *url = (char*)sqlite3_column_text(statement, 3);
  12569. // if(url==nil)
  12570. // url="";
  12571. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12572. int qty = sqlite3_column_int(statement, 3);
  12573. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12574. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12575. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12576. item[@"description"]= nsdescription;
  12577. item[@"img"]= nsurl;
  12578. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12579. count++;
  12580. }
  12581. printf("total time:");
  12582. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12583. ret[@"count"]= [NSNumber numberWithInt:count];
  12584. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12585. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12586. ret[@"result"]= [NSNumber numberWithInt:2];
  12587. sqlite3_finalize(statement);
  12588. }
  12589. [iSalesDB close_db:db];
  12590. dispatch_async(dispatch_get_main_queue(), ^{
  12591. UIApplication * app = [UIApplication sharedApplication];
  12592. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12593. appDelegate.wish_count =count;
  12594. [appDelegate update_count_mark];
  12595. result(ret);
  12596. });
  12597. });
  12598. }
  12599. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12600. {
  12601. // UIApplication * app = [UIApplication sharedApplication];
  12602. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12603. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12604. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12605. sqlite3 *db = [iSalesDB get_db];
  12606. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12607. NSString* product_id=params[@"product_id"];
  12608. NSString *item_count_str = params[@"item_count"];
  12609. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12610. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12611. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12612. // NSString *sql = @"";
  12613. for(int i=0;i<arr.count;i++)
  12614. {
  12615. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12616. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12617. __block int cart_count = 0;
  12618. if (!item_count_str) {
  12619. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12620. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12621. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12622. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12623. cart_count = [[model_set_components lastObject] intValue];
  12624. }];
  12625. }
  12626. if(count==0)
  12627. {
  12628. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12629. sqlite3_stmt *stmt;
  12630. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12631. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12632. if (item_count_arr) {
  12633. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12634. } else {
  12635. sqlite3_bind_int(stmt,2,cart_count);
  12636. }
  12637. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12638. [iSalesDB execSql:@"ROLLBACK" db:db];
  12639. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12640. [iSalesDB close_db:db];
  12641. DebugLog(@"add to wishlist error");
  12642. dispatch_async(dispatch_get_main_queue(), ^{
  12643. result(ret);
  12644. });
  12645. }
  12646. } else {
  12647. int qty = 0;
  12648. if (item_count_arr) {
  12649. qty = [item_count_arr[i] intValue];
  12650. } else {
  12651. qty = cart_count;
  12652. }
  12653. 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]];
  12654. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12655. [iSalesDB execSql:@"ROLLBACK" db:db];
  12656. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12657. [iSalesDB close_db:db];
  12658. DebugLog(@"add to wishlist error");
  12659. dispatch_async(dispatch_get_main_queue(), ^{
  12660. result(ret);
  12661. });
  12662. }
  12663. }
  12664. }
  12665. // [iSalesDB execSql:sql db:db];
  12666. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12667. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12668. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12669. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12670. [iSalesDB close_db:db];
  12671. ret[@"result"]= [NSNumber numberWithInt:2];
  12672. dispatch_async(dispatch_get_main_queue(), ^{
  12673. UIApplication * app = [UIApplication sharedApplication];
  12674. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12675. appDelegate.wish_count =count;
  12676. [appDelegate update_count_mark];
  12677. result(ret);
  12678. });
  12679. });
  12680. }
  12681. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12682. {
  12683. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12684. bool bLogin = appDelegate.bLogin;
  12685. __block NSString* contact_id = appDelegate.contact_id;
  12686. __block NSString* user = appDelegate.user;
  12687. __block NSString* order_code = appDelegate.order_code;
  12688. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12689. NSString* model_name = [params valueForKey:@"product_name"];
  12690. NSString* product_id = [params valueForKey:@"product_id"];
  12691. NSString* upc_code = [params valueForKey:@"upc_code"];
  12692. NSString* category = [params valueForKey:@"category"];
  12693. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12694. if(category==nil) {
  12695. category = default_category_id;
  12696. } else {
  12697. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12698. // 参数重有多个category id
  12699. if (arr_0.count > 1) {
  12700. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12701. // 取交集
  12702. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12703. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12704. [set_0 intersectSet:set_1];
  12705. if (set_0.count == 1) {
  12706. category = (NSString *)[set_0 anyObject];
  12707. } else {
  12708. if ([set_0 containsObject:default_category_id]) {
  12709. category = default_category_id;
  12710. } else {
  12711. category = (NSString *)[set_0 anyObject];
  12712. }
  12713. }
  12714. }
  12715. }
  12716. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12717. sqlite3 *db = [iSalesDB get_db];
  12718. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12719. NSString *sqlQuery = nil;
  12720. if(product_id==nil)
  12721. if(model_name==nil)
  12722. {
  12723. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upc_code,upc_code,upc_code,upc_code]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  12724. }
  12725. else
  12726. {
  12727. 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='%@';
  12728. }
  12729. else
  12730. 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=%@;
  12731. sqlite3_stmt * statement;
  12732. [ret setValue:@"2" forKey:@"result"];
  12733. [ret setValue:@"3" forKey:@"detail_section_count"];
  12734. // int count=0;
  12735. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12736. {
  12737. if (sqlite3_step(statement) == SQLITE_ROW)
  12738. {
  12739. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12740. char *name = (char*)sqlite3_column_text(statement, 0);
  12741. if(name==nil)
  12742. name="";
  12743. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12744. char *description = (char*)sqlite3_column_text(statement, 1);
  12745. if(description==nil)
  12746. description="";
  12747. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12748. int product_id = sqlite3_column_int(statement, 2);
  12749. char *color = (char*)sqlite3_column_text(statement, 3);
  12750. if(color==nil)
  12751. color="";
  12752. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12753. //
  12754. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12755. // if(legcolor==nil)
  12756. // legcolor="";
  12757. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12758. //
  12759. //
  12760. int availability = sqlite3_column_int(statement, 5);
  12761. //
  12762. int incoming_stock = sqlite3_column_int(statement, 6);
  12763. char *demension = (char*)sqlite3_column_text(statement, 7);
  12764. if(demension==nil)
  12765. demension="";
  12766. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12767. // ,,,,,,,,,
  12768. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12769. if(seat_height==nil)
  12770. seat_height="";
  12771. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12772. char *material = (char*)sqlite3_column_text(statement, 9);
  12773. if(material==nil)
  12774. material="";
  12775. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12776. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12777. if(box_dim==nil)
  12778. box_dim="";
  12779. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12780. char *volume = (char*)sqlite3_column_text(statement, 11);
  12781. if(volume==nil)
  12782. volume="";
  12783. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12784. double weight = sqlite3_column_double(statement, 12);
  12785. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12786. if(model_set==nil)
  12787. model_set="";
  12788. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12789. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12790. if(load_ability==nil)
  12791. load_ability="";
  12792. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12793. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12794. // if(default_category==nil)
  12795. // default_category="";
  12796. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12797. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12798. if(fabric_content==nil)
  12799. fabric_content="";
  12800. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12801. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12802. if(assembling==nil)
  12803. assembling="";
  12804. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12805. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12806. if(made_in==nil)
  12807. made_in="";
  12808. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12809. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12810. if(special_remarks==nil)
  12811. special_remarks="";
  12812. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12813. int stockUcom = sqlite3_column_double(statement, 20);
  12814. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12815. if(product_group==nil)
  12816. product_group="";
  12817. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12818. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12819. // if(fashion_selector==nil)
  12820. // fashion_selector="";
  12821. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12822. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12823. if(selector_field==nil)
  12824. selector_field="";
  12825. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12826. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12827. if(property_field==nil)
  12828. property_field="";
  12829. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12830. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12831. if(packaging==nil)
  12832. packaging="";
  12833. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12834. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12835. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12836. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12837. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12838. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12839. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12840. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12841. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12842. NSString* Availability=nil;
  12843. if(availability>0)
  12844. Availability=[NSString stringWithFormat:@"%d",availability];
  12845. else
  12846. Availability = @"Out of Stock";
  12847. [img_section setValue:Availability forKey:@"Availability"];
  12848. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12849. char *eta = (char*)sqlite3_column_text(statement, 25);
  12850. if(eta==nil)
  12851. eta="";
  12852. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12853. if ([nseta isEqualToString:@"null"]) {
  12854. nseta = @"";
  12855. }
  12856. if (availability <= 0) {
  12857. [img_section setValue:nseta forKey:@"ETA"];
  12858. }
  12859. int item_id = sqlite3_column_int(statement, 26);
  12860. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12861. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12862. NSString* Price=nil;
  12863. if(bLogin==false)
  12864. Price=@"Must Sign in.";
  12865. else
  12866. {
  12867. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12868. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12869. if(price==nil)
  12870. Price=@"No Price.";
  12871. else
  12872. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12873. }
  12874. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12875. [img_section setValue:Price forKey:@"price"];
  12876. [img_section setValue:nsname forKey:@"model_name"];
  12877. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12878. if (order_code) { // 离线order code即so#
  12879. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",order_code,item_id];
  12880. __block int cartQTY = 0;
  12881. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12882. cartQTY = sqlite3_column_int(stmt, 0);
  12883. }];
  12884. if (cartQTY > 0) {
  12885. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12886. }
  12887. }
  12888. [ret setObject:img_section forKey:@"img_section"];
  12889. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12890. int detail0_item_count=0;
  12891. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12892. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12893. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12894. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12895. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12896. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12897. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12898. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12899. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12900. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12901. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12902. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12903. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12904. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12905. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12906. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12907. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12908. {
  12909. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12910. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12911. }
  12912. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12913. [detail0_section setValue:@"kv" forKey:@"type"];
  12914. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12915. [ret setObject:detail0_section forKey:@"detail_0"];
  12916. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12917. // [detail1_section setValue:@"detail" forKey:@"target"];
  12918. // [detail1_section setValue:@"popup" forKey:@"action"];
  12919. // [detail1_section setValue:@"content" forKey:@"type"];
  12920. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12921. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12922. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12923. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12924. [ret setObject:detail1_section forKey:@"detail_1"];
  12925. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12926. [detail2_section setValue:@"detail" forKey:@"target"];
  12927. [detail2_section setValue:@"popup" forKey:@"action"];
  12928. [detail2_section setValue:@"content" forKey:@"type"];
  12929. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12930. [detail2_section setValue:@"true" forKey:@"single_row"];
  12931. [detail2_section setValue:@"local" forKey:@"data"];
  12932. [ret setObject:detail2_section forKey:@"detail_2"];
  12933. }
  12934. sqlite3_finalize(statement);
  12935. }
  12936. else
  12937. {
  12938. [ret setValue:@"8" forKey:@"result"];
  12939. }
  12940. // DebugLog(@"count:%d",count);
  12941. [iSalesDB close_db:db];
  12942. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12943. dispatch_async(dispatch_get_main_queue(), ^{
  12944. result(ret);
  12945. });
  12946. });
  12947. }
  12948. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12949. //{
  12950. //
  12951. // UIApplication * app = [UIApplication sharedApplication];
  12952. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12953. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12954. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12955. // sqlite3 *db = [iSalesDB get_db];
  12956. // NSString* collectId=params[@"collectId"];
  12957. //
  12958. //
  12959. //
  12960. //
  12961. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12962. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12963. // [iSalesDB execSql:sqlQuery db:db];
  12964. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12965. // [iSalesDB close_db:db];
  12966. //
  12967. //
  12968. // ret[@"result"]= [NSNumber numberWithInt:2];
  12969. // dispatch_async(dispatch_get_main_queue(), ^{
  12970. //
  12971. // appDelegate.wish_count =count;
  12972. //
  12973. // [appDelegate update_count_mark];
  12974. // result(ret);
  12975. // });
  12976. //
  12977. // });
  12978. //}
  12979. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12980. {
  12981. UIApplication * app = [UIApplication sharedApplication];
  12982. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12983. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12984. [iSalesDB disable_trigger];
  12985. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12986. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12987. [iSalesDB enable_trigger];
  12988. dispatch_async(dispatch_get_main_queue(), ^{
  12989. //
  12990. // NSString* user = [params valueForKey:@"user"];
  12991. //
  12992. // NSString* password = [params valueForKey:@"password"];
  12993. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12994. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12995. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12996. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12997. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12998. [appDelegate update_count_mark];
  12999. appDelegate.can_show_price =false;
  13000. appDelegate.can_see_price =false;
  13001. appDelegate.can_create_portfolio =false;
  13002. appDelegate.can_create_order =false;
  13003. appDelegate.can_cancel_order =false;
  13004. appDelegate.can_set_cart_price =false;
  13005. appDelegate.can_delete_order =false;
  13006. appDelegate.can_submit_order =false;
  13007. appDelegate.can_set_tearsheet_price =false;
  13008. appDelegate.can_update_contact_info = false;
  13009. appDelegate.save_order_logout = false;
  13010. appDelegate.submit_order_logout = false;
  13011. appDelegate.alert_sold_in_quantities = false;
  13012. appDelegate.ipad_perm =nil ;
  13013. appDelegate.user_type = USER_ROLE_UNKNOWN;
  13014. appDelegate.OrderFilter= nil;
  13015. [appDelegate SetSo:nil];
  13016. [appDelegate set_main_button_panel];
  13017. result(ret);
  13018. });
  13019. });
  13020. }
  13021. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  13022. {
  13023. // iSalesDB.
  13024. // UIApplication * app = [UIApplication sharedApplication];
  13025. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  13026. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  13027. [iSalesDB disable_trigger];
  13028. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  13029. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  13030. [iSalesDB enable_trigger];
  13031. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  13032. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  13033. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  13034. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  13035. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  13036. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  13037. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  13038. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  13039. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  13040. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  13041. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  13042. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  13043. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  13044. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  13045. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  13046. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  13047. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  13048. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  13049. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  13050. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  13051. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  13052. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  13053. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  13054. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  13055. [arr_name addObject:customer_first_name];
  13056. [arr_name addObject:customer_last_name];
  13057. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  13058. // default ship from
  13059. 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';";
  13060. __block NSString *cid = @"";
  13061. __block NSString *name = @"";
  13062. __block NSString *ext = @"";
  13063. __block NSString *contact = @"";
  13064. __block NSString *email = @"";
  13065. __block NSString *fax = @"";
  13066. __block NSString *phone = @"";
  13067. sqlite3 *db = [iSalesDB get_db];
  13068. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13069. cid = [self textAtColumn:0 statement:statment];
  13070. name = [self textAtColumn:1 statement:statment];
  13071. ext = [self textAtColumn:2 statement:statment];
  13072. contact = [self textAtColumn:3 statement:statment];
  13073. email = [self textAtColumn:4 statement:statment];
  13074. fax = [self textAtColumn:5 statement:statment];
  13075. phone = [self textAtColumn:6 statement:statment];
  13076. }];
  13077. // default carrier
  13078. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  13079. __block NSString *carrier = @"";
  13080. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  13081. carrier = [self textAtColumn:0 statement:statment];
  13082. }];
  13083. NSString* so_id = [self get_offline_soid:db];
  13084. if(so_id==nil)
  13085. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  13086. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms,carrier) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB','%@')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email,carrier];
  13087. int result_code =[iSalesDB execSql:sql_neworder db:db];
  13088. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  13089. //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'
  13090. //soId
  13091. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  13092. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  13093. sqlite3_stmt * statement;
  13094. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  13095. {
  13096. if (sqlite3_step(statement) == SQLITE_ROW)
  13097. {
  13098. // char *name = (char*)sqlite3_column_text(statement, 1);
  13099. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  13100. //ret = sqlite3_column_int(statement, 0);
  13101. char *soId = (char*)sqlite3_column_text(statement, 0);
  13102. if(soId==nil)
  13103. soId="";
  13104. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  13105. [ret setValue:nssoId forKey:@"soId"];
  13106. [ret setValue:nssoId forKey:@"orderCode"];
  13107. }
  13108. sqlite3_finalize(statement);
  13109. }
  13110. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  13111. [ret setValue:@"Regular Mode" forKey:@"mode"];
  13112. [iSalesDB close_db:db];
  13113. dispatch_async(dispatch_get_main_queue(), ^{
  13114. result(ret);
  13115. });
  13116. });
  13117. }
  13118. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  13119. {
  13120. UIApplication * app = [UIApplication sharedApplication];
  13121. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  13122. bool can_create_backorder= appDelegate.can_create_backorder;
  13123. NSString* user = appDelegate.user;
  13124. NSString* contact_id = appDelegate.contact_id;
  13125. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  13126. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  13127. sqlite3 *db = [iSalesDB get_db];
  13128. NSString* orderCode=params[@"orderCode"];
  13129. int sort = [[params objectForKey:@"sort"] intValue];
  13130. NSString *sort_str = @"";
  13131. switch (sort) {
  13132. case 0:{
  13133. sort_str = @"order by c.modify_time desc";
  13134. }
  13135. break;
  13136. case 1:{
  13137. sort_str = @"order by c.modify_time asc";
  13138. }
  13139. break;
  13140. case 2:{
  13141. sort_str = @"order by m.name asc";
  13142. }
  13143. break;
  13144. case 3:{
  13145. sort_str = @"order by m.name desc";
  13146. }
  13147. break;
  13148. case 4:{
  13149. sort_str = @"order by m.description asc";
  13150. }
  13151. break;
  13152. default:
  13153. break;
  13154. }
  13155. 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 ];
  13156. // 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 ];
  13157. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  13158. DebugLog(@"offline_login sql:%@",sqlQuery);
  13159. sqlite3_stmt * statement;
  13160. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  13161. NSDate *date1 = [NSDate date];
  13162. int count=0;
  13163. int cart_count=0;
  13164. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  13165. if ( dbresult== SQLITE_OK)
  13166. {
  13167. while (sqlite3_step(statement) == SQLITE_ROW)
  13168. {
  13169. // NSDate *row_date = [NSDate date];
  13170. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13171. int product_id = sqlite3_column_int(statement, 0);
  13172. char *str_price = (char*)sqlite3_column_text(statement, 1);
  13173. int item_id = sqlite3_column_int(statement, 7);
  13174. NSString* Price=nil;
  13175. if(str_price==nil)
  13176. {
  13177. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  13178. // NSDate *price_date = [NSDate date];
  13179. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  13180. // DebugLog(@"price time interval");
  13181. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  13182. if(price==nil)
  13183. Price=@"No Price.";
  13184. else
  13185. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  13186. }
  13187. else
  13188. {
  13189. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  13190. }
  13191. double discount = sqlite3_column_double(statement, 2);
  13192. int item_count = sqlite3_column_int(statement, 3);
  13193. char *line_note = (char*)sqlite3_column_text(statement, 4);
  13194. NSString *nsline_note=nil;
  13195. if(line_note!=nil)
  13196. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  13197. char *name = (char*)sqlite3_column_text(statement, 5);
  13198. NSString *nsname=nil;
  13199. if(name!=nil)
  13200. nsname= [[NSString alloc]initWithUTF8String:name];
  13201. char *description = (char*)sqlite3_column_text(statement, 6);
  13202. NSString *nsdescription=nil;
  13203. if(description!=nil)
  13204. nsdescription= [[NSString alloc]initWithUTF8String:description];
  13205. int stockUom = sqlite3_column_int(statement, 8);
  13206. int _id = sqlite3_column_int(statement, 9);
  13207. int availability = sqlite3_column_int(statement, 10);
  13208. // NSDate *subtotal_date = [NSDate date];
  13209. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  13210. // DebugLog(@"subtotal_date time interval");
  13211. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  13212. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  13213. double weight=[bsubtotaljson[@"weight"] doubleValue];
  13214. int carton=[bsubtotaljson[@"carton"] intValue];
  13215. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  13216. int mpack =[bsubtotaljson[@"mpack"] intValue];
  13217. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  13218. // NSDate *img_date = [NSDate date];
  13219. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  13220. // DebugLog(@"img_date time interval");
  13221. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  13222. itemjson[@"model"]=nsname;
  13223. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  13224. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  13225. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  13226. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  13227. itemjson[@"check"]=@"true";
  13228. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  13229. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  13230. itemjson[@"unit_price"]=Price;
  13231. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  13232. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  13233. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  13234. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  13235. itemjson[@"note"]=nsline_note;
  13236. if (!can_create_backorder) {
  13237. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  13238. }
  13239. // NSDate *date2 = [NSDate date];
  13240. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  13241. // DebugLog(@"model_bundle time interval");
  13242. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  13243. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  13244. count++;
  13245. // DebugLog(@"row time interval");
  13246. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  13247. }
  13248. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  13249. sqlite3_finalize(statement);
  13250. }
  13251. DebugLog(@"request cart total time interval");
  13252. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  13253. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  13254. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  13255. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  13256. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  13257. ret[@"count"]=[NSNumber numberWithInt:count ];
  13258. ret[@"mode"]=@"Regular Mode";
  13259. [iSalesDB close_db:db];
  13260. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  13261. DebugLog(@"general notes :%@",general_note);
  13262. ret[@"general_note"]= general_note;
  13263. dispatch_async(dispatch_get_main_queue(), ^{
  13264. result(ret);
  13265. });
  13266. });
  13267. }
  13268. @end