OLDataProvider.m 685 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311
  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. [ret setValue:nsfirst_name forKey:@"first_name"];
  445. }
  446. sqlite3_finalize(statement);
  447. }
  448. #ifdef DEBUG
  449. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  450. #endif
  451. return ret;
  452. }
  453. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  454. {
  455. assert(user!=nil);
  456. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  457. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  458. data[@"npd_url"]=COMPANY_WEB;
  459. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  460. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  461. NSString* date = [formatter stringFromDate:[NSDate date]];
  462. data[@"print_date"]=date;
  463. data[@"company_name"]=COMPANY_FULL_NAME;
  464. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  465. data[@"TOTAL_PAGE"]=0;
  466. data[@"CURRENT_PAGE"]=0;
  467. // [formatter setDateFormat:@"MM/dd/yyyy"];
  468. // date = [formatter stringFromDate:[NSDate date]];
  469. // data[@"creat_date"]=date;
  470. NSString* temp = NSTemporaryDirectory();
  471. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  472. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  473. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  474. // bool bwrite=
  475. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  476. data[@"img_barcode"]=qrpath;
  477. data[@"so_id"]=order_code;
  478. data[@"company"]=COMPANY_FULL_NAME;
  479. // 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];
  480. 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,discount from offline_order where so_id='%@'",order_code];
  481. sqlite3 *db = [iSalesDB get_db];
  482. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  483. sqlite3_stmt * statement;
  484. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  485. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  486. row0[@"count"]=[NSNumber numberWithInt:1];
  487. datasource[@"row0"]=row0;
  488. NSString* print_status=@"";
  489. NSString* order_type=@"Offline Quote";
  490. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  491. {
  492. if (sqlite3_step(statement) == SQLITE_ROW)
  493. {
  494. int erpOrderStatus = sqlite3_column_int(statement, 0);
  495. int status = sqlite3_column_int(statement, 26);
  496. if(status==2)
  497. {
  498. switch (erpOrderStatus) {
  499. case 10:
  500. // order_type=@"Quoted Order";
  501. print_status=@"Quote";
  502. break;
  503. case 11:
  504. print_status=@"Booked";
  505. break;
  506. case 12:
  507. print_status=@"Picked";
  508. break;
  509. case 13:
  510. print_status=@"Shipped";
  511. break;
  512. case 14:
  513. print_status=@"Closed";
  514. break;
  515. case 15:
  516. print_status=@"Canceled";
  517. break;
  518. case 16:
  519. print_status=@"All";
  520. break;
  521. case 18:
  522. print_status=@"Processing";
  523. break;
  524. default:
  525. break;
  526. }
  527. }
  528. else{
  529. switch (status) {
  530. case 0:
  531. {
  532. print_status=@"Quote";
  533. break;
  534. }
  535. case 1:
  536. {
  537. print_status=@"Saved";
  538. break;
  539. }
  540. case 3:
  541. {
  542. //canceled
  543. print_status=@"Canceled";
  544. break;
  545. }
  546. default:
  547. break;
  548. }
  549. }
  550. NSString* sender_name = [self textAtColumn:1 statement:statement];
  551. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  552. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  553. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  554. NSString* sender_email = [self textAtColumn:5 statement:statement];
  555. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  556. NSString* billing_name = [self textAtColumn:7 statement:statement];
  557. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  558. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  559. if(billing_phone.length>0)
  560. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  561. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  562. if(billing_fax.length>0)
  563. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  564. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  565. [billto_arr0 addObject:billing_phone];
  566. [billto_arr0 addObject:billing_fax];
  567. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  568. NSString* billing_email = [self textAtColumn:11 statement:statement];
  569. if(billing_email.length>0)
  570. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  571. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  572. if(billing_contact.length>0)
  573. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  574. NSString* receive_name = [self textAtColumn:12 statement:statement];
  575. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  576. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  577. if(receive_phone.length>0)
  578. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  579. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  580. if(receive_fax.length>0)
  581. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  582. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  583. [receive_arr0 addObject:receive_phone];
  584. [receive_arr0 addObject:receive_fax];
  585. NSString* receive_email = [self textAtColumn:16 statement:statement];
  586. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  587. if(receive_email.length>0)
  588. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  589. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  590. if(receive_contact.length>0)
  591. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  592. NSString* poNumber = [self textAtColumn:17 statement:statement];
  593. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  594. NSString* carrier = [self textAtColumn:19 statement:statement];
  595. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  596. NSString* terms = [self textAtColumn:21 statement:statement];
  597. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  598. NSString* port_destination = [self textAtColumn:23 statement:statement];
  599. NSString* etd = [self textAtColumn:24 statement:statement];
  600. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  601. if(sign_picpath.length==0)
  602. sign_picpath=nil;
  603. NSString* create_date = [self textAtColumn:27 statement:statement];
  604. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  605. NSString* general_notes=[self textAtColumn:33 statement:statement];
  606. NSString *customerID = [self textAtColumn:34 statement:statement];
  607. if (customerID.length == 0) {
  608. customerID = nil;
  609. }
  610. if(general_notes.length>0)
  611. {
  612. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  613. row2[@"item_0"]=@{@"g_notes":general_notes };
  614. row2[@"count"]=[NSNumber numberWithInt:1];
  615. datasource[@"row2"]=row2;
  616. }
  617. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  618. [billto_arr addObject:billing_ext];
  619. [billto_arr addObject:billing_pf];
  620. [billto_arr addObject:billing_contact];
  621. [billto_arr addObject:billing_email];
  622. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  623. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  624. [receive_arr addObject:receive_ext];
  625. [receive_arr addObject:receive_pf];
  626. [receive_arr addObject:receive_contact];
  627. [receive_arr addObject:receive_email];
  628. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  629. float shipping = sqlite3_column_double(statement, 28);
  630. float lift_gate_value = sqlite3_column_double(statement, 29);
  631. float handling_fee_value = sqlite3_column_double(statement, 30);
  632. #ifdef BUILD_CONTRAST
  633. float order_discount=sqlite3_column_double(statement, 35);
  634. #endif
  635. data[@"company"]=sender_name;
  636. data[@"shipfrom_addr"]=sender_ext;
  637. data[@"shipfrom_phone"]=sender_phone;
  638. data[@"shipfrom_fax"]=sender_fax;
  639. data[@"shipfrom_email"]=sender_email;
  640. data[@"warehouse_name"]=warehouse_name;
  641. data[@"bill_to_name"]=billing_name;
  642. data[@"bill_to_ext"]=billto;
  643. data[@"ship_to_name"]=receive_name;
  644. data[@"ship_to_ext"]=receive;
  645. data[@"po_no"]=poNumber;
  646. data[@"rep"]=sales_rep;
  647. data[@"e_schdule"]=etd;
  648. data[@"sales_terms"]=sales_terms;
  649. data[@"port_destination"]=port_destination;
  650. data[@"terms"]=terms;
  651. data[@"truck_carrier"]=carrier;
  652. data[@"vendor_no"]=vendor_no;
  653. data[@"customer_sign"]=sign_picpath;
  654. data[@"print_status"]=print_status;
  655. data[@"create_date"]=create_date;
  656. 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];;
  657. sqlite3_stmt * statement_cart;
  658. // NSDate *date1 = [NSDate date];
  659. int count=0;
  660. // int cart_count=0;
  661. float credits=0;
  662. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  663. if ( dbresult== SQLITE_OK)
  664. {
  665. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  666. int row1count=0;
  667. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  668. {
  669. // NSDate *row_date = [NSDate date];
  670. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  671. // int product_id = sqlite3_column_int(statement_cart, 0);
  672. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  673. int item_id = sqlite3_column_int(statement_cart, 7);
  674. double discount = sqlite3_column_double(statement_cart, 2);
  675. NSString* Price=nil;
  676. if(str_price==nil)
  677. {
  678. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  679. // NSDate *price_date = [NSDate date];
  680. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  681. // DebugLog(@"price time interval");
  682. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  683. if(price==nil)
  684. Price=@"No Price.";
  685. else
  686. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  687. }
  688. else
  689. {
  690. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  691. }
  692. float dunitprice=0;
  693. int item_count = sqlite3_column_int(statement_cart, 3);
  694. NSString* Amount=@"";
  695. if(![Price isEqualToString:@"No Price."])
  696. {
  697. dunitprice=[Price floatValue];
  698. dunitprice=dunitprice* (1-discount/100.0);
  699. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  700. credits+=dunitprice*item_count;
  701. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  702. }
  703. else
  704. {
  705. Price=@"";
  706. }
  707. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  708. // NSString *nsline_note=nil;
  709. // if(line_note!=nil)
  710. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  711. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  712. NSString *nsname=nil;
  713. if(name!=nil)
  714. nsname= [[NSString alloc]initWithUTF8String:name];
  715. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  716. NSString *nsdescription=nil;
  717. if(description!=nil)
  718. nsdescription= [[NSString alloc]initWithUTF8String:description];
  719. // int stockUom = sqlite3_column_int(statement_cart, 8);
  720. // int _id = sqlite3_column_int(statement_cart, 9);
  721. // NSDate *subtotal_date = [NSDate date];
  722. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  723. // DebugLog(@"subtotal_date time interval");
  724. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  725. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  726. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  727. // int carton=[bsubtotaljson[@"carton"] intValue];
  728. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  729. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  730. // NSDate *img_date = [NSDate date];
  731. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  732. // DebugLog(@"img_date time interval");
  733. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  734. itemjson[@"model"]=nsname;
  735. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  736. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  737. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  738. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  739. // itemjson[@"check"]=@"true";
  740. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  741. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  742. itemjson[@"unit_price"]=Price;
  743. itemjson[@"amount"]=Amount;
  744. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  745. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  746. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  747. // itemjson[@"note"]=nsline_note;
  748. // NSDate *date2 = [NSDate date];
  749. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  750. // DebugLog(@"model_bundle time interval");
  751. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  752. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  753. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  754. row1count++;
  755. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  756. for(int c=0;c<[combine[@"count"] intValue];c++)
  757. {
  758. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  759. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  760. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  761. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  762. credits+=combine_amount;
  763. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  764. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  765. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  766. row1count++;
  767. }
  768. count++;
  769. // DebugLog(@"row time interval");
  770. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  771. }
  772. row1[@"count"]=[NSNumber numberWithInt:row1count];
  773. datasource[@"row1"]=row1;
  774. data[@"table_model"]=datasource;
  775. sqlite3_finalize(statement_cart);
  776. }
  777. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  778. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  779. // if(credits>0)
  780. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  781. // else
  782. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  783. if(shipping>0)
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  785. else
  786. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  787. if(lift_gate_value>0)
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  789. else
  790. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  791. if(handling_fee_value>0)
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  793. else
  794. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  795. #ifdef BUILD_CONTRAST
  796. if(order_discount>0)
  797. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": [NSString stringWithFormat:@"-%.2f",credits*(order_discount/100.0)]};
  798. else
  799. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": @""};
  800. float total = credits*(1-order_discount/100.0)+shipping+lift_gate_value+handling_fee_value;
  801. if(total>0)
  802. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  803. else
  804. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": @""};
  805. fee_row0[@"count"]=[NSNumber numberWithInt:6];
  806. #else
  807. float total = credits+shipping+lift_gate_value+handling_fee_value;
  808. if(total>0)
  809. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  810. else
  811. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  812. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  813. #endif
  814. fee[@"row0"]=fee_row0;
  815. data[@"table_fee"]=fee;
  816. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  817. // ret[@"sign_url"] = sign_url;
  818. //
  819. // customer_contact = [self textAtColumn:52 statement:statement];
  820. // customer_email = [self textAtColumn:53 statement:statement];
  821. // customer_phone = [self textAtColumn:54 statement:statement];
  822. // customer_fax = [self textAtColumn:55 statement:statement];
  823. //
  824. //
  825. // int offline_edit=sqlite3_column_int(statement, 56);
  826. //
  827. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  828. //
  829. // char *soid = (char*)sqlite3_column_text(statement, 1);
  830. // if(soid==nil)
  831. // soid= "";
  832. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  833. // // so#
  834. // ret[@"so#"] = nssoid;
  835. //
  836. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  837. // if(poNumber==nil)
  838. // poNumber= "";
  839. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  840. //
  841. //
  842. //
  843. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  844. // if(create_time==nil)
  845. // create_time= "";
  846. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  847. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  848. //
  849. // int status = sqlite3_column_int(statement, 4);
  850. // int erpStatus = sqlite3_column_int(statement, 49);
  851. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  852. //
  853. // // status
  854. // if (status > 1 && status != 3) {
  855. // status = erpStatus;
  856. // } else if (status == 3) {
  857. // status = 15;
  858. // }
  859. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  860. // ret[@"order_status"] = nsstatus;
  861. //
  862. //
  863. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  864. // if(company_name==nil)
  865. // company_name= "";
  866. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  867. // // company name
  868. // ret[@"company_name"] = nscompany_name;
  869. //
  870. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  871. // if(customer_contact==nil)
  872. // customer_contact= "";
  873. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  874. //
  875. //
  876. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  877. // if(addr_1==nil)
  878. // addr_1="";
  879. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  880. //
  881. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  882. // if(addr_2==nil)
  883. // addr_2="";
  884. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  885. //
  886. //
  887. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  888. // if(addr_3==nil)
  889. // addr_3="";
  890. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  891. //
  892. //
  893. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  894. // if(addr_4==nil)
  895. // addr_4="";
  896. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  897. //
  898. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  899. // [arr_addr addObject:nsaddr_1];
  900. // [arr_addr addObject:nsaddr_2];
  901. // [arr_addr addObject:nsaddr_3];
  902. // [arr_addr addObject:nsaddr_4];
  903. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  904. //
  905. //
  906. // char *logist = (char*)sqlite3_column_text(statement, 11);
  907. // if(logist==nil)
  908. // logist= "";
  909. // NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  910. // if (status == -11 || status == 10 || status == 11) {
  911. // DebugLogist = [self textAtColumn:59 statement:statement];
  912. // };
  913. //
  914. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  915. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  916. // shipping = @"Shipping To Be Quoted";
  917. // } else {
  918. // shippingFee = sqlite3_column_double(statement, 12);
  919. // }
  920. //
  921. // // Shipping
  922. // ret[@"Shipping"] = shipping;
  923. //
  924. // int have_lift_gate = sqlite3_column_int(statement, 17);
  925. // lift_gate = sqlite3_column_double(statement, 13);
  926. // // Liftgate Fee(No loading dock)
  927. // if (!have_lift_gate) {
  928. // lift_gate = 0;
  929. // }
  930. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  931. // if (sqlite3_column_int(statement, 57)) {
  932. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  933. // }
  934. //
  935. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  936. // if(general_notes==nil)
  937. // general_notes= "";
  938. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  939. //
  940. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  941. // if(internal_notes==nil)
  942. // internal_notes= "";
  943. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  944. //
  945. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  946. // if(payment_type==nil)
  947. // payment_type= "";
  948. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  949. //
  950. //
  951. //
  952. // // order info
  953. // orderinfo = [self textFileName:@"order_info.html"];
  954. //
  955. //
  956. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  957. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  958. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  959. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  960. //
  961. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  964. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  965. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  969. //
  970. // NSString *payment = nil;
  971. // if([nspayment_type isEqualToString:@"Credit Card"])
  972. // {
  973. // payment = [self textFileName:@"creditcardpayment.html"];
  974. //
  975. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  976. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  977. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  978. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  979. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  980. // NSString *card_type = [self textAtColumn:42 statement:statement];
  981. // if (card_type.length > 0) { // 显示星号
  982. // card_type = @"****";
  983. // }
  984. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  985. // if (card_number.length > 0 && card_number.length > 4) {
  986. // for (int i = 0; i < card_number.length - 4; i++) {
  987. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  988. // }
  989. // } else {
  990. // card_number = @"";
  991. // }
  992. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  993. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  994. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  995. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  996. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  997. // card_expiration = @"****";
  998. // }
  999. //
  1000. //
  1001. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1002. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1003. //
  1004. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1005. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1006. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1007. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1008. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1009. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1010. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1011. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1012. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1013. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1014. //
  1015. //
  1016. // }
  1017. // else
  1018. // {
  1019. // payment=[self textFileName:@"normalpayment.html"];
  1020. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1021. // }
  1022. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1023. //
  1024. // ret[@"result"]= [NSNumber numberWithInt:2];
  1025. //
  1026. // // more info
  1027. // moreInfo = [self textFileName:@"more_info.html"];
  1028. //
  1029. //
  1030. // // ShipToCompany_or_&nbsp
  1031. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1032. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1033. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1034. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1035. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1036. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1037. //
  1038. //
  1039. // // ShipFromCompany_or_&nbsp
  1040. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1041. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1042. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1043. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1044. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1045. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1046. //
  1047. //
  1048. // // FreightBillToCompany_or_&nbsp
  1049. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1051. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1053. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1055. //
  1056. //
  1057. // // MerchandiseBillToCompany_or_&nbsp
  1058. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1060. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1062. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1064. //
  1065. //
  1066. // // ReturnToCompany_or_&nbsp
  1067. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1069. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1071. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1073. // //
  1074. //
  1075. // // DebugLog(@"more info : %@",moreInfo);
  1076. //
  1077. // // handling fee
  1078. // handlingFee = sqlite3_column_double(statement, 33);
  1079. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1080. // if (sqlite3_column_int(statement, 58)) {
  1081. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1082. // }
  1083. // //
  1084. //
  1085. // // customer info
  1086. // customerID = [self textAtColumn:36 statement:statement];
  1087. //
  1088. // // mode
  1089. // ret[@"mode"] = appDelegate.mode;
  1090. //
  1091. // // model_count
  1092. // ret[@"model_count"] = @(0);
  1093. }
  1094. sqlite3_finalize(statement);
  1095. }
  1096. [iSalesDB close_db:db];
  1097. data[@"order_type"]=order_type;
  1098. #ifdef DEBUG
  1099. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1100. #endif
  1101. return data;
  1102. }
  1103. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1104. {
  1105. assert(params[@"user"]!=nil);
  1106. NSString* template_file=nil;
  1107. switch([params[@"thumb"] intValue])
  1108. {
  1109. case 0:
  1110. template_file= @"so.json";
  1111. break;
  1112. case 1:
  1113. template_file= @"so_thumb.json";
  1114. break;
  1115. default:
  1116. template_file= @"so.json";
  1117. }
  1118. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1119. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1120. if(data[@"table_model"][@"row2"]==nil)
  1121. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1122. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1123. DebugLog(@"%@",file);
  1124. // return nil;
  1125. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1126. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1127. if (file) {
  1128. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1129. [dic setObject:file forKey:@"pdf_path"];
  1130. dic[@"isLocalFile"]=@"true";
  1131. return [RAConvertor dict2data:dic];
  1132. }
  1133. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1134. dic[@"isLocalFile"]=@"true";
  1135. return [RAConvertor dict2data:dic];
  1136. }
  1137. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1138. {
  1139. // assert(params[@"contact_id"]!=nil);
  1140. // assert(params[@"user"]!=nil);
  1141. if(params[@"user"]==nil)
  1142. {
  1143. AppDelegate *appDelegate = nil;
  1144. //some UI methods ej
  1145. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1146. if(RASingleton.sharedInstance.user!=nil)
  1147. [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  1148. }
  1149. NSString* template_file=nil;
  1150. switch([params[@"pdf_style"] intValue])
  1151. {
  1152. case 0:
  1153. template_file= @"portfolio_2x3.json";
  1154. break;
  1155. case 1:
  1156. template_file= @"portfolio_3x2.json";
  1157. break;
  1158. default:
  1159. template_file= @"portfolio_3x2.json";
  1160. }
  1161. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1162. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1163. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1164. DebugLog(@"%@",file);
  1165. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1166. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1167. if (file) {
  1168. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1169. [dic setObject:file forKey:@"pdf_path"];
  1170. dic[@"isLocalFile"]=@"true";
  1171. return [RAConvertor dict2data:dic];
  1172. }
  1173. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1174. dic[@"isLocalFile"]=@"true";
  1175. return [RAConvertor dict2data:dic];
  1176. }
  1177. +(NSString*) get_offline_soid:(sqlite3*)db
  1178. {
  1179. NSString* soid=nil;
  1180. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1181. [formatter setDateFormat:@"yyMMdd"];
  1182. NSString* date = [formatter stringFromDate:[NSDate date]];
  1183. for(int i=1;i<999;i++)
  1184. {
  1185. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1186. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1187. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1188. if(count==0)
  1189. return soid;
  1190. }
  1191. return nil;
  1192. }
  1193. +(NSArray*) enumOfflineOrder
  1194. {
  1195. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1196. sqlite3 *db = [iSalesDB get_db];
  1197. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1198. sqlite3_stmt * statement;
  1199. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1200. if ( dbresult== SQLITE_OK)
  1201. {
  1202. while (sqlite3_step(statement) == SQLITE_ROW)
  1203. {
  1204. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1205. NSString* nsso_id=nil;
  1206. if(so_id!=nil)
  1207. {
  1208. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1209. }
  1210. [ret addObject:nsso_id];
  1211. }
  1212. sqlite3_finalize(statement);
  1213. }
  1214. [iSalesDB close_db:db];
  1215. return ret;
  1216. }
  1217. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1218. {
  1219. if(submitlist==nil)
  1220. return;
  1221. NSString *userdir = [OLDataProvider getUserPath];
  1222. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1223. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1224. }
  1225. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1226. {
  1227. if(orderlist==nil)
  1228. return;
  1229. NSString *userdir = [OLDataProvider getUserPath];
  1230. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1231. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1232. }
  1233. +(NSMutableDictionary*) getScanSubmitList
  1234. {
  1235. NSString *userdir = [OLDataProvider getUserPath];
  1236. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1237. NSMutableDictionary* submitlist = nil;
  1238. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1239. submitlist = [NSMutableDictionary new];
  1240. }
  1241. else
  1242. {
  1243. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1244. }
  1245. return submitlist;
  1246. }
  1247. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1248. //// NSString *userdir = [self getUserPath:user];
  1249. // NSMutableDictionary* orderlist = nil;
  1250. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1251. //
  1252. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1253. //
  1254. //
  1255. // orderlist = [NSMutableDictionary new];
  1256. //
  1257. // // [orderlist addObject:appDelegate.order_code];
  1258. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1259. //
  1260. // }
  1261. // else
  1262. // {
  1263. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1264. //
  1265. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1266. //
  1267. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1268. // }
  1269. // return orderlist;
  1270. //}
  1271. +(NSMutableDictionary*) getScanOrderList{
  1272. NSString *userdir = [OLDataProvider getUserPath];
  1273. NSMutableDictionary* orderlist = nil;
  1274. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1275. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1276. orderlist = [NSMutableDictionary new];
  1277. // [orderlist addObject:appDelegate.order_code];
  1278. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1279. }
  1280. else
  1281. {
  1282. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1283. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1284. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1285. }
  1286. return orderlist;
  1287. }
  1288. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1289. {
  1290. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1291. // NSString *documents = [paths objectAtIndex:0];
  1292. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1293. // NSString *userdir = [OLDataProvider getUserPath];
  1294. NSMutableDictionary* orderlist = [self getScanOrderList];
  1295. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1296. int count = [orderlist[@"count"] intValue];
  1297. if(count==0)
  1298. {
  1299. // order list 为空,不用维护
  1300. #ifdef RA_NOTIFICATION
  1301. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1302. #else
  1303. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1304. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1305. #endif
  1306. return;
  1307. }
  1308. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1309. //
  1310. // // order list 为空,不用维护
  1311. //
  1312. //
  1313. // // [orderlist addObject:appDelegate.order_code];
  1314. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1315. //
  1316. // }
  1317. // else
  1318. // {
  1319. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1320. //
  1321. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1322. //
  1323. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1324. // }
  1325. //==================== order list 删除
  1326. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1327. NSMutableDictionary* submititem =nil;
  1328. int newcount = 0;
  1329. for(int i=0;i<count;i++)
  1330. {
  1331. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1332. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1333. {
  1334. submititem = item;
  1335. continue;
  1336. }
  1337. else
  1338. {
  1339. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1340. newcount++;
  1341. }
  1342. }
  1343. if(submititem==nil)
  1344. {
  1345. //order list 里没有和当前打开的order code一致的order
  1346. //这种情况往往是因为重复点提交按钮,order已关闭。
  1347. return;
  1348. }
  1349. newlist[@"count"] = @(newcount);
  1350. [self saveScanOrderList:newlist];
  1351. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1352. //////////////////---=============================== submitlist 添加
  1353. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1354. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1355. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1356. //
  1357. // submitlist = [NSMutableDictionary new];
  1358. //
  1359. //
  1360. // }
  1361. // else
  1362. // {
  1363. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1364. //
  1365. //
  1366. // }
  1367. int submitcount =[submitlist[@"count"] intValue];
  1368. submititem[@"order_status"] = soid;//@"Submitted Order";
  1369. submititem[@"customer_name"] = company;//@"Submitted Order";
  1370. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1371. submitlist[@"count"] = @(submitcount+1);
  1372. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1373. [self saveScanSubmitList:submitlist];
  1374. #ifdef RA_NOTIFICATION
  1375. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1376. #else
  1377. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1378. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1379. #endif
  1380. }
  1381. +(void) save2submitScanOrder1
  1382. {
  1383. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1384. // NSString *documents = [paths objectAtIndex:0];
  1385. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1386. // NSString *userdir = [OLDataProvider getUserPath];
  1387. NSMutableDictionary* orderlist = [self getScanOrderList];
  1388. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1389. int count = [orderlist[@"count"] intValue];
  1390. if(count==0)
  1391. {
  1392. // order list 为空,不用维护
  1393. #ifdef RA_NOTIFICATION
  1394. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1395. #else
  1396. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1397. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1398. #endif
  1399. return;
  1400. return;
  1401. }
  1402. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1403. int newcount = 0;
  1404. for(int i=0;i<count;i++)
  1405. {
  1406. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1407. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1408. continue;
  1409. else
  1410. {
  1411. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1412. newcount++;
  1413. }
  1414. }
  1415. newlist[@"count"] = @(newcount);
  1416. [self saveScanOrderList:newlist];
  1417. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1418. #ifdef RA_NOTIFICATION
  1419. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1420. #else
  1421. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1422. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1423. #endif
  1424. }
  1425. //+(void) createScanTempFolder
  1426. //{
  1427. // return;
  1428. //// NSString* scanFilePath = [self getScanTempPath];
  1429. //// BOOL bdir=YES;
  1430. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1431. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1432. //// {
  1433. ////
  1434. //// NSError *error = nil;
  1435. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1436. ////
  1437. //// if(!bsuccess)
  1438. //// DebugLog(@"Create cache folder failed");
  1439. ////
  1440. //// // if(bsuccess)
  1441. //// // {
  1442. //// // sqlite3 *db = [self get_db];
  1443. //// //
  1444. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1445. //// // [iSalesDB close_db:db];
  1446. //// // }
  1447. //// }
  1448. //}
  1449. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1450. {
  1451. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1452. NSString *documents = [paths objectAtIndex:0];
  1453. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1454. NSString* servername = addressDic[@"name"];
  1455. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1456. NSString* user = RASingleton.sharedInstance.user;
  1457. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1458. orderpath = [orderpath stringByAppendingPathComponent:user];
  1459. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1460. BOOL bdir=YES;
  1461. NSFileManager* fileManager = [NSFileManager defaultManager];
  1462. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1463. {
  1464. NSError *error = nil;
  1465. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1466. if(!bsuccess)
  1467. DebugLog(@"Create cache folder failed");
  1468. // if(bsuccess)
  1469. // {
  1470. // sqlite3 *db = [self get_db];
  1471. //
  1472. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1473. // [iSalesDB close_db:db];
  1474. // }
  1475. }
  1476. return orderpath;
  1477. }
  1478. +(NSString*) getScanOrderPath
  1479. {
  1480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1481. NSString *documents = [paths objectAtIndex:0];
  1482. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1483. NSString* servername = addressDic[@"name"];
  1484. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1485. NSString* user = RASingleton.sharedInstance.user;
  1486. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1487. orderpath = [orderpath stringByAppendingPathComponent:user];
  1488. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1489. BOOL bdir=YES;
  1490. NSFileManager* fileManager = [NSFileManager defaultManager];
  1491. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1492. {
  1493. NSError *error = nil;
  1494. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1495. if(!bsuccess)
  1496. DebugLog(@"Create cache folder failed");
  1497. // if(bsuccess)
  1498. // {
  1499. // sqlite3 *db = [self get_db];
  1500. //
  1501. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1502. // [iSalesDB close_db:db];
  1503. // }
  1504. }
  1505. return orderpath;
  1506. }
  1507. //+(NSString*) getScanTempPath
  1508. //{
  1509. // return nil;
  1510. ////
  1511. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1512. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1513. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1514. //}
  1515. //+(void) moveScanTemp2Order
  1516. //{
  1517. // return;
  1518. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1519. ////
  1520. //// NSError *error = nil;
  1521. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1522. ////
  1523. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1524. //// // for(int i=0;i<arr_files.count;i++)
  1525. //// // {
  1526. //// // NSString* file=arr_files[i];
  1527. //// //
  1528. //// //
  1529. //// // }
  1530. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1531. ////
  1532. //
  1533. //}
  1534. //+(NSString*) getUserPath: (NSString*) user
  1535. //{
  1536. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1537. // NSString *documents = [paths objectAtIndex:0];
  1538. //
  1539. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1540. //
  1541. // NSString* servername = addressDic[@"name"];
  1542. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1543. //// NSString* user = RASingleton.sharedInstance.user;
  1544. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1545. // userpath = [userpath stringByAppendingPathComponent:user];
  1546. // return userpath;
  1547. //}
  1548. +(NSString*) getServerPath
  1549. {
  1550. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1551. NSString *documents = [paths objectAtIndex:0];
  1552. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1553. NSString* servername = addressDic[@"name"];
  1554. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1555. // NSString* user = RASingleton.sharedInstance.user;
  1556. // NSString* user = RASingleton.sharedInstance.user;
  1557. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1558. // userpath = [userpath stringByAppendingPathComponent:user];
  1559. NSFileManager* fileManager = [NSFileManager defaultManager];
  1560. BOOL bdir=YES;
  1561. if(! [fileManager fileExistsAtPath:serverpath isDirectory:&bdir])
  1562. {
  1563. NSError *error = nil;
  1564. [fileManager createDirectoryAtPath:serverpath withIntermediateDirectories:YES attributes:nil error:&error];
  1565. }
  1566. return serverpath;
  1567. }
  1568. +(NSString*) getUserPath
  1569. {
  1570. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1571. NSString *documents = [paths objectAtIndex:0];
  1572. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1573. NSString* servername = addressDic[@"name"];
  1574. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1575. // NSString* user = RASingleton.sharedInstance.user;
  1576. NSString* user = RASingleton.sharedInstance.user;
  1577. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1578. userpath = [userpath stringByAppendingPathComponent:user];
  1579. NSFileManager* fileManager = [NSFileManager defaultManager];
  1580. BOOL bdir=YES;
  1581. if(! [fileManager fileExistsAtPath:userpath isDirectory:&bdir])
  1582. {
  1583. NSError *error = nil;
  1584. [fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:&error];
  1585. }
  1586. return userpath;
  1587. }
  1588. +(bool) isSavedScanOrder:(NSString*) order_code
  1589. {
  1590. if(order_code == nil)
  1591. return false;
  1592. NSMutableDictionary * orderlist=[self getScanOrderList];
  1593. int count = [orderlist[@"count"] intValue];
  1594. for(int i=0;i<count;i++)
  1595. {
  1596. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1597. if([item[@"order_code"] isEqualToString: order_code ])
  1598. {
  1599. return true;
  1600. }
  1601. }
  1602. return false;
  1603. }
  1604. +(NSString*) getScanPath
  1605. {
  1606. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1607. if(appDelegate.order_code==nil)
  1608. {
  1609. appDelegate.order_code=[[NSUUID new] UUIDString];
  1610. }
  1611. return [self getScanOrderPath];
  1612. // else
  1613. // {
  1614. // [self createScanTempFolder];
  1615. // return [self getScanTempPath];
  1616. // }
  1617. }
  1618. +(void) scanCloseOrder
  1619. {
  1620. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1621. {
  1622. // 当前订单先关闭
  1623. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1624. appDelegate.order_code = nil;
  1625. // RASingleton.sharedInstance.scan_temp_code = nil;
  1626. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1627. { appDelegate.customerInfo = nil;
  1628. appDelegate.contact_id = nil;
  1629. }
  1630. RASingleton.sharedInstance.scan_cart = nil;
  1631. RASingleton.sharedInstance.price_type = 1;
  1632. // [appDelegate updateScanButton:false];
  1633. [appDelegate update_count_mark];
  1634. [appDelegate closeOrder];
  1635. }
  1636. }
  1637. +(NSString*) createScanTempCode
  1638. {
  1639. return [[NSUUID new] UUIDString];
  1640. }
  1641. +(NSString*) scanTemplatePath:(NSString*)file
  1642. {
  1643. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1644. NSString *documents = [paths objectAtIndex:0];
  1645. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1646. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1647. return templatefile;
  1648. }
  1649. +(void) realguestLogin
  1650. {
  1651. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1652. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1653. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1654. }
  1655. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1656. {
  1657. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1658. NSString *documents = [paths objectAtIndex:0];
  1659. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1660. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1661. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1662. return [dict mutableCopy];
  1663. }
  1664. +(void) deleteSubmittedOrder:(NSString*) order_code
  1665. {
  1666. NSString *userdir = [OLDataProvider getUserPath];
  1667. if(order_code.length==0)
  1668. return;
  1669. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1670. // NSString *documents = [paths objectAtIndex:0];
  1671. // if([appDelegate.order_code isEqualToString:order_code])
  1672. // {
  1673. // //要删除的订单是当前打开的订单;
  1674. // appDelegate.order_code = nil;
  1675. // appDelegate.customerInfo = nil;
  1676. // RASingleton.sharedInstance.scan_cart = nil;
  1677. // appDelegate.contact_id = nil;
  1678. // // [appDelegate updateScanButton:false];
  1679. //
  1680. // [appDelegate update_count_mark];
  1681. // }
  1682. //删除订单目录
  1683. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1684. //维护订单列表
  1685. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1686. int count = [submitlist[@"count"] intValue];
  1687. if(count==0)
  1688. {
  1689. // order list 为空,不用维护
  1690. #ifdef RA_NOTIFICATION
  1691. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1692. #else
  1693. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1694. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1695. #endif
  1696. return;
  1697. }
  1698. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1699. //
  1700. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1701. //
  1702. //
  1703. //
  1704. // // [orderlist addObject:appDelegate.order_code];
  1705. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1706. //
  1707. // }
  1708. // else
  1709. // {
  1710. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1711. //
  1712. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1713. //
  1714. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1715. // }
  1716. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1717. int newcount = 0;
  1718. for(int i=0;i<count;i++)
  1719. {
  1720. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1721. if([item[@"order_code"] isEqualToString: order_code ])
  1722. continue;
  1723. else
  1724. {
  1725. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1726. newcount++;
  1727. }
  1728. }
  1729. newlist[@"count"] = @(newcount);
  1730. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1731. [self saveScanSubmitList:newlist];
  1732. #ifdef RA_NOTIFICATION
  1733. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1734. #else
  1735. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1736. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1737. #endif
  1738. }
  1739. +(void) updateScanOrder:(NSString*) order_code
  1740. {
  1741. #ifdef SCANNER_ORDER
  1742. // NSString *userdir = [OLDataProvider getUserPath];
  1743. if(order_code.length==0)
  1744. return;
  1745. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1746. // NSString *documents = [paths objectAtIndex:0];
  1747. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1748. NSMutableDictionary* orderlist = [self getScanOrderList];
  1749. int count = [orderlist[@"count"] intValue];
  1750. if(count==0)
  1751. {
  1752. // order list 为空,不用维护
  1753. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1754. return;
  1755. }
  1756. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1757. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1758. //
  1759. //
  1760. //
  1761. // // [orderlist addObject:appDelegate.order_code];
  1762. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1763. //
  1764. // }
  1765. // else
  1766. // {
  1767. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1768. //
  1769. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1770. //
  1771. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1772. // }
  1773. //
  1774. //
  1775. // int count = [orderlist[@"count"] intValue];
  1776. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1777. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1778. float p =0;
  1779. float s =0;
  1780. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1781. {
  1782. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1783. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1784. // remove net price
  1785. // double msurcharge =0;
  1786. // int mcount = [cartitem[@"count"] intValue];
  1787. // NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1788. // if(RASingleton.sharedInstance.price_type==1)
  1789. // {
  1790. // if([cartitem[@"special_price"] boolValue])
  1791. // {
  1792. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1793. // }
  1794. // else
  1795. // {
  1796. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1797. // }
  1798. // }
  1799. // s+=msurcharge;
  1800. }
  1801. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1802. int order_qty=0;
  1803. for(int c=0;c<[section[@"count"] intValue];c++)
  1804. {
  1805. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1806. order_qty+=[item[@"count"] intValue];
  1807. }
  1808. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1809. // int newcount = 0;
  1810. for(int i=0;i<count;i++)
  1811. {
  1812. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1813. if([item[@"order_code"] isEqualToString: order_code ])
  1814. {
  1815. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1816. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1817. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1818. }
  1819. }
  1820. // newlist[@"count"] = @(newcount);
  1821. [self saveScanOrderList:orderlist];
  1822. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1823. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1824. #endif
  1825. }
  1826. +(void) deleteScanOrder:(NSString*) order_code
  1827. {
  1828. NSString *userdir = [OLDataProvider getUserPath];
  1829. if(order_code.length==0)
  1830. return;
  1831. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1832. // NSString *documents = [paths objectAtIndex:0];
  1833. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1834. if([appDelegate.order_code isEqualToString:order_code])
  1835. {
  1836. //要删除的订单是当前打开的订单;
  1837. // appDelegate.order_code = nil;
  1838. // appDelegate.customerInfo = nil;
  1839. // RASingleton.sharedInstance.scan_cart = nil;
  1840. // appDelegate.contact_id = nil;
  1841. [self scanCloseOrder];
  1842. // [appDelegate updateScanButton:false];
  1843. [appDelegate update_count_mark];
  1844. }
  1845. //删除订单目录
  1846. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1847. //维护订单列表
  1848. NSMutableDictionary* orderlist = [self getScanOrderList];
  1849. int count = [orderlist[@"count"] intValue];
  1850. if(count==0)
  1851. {
  1852. // order list 为空,不用维护
  1853. #ifdef RA_NOTIFICATION
  1854. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1855. #else
  1856. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1857. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1858. #endif
  1859. return;
  1860. }
  1861. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1862. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1863. //
  1864. //
  1865. //
  1866. // // [orderlist addObject:appDelegate.order_code];
  1867. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1868. //
  1869. // }
  1870. // else
  1871. // {
  1872. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1873. //
  1874. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1875. //
  1876. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1877. // }
  1878. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1879. int newcount = 0;
  1880. for(int i=0;i<count;i++)
  1881. {
  1882. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1883. if([item[@"order_code"] isEqualToString: order_code ])
  1884. continue;
  1885. else
  1886. {
  1887. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1888. newcount++;
  1889. }
  1890. }
  1891. newlist[@"count"] = @(newcount);
  1892. [self saveScanOrderList:newlist];
  1893. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1894. #ifdef RA_NOTIFICATION
  1895. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1896. #else
  1897. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1898. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1899. #endif
  1900. }
  1901. #ifdef SCANNER_ORDER
  1902. +(void) scanRefreshCart
  1903. {
  1904. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1905. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  1906. NSMutableDictionary * section = cart[@"section_0"];
  1907. NSMutableDictionary* newsection = [NSMutableDictionary new];
  1908. int count = [section[@"count"] intValue];
  1909. int newcount = 0;
  1910. for(int i=0;i<count;i++)
  1911. {
  1912. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1913. NSString* model = item[@"model"];
  1914. NSDictionary* modeljson=appDelegate.scan_model[model];
  1915. if(modeljson==nil)
  1916. continue;
  1917. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  1918. cartitem[@"note"]=item[@"note"];
  1919. cartitem[@"discount"]=item[@"discount"];
  1920. cart[@"count"]=item[@"count"];
  1921. // [item removeObjectForKey:@"discount"];
  1922. // [item removeObjectForKey:@"note"];
  1923. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  1924. newcount++;
  1925. }
  1926. newsection[@"count"]=@(newcount);
  1927. newsection[@"available"] = @(1);
  1928. newsection[@"title"] = @"Available List";
  1929. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  1930. }
  1931. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  1932. {
  1933. // _modelJson = modelJson;
  1934. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1935. // self.labelPrice0.text = appDelegate.price0_name;
  1936. // self.labelPrice1.text = appDelegate.price1_name;
  1937. // self.labelPrice2.text = appDelegate.price2_name;
  1938. // self.labelPrice3.text = appDelegate.price3_name;
  1939. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1940. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  1941. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  1942. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  1943. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  1944. #ifdef DEBUG
  1945. DebugLog(@"%@", [RAConvertor dict2string:_modelJson]);
  1946. #endif
  1947. NSString * port=_modelJson[@"port"];
  1948. if(port.length==0)
  1949. port = @"N/A";
  1950. NSString * origin=_modelJson[@"origin"];
  1951. if(origin.length==0)
  1952. origin = @"N/A";
  1953. NSString * dimension=_modelJson[@"dimension"];
  1954. if(dimension.length==0)
  1955. dimension = @"N/A";
  1956. // _labelModel.text=_modelJson[@"model"];
  1957. // _labelDescription.text=_modelJson[@"description"];
  1958. // _labelDimension.text=dimension;
  1959. // _labelCuft.text=_modelJson[@"unit_cuft"];
  1960. // _labelOrigin.text=origin;
  1961. // _labelPort.text=port;
  1962. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  1963. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  1964. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  1965. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  1966. if(![price0 isEqualToString: @"N/A"])
  1967. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  1968. if(![price1 isEqualToString: @"N/A"])
  1969. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  1970. if(![price2 isEqualToString: @"N/A"])
  1971. {
  1972. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  1973. // if(RASingleton.sharedInstance.price_type==1)
  1974. _modelJson [@"special_price"] = @true;
  1975. }
  1976. if(![price3 isEqualToString: @"N/A"])
  1977. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  1978. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  1979. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  1980. {
  1981. // if(RASingleton.sharedInstance.price_type==1)
  1982. _modelJson [@"net_price"] = @true;
  1983. // remove net price
  1984. _modelJson [@"net_price"] = @false;
  1985. }
  1986. //
  1987. // if(![price1 isEqualToString:@"N/A"])
  1988. // {
  1989. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  1990. // _modelJson[@"price2"]= price2;
  1991. // }
  1992. // else
  1993. // {
  1994. // price2=@"N/A";
  1995. // }
  1996. //
  1997. // if([price0 isEqualToString:@"N/A"])
  1998. // _labelPriceCTNR.text= @"";//price0;
  1999. // else
  2000. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2001. //
  2002. // if([price3 isEqualToString:@"N/A"])
  2003. // _labelPriceNCA.text= price3;
  2004. // else
  2005. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2006. // _labelPrice25p.text=price2;
  2007. //
  2008. // if([_modelJson [@"special_price"] boolValue])
  2009. // {
  2010. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2011. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2012. // else
  2013. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2014. // }
  2015. // else
  2016. // {
  2017. // _labelPriceSpecial.text=@"";//@"N/A";
  2018. // }
  2019. //
  2020. // if([_modelJson [@"net_price"] boolValue])
  2021. // {
  2022. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2023. //// {
  2024. //// _labelPriceNet.text=_modelJson[@"price1"];
  2025. ////// _labelPriceNCA.text=@"N/A";
  2026. //// }
  2027. //// else
  2028. //// {
  2029. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2030. ////// _labelPriceNCA.text=@"N/A";
  2031. //// }
  2032. //
  2033. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2034. // {
  2035. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2036. //// _labelPriceNCA.text=@"N/A";
  2037. // }
  2038. // else
  2039. // {
  2040. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2041. //// _labelPriceNCA.text=@"N/A";
  2042. // }
  2043. //
  2044. // }
  2045. // else
  2046. // {
  2047. // _labelPriceNet.text=@"";//@"N/A";
  2048. // }
  2049. //
  2050. // {
  2051. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2052. // {
  2053. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2054. //// _labelPriceNCA.text=@"N/A";
  2055. // }
  2056. // else
  2057. // {
  2058. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2059. //// _labelPriceNet.text=@"N/A";
  2060. // }
  2061. // }
  2062. NSString * available=_modelJson[@"available"];
  2063. if(available.length==0)
  2064. available = @"N/A";
  2065. // _labelAvailable.text=available;
  2066. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2067. if(unit_cuft.length==0)
  2068. unit_cuft = @"N/A";
  2069. else
  2070. {
  2071. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2072. }
  2073. NSString * cell_price;
  2074. if(RASingleton.sharedInstance.price_type==1)
  2075. {
  2076. // if(_modelJson [@"net_price"])
  2077. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2078. // else if(_modelJson [@"special_price"])
  2079. // {
  2080. // unit_price= _modelJson[@"price2"];
  2081. // }
  2082. // else
  2083. // {
  2084. // unit_price= _modelJson[@"price1"];
  2085. // }
  2086. }
  2087. else
  2088. {
  2089. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2090. }
  2091. NSString * mpack=_modelJson[@"stockUom"];
  2092. if([mpack isEqualToString:@"N/A"])
  2093. mpack=@"1";
  2094. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2095. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2096. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2097. NSMutableDictionary* jitem = nil;
  2098. jitem = [_modelJson mutableCopy];
  2099. int stockUom =[jitem[@"stockUom"] intValue];
  2100. if(stockUom==0)
  2101. stockUom=1;
  2102. // bool newitem = true;
  2103. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2104. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2105. // for(int i=0;i<count;i++)
  2106. // {
  2107. //
  2108. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2109. //
  2110. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2111. // {
  2112. //
  2113. //// int oldcount = [litem[@"stockUom"] intValue];
  2114. // newitem = false;
  2115. //
  2116. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2117. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2118. //
  2119. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2120. // break;
  2121. //
  2122. //
  2123. // }
  2124. //
  2125. //
  2126. //
  2127. // }
  2128. // if(true)
  2129. // {
  2130. jitem[@"count"]=@(stockUom);
  2131. jitem[@"check"]=@(true);
  2132. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2133. NSString * unit_price;
  2134. if(RASingleton.sharedInstance.price_type==0)
  2135. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2136. else if(_modelJson [@"special_price"])
  2137. {
  2138. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2139. }
  2140. else if(_modelJson [@"net_price"])
  2141. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2142. else
  2143. {
  2144. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2145. }
  2146. if([unit_price isEqualToString:@"N/A"])
  2147. unit_price = @"0";
  2148. else
  2149. {
  2150. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2151. }
  2152. jitem[@"unit_price"] = unit_price;
  2153. jitem[@"erp_unit_price"] = unit_price;
  2154. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2155. // section[@"count"]= @(count+1);
  2156. // count++;
  2157. // }
  2158. return jitem;
  2159. }
  2160. +(void) saveScanCart:(NSMutableDictionary*) cart
  2161. {
  2162. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2163. if(appDelegate.order_code.length<=0)
  2164. return;
  2165. if(cart==nil)
  2166. return;
  2167. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2168. // NSString *documents = [paths objectAtIndex:0];
  2169. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2170. if(RASingleton.sharedInstance.price_type==0)
  2171. {
  2172. NSMutableDictionary * section = cart[@"section_0"];
  2173. int count = [section[@"count"] intValue];
  2174. for(int i=0;i<count;i++)
  2175. {
  2176. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2177. [item removeObjectForKey:@"discount"];
  2178. [item removeObjectForKey:@"note"];
  2179. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2180. }
  2181. cart[@"section_0"]=section;
  2182. }
  2183. NSString *orderdir = [self getScanPath];
  2184. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2185. cart[@"price_group"] = appDelegate.price_group;
  2186. [RAUtils dicttofile:cartpath dict:cart];
  2187. }
  2188. #endif
  2189. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2190. {
  2191. assert(add_params[@"user"]!=nil);
  2192. // assert(add_params[@"contact_id"]!=nil);
  2193. // assert(add_params[@"password"]!=nil);
  2194. NSString* serial= [[NSUUID UUID] UUIDString];
  2195. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2196. NSString *cachefolder = [paths objectAtIndex:0];
  2197. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2198. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2199. NSFileManager* fileManager = [NSFileManager defaultManager];
  2200. BOOL bdir=YES;
  2201. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2202. {
  2203. NSError *error = nil;
  2204. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2205. if(!bsuccess)
  2206. DebugLog(@"Create UPLOAD folder failed");
  2207. }
  2208. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2209. ret[@"contact"]=[self prepareContact:serial];
  2210. ret[@"wishlist"]=[self prepareWishlist:serial];
  2211. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2212. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2213. ret[@"view_portfolio"] = [self preparePDF:serial];
  2214. NSString* str= [RAConvertor dict2string:ret];
  2215. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2216. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2217. NSError *error=nil;
  2218. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2219. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2220. ZipArchive* zip = [[ZipArchive alloc] init];
  2221. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2222. for(int i=0;i<arr_files.count;i++)
  2223. {
  2224. NSString* file=arr_files[i];
  2225. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2226. }
  2227. if( ![zip CloseZipFile2] )
  2228. {
  2229. zippath = @"";
  2230. }
  2231. ret[@"file"]=zippath;
  2232. return ret;
  2233. }
  2234. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2235. {
  2236. if(filename.length==0)
  2237. return false;
  2238. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2239. bool ret=false;
  2240. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2241. NSString *cachefolder = [paths objectAtIndex:0];
  2242. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2243. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2244. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2245. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2246. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2247. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2248. NSFileManager* fileManager = [NSFileManager defaultManager];
  2249. BOOL bdir=NO;
  2250. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2251. {
  2252. NSError *error = nil;
  2253. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2254. {
  2255. ret=false;
  2256. }
  2257. else
  2258. {
  2259. ret=true;
  2260. }
  2261. }
  2262. return ret;
  2263. }
  2264. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2265. {
  2266. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2267. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2268. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2269. NSString* where=@" 1=1";
  2270. if (ver!=nil) {
  2271. where=@"is_dirty=1";
  2272. }
  2273. sqlite3 *db = [iSalesDB get_db];
  2274. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2275. sqlite3_stmt * statement;
  2276. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2277. int count=0;
  2278. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2279. if ( dbresult== SQLITE_OK)
  2280. {
  2281. while (sqlite3_step(statement) == SQLITE_ROW)
  2282. {
  2283. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2284. int _id = sqlite3_column_int(statement, 0);
  2285. int product_id = sqlite3_column_int(statement, 1);
  2286. int item_id = sqlite3_column_int(statement, 2);
  2287. int qty = sqlite3_column_int(statement, 3);
  2288. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2289. NSString* nscreate_time=nil;
  2290. if(create_time!=nil)
  2291. {
  2292. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2293. }
  2294. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2295. NSString* nsmodify_time=nil;
  2296. if(modify_time!=nil)
  2297. {
  2298. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2299. }
  2300. int is_delete = sqlite3_column_int(statement, 6);
  2301. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2302. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2303. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2304. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2305. itemjson[@"create_time"]=nscreate_time;
  2306. itemjson[@"modify_time"]=nsmodify_time;
  2307. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2308. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2309. count++;
  2310. }
  2311. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2312. sqlite3_finalize(statement);
  2313. }
  2314. ret[@"count"]=[NSNumber numberWithInt:count ];
  2315. [iSalesDB close_db:db];
  2316. return ret;
  2317. }
  2318. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2319. {
  2320. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2321. // UIApplication * app = [UIApplication sharedApplication];
  2322. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2323. assert(add_params[@"user"]!=nil);
  2324. // assert(add_params[@"password"]!=nil);
  2325. 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 ];
  2326. // 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 ];
  2327. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2328. DebugLog(@"offline_login sql:%@",sqlQuery);
  2329. sqlite3_stmt * statement;
  2330. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2331. int count=0;
  2332. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2333. if ( dbresult== SQLITE_OK)
  2334. {
  2335. while (sqlite3_step(statement) == SQLITE_ROW)
  2336. {
  2337. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2338. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2339. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2340. int product_id = sqlite3_column_int(statement, 0);
  2341. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2342. int item_id = sqlite3_column_int(statement, 7);
  2343. NSString* Price=nil;
  2344. if(str_price==nil)
  2345. {
  2346. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2347. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2348. if(price==nil)
  2349. Price=@"No Price.";
  2350. else
  2351. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2352. }
  2353. else
  2354. {
  2355. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2356. }
  2357. double discount = sqlite3_column_double(statement, 2);
  2358. int item_count = sqlite3_column_int(statement, 3);
  2359. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2360. NSString *nsline_note=nil;
  2361. if(line_note!=nil)
  2362. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2363. char *name = (char*)sqlite3_column_text(statement, 5);
  2364. NSString *nsname=nil;
  2365. if(name!=nil)
  2366. nsname= [[NSString alloc]initWithUTF8String:name];
  2367. char *description = (char*)sqlite3_column_text(statement, 6);
  2368. NSString *nsdescription=nil;
  2369. if(description!=nil)
  2370. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2371. // int stockUom = sqlite3_column_int(statement, 8);
  2372. // int _id = sqlite3_column_int(statement, 9);
  2373. //
  2374. //
  2375. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2376. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2377. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2378. // int carton=[bsubtotaljson[@"carton"] intValue];
  2379. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2380. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2381. itemjson[@"model"]=nsname;
  2382. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2383. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2384. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2385. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2386. // itemjson[@"check"]=@"true";
  2387. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2388. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2389. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2390. itemjson[@"unit_price"]=Price;
  2391. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2392. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2393. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2394. itemjson[@"note"]=nsline_note;
  2395. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2396. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2397. count++;
  2398. }
  2399. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2400. sqlite3_finalize(statement);
  2401. }
  2402. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2403. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2404. return ret;
  2405. }
  2406. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2407. {
  2408. assert(add_params[@"user"]!=nil);
  2409. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2410. sqlite3 *db = [iSalesDB get_db];
  2411. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id,discount from offline_order where order_id is null";
  2412. // for(int i=0;i<arr_soid.count;i++)
  2413. sqlite3_stmt * statement;
  2414. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2415. //int count=0;
  2416. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2417. if ( dbresult== SQLITE_OK)
  2418. {
  2419. int count=0;
  2420. while (sqlite3_step(statement) == SQLITE_ROW)
  2421. {
  2422. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2423. int _id = sqlite3_column_int(statement, 0);
  2424. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2425. NSString* nssync_data=nil;
  2426. if(sync_data!=nil)
  2427. {
  2428. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2429. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2430. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2431. }
  2432. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2433. NSString* nsimg_1=nil;
  2434. if(img_1!=nil)
  2435. {
  2436. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2437. [self copy_upImg:serial file:nsimg_1];
  2438. }
  2439. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2440. NSString* nsso_no=nil;
  2441. if(so_no!=nil)
  2442. {
  2443. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2444. }
  2445. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2446. itemjson[@"order_type"]=@"submit order";
  2447. else
  2448. itemjson[@"order_type"]=@"archive order";
  2449. double discount =sqlite3_column_double(statement, 4);
  2450. itemjson[@"discount"]=@(discount);
  2451. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2452. itemjson[@"json_data"]= nssync_data;
  2453. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2454. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2455. count++;
  2456. }
  2457. ret[@"count"]=[NSNumber numberWithInt:count ];
  2458. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2459. }
  2460. sqlite3_finalize(statement);
  2461. [iSalesDB close_db:db];
  2462. return ret;
  2463. }
  2464. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2465. {
  2466. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2467. sqlite3 *db = [iSalesDB get_db];
  2468. // UIApplication * app = [UIApplication sharedApplication];
  2469. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2470. 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";
  2471. sqlite3_stmt * statement;
  2472. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2473. int count=0;
  2474. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2475. if ( dbresult== SQLITE_OK)
  2476. {
  2477. while (sqlite3_step(statement) == SQLITE_ROW)
  2478. {
  2479. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2480. int _id = sqlite3_column_int(statement, 0);
  2481. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2482. NSString* nsimg_0=nil;
  2483. if(img_0!=nil)
  2484. {
  2485. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2486. [self copy_upImg:serial file:nsimg_0];
  2487. }
  2488. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2489. NSString* nsimg_1=nil;
  2490. if(img_1!=nil)
  2491. {
  2492. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2493. [self copy_upImg:serial file:nsimg_1];
  2494. }
  2495. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2496. NSString* nsimg_2=nil;
  2497. if(img_2!=nil)
  2498. {
  2499. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2500. [self copy_upImg:serial file:nsimg_2];
  2501. }
  2502. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2503. NSString* nssync_data=nil;
  2504. if(sync_data!=nil)
  2505. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2506. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2507. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2508. itemjson[@"json_data"]= nssync_data;
  2509. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2510. count++;
  2511. }
  2512. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2513. sqlite3_finalize(statement);
  2514. }
  2515. ret[@"count"]=[NSNumber numberWithInt:count ];
  2516. [iSalesDB close_db:db];
  2517. return ret;
  2518. }
  2519. +(bool) check_offlinedata
  2520. {
  2521. // UIApplication * app = [UIApplication sharedApplication];
  2522. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2523. sqlite3 *db = [iSalesDB get_db];
  2524. NSString * where=@"1=1";
  2525. // if(RASingleton.sharedInstance.user!=nil)
  2526. // where=[NSString stringWithFormat:@"lower(username)='%@'",RASingleton.sharedInstance.user.lowercaseString];
  2527. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2528. [iSalesDB close_db:db];
  2529. if(count==0)
  2530. {
  2531. return false;
  2532. }
  2533. return true;
  2534. //
  2535. //[iSalesDB close_db:db];
  2536. }
  2537. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2538. {
  2539. // UIApplication * app = [UIApplication sharedApplication];
  2540. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2541. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2542. sqlite3 *db = [iSalesDB get_db];
  2543. NSString* collectId=params[@"collectId"];
  2544. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2545. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2546. [iSalesDB execSql:sqlQuery db:db];
  2547. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2548. [iSalesDB close_db:db];
  2549. // appDelegate.wish_count =count;
  2550. //
  2551. // [appDelegate update_count_mark];
  2552. ret[@"result"]= [NSNumber numberWithInt:2];
  2553. ret[@"wish_count"]=@(count);
  2554. return ret;
  2555. }
  2556. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2557. //{
  2558. //
  2559. // UIApplication * app = [UIApplication sharedApplication];
  2560. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2561. //
  2562. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2563. // sqlite3 *db = [iSalesDB get_db];
  2564. // NSString* product_id=params[@"product_id"];
  2565. //
  2566. //
  2567. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2568. //
  2569. //
  2570. // for(int i=0;i<arr.count;i++)
  2571. // {
  2572. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2573. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2574. // if(count==0)
  2575. // {
  2576. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2577. // [iSalesDB execSql:sqlQuery db:db];
  2578. // }
  2579. // }
  2580. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2581. // [iSalesDB close_db:db];
  2582. //
  2583. // appDelegate.wish_count =count;
  2584. //
  2585. // [appDelegate update_count_mark];
  2586. // ret[@"result"]= [NSNumber numberWithInt:2];
  2587. // return ret;
  2588. // //
  2589. // //return ret;
  2590. //}
  2591. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2592. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2593. DebugLog(@"time interval: %lf",interval);
  2594. }
  2595. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2596. //{
  2597. // UIApplication * app = [UIApplication sharedApplication];
  2598. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2599. //
  2600. // int sort = [[params objectForKey:@"sort"] intValue];
  2601. // NSString *sort_str = @"";
  2602. // switch (sort) {
  2603. // case 0:{
  2604. // sort_str = @"order by w.modify_time desc";
  2605. // }
  2606. // break;
  2607. // case 1:{
  2608. // sort_str = @"order by w.modify_time asc";
  2609. // }
  2610. // break;
  2611. // case 2:{
  2612. // sort_str = @"order by m.name asc";
  2613. // }
  2614. // break;
  2615. // case 3:{
  2616. // sort_str = @"order by m.name desc";
  2617. // }
  2618. // break;
  2619. // case 4:{
  2620. // sort_str = @"order by m.description asc";
  2621. // }
  2622. // break;
  2623. //
  2624. // default:
  2625. // break;
  2626. //
  2627. // }
  2628. //
  2629. //
  2630. //// NSString* user = RASingleton.sharedInstance.user;
  2631. //
  2632. // sqlite3 *db = [iSalesDB get_db];
  2633. //
  2634. // // order by w.create_time
  2635. // 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];
  2636. //
  2637. //// 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];
  2638. //
  2639. //
  2640. // sqlite3_stmt * statement;
  2641. //
  2642. // NSDate *date1 = [NSDate date];
  2643. //// NSDate *date2 = nil;
  2644. //
  2645. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2646. // int count=0;
  2647. //
  2648. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2649. // {
  2650. //
  2651. //
  2652. // while (sqlite3_step(statement) == SQLITE_ROW)
  2653. // {
  2654. //
  2655. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2656. //
  2657. // int product_id = sqlite3_column_double(statement, 0);
  2658. //
  2659. //
  2660. //
  2661. //
  2662. //
  2663. // char *description = (char*)sqlite3_column_text(statement, 1);
  2664. // if(description==nil)
  2665. // description= "";
  2666. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2667. //
  2668. // int item_id = sqlite3_column_double(statement, 2);
  2669. //
  2670. // NSDate *date_image = [NSDate date];
  2671. //
  2672. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2673. //
  2674. // printf("image : ");
  2675. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2676. //
  2677. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2678. // // if(url==nil)
  2679. // // url="";
  2680. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2681. //
  2682. // int qty = sqlite3_column_int(statement, 3);
  2683. //
  2684. //
  2685. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2686. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2687. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2688. // item[@"description"]= nsdescription;
  2689. // item[@"img"]= nsurl;
  2690. //
  2691. //
  2692. //
  2693. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2694. // count++;
  2695. //
  2696. // }
  2697. // printf("total time:");
  2698. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2699. //
  2700. // ret[@"count"]= [NSNumber numberWithInt:count];
  2701. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2702. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2703. // ret[@"result"]= [NSNumber numberWithInt:2];
  2704. //
  2705. //
  2706. // appDelegate.wish_count =count;
  2707. //
  2708. // [appDelegate update_count_mark];
  2709. // sqlite3_finalize(statement);
  2710. //
  2711. //
  2712. //
  2713. //
  2714. // }
  2715. //
  2716. // [iSalesDB close_db:db];
  2717. //
  2718. // return ret;
  2719. //}
  2720. +(NSDictionary*) offline_notimpl
  2721. {
  2722. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2723. ret[@"result"]=@"8";
  2724. ret[@"err_msg"]=@"offline mode does not support this function.";
  2725. return ret;
  2726. }
  2727. +(NSDictionary*) offline_home
  2728. {
  2729. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2730. NSString *cachefolder = [paths objectAtIndex:0];
  2731. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2732. NSData* json =nil;
  2733. json=[NSData dataWithContentsOfFile:img_cache];
  2734. if(json==nil)
  2735. return nil;
  2736. NSError *error=nil;
  2737. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2738. return menu;
  2739. }
  2740. +(NSDictionary*) offline_category_menu
  2741. {
  2742. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2743. NSString *cachefolder = [paths objectAtIndex:0];
  2744. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2745. NSData* json =nil;
  2746. json=[NSData dataWithContentsOfFile:img_cache];
  2747. if(json==nil)
  2748. return nil;
  2749. NSError *error=nil;
  2750. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2751. return menu;
  2752. }
  2753. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2754. {
  2755. NSString* offline_command=params[@"offline_Command"];
  2756. NSDictionary* ret=nil;
  2757. if([offline_command isEqualToString:@"model_NIYMAL"])
  2758. {
  2759. NSString* category = params[@"category"];
  2760. ret = [self refresh_model_NIYMAL:category];
  2761. }
  2762. return ret;
  2763. }
  2764. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2765. {
  2766. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2767. [ret setValue:@"2" forKey:@"result"];
  2768. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2769. sqlite3* db= [iSalesDB get_db];
  2770. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2771. [iSalesDB close_db:db];
  2772. [ret setObject:detail1_section forKey:@"detail_1"];
  2773. return ret;
  2774. }
  2775. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2776. {
  2777. //assert(user!=nil);
  2778. // UIApplication * app = [UIApplication sharedApplication];
  2779. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2780. // NSArray* arr1 = [self get_user_all_price_type];
  2781. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2782. // NSSet *set1 = [NSSet setWithArray:arr1];
  2783. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2784. // if(appDelegate.contact_id==nil)
  2785. // set2=[set1 mutableCopy];
  2786. // else
  2787. // [set2 intersectsSet:set1];
  2788. // NSArray *retarr = [set2 allObjects];
  2789. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2790. // sqlite3 *db = [iSalesDB get_db];
  2791. NSString* sqlQuery = nil;
  2792. if(contact_id==nil)
  2793. {
  2794. if(!blogin)
  2795. return nil;
  2796. 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];
  2797. }
  2798. else
  2799. 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];
  2800. sqlite3_stmt * statement;
  2801. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2802. int count=0;
  2803. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2804. {
  2805. while (sqlite3_step(statement) == SQLITE_ROW)
  2806. {
  2807. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2808. char *name = (char*)sqlite3_column_text(statement, 0);
  2809. if(name==nil)
  2810. name="";
  2811. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2812. // double price = sqlite3_column_double(statement, 1);
  2813. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2814. // if(isnull==nil)
  2815. // item[nsname]= @"No Price";
  2816. // else
  2817. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2818. char *price = (char*)sqlite3_column_text(statement, 1);
  2819. if(price!=nil)
  2820. {
  2821. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2822. nsprice=[AESCrypt fastdecrypt:nsprice];
  2823. if(nsprice.length>0)
  2824. {
  2825. double dp= [nsprice doubleValue];
  2826. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2827. }
  2828. }
  2829. else
  2830. {
  2831. item[nsname]= @"No Price";
  2832. }
  2833. // int type= sqlite3_column_int(statement, 2);
  2834. //item[@"type"]=@"price";
  2835. // item[nsname]= nsprice;
  2836. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2837. count++;
  2838. }
  2839. ret[@"count"]= [NSNumber numberWithInt:count];
  2840. sqlite3_finalize(statement);
  2841. }
  2842. // [iSalesDB close_db:db];
  2843. return ret;
  2844. }
  2845. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2846. //{
  2847. // NSArray* arr1 = [self get_user_all_price_type:db];
  2848. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2849. //
  2850. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2851. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2852. // // [set2 intersectsSet:set1];
  2853. // //
  2854. // //
  2855. // // NSArray *retarr = [set2 allObjects];
  2856. //
  2857. // NSString* whereprice=nil;
  2858. // if(contact_id==nil)
  2859. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2860. // else
  2861. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2862. //
  2863. //
  2864. // // sqlite3 *db = [iSalesDB get_db];
  2865. //
  2866. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2867. // sqlite3_stmt * statement;
  2868. //
  2869. //
  2870. // NSNumber* ret = nil;
  2871. // double dprice=DBL_MAX;
  2872. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2873. // {
  2874. //
  2875. //
  2876. // while (sqlite3_step(statement) == SQLITE_ROW)
  2877. // {
  2878. //
  2879. // // double val = sqlite3_column_double(statement, 0);
  2880. // char *price = (char*)sqlite3_column_text(statement, 0);
  2881. // if(price!=nil)
  2882. // {
  2883. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2884. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2885. // if(nsprice.length>0)
  2886. // {
  2887. // double dp= [nsprice doubleValue];
  2888. // if(dp<dprice)
  2889. // dprice=dp;
  2890. // }
  2891. // }
  2892. // }
  2893. //
  2894. //
  2895. //
  2896. //
  2897. // sqlite3_finalize(statement);
  2898. //
  2899. //
  2900. //
  2901. //
  2902. // }
  2903. //
  2904. // // [iSalesDB close_db:db];
  2905. //
  2906. // if(dprice==DBL_MAX)
  2907. // ret= nil;
  2908. // else
  2909. // ret= [NSNumber numberWithDouble:dprice];
  2910. // return ret;
  2911. //}
  2912. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2913. {
  2914. assert(user!=nil);
  2915. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2916. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2917. NSString* whereprice=nil;
  2918. if(contact_id==nil)
  2919. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2920. else
  2921. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2922. // sqlite3 *db = [iSalesDB get_db];
  2923. NSString *productIdCondition = @"1 = 1";
  2924. if (product_id) {
  2925. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2926. }
  2927. NSString *itemIdCondition = @"";
  2928. if (item_id) {
  2929. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2930. }
  2931. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2932. sqlite3_stmt * statement;
  2933. NSNumber* ret = nil;
  2934. double dprice=DBL_MAX;
  2935. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2936. {
  2937. while (sqlite3_step(statement) == SQLITE_ROW)
  2938. {
  2939. // double val = sqlite3_column_double(statement, 0);
  2940. char *price = (char*)sqlite3_column_text(statement, 0);
  2941. if(price!=nil)
  2942. {
  2943. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2944. nsprice=[AESCrypt fastdecrypt:nsprice];
  2945. if(nsprice.length>0)
  2946. {
  2947. double dp= [nsprice doubleValue];
  2948. if(dp<dprice)
  2949. dprice=dp;
  2950. }
  2951. }
  2952. }
  2953. sqlite3_finalize(statement);
  2954. }
  2955. // [iSalesDB close_db:db];
  2956. if(dprice==DBL_MAX)
  2957. ret= nil;
  2958. else
  2959. ret= [NSNumber numberWithDouble:dprice];
  2960. return ret;
  2961. }
  2962. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2963. {
  2964. assert(user!=nil);
  2965. NSArray* ret=nil;
  2966. // sqlite3 *db = [iSalesDB get_db];
  2967. // no customer assigned , use login user contact_id
  2968. // UIApplication * app = [UIApplication sharedApplication];
  2969. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2970. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2971. sqlite3_stmt * statement;
  2972. // int count=0;
  2973. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2974. {
  2975. if (sqlite3_step(statement) == SQLITE_ROW)
  2976. {
  2977. char *val = (char*)sqlite3_column_text(statement, 0);
  2978. if(val==nil)
  2979. val="";
  2980. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2981. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2982. }
  2983. sqlite3_finalize(statement);
  2984. }
  2985. // [iSalesDB close_db:db];
  2986. return ret;
  2987. }
  2988. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2989. {
  2990. // sqlite3 *db = [iSalesDB get_db];
  2991. if(contact_id==nil)
  2992. {
  2993. // no customer assigned , use login user contact_id
  2994. // UIApplication * app = [UIApplication sharedApplication];
  2995. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2996. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2997. sqlite3_stmt * statement;
  2998. // int count=0;
  2999. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3000. {
  3001. if (sqlite3_step(statement) == SQLITE_ROW)
  3002. {
  3003. char *val = (char*)sqlite3_column_text(statement, 0);
  3004. if(val==nil)
  3005. val="";
  3006. contact_id = [[NSString alloc]initWithUTF8String:val];
  3007. }
  3008. sqlite3_finalize(statement);
  3009. }
  3010. if(contact_id.length<=0)
  3011. {
  3012. // [iSalesDB close_db:db];
  3013. return nil;
  3014. }
  3015. }
  3016. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3017. sqlite3_stmt * statement;
  3018. NSArray* ret=nil;
  3019. // int count=0;
  3020. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3021. {
  3022. if (sqlite3_step(statement) == SQLITE_ROW)
  3023. {
  3024. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3025. if(price_type==nil)
  3026. price_type="";
  3027. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3028. if(nsprice_type.length>0)
  3029. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3030. else
  3031. ret=nil;
  3032. }
  3033. sqlite3_finalize(statement);
  3034. }
  3035. // [iSalesDB close_db:db];
  3036. return ret;
  3037. }
  3038. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3039. {
  3040. NSString* ret= nil;
  3041. // sqlite3 *db = [iSalesDB get_db];
  3042. NSString *sqlQuery = nil;
  3043. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3044. // 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;
  3045. // 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
  3046. if(product_id==nil && model_name)
  3047. 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;
  3048. else if (product_id)
  3049. 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
  3050. sqlite3_stmt * statement;
  3051. // int count=0;
  3052. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3053. {
  3054. if (sqlite3_step(statement) == SQLITE_ROW)
  3055. {
  3056. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3057. if(imgurl==nil)
  3058. imgurl="";
  3059. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3060. ret=nsimgurl;
  3061. }
  3062. sqlite3_finalize(statement);
  3063. }
  3064. else
  3065. {
  3066. [ret setValue:@"8" forKey:@"result"];
  3067. }
  3068. // [iSalesDB close_db:db];
  3069. DebugLog(@"data string: %@",ret );
  3070. return ret;
  3071. }
  3072. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3073. {
  3074. NSString* ret= nil;
  3075. sqlite3 *db = [iSalesDB get_db];
  3076. NSString *sqlQuery = nil;
  3077. if(product_id==nil)
  3078. if(upc_code==nil)
  3079. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3080. else
  3081. 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='%@';
  3082. else
  3083. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3084. sqlite3_stmt * statement;
  3085. // int count=0;
  3086. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3087. {
  3088. if (sqlite3_step(statement) == SQLITE_ROW)
  3089. {
  3090. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3091. if(default_category==nil)
  3092. default_category="";
  3093. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3094. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3095. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3096. ret=nsdefault_category;
  3097. }
  3098. sqlite3_finalize(statement);
  3099. }
  3100. else
  3101. {
  3102. [ret setValue:@"8" forKey:@"result"];
  3103. }
  3104. [iSalesDB close_db:db];
  3105. DebugLog(@"data string: %@",ret );
  3106. return ret;
  3107. }
  3108. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3109. NSString *sql = nil;
  3110. if (product_id != nil) {
  3111. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3112. } else {
  3113. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3114. }
  3115. NSString *result_set = [iSalesDB jk_queryText:sql];
  3116. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3117. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3118. return result_set;
  3119. }
  3120. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3121. //{
  3122. //
  3123. //
  3124. // assert(params[@"user"]!=nil);
  3125. //
  3126. //
  3127. //
  3128. // NSString* model_name = [params valueForKey:@"product_name"];
  3129. //
  3130. // NSString* product_id = [params valueForKey:@"product_id"];
  3131. //
  3132. // NSString* category = [params valueForKey:@"category"];
  3133. //
  3134. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3135. // if(category==nil) {
  3136. // category = default_category_id;
  3137. // } else {
  3138. //
  3139. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3140. // // 参数重有多个category id
  3141. // if (arr_0.count > 1) {
  3142. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3143. //
  3144. // // 取交集
  3145. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3146. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3147. // [set_0 intersectSet:set_1];
  3148. //
  3149. // if (set_0.count == 1) {
  3150. // category = (NSString *)[set_0 anyObject];
  3151. // } else {
  3152. // if ([set_0 containsObject:default_category_id]) {
  3153. // category = default_category_id;
  3154. // } else {
  3155. // category = (NSString *)[set_0 anyObject];
  3156. // }
  3157. // }
  3158. // }
  3159. // }
  3160. //
  3161. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3162. //
  3163. //
  3164. //
  3165. //
  3166. //
  3167. //
  3168. // sqlite3 *db = [iSalesDB get_db];
  3169. //
  3170. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3171. //
  3172. //
  3173. // NSString *sqlQuery = nil;
  3174. //
  3175. // if(product_id==nil)
  3176. // 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='%@';
  3177. // else
  3178. //
  3179. // 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=%@;
  3180. //
  3181. //
  3182. // sqlite3_stmt * statement;
  3183. // [ret setValue:@"2" forKey:@"result"];
  3184. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3185. //
  3186. // // int count=0;
  3187. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3188. // {
  3189. //
  3190. //
  3191. // if (sqlite3_step(statement) == SQLITE_ROW)
  3192. // {
  3193. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3194. //
  3195. //
  3196. // char *name = (char*)sqlite3_column_text(statement, 0);
  3197. // if(name==nil)
  3198. // name="";
  3199. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3200. //
  3201. // char *description = (char*)sqlite3_column_text(statement, 1);
  3202. // if(description==nil)
  3203. // description="";
  3204. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3205. //
  3206. //
  3207. // int product_id = sqlite3_column_int(statement, 2);
  3208. //
  3209. //
  3210. // char *color = (char*)sqlite3_column_text(statement, 3);
  3211. // if(color==nil)
  3212. // color="";
  3213. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3214. // //
  3215. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3216. // // if(legcolor==nil)
  3217. // // legcolor="";
  3218. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3219. // //
  3220. // //
  3221. // int availability = sqlite3_column_int(statement, 5);
  3222. // //
  3223. // int incoming_stock = sqlite3_column_int(statement, 6);
  3224. //
  3225. //
  3226. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3227. // if(demension==nil)
  3228. // demension="";
  3229. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3230. //
  3231. //
  3232. //
  3233. // // ,,,,,,,,,
  3234. //
  3235. //
  3236. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3237. // if(seat_height==nil)
  3238. // seat_height="";
  3239. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3240. //
  3241. //
  3242. // char *material = (char*)sqlite3_column_text(statement, 9);
  3243. // if(material==nil)
  3244. // material="";
  3245. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3246. //
  3247. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3248. // if(box_dim==nil)
  3249. // box_dim="";
  3250. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3251. //
  3252. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3253. // if(volume==nil)
  3254. // volume="";
  3255. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3256. //
  3257. // double weight = sqlite3_column_double(statement, 12);
  3258. //
  3259. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3260. // if(model_set==nil)
  3261. // model_set="";
  3262. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3263. //
  3264. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3265. // if(load_ability==nil)
  3266. // load_ability="";
  3267. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3268. //
  3269. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3270. //// if(default_category==nil)
  3271. //// default_category="";
  3272. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3273. //
  3274. //
  3275. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3276. // if(fabric_content==nil)
  3277. // fabric_content="";
  3278. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3279. //
  3280. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3281. // if(assembling==nil)
  3282. // assembling="";
  3283. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3284. //
  3285. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3286. // if(made_in==nil)
  3287. // made_in="";
  3288. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3289. //
  3290. //
  3291. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3292. // if(special_remarks==nil)
  3293. // special_remarks="";
  3294. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3295. //
  3296. //
  3297. // int stockUcom = sqlite3_column_double(statement, 20);
  3298. //
  3299. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3300. // if(product_group==nil)
  3301. // product_group="";
  3302. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3303. //
  3304. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3305. // // if(fashion_selector==nil)
  3306. // // fashion_selector="";
  3307. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3308. //
  3309. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3310. // if(selector_field==nil)
  3311. // selector_field="";
  3312. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3313. //
  3314. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3315. // if(property_field==nil)
  3316. // property_field="";
  3317. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3318. //
  3319. //
  3320. //
  3321. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3322. // if(packaging==nil)
  3323. // packaging="";
  3324. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3325. //
  3326. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3327. //
  3328. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3329. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3330. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3331. //
  3332. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3333. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3334. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3335. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3336. //
  3337. //
  3338. //
  3339. //
  3340. //
  3341. // NSString* Availability=nil;
  3342. // if(availability>0)
  3343. // Availability=[NSString stringWithFormat:@"%d",availability];
  3344. // else
  3345. // Availability = @"Out of Stock";
  3346. //
  3347. // [img_section setValue:Availability forKey:@"Availability"];
  3348. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3349. //
  3350. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3351. //
  3352. //
  3353. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3354. // if(eta==nil)
  3355. // eta="";
  3356. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3357. // if ([nseta isEqualToString:@"null"]) {
  3358. // nseta = @"";
  3359. // }
  3360. // if (availability <= 0) {
  3361. // [img_section setValue:nseta forKey:@"ETA"];
  3362. // }
  3363. //
  3364. //
  3365. // int item_id = sqlite3_column_int(statement, 26);
  3366. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3367. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3368. //
  3369. // NSString* Price=nil;
  3370. // if(appDelegate.bLogin==false)
  3371. // Price=@"Must Sign in.";
  3372. // else
  3373. // {
  3374. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3375. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3376. // if(price==nil)
  3377. // Price=@"No Price.";
  3378. // else
  3379. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3380. // }
  3381. //
  3382. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3383. // [img_section setValue:Price forKey:@"price"];
  3384. // [img_section setValue:nsname forKey:@"model_name"];
  3385. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3386. //
  3387. // if (appDelegate.order_code) { // 离线order code即so#
  3388. //
  3389. // 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];
  3390. // __block int cartQTY = 0;
  3391. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3392. //
  3393. // cartQTY = sqlite3_column_int(stmt, 0);
  3394. //
  3395. // }];
  3396. //
  3397. // if (cartQTY > 0) {
  3398. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3399. // }
  3400. // }
  3401. //
  3402. // [ret setObject:img_section forKey:@"img_section"];
  3403. //
  3404. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3405. // int detail0_item_count=0;
  3406. //
  3407. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3408. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3409. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3410. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3411. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3412. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3413. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3414. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3415. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3416. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3417. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3418. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3419. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3420. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3421. //
  3422. //
  3423. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3424. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3425. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3426. // {
  3427. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3428. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3429. // }
  3430. //
  3431. //
  3432. //
  3433. //
  3434. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3435. // [detail0_section setValue:@"kv" forKey:@"type"];
  3436. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3437. //
  3438. // [ret setObject:detail0_section forKey:@"detail_0"];
  3439. //
  3440. //
  3441. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3442. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3443. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3444. // // [detail1_section setValue:@"content" forKey:@"type"];
  3445. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3446. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3447. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3448. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3449. // [ret setObject:detail1_section forKey:@"detail_1"];
  3450. //
  3451. //
  3452. //
  3453. //
  3454. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3455. // [detail2_section setValue:@"detail" forKey:@"target"];
  3456. // [detail2_section setValue:@"popup" forKey:@"action"];
  3457. // [detail2_section setValue:@"content" forKey:@"type"];
  3458. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3459. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3460. // [detail2_section setValue:@"local" forKey:@"data"];
  3461. // [ret setObject:detail2_section forKey:@"detail_2"];
  3462. // }
  3463. //
  3464. //
  3465. //
  3466. //
  3467. // sqlite3_finalize(statement);
  3468. // }
  3469. // else
  3470. // {
  3471. // [ret setValue:@"8" forKey:@"result"];
  3472. // }
  3473. //// DebugLog(@"count:%d",count);
  3474. //
  3475. //
  3476. // [iSalesDB close_db:db];
  3477. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3478. //
  3479. // return ret;
  3480. //}
  3481. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3482. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3483. NSString* keyword = [params valueForKey:@"keyword"];
  3484. keyword=keyword.lowercaseString;
  3485. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3486. int limit = [[params valueForKey:@"limit"] intValue];
  3487. int offset = [[params valueForKey:@"offset"] intValue];
  3488. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3489. NSString *limit_str = @"";
  3490. if (limited) {
  3491. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3492. }
  3493. sqlite3 *db = [iSalesDB get_db];
  3494. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3495. // UIApplication * app = [UIApplication sharedApplication];
  3496. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3497. NSString *sqlQuery = nil;
  3498. if(exactMatch )
  3499. 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 ;
  3500. else
  3501. 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
  3502. DebugLog(@"offline_search sql:%@",sqlQuery);
  3503. sqlite3_stmt * statement;
  3504. [ret setValue:@"2" forKey:@"result"];
  3505. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3506. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3507. // int count=0;
  3508. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3509. {
  3510. int i=0;
  3511. while (sqlite3_step(statement) == SQLITE_ROW)
  3512. {
  3513. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3514. // char *name = (char*)sqlite3_column_text(statement, 1);
  3515. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3516. char *name = (char*)sqlite3_column_text(statement, 0);
  3517. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3518. char *description = (char*)sqlite3_column_text(statement, 1);
  3519. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3520. int product_id = sqlite3_column_int(statement, 2);
  3521. // char *url = (char*)sqlite3_column_text(statement, 3);
  3522. // if(url==nil)
  3523. // url="";
  3524. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3525. int wid = sqlite3_column_int(statement, 3);
  3526. int closeout = sqlite3_column_int(statement, 4);
  3527. int cid = sqlite3_column_int(statement, 5);
  3528. int wisdelete = sqlite3_column_int(statement, 6);
  3529. int more_color = sqlite3_column_int(statement, 7);
  3530. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3531. if(wid !=0 && wisdelete != 1)
  3532. [item setValue:@"true" forKey:@"wish_exists"];
  3533. else
  3534. [item setValue:@"false" forKey:@"wish_exists"];
  3535. if(closeout==0)
  3536. [item setValue:@"false" forKey:@"is_closeout"];
  3537. else
  3538. [item setValue:@"true" forKey:@"is_closeout"];
  3539. if(cid==0)
  3540. [item setValue:@"false" forKey:@"cart_exists"];
  3541. else
  3542. [item setValue:@"true" forKey:@"cart_exists"];
  3543. if (more_color == 0) {
  3544. [item setObject:@(false) forKey:@"more_color"];
  3545. } else if (more_color == 1) {
  3546. [item setObject:@(true) forKey:@"more_color"];
  3547. }
  3548. [item addEntriesFromDictionary:imgjson];
  3549. // [item setValue:nsurl forKey:@"img"];
  3550. [item setValue:nsname forKey:@"fash_name"];
  3551. [item setValue:nsdescription forKey:@"description"];
  3552. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3553. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3554. i++;
  3555. }
  3556. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3557. [ret setObject:items forKey:@"items"];
  3558. sqlite3_finalize(statement);
  3559. }
  3560. DebugLog(@"count:%d",count);
  3561. [iSalesDB close_db:db];
  3562. #ifdef DEBUG
  3563. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3564. #endif
  3565. return ret;
  3566. }
  3567. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3568. {
  3569. return [self search:params limited:YES];
  3570. }
  3571. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3572. //{
  3573. // UIApplication * app = [UIApplication sharedApplication];
  3574. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3575. //
  3576. //// [iSalesDB disable_trigger]
  3577. // [iSalesDB disable_trigger];
  3578. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3579. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3580. //
  3581. // [iSalesDB enable_trigger];
  3582. //// [iSalesDB enable_trigger]
  3583. // //
  3584. // // NSString* user = [params valueForKey:@"user"];
  3585. // //
  3586. // // NSString* password = [params valueForKey:@"password"];
  3587. //
  3588. //
  3589. //
  3590. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3591. //
  3592. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3593. //
  3594. //
  3595. //
  3596. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3597. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3598. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3599. //
  3600. // [appDelegate update_count_mark];
  3601. //
  3602. //
  3603. // appDelegate.can_show_price =false;
  3604. // appDelegate.can_see_price =false;
  3605. // appDelegate.can_create_portfolio =false;
  3606. // appDelegate.can_create_order =false;
  3607. //
  3608. //
  3609. // appDelegate.can_cancel_order =false;
  3610. // appDelegate.can_set_cart_price =false;
  3611. // appDelegate.can_delete_order =false;
  3612. // appDelegate.can_submit_order =false;
  3613. // appDelegate.can_set_tearsheet_price =false;
  3614. // appDelegate.can_update_contact_info = false;
  3615. //
  3616. // appDelegate.save_order_logout = false;
  3617. // appDelegate.submit_order_logout = false;
  3618. // appDelegate.alert_sold_in_quantities = false;
  3619. //
  3620. // appDelegate.ipad_perm =nil ;
  3621. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3622. // appDelegate.OrderFilter= nil;
  3623. // [appDelegate SetSo:nil];
  3624. // [appDelegate set_main_button_panel];
  3625. //
  3626. //
  3627. // // sqlite3 *db = [iSalesDB get_db];
  3628. // //
  3629. // //
  3630. // //
  3631. // //
  3632. // //
  3633. // // 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"]];
  3634. // //
  3635. // //
  3636. // //
  3637. // //
  3638. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3639. // // sqlite3_stmt * statement;
  3640. // //
  3641. // //
  3642. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3643. // //
  3644. // //
  3645. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3646. // // {
  3647. // //
  3648. // //
  3649. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3650. // // {
  3651. // //
  3652. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3653. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3654. // //
  3655. // //
  3656. // //
  3657. // // int can_show_price = sqlite3_column_int(statement, 0);
  3658. // // int can_see_price = sqlite3_column_int(statement, 1);
  3659. // //
  3660. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3661. // // if(contact_id==nil)
  3662. // // contact_id="";
  3663. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3664. // //
  3665. // // int user_type = sqlite3_column_int(statement, 3);
  3666. // //
  3667. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3668. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3669. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3670. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3671. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3672. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3673. // // int can_create_order = sqlite3_column_int(statement, 10);
  3674. // //
  3675. // //
  3676. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3677. // // if(mode==nil)
  3678. // // mode="";
  3679. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3680. // //
  3681. // //
  3682. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3683. // // if(username==nil)
  3684. // // username="";
  3685. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3686. // //
  3687. // //
  3688. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3689. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3690. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3691. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3692. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3693. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3694. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3695. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3696. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3697. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3698. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3699. // //
  3700. // // [header setValue:nsusername forKey:@"username"];
  3701. // //
  3702. // //
  3703. // // [ret setObject:header forKey:@"header"];
  3704. // // [ret setValue:nsmode forKey:@"mode"];
  3705. // //
  3706. // //
  3707. // // }
  3708. // //
  3709. // //
  3710. // //
  3711. // // sqlite3_finalize(statement);
  3712. // // }
  3713. // //
  3714. // //
  3715. // //
  3716. // // [iSalesDB close_db:db];
  3717. // //
  3718. // //
  3719. // //
  3720. // //
  3721. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3722. //
  3723. // return ret;
  3724. //}
  3725. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3726. //{
  3727. //
  3728. // UIApplication * app = [UIApplication sharedApplication];
  3729. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3730. //
  3731. // [iSalesDB disable_trigger];
  3732. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3733. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3734. // [iSalesDB enable_trigger];
  3735. //
  3736. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3737. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3738. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3739. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3740. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3741. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3742. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3743. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3744. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3745. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3746. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3747. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3748. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3749. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3750. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3751. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3752. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3753. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3754. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3755. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3756. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3757. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3758. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3759. //
  3760. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3761. //
  3762. // [arr_name addObject:customer_first_name];
  3763. // [arr_name addObject:customer_last_name];
  3764. //
  3765. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3766. //
  3767. // // default ship from
  3768. // 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';";
  3769. //
  3770. // __block NSString *cid = @"";
  3771. // __block NSString *name = @"";
  3772. // __block NSString *ext = @"";
  3773. // __block NSString *contact = @"";
  3774. // __block NSString *email = @"";
  3775. // __block NSString *fax = @"";
  3776. // __block NSString *phone = @"";
  3777. //
  3778. // sqlite3 *db = [iSalesDB get_db];
  3779. //
  3780. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3781. //
  3782. // cid = [self textAtColumn:0 statement:statment];
  3783. // name = [self textAtColumn:1 statement:statment];
  3784. // ext = [self textAtColumn:2 statement:statment];
  3785. // contact = [self textAtColumn:3 statement:statment];
  3786. // email = [self textAtColumn:4 statement:statment];
  3787. // fax = [self textAtColumn:5 statement:statment];
  3788. // phone = [self textAtColumn:6 statement:statment];
  3789. //
  3790. // }];
  3791. //
  3792. // NSString* so_id = [self get_offline_soid:db];
  3793. // if(so_id==nil)
  3794. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3795. //
  3796. // 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];
  3797. //
  3798. //
  3799. //
  3800. // int result =[iSalesDB execSql:sql_neworder db:db];
  3801. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3802. //
  3803. //
  3804. //
  3805. // //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'
  3806. // //soId
  3807. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3808. //
  3809. //
  3810. //
  3811. //
  3812. //
  3813. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3814. // sqlite3_stmt * statement;
  3815. //
  3816. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3817. // {
  3818. // if (sqlite3_step(statement) == SQLITE_ROW)
  3819. // {
  3820. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3821. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3822. //
  3823. // //ret = sqlite3_column_int(statement, 0);
  3824. //
  3825. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3826. // if(soId==nil)
  3827. // soId="";
  3828. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3829. // [ret setValue:nssoId forKey:@"soId"];
  3830. // [ret setValue:nssoId forKey:@"orderCode"];
  3831. //
  3832. // }
  3833. // sqlite3_finalize(statement);
  3834. // }
  3835. //
  3836. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3837. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3838. //
  3839. //
  3840. // [iSalesDB close_db:db];
  3841. //
  3842. // return [RAConvertor dict2data:ret];
  3843. //
  3844. //}
  3845. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3846. {
  3847. //assert(params[@"order_code"]);
  3848. // assert(params[@"order_code"]);
  3849. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3850. NSString* orderCode = [params valueForKey:@"orderCode"];
  3851. NSString* app_order_code= params[@"appDelegate.order_code"];
  3852. // UIApplication * app = [UIApplication sharedApplication];
  3853. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3854. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3855. {
  3856. [iSalesDB disable_trigger];
  3857. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3858. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3859. [iSalesDB enable_trigger];
  3860. }
  3861. sqlite3 *db = [iSalesDB get_db];
  3862. int cart_count=[self query_ordercartcount:orderCode db:db];
  3863. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3864. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3865. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3866. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3867. [iSalesDB close_db:db];
  3868. return [RAConvertor dict2data:ret];
  3869. }
  3870. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3871. {
  3872. assert(params[@"can_create_backorder"]!=nil);
  3873. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3874. sqlite3 *db = [iSalesDB get_db];
  3875. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3876. int count =0;
  3877. if(params[@"count"]!=nil)
  3878. {
  3879. count = [params[@"count"] intValue];
  3880. }
  3881. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3882. NSString* product_id=params[@"product_id"];
  3883. NSString* orderCode=params[@"orderCode"];
  3884. NSString *qty = params[@"qty"];
  3885. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3886. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3887. __block int number_of_outOfStock = 0;
  3888. for(int i=0;i<arr_id.count;i++)
  3889. {
  3890. NSInteger item_qty= count;
  3891. if (qty) {
  3892. item_qty = [qty_arr[i] integerValue];
  3893. }
  3894. if(item_qty==0)
  3895. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3896. // 检查新加Model数量是否大于库存
  3897. if (![params[@"can_create_backorder"] boolValue]) {
  3898. __block BOOL needContinue = NO;
  3899. [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) {
  3900. int availability = sqlite3_column_int(stmt, 0);
  3901. // 库存小于购买量为缺货
  3902. if (availability < item_qty || availability <= 0) {
  3903. number_of_outOfStock++;
  3904. needContinue = YES;
  3905. }
  3906. }];
  3907. if (needContinue) {
  3908. continue;
  3909. }
  3910. }
  3911. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3912. NSString* sql=nil;
  3913. sqlite3_stmt *stmt = nil;
  3914. BOOL shouldStep = NO;
  3915. if(_id<0)
  3916. {
  3917. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3918. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3919. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3920. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3921. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3922. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3923. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3924. shouldStep = YES;
  3925. }
  3926. else
  3927. {
  3928. if (qty) { // wish list move to cart
  3929. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3930. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3931. sqlite3_bind_int(stmt, 1, _id);
  3932. shouldStep = YES;
  3933. } else {
  3934. 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];
  3935. __block BOOL update = YES;
  3936. if (![params[@"can_create_backorder"] boolValue]) {
  3937. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3938. int newQTY = sqlite3_column_int(stmt, 0);
  3939. int availability = sqlite3_column_int(stmt, 1);
  3940. if (newQTY > availability) { // 库存不够
  3941. update = NO;
  3942. number_of_outOfStock++;
  3943. }
  3944. }];
  3945. }
  3946. if (update) {
  3947. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3948. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3949. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3950. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3951. shouldStep = YES;
  3952. }
  3953. }
  3954. }
  3955. if (shouldStep) {
  3956. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3957. [iSalesDB execSql:@"ROLLBACK" db:db];
  3958. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3959. [iSalesDB close_db:db];
  3960. DebugLog(@"add to cart error");
  3961. return [RAConvertor dict2data:ret];
  3962. }
  3963. }
  3964. }
  3965. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3966. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3967. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3968. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3969. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3970. if (![params[@"can_create_backorder"] boolValue]) {
  3971. if (number_of_outOfStock > 0) {
  3972. ret[@"result"]=[NSNumber numberWithInt:8];
  3973. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3974. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3975. }
  3976. }
  3977. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3978. [iSalesDB close_db:db];
  3979. return [RAConvertor dict2data:ret];
  3980. }
  3981. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3982. {
  3983. // UIApplication * app = [UIApplication sharedApplication];
  3984. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3985. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3986. 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];
  3987. sqlite3_stmt * statement;
  3988. int count=0;
  3989. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3990. {
  3991. while (sqlite3_step(statement) == SQLITE_ROW)
  3992. {
  3993. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3994. int bitem_id = sqlite3_column_int(statement, 0);
  3995. int bitem_qty = sqlite3_column_int(statement, 1);
  3996. char *name = (char*)sqlite3_column_text(statement, 2);
  3997. if(name==nil)
  3998. name="";
  3999. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4000. char *description = (char*)sqlite3_column_text(statement, 3);
  4001. if(description==nil)
  4002. description="";
  4003. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4004. double unit_price = sqlite3_column_double(statement, 4);
  4005. int use_unitprice = sqlite3_column_int(statement, 5);
  4006. if(use_unitprice!=1)
  4007. {
  4008. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4009. }
  4010. itemjson[@"model"]=nsname;
  4011. itemjson[@"description"]=nsdescription;
  4012. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4013. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4014. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4015. if(compute)
  4016. {
  4017. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4018. }
  4019. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4020. count++;
  4021. }
  4022. sqlite3_finalize(statement);
  4023. }
  4024. ret[@"count"]=@(count);
  4025. if(count==0)
  4026. return nil;
  4027. else
  4028. return ret;
  4029. }
  4030. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4031. {
  4032. //compute: add part to subtotal;
  4033. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4034. dict_item[@(item_id)]=@"1";
  4035. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4036. double cuft=0;
  4037. double weight=0;
  4038. int carton=0;
  4039. int impack=0;
  4040. 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];
  4041. sqlite3_stmt * statement;
  4042. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4043. {
  4044. if (sqlite3_step(statement) == SQLITE_ROW)
  4045. {
  4046. double ulength = sqlite3_column_double(statement, 0);
  4047. double uwidth = sqlite3_column_double(statement, 1);
  4048. double uheight = sqlite3_column_double(statement, 2);
  4049. double uweight = sqlite3_column_double(statement, 3);
  4050. double mlength = sqlite3_column_double(statement, 4);
  4051. double mwidth = sqlite3_column_double(statement, 5);
  4052. double mheight = sqlite3_column_double(statement, 6);
  4053. double mweight = sqlite3_column_double(statement, 7);
  4054. double ilength = sqlite3_column_double(statement, 8);
  4055. double iwidth = sqlite3_column_double(statement, 9);
  4056. double iheight = sqlite3_column_double(statement, 10);
  4057. double iweight = sqlite3_column_double(statement, 11);
  4058. // int pcs = sqlite3_column_int(statement,12);
  4059. int mpack = sqlite3_column_int(statement, 13);
  4060. impack = mpack;
  4061. int ipack = sqlite3_column_int(statement, 14);
  4062. // double icbf = sqlite3_column_double(statement, 16);
  4063. // double mcbf = sqlite3_column_double(statement, 17);
  4064. if(ipack==0)
  4065. {
  4066. carton= count/mpack ;
  4067. weight = mweight*carton;
  4068. cuft= carton*(mlength*mwidth*mheight);
  4069. int remain=count%mpack;
  4070. if(remain==0)
  4071. {
  4072. //do nothing;
  4073. }
  4074. else
  4075. {
  4076. carton++;
  4077. weight += uweight*remain;
  4078. cuft += (ulength*uwidth*uheight)*remain;
  4079. }
  4080. }
  4081. else
  4082. {
  4083. carton = count/(mpack*ipack);
  4084. weight = mweight*carton;
  4085. cuft= carton*(mlength*mwidth*mheight);
  4086. int remain=count%(mpack*ipack);
  4087. if(remain==0)
  4088. {
  4089. // do nothing;
  4090. }
  4091. else
  4092. {
  4093. carton++;
  4094. int icarton =remain/ipack;
  4095. int iremain=remain%ipack;
  4096. weight += iweight*icarton;
  4097. cuft += (ilength*iwidth*iheight)*icarton;
  4098. if(iremain==0)
  4099. {
  4100. //do nothing;
  4101. }
  4102. else
  4103. {
  4104. weight += uweight*iremain;
  4105. cuft += (ulength*uwidth*uheight)*iremain;
  4106. }
  4107. }
  4108. }
  4109. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4110. double ucbf = sqlite3_column_double(statement, 15);
  4111. cuft=ucbf*count;
  4112. weight= uweight*count;
  4113. #endif
  4114. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4115. double ucbf = sqlite3_column_double(statement, 15);
  4116. cuft=ucbf*count;
  4117. weight= uweight*count;
  4118. #endif
  4119. }
  4120. sqlite3_finalize(statement);
  4121. }
  4122. if(compute)
  4123. {
  4124. NSArray * arr_count=nil;
  4125. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4126. for(int i=0;i<arr_bundle.count;i++)
  4127. {
  4128. dict_item[arr_bundle[i]]=@"1";
  4129. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4130. cuft+=[bundlejson[@"cuft"] doubleValue];
  4131. weight+=[bundlejson[@"weight"] doubleValue];
  4132. carton+=[bundlejson[@"carton"] intValue];
  4133. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4134. }
  4135. }
  4136. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4137. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4138. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4139. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4140. ret[@"items"]=dict_item;
  4141. return ret;
  4142. }
  4143. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4144. {
  4145. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4146. // 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 ];
  4147. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4148. DebugLog(@"offline_login sql:%@",sqlQuery);
  4149. sqlite3_stmt * statement;
  4150. int cart_count=0;
  4151. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4152. if ( dbresult== SQLITE_OK)
  4153. {
  4154. while (sqlite3_step(statement) == SQLITE_ROW)
  4155. {
  4156. int item_id = sqlite3_column_int(statement, 0);
  4157. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4158. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4159. }
  4160. sqlite3_finalize(statement);
  4161. }
  4162. return cart_count;
  4163. }
  4164. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4165. //{
  4166. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4167. // sqlite3 *db = [iSalesDB get_db];
  4168. // UIApplication * app = [UIApplication sharedApplication];
  4169. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4170. //
  4171. // NSString* orderCode=params[@"orderCode"];
  4172. //
  4173. // int sort = [[params objectForKey:@"sort"] intValue];
  4174. // NSString *sort_str = @"";
  4175. // switch (sort) {
  4176. // case 0:{
  4177. // sort_str = @"order by c.modify_time desc";
  4178. // }
  4179. // break;
  4180. // case 1:{
  4181. // sort_str = @"order by c.modify_time asc";
  4182. // }
  4183. // break;
  4184. // case 2:{
  4185. // sort_str = @"order by m.name asc";
  4186. // }
  4187. // break;
  4188. // case 3:{
  4189. // sort_str = @"order by m.name desc";
  4190. // }
  4191. // break;
  4192. // case 4:{
  4193. // sort_str = @"order by m.description asc";
  4194. // }
  4195. // break;
  4196. //
  4197. // default:
  4198. // break;
  4199. //
  4200. // }
  4201. //
  4202. //
  4203. //
  4204. // 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 ];
  4205. //
  4206. //
  4207. //// 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 ];
  4208. //
  4209. //
  4210. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4211. //
  4212. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4213. // sqlite3_stmt * statement;
  4214. //
  4215. //
  4216. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4217. //
  4218. // NSDate *date1 = [NSDate date];
  4219. //
  4220. // int count=0;
  4221. // int cart_count=0;
  4222. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4223. // if ( dbresult== SQLITE_OK)
  4224. // {
  4225. //
  4226. //
  4227. // while (sqlite3_step(statement) == SQLITE_ROW)
  4228. // {
  4229. //// NSDate *row_date = [NSDate date];
  4230. //
  4231. //
  4232. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4233. //
  4234. //
  4235. //
  4236. //
  4237. // int product_id = sqlite3_column_int(statement, 0);
  4238. //
  4239. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4240. //
  4241. // int item_id = sqlite3_column_int(statement, 7);
  4242. //
  4243. // NSString* Price=nil;
  4244. // if(str_price==nil)
  4245. // {
  4246. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4247. //// NSDate *price_date = [NSDate date];
  4248. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4249. //// DebugLog(@"price time interval");
  4250. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4251. //
  4252. // if(price==nil)
  4253. // Price=@"No Price.";
  4254. // else
  4255. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4256. // }
  4257. // else
  4258. // {
  4259. //
  4260. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4261. // }
  4262. //
  4263. //
  4264. // double discount = sqlite3_column_double(statement, 2);
  4265. // int item_count = sqlite3_column_int(statement, 3);
  4266. //
  4267. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4268. // NSString *nsline_note=nil;
  4269. // if(line_note!=nil)
  4270. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4271. //
  4272. //
  4273. // char *name = (char*)sqlite3_column_text(statement, 5);
  4274. // NSString *nsname=nil;
  4275. // if(name!=nil)
  4276. // nsname= [[NSString alloc]initWithUTF8String:name];
  4277. //
  4278. // char *description = (char*)sqlite3_column_text(statement, 6);
  4279. // NSString *nsdescription=nil;
  4280. // if(description!=nil)
  4281. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4282. //
  4283. //
  4284. //
  4285. // int stockUom = sqlite3_column_int(statement, 8);
  4286. // int _id = sqlite3_column_int(statement, 9);
  4287. // int availability = sqlite3_column_int(statement, 10);
  4288. //
  4289. //// NSDate *subtotal_date = [NSDate date];
  4290. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4291. //// DebugLog(@"subtotal_date time interval");
  4292. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4293. //
  4294. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4295. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4296. // int carton=[bsubtotaljson[@"carton"] intValue];
  4297. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4298. //
  4299. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4300. //// NSDate *img_date = [NSDate date];
  4301. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4302. //// DebugLog(@"img_date time interval");
  4303. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4304. //
  4305. // itemjson[@"model"]=nsname;
  4306. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4307. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4308. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4309. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4310. // itemjson[@"check"]=@"true";
  4311. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4312. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4313. // itemjson[@"unit_price"]=Price;
  4314. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4315. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4316. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4317. // itemjson[@"note"]=nsline_note;
  4318. // if (!appDelegate.can_create_backorder) {
  4319. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4320. // }
  4321. //// NSDate *date2 = [NSDate date];
  4322. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4323. //// DebugLog(@"model_bundle time interval");
  4324. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4325. //
  4326. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4327. // count++;
  4328. //
  4329. //// DebugLog(@"row time interval");
  4330. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4331. // }
  4332. //
  4333. //
  4334. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4335. // sqlite3_finalize(statement);
  4336. // }
  4337. //
  4338. //
  4339. // DebugLog(@"request cart total time interval");
  4340. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4341. //
  4342. //
  4343. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4344. //
  4345. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4346. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4347. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4348. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4349. //
  4350. // ret[@"mode"]=@"Regular Mode";
  4351. //
  4352. // [iSalesDB close_db:db];
  4353. //
  4354. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4355. //
  4356. // DebugLog(@"general notes :%@",general_note);
  4357. //
  4358. // ret[@"general_note"]= general_note;
  4359. //
  4360. // return [RAConvertor dict2data:ret];
  4361. //}
  4362. +(NSData*) offline_login :(NSMutableDictionary *) params
  4363. {
  4364. NSString* user = [params valueForKey:@"user"];
  4365. NSString* password = [params valueForKey:@"password"];
  4366. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4367. sqlite3 *db = [iSalesDB get_db];
  4368. 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"]];
  4369. DebugLog(@"offline_login sql:%@",sqlQuery);
  4370. sqlite3_stmt * statement;
  4371. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4372. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4373. {
  4374. if (sqlite3_step(statement) == SQLITE_ROW)
  4375. {
  4376. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4377. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4378. int can_show_price = sqlite3_column_int(statement, 0);
  4379. int can_see_price = sqlite3_column_int(statement, 1);
  4380. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4381. if(contact_id==nil)
  4382. contact_id="";
  4383. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4384. int user_type = sqlite3_column_int(statement, 3);
  4385. int can_cancel_order = sqlite3_column_int(statement, 4);
  4386. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4387. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4388. int can_delete_order = sqlite3_column_int(statement, 7);
  4389. int can_submit_order = sqlite3_column_int(statement, 8);
  4390. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4391. int can_create_order = sqlite3_column_int(statement, 10);
  4392. char *mode = (char*)sqlite3_column_text(statement, 11);
  4393. if(mode==nil)
  4394. mode="";
  4395. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4396. char *username = (char*)sqlite3_column_text(statement, 12);
  4397. if(username==nil)
  4398. username="";
  4399. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4400. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4401. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4402. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4403. [header setValue:nscontact_id forKey:@"contact_id"];
  4404. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4405. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4406. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4407. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4408. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4409. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4410. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4411. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4412. #ifdef BUILD_CONTRAST
  4413. [header setValue:[NSNumber numberWithBool:1] forKey:@"alert_sold_in_quantities"];
  4414. #endif
  4415. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4416. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4417. [header setValue:nsusername forKey:@"username"];
  4418. NSError* error=nil;
  4419. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4420. [header setValue:statusFilter forKey:@"statusFilter"];
  4421. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4422. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4423. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4424. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4425. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4426. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4427. [ret setObject:header forKey:@"header"];
  4428. [ret setValue:nsmode forKey:@"mode"];
  4429. }
  4430. sqlite3_finalize(statement);
  4431. }
  4432. [iSalesDB close_db:db];
  4433. #ifdef DEBUG
  4434. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4435. #endif
  4436. return [RAConvertor dict2data:ret];
  4437. }
  4438. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4439. {
  4440. assert(params[@"mode"]!=nil);
  4441. NSString* contactId = [params valueForKey:@"contactId"];
  4442. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4443. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4444. sqlite3 *db = [iSalesDB get_db];
  4445. NSString *sqlQuery = nil;
  4446. {
  4447. 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];
  4448. }
  4449. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4450. sqlite3_stmt * statement;
  4451. [ret setValue:@"2" forKey:@"result"];
  4452. int carrier = -1;
  4453. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4454. {
  4455. //int i = 0;
  4456. if (sqlite3_step(statement) == SQLITE_ROW)
  4457. {
  4458. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4459. // int editable = sqlite3_column_int(statement, 0);
  4460. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4461. NSString *nscompany_name =nil;
  4462. if(company_name==nil)
  4463. nscompany_name=@"";
  4464. else
  4465. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4466. char *country = (char*)sqlite3_column_text(statement, 2);
  4467. if(country==nil)
  4468. country="";
  4469. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4470. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4471. // if(addr==nil)
  4472. // addr="";
  4473. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4474. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4475. if(zipcode==nil)
  4476. zipcode="";
  4477. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4478. char *state = (char*)sqlite3_column_text(statement, 5);
  4479. if(state==nil)
  4480. state="";
  4481. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4482. char *city = (char*)sqlite3_column_text(statement, 6);
  4483. if(city==nil)
  4484. city="";
  4485. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4486. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4487. // NSString *nscontact_name = nil;
  4488. // if(contact_name==nil)
  4489. // nscontact_name=@"";
  4490. // else
  4491. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4492. char *phone = (char*)sqlite3_column_text(statement, 8);
  4493. NSString *nsphone = nil;
  4494. if(phone==nil)
  4495. nsphone=@"";
  4496. else
  4497. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4498. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4499. // if(contact_id==nil)
  4500. // contact_id="";
  4501. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4502. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4503. if(addr_1==nil)
  4504. addr_1="";
  4505. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4506. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4507. if(addr_2==nil)
  4508. addr_2="";
  4509. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4510. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4511. if(addr_3==nil)
  4512. addr_3="";
  4513. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4514. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4515. if(addr_4==nil)
  4516. addr_4="";
  4517. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4518. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4519. if(first_name==nil)
  4520. first_name="";
  4521. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4522. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4523. if(last_name==nil)
  4524. last_name="";
  4525. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4526. char *fax = (char*)sqlite3_column_text(statement, 16);
  4527. NSString *nsfax = nil;
  4528. if(fax==nil)
  4529. nsfax=@"";
  4530. else
  4531. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4532. char *email = (char*)sqlite3_column_text(statement, 17);
  4533. NSString *nsemail = nil;
  4534. if(email==nil)
  4535. nsemail=@"";
  4536. else
  4537. nsemail= [[NSString alloc]initWithUTF8String:email];
  4538. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4539. NSString *nsimg_0 = nil;
  4540. if(img_0==nil)
  4541. nsimg_0=@"";
  4542. else
  4543. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4544. [self copy_bcardImg:nsimg_0];
  4545. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4546. NSString *nsimg_1 = nil;
  4547. if(img_1==nil)
  4548. nsimg_1=@"";
  4549. else
  4550. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4551. [self copy_bcardImg:nsimg_1];
  4552. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4553. NSString *nsimg_2 = nil;
  4554. if(img_2==nil)
  4555. nsimg_2=@"";
  4556. else
  4557. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4558. [self copy_bcardImg:nsimg_2];
  4559. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4560. NSString *nsprice_type = nil;
  4561. if(price_type==nil)
  4562. nsprice_type=@"";
  4563. else
  4564. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4565. char *notes = (char*)sqlite3_column_text(statement, 22);
  4566. NSString *nsnotes = nil;
  4567. if(notes==nil)
  4568. nsnotes=@"";
  4569. else
  4570. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4571. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4572. NSString *nssalesrep = nil;
  4573. if(salesrep==nil)
  4574. nssalesrep=@"";
  4575. else
  4576. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4577. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4578. carrier = sqlite3_column_int(statement, 25);
  4579. {
  4580. // decrypt
  4581. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4582. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4583. nsphone=[AESCrypt fastdecrypt:nsphone];
  4584. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4585. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4586. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4587. }
  4588. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4589. [arr_name addObject:nsfirst_name];
  4590. [arr_name addObject:nslast_name];
  4591. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4592. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4593. [arr_ext addObject:nsaddr_1];
  4594. [arr_ext addObject:nsaddr_2];
  4595. [arr_ext addObject:nsaddr_3];
  4596. [arr_ext addObject:nsaddr_4];
  4597. [arr_ext addObject:@"\r\n"];
  4598. [arr_ext addObject:nscity];
  4599. [arr_ext addObject:nsstate];
  4600. [arr_ext addObject:nszipcode];
  4601. [arr_ext addObject:nscountry];
  4602. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4603. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4604. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4605. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4606. [item setValue:nscountry forKey:@"customer_country"];
  4607. [item setValue:nsphone forKey:@"customer_phone"];
  4608. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4609. [item setValue:nscompany_name forKey:@"customer_name"];
  4610. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4611. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4612. [item setValue:nsext forKey:@"customer_contact_ext"];
  4613. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4614. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4615. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4616. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4617. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4618. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4619. [item setValue:nslast_name forKey:@"customer_last_name"];
  4620. [item setValue:nscity forKey:@"customer_city"];
  4621. [item setValue:nsstate forKey:@"customer_state"];
  4622. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4623. [item setValue:contactId forKey:@"customer_cid"];
  4624. [item setValue:nscontact_name forKey:@"customer_contact"];
  4625. [item setValue:nsfax forKey:@"customer_fax"];
  4626. [item setValue:nsemail forKey:@"customer_email"];
  4627. [item setValue:contact_type forKey:@"customer_contact_type"];
  4628. // i++;
  4629. }
  4630. // UIApplication * app = [UIApplication sharedApplication];
  4631. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4632. [ret setValue:params[@"mode"] forKey:@"mode"];
  4633. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4634. sqlite3_finalize(statement);
  4635. }
  4636. [iSalesDB close_db:db];
  4637. NSString* scarrier = [self offline_getCarrier:carrier];
  4638. item[@"customer_truck_carrier"] = scarrier;
  4639. [ret setObject:item forKey:@"customerInfo"];
  4640. #ifdef DEBUG
  4641. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4642. #endif
  4643. return ret;
  4644. }
  4645. + (bool) copy_bcardImg:(NSString*) filename
  4646. {
  4647. if(filename.length==0)
  4648. return false;
  4649. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4650. bool ret=false;
  4651. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4652. NSString *cachefolder = [paths objectAtIndex:0];
  4653. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4654. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4655. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4656. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4657. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4658. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4659. //
  4660. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4661. NSFileManager* fileManager = [NSFileManager defaultManager];
  4662. BOOL bdir=NO;
  4663. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4664. {
  4665. NSError *error = nil;
  4666. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4667. {
  4668. ret=false;
  4669. }
  4670. else
  4671. {
  4672. ret=true;
  4673. }
  4674. // NSError *error = nil;
  4675. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4676. //
  4677. // if(!bsuccess)
  4678. // {
  4679. // DebugLog(@"Create offline_createimg folder failed");
  4680. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4681. // return [RAConvertor dict2data:ret];
  4682. // }
  4683. // if(bsuccess)
  4684. // {
  4685. // sqlite3 *db = [self get_db];
  4686. //
  4687. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4688. // [iSalesDB close_db:db];
  4689. // }
  4690. }
  4691. return ret;
  4692. //
  4693. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4694. // if(bsuccess)
  4695. // {
  4696. // NSError *error = nil;
  4697. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4698. // {
  4699. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4700. // }
  4701. // else
  4702. // {
  4703. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4704. //
  4705. // ret[@"img_url_aname"]=filename;
  4706. // ret[@"img_url"]=savedImagePath;
  4707. // }
  4708. // }
  4709. }
  4710. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4711. {
  4712. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4713. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4714. NSString *cachefolder = [paths objectAtIndex:0];
  4715. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4716. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4717. NSFileManager* fileManager = [NSFileManager defaultManager];
  4718. BOOL bdir=YES;
  4719. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4720. {
  4721. NSError *error = nil;
  4722. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4723. if(!bsuccess)
  4724. {
  4725. DebugLog(@"Create offline_createimg folder failed");
  4726. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4727. return [RAConvertor dict2data:ret];
  4728. }
  4729. // if(bsuccess)
  4730. // {
  4731. // sqlite3 *db = [self get_db];
  4732. //
  4733. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4734. // [iSalesDB close_db:db];
  4735. // }
  4736. }
  4737. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4738. {
  4739. NSError *error = nil;
  4740. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4741. if(!bsuccess)
  4742. {
  4743. DebugLog(@"Create img_cache folder failed");
  4744. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4745. return [RAConvertor dict2data:ret];
  4746. }
  4747. // if(bsuccess)
  4748. // {
  4749. // sqlite3 *db = [self get_db];
  4750. //
  4751. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4752. // [iSalesDB close_db:db];
  4753. // }
  4754. }
  4755. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4756. //JEPG格式
  4757. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4758. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4759. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4760. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4761. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4762. if(bsuccess)
  4763. {
  4764. NSError *error = nil;
  4765. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4766. {
  4767. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4768. }
  4769. else
  4770. {
  4771. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4772. ret[@"img_url_aname"]=filename;
  4773. ret[@"img_url"]=filename;
  4774. }
  4775. }
  4776. else
  4777. {
  4778. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4779. }
  4780. return [RAConvertor dict2data:ret];
  4781. }
  4782. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4783. {
  4784. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4785. category = [category substringToIndex:3];
  4786. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4787. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4788. params[@"category"]= category;
  4789. ret[@"params"]= params;
  4790. [ret setValue:@"detail" forKey:@"target"];
  4791. [ret setValue:@"popup" forKey:@"action"];
  4792. [ret setValue:@"content" forKey:@"type"];
  4793. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4794. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4795. [ret setValue:@"true" forKey:@"single_row"];
  4796. [ret setValue:@"true" forKey:@"partial_refresh"];
  4797. // sqlite3 *db = [iSalesDB get_db];
  4798. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4799. NSString *sqlQuery =nil;
  4800. #ifdef BUILD_NPD
  4801. 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 ;
  4802. #else
  4803. 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];
  4804. #endif
  4805. sqlite3_stmt * statement;
  4806. int count = 0;
  4807. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4808. // int count=0;
  4809. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4810. {
  4811. int i=0;
  4812. while (sqlite3_step(statement) == SQLITE_ROW)
  4813. {
  4814. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4815. // char *name = (char*)sqlite3_column_text(statement, 1);
  4816. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4817. char *name = (char*)sqlite3_column_text(statement, 0);
  4818. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4819. int product_id = sqlite3_column_int(statement, 1);
  4820. char *url = (char*)sqlite3_column_text(statement, 2);
  4821. if(url==nil)
  4822. url="";
  4823. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4824. [item setValue:nsurl forKey:@"picture_path"];
  4825. [item setValue:nsname forKey:@"fash_name"];
  4826. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4827. [item setValue:category forKey:@"category"];
  4828. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4829. i++;
  4830. }
  4831. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4832. sqlite3_finalize(statement);
  4833. }
  4834. DebugLog(@"count:%d",count);
  4835. // [iSalesDB close_db:db];
  4836. return ret;
  4837. }
  4838. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4839. {
  4840. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4841. [ret setValue:key forKey:@"key"];
  4842. [ret setValue:value forKey:@"val"];
  4843. [ret setValue:@"price" forKey:@"type"];
  4844. return ret;
  4845. }
  4846. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4847. {
  4848. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4849. [ret setValue:key forKey:@"key"];
  4850. [ret setValue:value forKey:@"val"];
  4851. return ret;
  4852. }
  4853. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4854. {
  4855. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4856. [ret setValue:@"0" forKey:@"img_count"];
  4857. // sqlite3 *db = [iSalesDB get_db];
  4858. 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 ;
  4859. sqlite3_stmt * statement;
  4860. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4861. {
  4862. int i=0;
  4863. if (sqlite3_step(statement) == SQLITE_ROW)
  4864. {
  4865. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4866. // char *name = (char*)sqlite3_column_text(statement, 1);
  4867. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4868. char *value = (char*)sqlite3_column_text(statement, 0);
  4869. if(value==nil)
  4870. value="";
  4871. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4872. char *key = (char*)sqlite3_column_text(statement, 1);
  4873. if(key==nil)
  4874. key="";
  4875. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4876. [item setValue:nsvalue forKey:@"val"];
  4877. [item setValue:nskey forKey:@"key"];
  4878. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4879. [ret setValue:@"1" forKey:@"count"];
  4880. i++;
  4881. }
  4882. sqlite3_finalize(statement);
  4883. }
  4884. // [iSalesDB close_db:db];
  4885. return ret;
  4886. }
  4887. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4888. {
  4889. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4890. [ret setValue:@"0" forKey:@"count"];
  4891. // sqlite3 *db = [iSalesDB get_db];
  4892. 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;
  4893. sqlite3_stmt * statement;
  4894. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4895. {
  4896. int i=0;
  4897. while (sqlite3_step(statement) == SQLITE_ROW)
  4898. {
  4899. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4900. // char *name = (char*)sqlite3_column_text(statement, 1);
  4901. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4902. char *value = (char*)sqlite3_column_text(statement, 0);
  4903. if(value==nil)
  4904. value="";
  4905. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4906. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4907. if(selector_display==nil)
  4908. selector_display="";
  4909. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4910. int product_id = sqlite3_column_int(statement, 2);
  4911. char *category = (char*)sqlite3_column_text(statement, 3);
  4912. if(category==nil)
  4913. category="";
  4914. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4915. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4916. [item setValue:nsvalue forKey:@"title"];
  4917. [item setValue:url forKey:@"pic_url"];
  4918. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4919. [params setValue:@"2" forKey:@"count"];
  4920. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4921. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4922. [param0 setValue:@"product_id" forKey:@"name"];
  4923. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4924. [param1 setValue:nscategory forKey:@"val"];
  4925. [param1 setValue:@"category" forKey:@"name"];
  4926. [params setObject:param0 forKey:@"param_0"];
  4927. [params setObject:param1 forKey:@"param_1"];
  4928. [item setObject:params forKey:@"params"];
  4929. [ret setValue:nsselector_display forKey:@"name"];
  4930. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4931. i++;
  4932. }
  4933. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4934. [ret setValue:@"switch" forKey:@"action"];
  4935. sqlite3_finalize(statement);
  4936. }
  4937. // [iSalesDB close_db:db];
  4938. return ret;
  4939. }
  4940. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4941. {
  4942. // model 在 category search 显示的图片。
  4943. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4944. // sqlite3 *db = [iSalesDB get_db];
  4945. 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];
  4946. sqlite3_stmt * statement;
  4947. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4948. {
  4949. while (sqlite3_step(statement) == SQLITE_ROW)
  4950. {
  4951. char *url = (char*)sqlite3_column_text(statement, 0);
  4952. if(url==nil)
  4953. url="";
  4954. int type = sqlite3_column_int(statement, 1);
  4955. if(type==0)
  4956. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4957. else
  4958. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4959. }
  4960. sqlite3_finalize(statement);
  4961. }
  4962. // [iSalesDB close_db:db];
  4963. return ret;
  4964. }
  4965. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4966. {
  4967. int item_id=-1;
  4968. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4969. sqlite3_stmt * statement;
  4970. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4971. {
  4972. if (sqlite3_step(statement) == SQLITE_ROW)
  4973. {
  4974. item_id = sqlite3_column_int(statement, 0);
  4975. }
  4976. sqlite3_finalize(statement);
  4977. }
  4978. return item_id;
  4979. }
  4980. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4981. {
  4982. // NSString* ret = @"";
  4983. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4984. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4985. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4986. sqlite3_stmt * statement;
  4987. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4988. {
  4989. while (sqlite3_step(statement) == SQLITE_ROW)
  4990. {
  4991. int bitem_id = sqlite3_column_int(statement, 0);
  4992. int bitem_qty = sqlite3_column_int(statement, 1);
  4993. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4994. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4995. }
  4996. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4997. sqlite3_finalize(statement);
  4998. }
  4999. // if(ret==nil)
  5000. // ret=@"";
  5001. *count=arr_count;
  5002. return arr_bundle;
  5003. }
  5004. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5005. {
  5006. // get default sold qty, return -1 if model not found;
  5007. int ret = -1;
  5008. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5009. sqlite3_stmt * statement;
  5010. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5011. {
  5012. if (sqlite3_step(statement) == SQLITE_ROW)
  5013. {
  5014. ret = sqlite3_column_int(statement, 0);
  5015. }
  5016. sqlite3_finalize(statement);
  5017. }
  5018. return ret;
  5019. }
  5020. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5021. {
  5022. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5023. [ret setValue:@"0" forKey:@"img_count"];
  5024. // sqlite3 *db = [iSalesDB get_db];
  5025. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5026. sqlite3_stmt * statement;
  5027. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5028. {
  5029. int i=0;
  5030. while (sqlite3_step(statement) == SQLITE_ROW)
  5031. {
  5032. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5033. // char *name = (char*)sqlite3_column_text(statement, 1);
  5034. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5035. char *url = (char*)sqlite3_column_text(statement, 0);
  5036. if(url==nil)
  5037. url="";
  5038. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5039. [item setValue:nsurl forKey:@"s"];
  5040. [item setValue:nsurl forKey:@"l"];
  5041. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5042. i++;
  5043. }
  5044. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5045. sqlite3_finalize(statement);
  5046. }
  5047. // [iSalesDB close_db:db];
  5048. return ret;
  5049. }
  5050. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5051. // UIApplication * app = [UIApplication sharedApplication];
  5052. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5053. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5054. sqlite3 *db = [iSalesDB get_db];
  5055. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5056. NSString* product_id=params[@"product_id"];
  5057. NSString *item_count_str = params[@"item_count"];
  5058. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5059. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5060. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5061. // NSString *sql = @"";
  5062. for(int i=0;i<arr.count;i++)
  5063. {
  5064. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5065. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5066. __block int cart_count = 0;
  5067. if (!item_count_str) {
  5068. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5069. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5070. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5071. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5072. cart_count = [[model_set_components lastObject] intValue];
  5073. }];
  5074. }
  5075. if(count==0)
  5076. {
  5077. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5078. sqlite3_stmt *stmt;
  5079. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5080. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5081. if (item_count_arr) {
  5082. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5083. } else {
  5084. sqlite3_bind_int(stmt,2,cart_count);
  5085. }
  5086. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5087. [iSalesDB execSql:@"ROLLBACK" db:db];
  5088. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5089. [iSalesDB close_db:db];
  5090. DebugLog(@"add to wishlist error");
  5091. return ret;
  5092. }
  5093. } else {
  5094. int qty = 0;
  5095. if (item_count_arr) {
  5096. qty = [item_count_arr[i] intValue];
  5097. } else {
  5098. qty = cart_count;
  5099. }
  5100. 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]];
  5101. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5102. [iSalesDB execSql:@"ROLLBACK" db:db];
  5103. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5104. [iSalesDB close_db:db];
  5105. DebugLog(@"add to wishlist error");
  5106. return ret;
  5107. }
  5108. }
  5109. }
  5110. // [iSalesDB execSql:sql db:db];
  5111. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5112. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5113. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5114. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5115. [iSalesDB close_db:db];
  5116. ret[@"wish_count"]=@(count);
  5117. ret[@"result"]= [NSNumber numberWithInt:2];
  5118. return ret;
  5119. }
  5120. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5121. // 0 category
  5122. // 1 search
  5123. // 2 itemsearch
  5124. NSData *ret = nil;
  5125. NSDictionary *items = nil;
  5126. switch (from) {
  5127. case 0:{
  5128. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5129. }
  5130. break;
  5131. case 1:{
  5132. items = [[self search:params limited:NO] objectForKey:@"items"];
  5133. }
  5134. break;
  5135. case 2:{
  5136. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5137. }
  5138. break;
  5139. default:
  5140. break;
  5141. }
  5142. if (!items) {
  5143. return ret;
  5144. }
  5145. int count = [[items objectForKey:@"count"] intValue];
  5146. NSMutableString *product_id_str = [@"" mutableCopy];
  5147. for (int i = 0; i < count; i++) {
  5148. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5149. NSDictionary *item = [items objectForKey:key];
  5150. NSString *product_id = [item objectForKey:@"product_id"];
  5151. if (i == 0) {
  5152. [product_id_str appendString:product_id];
  5153. } else {
  5154. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5155. }
  5156. }
  5157. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5158. if ([add_to isEqualToString:@"cart"]) {
  5159. NSString *order_code = [params objectForKey:@"orderCode"];
  5160. if (order_code.length) {
  5161. NSDictionary *newParams = @{
  5162. @"product_id" : product_id_str,
  5163. @"orderCode" : order_code,
  5164. @"can_create_backorder":params[@"can_create_backorder"]
  5165. };
  5166. ret = [self offline_add2cart:newParams.mutableCopy];
  5167. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5168. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5169. ret = [RAConvertor dict2data:retDic];
  5170. }
  5171. } else if([add_to isEqualToString:@"wishlist"]) {
  5172. NSDictionary *newParams = @{
  5173. @"product_id" : product_id_str
  5174. };
  5175. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5176. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5177. ret = [RAConvertor dict2data:retDic];
  5178. } else if([add_to isEqualToString:@"portfolio"]) {
  5179. NSDictionary *newParams = @{
  5180. @"product_id" : product_id_str
  5181. };
  5182. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5183. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5184. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5185. ret = [RAConvertor dict2data:retDic];
  5186. }
  5187. return ret;
  5188. }
  5189. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5190. {
  5191. return [self addAll:params from:0];
  5192. }
  5193. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5194. {
  5195. return [self addAll:params from:1];
  5196. }
  5197. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5198. {
  5199. return [self addAll:params from:2];
  5200. }
  5201. #pragma mark - Jack
  5202. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5203. // "val_227" : {
  5204. // "check" : 1,
  5205. // "value" : "US United States",
  5206. // "value_id" : "228"
  5207. // },
  5208. if (!countryCode) {
  5209. countryCode = @"US";
  5210. }
  5211. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5212. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5213. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5214. int code_id = sqlite3_column_int(stmt, 3); // id
  5215. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5216. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5217. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5218. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5219. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5220. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5221. }
  5222. long n = *count;
  5223. *count = n + 1;
  5224. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5225. [container setValue:countryDic forKey:key];
  5226. }] mutableCopy];
  5227. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5228. return ret;
  5229. }
  5230. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5231. // "val_227" : {
  5232. // "check" : 1,
  5233. // "value" : "US United States",
  5234. // "value_id" : "228"
  5235. // },
  5236. __block NSString* OrderCarrier =@"";
  5237. // sqlite3 *db = [iSalesDB get_db];
  5238. // sqlite3_stmt * statement;
  5239. [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) {
  5240. OrderCarrier = [self textAtColumn:0 statement:statment];
  5241. // sqlite3_finalize(statement);
  5242. }];
  5243. // [iSalesDB close_db:db];
  5244. __block int c=0;
  5245. 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) {
  5246. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5247. int code_id = sqlite3_column_int(stmt, 2); // id
  5248. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5249. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5250. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5251. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5252. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5253. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5254. }
  5255. c++;
  5256. long n = *count;
  5257. *count = n + 1;
  5258. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5259. [container setValue:carrierDic forKey:key];
  5260. }] mutableCopy];
  5261. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5262. return ret;
  5263. }
  5264. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5265. // "val_227" : {
  5266. // "check" : 1,
  5267. // "value" : "US United States",
  5268. // "value_id" : "228"
  5269. // },
  5270. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5271. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5272. int code_id = sqlite3_column_int(stmt, 2); // id
  5273. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5274. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5275. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5276. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5277. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5278. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5279. }
  5280. long n = *count;
  5281. *count = n + 1;
  5282. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5283. [container setValue:carrierDic forKey:key];
  5284. }] mutableCopy];
  5285. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5286. return ret;
  5287. }
  5288. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5289. // // "val_227" : {
  5290. // // "check" : 1,
  5291. // // "value" : "US United States",
  5292. // // "value_id" : "228"
  5293. // // },
  5294. //
  5295. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5296. // NSString * code = [iSalesDB jk_queryText:Sql];
  5297. //
  5298. // return [self offline_getCarrier:code];
  5299. //
  5300. //}
  5301. + (NSString *)offline_getCarrier:(int)code {
  5302. // "val_227" : {
  5303. // "check" : 1,
  5304. // "value" : "US United States",
  5305. // "value_id" : "228"
  5306. // },
  5307. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5308. NSString * ret = [iSalesDB jk_queryText:Sql];
  5309. return ret;
  5310. }
  5311. + (int )offline_getCarrierCode:(NSString*)name {
  5312. // "val_227" : {
  5313. // "check" : 1,
  5314. // "value" : "US United States",
  5315. // "value_id" : "228"
  5316. // },
  5317. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5318. NSString * ret = [iSalesDB jk_queryText:Sql];
  5319. return [ret intValue];
  5320. }
  5321. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5322. countryCode = [self translateSingleQuote:countryCode];
  5323. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5324. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5325. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5326. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5327. if (name == NULL) {
  5328. name = "";
  5329. }
  5330. if (code == NULL) {
  5331. code = "";
  5332. }
  5333. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5334. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5335. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5336. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5337. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5338. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5339. }
  5340. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5341. [container setValue:stateDic forKey:key];
  5342. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5343. DebugLog(@"query all state error: %@",err_msg);
  5344. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5345. // [stateDic setValue:@"Other" forKey:@"value"];
  5346. // [stateDic setValue:@"" forKey:@"value_id"];
  5347. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5348. //
  5349. // if (state_code && [@"" isEqualToString:state_code]) {
  5350. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5351. // }
  5352. //
  5353. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5354. // [container setValue:stateDic forKey:key];
  5355. }] mutableCopy];
  5356. [ret removeObjectForKey:@"result"];
  5357. // failure 可以不用了,一样的
  5358. if (ret.allKeys.count == 0) {
  5359. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5360. [stateDic setValue:@"Other" forKey:@"value"];
  5361. [stateDic setValue:@"" forKey:@"value_id"];
  5362. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5363. if (state_code && [@"" isEqualToString:state_code]) {
  5364. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5365. }
  5366. NSString *key = [NSString stringWithFormat:@"val_0"];
  5367. [ret setValue:stateDic forKey:key];
  5368. }
  5369. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5370. return ret;
  5371. }
  5372. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5373. codeId = [self translateSingleQuote:codeId];
  5374. 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];
  5375. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5376. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5377. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5378. if (name == NULL) {
  5379. name = "";
  5380. }
  5381. if (code == NULL) {
  5382. code = "";
  5383. }
  5384. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5385. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5386. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5387. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5388. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5389. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5390. }
  5391. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5392. [container setValue:stateDic forKey:key];
  5393. }] mutableCopy];
  5394. [ret removeObjectForKey:@"result"];
  5395. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5396. return ret;
  5397. }
  5398. + (NSDictionary *)offline_getPrice {
  5399. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5400. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5401. char *name = (char *) sqlite3_column_text(stmt, 1);
  5402. int type = sqlite3_column_int(stmt, 2);
  5403. int orderBy = sqlite3_column_int(stmt, 3);
  5404. if (name == NULL) {
  5405. name = "";
  5406. }
  5407. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5408. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5409. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5410. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5411. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5412. if (orderBy == 0) {
  5413. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5414. }
  5415. [container setValue:priceDic forKey:key];
  5416. }] mutableCopy];
  5417. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5418. return ret;
  5419. }
  5420. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5421. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5422. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5423. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5424. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5425. // int _id = sqlite3_column_int(stmt, 0);
  5426. NSString *name = [self textAtColumn:1 statement:stmt];
  5427. NSDictionary *typeDic = @{
  5428. @"value_id" : name,
  5429. @"value" : name,
  5430. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5431. };
  5432. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5433. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5434. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5435. }];
  5436. return ret;
  5437. }
  5438. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5439. // 首先从offline_login表中取出sales_code
  5440. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5441. // NSString *user = app.user;
  5442. user = [self translateSingleQuote:user];
  5443. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5444. __block NSString *user_code = @"";
  5445. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5446. char *code = (char *)sqlite3_column_text(stmt, 0);
  5447. if (code == NULL) {
  5448. code = "";
  5449. }
  5450. user_code = [NSString stringWithUTF8String:code];
  5451. }];
  5452. // 再取所有salesRep
  5453. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5454. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5455. // 1 name 2 code 3 salesrep_id
  5456. char *name = (char *)sqlite3_column_text(stmt, 1);
  5457. char *code = (char *)sqlite3_column_text(stmt, 2);
  5458. int salesrep_id = sqlite3_column_int(stmt, 3);
  5459. if (name == NULL) {
  5460. name = "";
  5461. }
  5462. if (code == NULL) {
  5463. code = "";
  5464. }
  5465. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5466. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5467. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5468. // 比较code 相等则check
  5469. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5470. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5471. }
  5472. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5473. }] mutableCopy];
  5474. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5475. return ret;
  5476. }
  5477. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5478. zipcode = [self translateSingleQuote:zipcode];
  5479. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5480. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5481. char *country = (char *)sqlite3_column_text(stmt, 0);
  5482. char *state = (char *)sqlite3_column_text(stmt, 1);
  5483. char *city = (char *)sqlite3_column_text(stmt, 2);
  5484. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5485. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5486. if (country == NULL) {
  5487. country = "";
  5488. }
  5489. if (state == NULL) {
  5490. state = "";
  5491. }
  5492. if (city == NULL) {
  5493. city = "";
  5494. }
  5495. if (country_code == NULL) {
  5496. country_code = "";
  5497. }
  5498. if (state_code == NULL) {
  5499. state_code = "";
  5500. }
  5501. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5502. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5503. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5504. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5505. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5506. }] mutableCopy];
  5507. return ret;
  5508. }
  5509. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5510. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5511. [item setValue:value forKey:valueKey];
  5512. [dic setValue:item forKey:itemKey];
  5513. }
  5514. + (NSString *)countryCodeByid:(NSString *)code_id {
  5515. NSString *ret = nil;
  5516. code_id = [self translateSingleQuote:code_id];
  5517. sqlite3 *db = [iSalesDB get_db];
  5518. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5519. sqlite3_stmt * statement;
  5520. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5521. while (sqlite3_step(statement) == SQLITE_ROW) {
  5522. char *code = (char *)sqlite3_column_text(statement, 0);
  5523. if (code == NULL) {
  5524. code = "";
  5525. }
  5526. ret = [NSString stringWithUTF8String:code];
  5527. }
  5528. sqlite3_finalize(statement);
  5529. }
  5530. [iSalesDB close_db:db];
  5531. return ret;
  5532. }
  5533. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5534. NSString *ret = nil;
  5535. code = [self translateSingleQuote:code];
  5536. sqlite3 *db = [iSalesDB get_db];
  5537. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5538. sqlite3_stmt * statement;
  5539. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5540. while (sqlite3_step(statement) == SQLITE_ROW) {
  5541. char *_id = (char *)sqlite3_column_text(statement, 0);
  5542. if (_id == NULL) {
  5543. _id = "";
  5544. }
  5545. ret = [NSString stringWithFormat:@"%s",_id];
  5546. }
  5547. sqlite3_finalize(statement);
  5548. }
  5549. [iSalesDB close_db:db];
  5550. return ret;
  5551. }
  5552. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5553. NSString *name = nil;
  5554. codeId = [self translateSingleQuote:codeId];
  5555. sqlite3 *db = [iSalesDB get_db];
  5556. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5557. sqlite3_stmt * statement;
  5558. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5559. while (sqlite3_step(statement) == SQLITE_ROW) {
  5560. char *value = (char *)sqlite3_column_text(statement, 0);
  5561. if (value == NULL) {
  5562. value = "";
  5563. }
  5564. name = [NSString stringWithUTF8String:value];
  5565. }
  5566. sqlite3_finalize(statement);
  5567. }
  5568. [iSalesDB close_db:db];
  5569. return name;
  5570. }
  5571. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5572. NSString *ret = nil;
  5573. sqlite3 *db = [iSalesDB get_db];
  5574. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5575. sqlite3_stmt * statement;
  5576. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5577. while (sqlite3_step(statement) == SQLITE_ROW) {
  5578. char *name = (char *)sqlite3_column_text(statement, 0);
  5579. if (name == NULL) {
  5580. name = "";
  5581. }
  5582. ret = [NSString stringWithUTF8String:name];
  5583. }
  5584. sqlite3_finalize(statement);
  5585. }
  5586. [iSalesDB close_db:db];
  5587. return ret;
  5588. }
  5589. + (NSString *)salesRepCodeById:(NSString *)_id {
  5590. NSString *ret = nil;
  5591. _id = [self translateSingleQuote:_id];
  5592. sqlite3 *db = [iSalesDB get_db];
  5593. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5594. sqlite3_stmt * statement;
  5595. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5596. while (sqlite3_step(statement) == SQLITE_ROW) {
  5597. char *rep = (char *)sqlite3_column_text(statement, 0);
  5598. if (rep == NULL) {
  5599. rep = "";
  5600. }
  5601. ret = [NSString stringWithUTF8String:rep];
  5602. }
  5603. sqlite3_finalize(statement);
  5604. }
  5605. [iSalesDB close_db:db];
  5606. return ret;
  5607. }
  5608. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5609. char *tx = (char *)sqlite3_column_text(stmt, col);
  5610. if (tx == NULL) {
  5611. tx = "";
  5612. }
  5613. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5614. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5615. if (!text) {
  5616. text = @"";
  5617. }
  5618. // 将字符全部为' '的字符串干掉
  5619. int spaceCount = 0;
  5620. for (int i = 0; i < text.length; i++) {
  5621. if ([text characterAtIndex:i] == ' ') {
  5622. spaceCount++;
  5623. }
  5624. }
  5625. if (spaceCount == text.length) {
  5626. text = @"";
  5627. }
  5628. return text;
  5629. }
  5630. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5631. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5632. NSData *data = [NSData dataWithContentsOfFile:path];
  5633. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5634. return ret;
  5635. }
  5636. + (NSString *)textFileName:(NSString *)name {
  5637. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5638. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5639. if (!text) {
  5640. text = @"";
  5641. }
  5642. return text;
  5643. }
  5644. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5645. return [[dic objectForKey:key] mutableCopy];
  5646. }
  5647. + (id)translateSingleQuote:(NSString *)string {
  5648. if ([string isKindOfClass:[NSString class]])
  5649. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5650. return string;
  5651. }
  5652. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5653. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5654. }
  5655. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5656. NSString* ret= nil;
  5657. NSString *sqlQuery = nil;
  5658. // 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
  5659. 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];
  5660. sqlite3_stmt * statement;
  5661. // int count=0;
  5662. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5663. {
  5664. if (sqlite3_step(statement) == SQLITE_ROW)
  5665. {
  5666. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5667. if(imgurl==nil)
  5668. imgurl="";
  5669. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5670. ret=nsimgurl;
  5671. }
  5672. sqlite3_finalize(statement);
  5673. }
  5674. else
  5675. {
  5676. [ret setValue:@"8" forKey:@"result"];
  5677. }
  5678. // [iSalesDB close_db:db];
  5679. // DebugLog(@"data string: %@",ret );
  5680. return ret;
  5681. }
  5682. #pragma mark contact Advanced search
  5683. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5684. {
  5685. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5686. return [RAConvertor dict2data:contactAdvanceDic];
  5687. }
  5688. #pragma mark create new contact
  5689. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5690. assert(params[@"user"]!=nil);
  5691. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5692. NSData *data = [NSData dataWithContentsOfFile:path];
  5693. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5694. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5695. NSString *countryCode = nil;
  5696. NSString *countryCode_id = nil;
  5697. NSString *stateCode = nil;
  5698. NSString *city = nil;
  5699. NSString *zipCode = nil;
  5700. // NSString *carrierCode = nil;
  5701. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5702. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5703. NSString *code_id = params[@"country"];
  5704. countryCode_id = code_id;
  5705. countryCode = [self countryCodeByid:code_id];
  5706. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5707. NSString *zip_code = params[@"zipcode"];
  5708. // 剔除全部为空格
  5709. int spaceCount = 0;
  5710. for (int i = 0; i < zip_code.length; i++) {
  5711. if ([zip_code characterAtIndex:i] == ' ') {
  5712. spaceCount++;
  5713. }
  5714. }
  5715. if (spaceCount == zip_code.length) {
  5716. zip_code = @"";
  5717. }
  5718. zipCode = zip_code;
  5719. if (zipCode.length > 0) {
  5720. countryCode_id = params[@"country"];
  5721. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5722. countryCode = [dic valueForKey:@"country_code"];
  5723. if (!countryCode) {
  5724. // countryCode = @"US";
  5725. NSString *code_id = params[@"country"];
  5726. countryCode = [self countryCodeByid:code_id];
  5727. }
  5728. stateCode = [dic valueForKey:@"state_code"];
  5729. if (!stateCode.length) {
  5730. stateCode = params[@"state"];
  5731. }
  5732. city = [dic valueForKey:@"city"];
  5733. if (!city.length) {
  5734. city = params[@"city"];
  5735. }
  5736. // zip code
  5737. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5738. [zipDic setValue:zipCode forKey:@"value"];
  5739. [section_0 setValue:zipDic forKey:@"item_11"];
  5740. } else {
  5741. NSString *code_id = params[@"country"];
  5742. countryCode = [self countryCodeByid:code_id];
  5743. stateCode = params[@"state"];
  5744. city = params[@"city"];
  5745. }
  5746. }
  5747. } else {
  5748. // default: US United States
  5749. countryCode = @"US";
  5750. countryCode_id = @"228";
  5751. }
  5752. // country
  5753. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5754. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5755. // state
  5756. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5757. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5758. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5759. // city
  5760. if (city) {
  5761. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5762. [cityDic setValue:city forKey:@"value"];
  5763. [section_0 setValue:cityDic forKey:@"item_13"];
  5764. }
  5765. // price type
  5766. NSDictionary *priceDic = [self offline_getPrice];
  5767. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5768. // contact type
  5769. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5770. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5771. // Sales Rep
  5772. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5773. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5774. #ifdef BUILD_CONTRAST
  5775. //carrier
  5776. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5777. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5778. #endif
  5779. [ret setValue:section_0 forKey:@"section_0"];
  5780. return [RAConvertor dict2data:ret];
  5781. }
  5782. #pragma mark save
  5783. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5784. NSString *addr = nil;
  5785. NSString *contact_name = nil;
  5786. // int carrier = [params[@"carrier"] intValue];
  5787. NSString *companyName = [params objectForKey:@"company"];
  5788. if (companyName) {
  5789. companyName = [AESCrypt fastencrypt:companyName];
  5790. } else {
  5791. companyName = @"";
  5792. }
  5793. DebugLog(@"company");
  5794. companyName = [self translateSingleQuote:companyName];
  5795. NSString *addr1 = [params objectForKey:@"address"];
  5796. NSString *addr2 = [params objectForKey:@"address2"];
  5797. NSString *addr3 = [params objectForKey:@"address_3"];
  5798. NSString *addr4 = [params objectForKey:@"address_4"];
  5799. if (!addr2) {
  5800. addr2 = @"";
  5801. }
  5802. if (!addr3) {
  5803. addr3 = @"";
  5804. }
  5805. if (!addr4) {
  5806. addr4 = @"";
  5807. }
  5808. if (!addr1) {
  5809. addr1 = @"";
  5810. }
  5811. DebugLog(@"addr");
  5812. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5813. addr = [AESCrypt fastencrypt:addr];
  5814. addr = [self translateSingleQuote:addr];
  5815. if (addr1 && ![addr1 isEqualToString:@""]) {
  5816. addr1 = [AESCrypt fastencrypt:addr1];
  5817. }
  5818. addr1 = [self translateSingleQuote:addr1];
  5819. addr2 = [self translateSingleQuote:addr2];
  5820. addr3 = [self translateSingleQuote:addr3];
  5821. addr4 = [self translateSingleQuote:addr4];
  5822. NSString *country = [params objectForKey:@"country"];
  5823. if (country) {
  5824. country = [self countryNameByCountryCodeId:country];
  5825. } else {
  5826. country = @"";
  5827. }
  5828. int carrier = [params[@"carrier"] intValue];
  5829. DebugLog(@"country");
  5830. country = [self translateSingleQuote:country];
  5831. NSString *state = [params objectForKey:@"state"];
  5832. if (!state) {
  5833. state = @"";
  5834. }
  5835. DebugLog(@"state");
  5836. state = [self translateSingleQuote:state];
  5837. NSString *city = [params objectForKey:@"city"];
  5838. if (!city) {
  5839. city = @"";
  5840. }
  5841. city = [self translateSingleQuote:city];
  5842. NSString *zipcode = [params objectForKey:@"zipcode"];
  5843. if (!zipcode) {
  5844. zipcode = @"";
  5845. }
  5846. DebugLog(@"zip");
  5847. zipcode = [self translateSingleQuote:zipcode];
  5848. NSString *fistName = [params objectForKey:@"firstname"];
  5849. if (!fistName) {
  5850. fistName = @"";
  5851. }
  5852. NSString *lastName = [params objectForKey:@"lastname"];
  5853. if (!lastName) {
  5854. lastName = @"";
  5855. }
  5856. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5857. DebugLog(@"contact_name");
  5858. contact_name = [self translateSingleQuote:contact_name];
  5859. fistName = [self translateSingleQuote:fistName];
  5860. lastName = [self translateSingleQuote:lastName];
  5861. NSString *phone = [params objectForKey:@"phone"];
  5862. if (phone) {
  5863. phone = [AESCrypt fastencrypt:phone];
  5864. } else {
  5865. phone = @"";
  5866. }
  5867. DebugLog(@"PHONE");
  5868. phone = [self translateSingleQuote:phone];
  5869. NSString *fax = [params objectForKey:@"fax"];
  5870. if (!fax) {
  5871. fax = @"";
  5872. }
  5873. DebugLog(@"FAX");
  5874. fax = [self translateSingleQuote:fax];
  5875. NSString *email = [params objectForKey:@"email"];
  5876. if (!email) {
  5877. email = @"";
  5878. }
  5879. DebugLog(@"EMAIL:%@",email);
  5880. email = [self translateSingleQuote:email];
  5881. NSString *notes = [params objectForKey:@"contact_notes"];
  5882. if (!notes) {
  5883. notes = @"";
  5884. }
  5885. DebugLog(@"NOTE:%@",notes);
  5886. notes = [self translateSingleQuote:notes];
  5887. NSString *price = [params objectForKey:@"price_name"];
  5888. if (price) {
  5889. price = [self priceNameByPriceId:price];
  5890. } else {
  5891. price = @"";
  5892. }
  5893. DebugLog(@"PRICE");
  5894. price = [self translateSingleQuote:price];
  5895. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5896. if (salesRep) {
  5897. salesRep = [self salesRepCodeById:salesRep];
  5898. } else {
  5899. salesRep = @"";
  5900. }
  5901. salesRep = [self translateSingleQuote:salesRep];
  5902. NSString *img = [params objectForKey:@"business_card"];
  5903. NSArray *array = [img componentsSeparatedByString:@","];
  5904. NSString *img_0 = array[0];
  5905. if (!img_0) {
  5906. img_0 = @"";
  5907. }
  5908. img_0 = [self translateSingleQuote:img_0];
  5909. NSString *img_1 = array[1];
  5910. if (!img_1) {
  5911. img_1 = @"";
  5912. }
  5913. img_1 = [self translateSingleQuote:img_1];
  5914. NSString *img_2 = array[2];
  5915. if (!img_2) {
  5916. img_2 = @"";
  5917. }
  5918. img_2 = [self translateSingleQuote:img_2];
  5919. NSString *contact_id = [NSUUID UUID].UUIDString;
  5920. NSString *contact_type = [params objectForKey:@"type_name"];
  5921. if (!contact_type) {
  5922. contact_type = @"";
  5923. }
  5924. contact_type = [self translateSingleQuote:contact_type];
  5925. // 判断更新时是否为customer
  5926. if (update) {
  5927. contact_id = [params objectForKey:@"contact_id"];
  5928. if (!contact_id) {
  5929. contact_id = @"";
  5930. }
  5931. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5932. __block int customer = 0;
  5933. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5934. customer = sqlite3_column_int(stmt, 0);
  5935. }];
  5936. isCustomer = customer ? YES : NO;
  5937. }
  5938. NSMutableDictionary *sync_dic = [params mutableCopy];
  5939. if (isCustomer) {
  5940. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5941. } else {
  5942. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5943. }
  5944. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  5945. NSString *sync_data = nil;
  5946. NSString *sql = nil;
  5947. if (update){
  5948. contact_id = [params objectForKey:@"contact_id"];
  5949. if (!contact_id) {
  5950. contact_id = @"";
  5951. }
  5952. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5953. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5954. sync_data = [RAConvertor dict2string:sync_dic];
  5955. sync_data = [self translateSingleQuote:sync_data];
  5956. 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];
  5957. } else {
  5958. contact_id = [self translateSingleQuote:contact_id];
  5959. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5960. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5961. sync_data = [RAConvertor dict2string:sync_dic];
  5962. sync_data = [self translateSingleQuote:sync_data];
  5963. 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];
  5964. }
  5965. int result = [iSalesDB execSql:sql];
  5966. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5967. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5968. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5969. }
  5970. #pragma mark save new contact
  5971. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5972. {
  5973. return [self offline_saveContact:params update:NO isCustomer:YES];
  5974. }
  5975. #pragma mark edit contact
  5976. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5977. {
  5978. assert(params[@"user"]!=nil);
  5979. // {
  5980. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5981. // password = 123456;
  5982. // user = EvanK;
  5983. // }
  5984. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5985. NSData *data = [NSData dataWithContentsOfFile:path];
  5986. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5987. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5988. NSString *countryCode = nil;
  5989. NSString *countryCode_id = nil;
  5990. NSString *stateCode = nil;
  5991. __block int carrierCode = -1;
  5992. /*------contact infor------*/
  5993. __block NSString *country = nil;
  5994. __block NSString *company_name = nil;
  5995. __block NSString *contact_id = params[@"contact_id"];
  5996. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5997. __block NSString *zipcode = nil;
  5998. __block NSString *state = nil; // state_code
  5999. __block NSString *city = nil; //
  6000. __block NSString *firt_name,*last_name;
  6001. __block NSString *phone,*fax,*email;
  6002. __block NSString *notes,*price_type,*sales_rep;
  6003. __block NSString *img_0,*img_1,*img_2;
  6004. __block NSString *contact_type;
  6005. contact_id = [self translateSingleQuote:contact_id];
  6006. 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];
  6007. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6008. country = [self textAtColumn:0 statement:stmt]; // country name
  6009. company_name = [self textAtColumn:1 statement:stmt];
  6010. addr_1 = [self textAtColumn:2 statement:stmt];
  6011. addr_2 = [self textAtColumn:3 statement:stmt];
  6012. addr_3 = [self textAtColumn:4 statement:stmt];
  6013. addr_4 = [self textAtColumn:5 statement:stmt];
  6014. zipcode = [self textAtColumn:6 statement:stmt];
  6015. state = [self textAtColumn:7 statement:stmt]; // state code
  6016. city = [self textAtColumn:8 statement:stmt];
  6017. firt_name = [self textAtColumn:9 statement:stmt];
  6018. last_name = [self textAtColumn:10 statement:stmt];
  6019. phone = [self textAtColumn:11 statement:stmt];
  6020. fax = [self textAtColumn:12 statement:stmt];
  6021. email = [self textAtColumn:13 statement:stmt];
  6022. notes = [self textAtColumn:14 statement:stmt];
  6023. price_type = [self textAtColumn:15 statement:stmt]; // name
  6024. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6025. img_0 = [self textAtColumn:17 statement:stmt];
  6026. img_1 = [self textAtColumn:18 statement:stmt];
  6027. img_2 = [self textAtColumn:19 statement:stmt];
  6028. contact_type = [self textAtColumn:20 statement:stmt];
  6029. carrierCode = sqlite3_column_int(stmt, 21);
  6030. }];
  6031. // decrypt
  6032. if (company_name) {
  6033. company_name = [AESCrypt fastdecrypt:company_name];
  6034. }
  6035. if (addr_1) {
  6036. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6037. }
  6038. if (phone) {
  6039. phone = [AESCrypt fastdecrypt:phone];
  6040. }
  6041. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6042. countryCode = [iSalesDB jk_queryText:countrySql];
  6043. stateCode = state;
  6044. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6045. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6046. NSString *code_id = params[@"country"];
  6047. countryCode_id = code_id;
  6048. countryCode = [self countryCodeByid:code_id];
  6049. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6050. NSString *zip_code = params[@"zipcode"];
  6051. // 剔除全部为空格
  6052. int spaceCount = 0;
  6053. for (int i = 0; i < zip_code.length; i++) {
  6054. if ([zip_code characterAtIndex:i] == ' ') {
  6055. spaceCount++;
  6056. }
  6057. }
  6058. if (spaceCount == zip_code.length) {
  6059. zip_code = @"";
  6060. }
  6061. if (zipcode.length > 0) {
  6062. zipcode = zip_code;
  6063. countryCode_id = params[@"country"];
  6064. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6065. countryCode = [dic valueForKey:@"country_code"];
  6066. if (!countryCode) {
  6067. NSString *code_id = params[@"country"];
  6068. countryCode = [self countryCodeByid:code_id];
  6069. }
  6070. stateCode = [dic valueForKey:@"state_code"];
  6071. if (!stateCode.length) {
  6072. stateCode = params[@"state"];
  6073. }
  6074. city = [dic valueForKey:@"city"];
  6075. if (!city.length) {
  6076. city = params[@"city"];
  6077. }
  6078. // zip code
  6079. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6080. [zipDic setValue:zipcode forKey:@"value"];
  6081. [section_0 setValue:zipDic forKey:@"item_8"];
  6082. } else {
  6083. NSString *code_id = params[@"country"];
  6084. countryCode = [self countryCodeByid:code_id];
  6085. stateCode = params[@"state"];
  6086. city = params[@"city"];
  6087. }
  6088. }
  6089. }
  6090. // 0 Country
  6091. // 1 Company Name
  6092. // 2 Contact ID
  6093. // 3 Picture
  6094. // 4 Address 1
  6095. // 5 Address 2
  6096. // 6 Address 3
  6097. // 7 Address 4
  6098. // 8 Zip Code
  6099. // 9 State/Province
  6100. // 10 City
  6101. // 11 Contact First Name
  6102. // 12 Contact Last Name
  6103. // 13 Phone
  6104. // 14 Fax
  6105. // 15 Email
  6106. // 16 Contact Notes
  6107. // 17 Price Type
  6108. // 18 Contact Type
  6109. // 19 Sales Rep
  6110. // country
  6111. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6112. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6113. // company
  6114. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6115. // contact_id
  6116. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6117. // picture
  6118. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6119. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6120. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6121. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6122. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6123. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6124. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6125. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6126. // addr 1 2 3 4
  6127. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6128. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6129. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6130. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6131. // zip code
  6132. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6133. // state
  6134. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6135. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6136. // city
  6137. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6138. // first last
  6139. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6140. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6141. // phone fax email
  6142. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6143. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6144. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6145. // notes
  6146. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6147. // price
  6148. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6149. for (NSString *key in priceDic.allKeys) {
  6150. if ([key containsString:@"val_"]) {
  6151. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6152. if ([dic[@"value"] isEqualToString:price_type]) {
  6153. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6154. [priceDic setValue:dic forKey:key];
  6155. }
  6156. }
  6157. }
  6158. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6159. // Contact Rep
  6160. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6161. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6162. // Sales Rep
  6163. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6164. for (NSString *key in repDic.allKeys) {
  6165. if ([key containsString:@"val_"]) {
  6166. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6167. NSString *value = dic[@"value"];
  6168. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6169. if (code && [code isEqualToString:sales_rep]) {
  6170. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6171. [repDic setValue:dic forKey:key];
  6172. }
  6173. }
  6174. }
  6175. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6176. #ifdef BUILD_CONTRAST
  6177. //carrier
  6178. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6179. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6180. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6181. #endif
  6182. [ret setValue:section_0 forKey:@"section_0"];
  6183. return [RAConvertor dict2data:ret];
  6184. }
  6185. #pragma mark save contact
  6186. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6187. {
  6188. return [self offline_saveContact:params update:YES isCustomer:YES];
  6189. }
  6190. #pragma mark category
  6191. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6192. if (ck) {
  6193. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6194. for (NSString *key in res.allKeys) {
  6195. if (![key isEqualToString:@"count"]) {
  6196. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6197. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6198. if ([val[@"value"] isEqualToString:ck]) {
  6199. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6200. }
  6201. [res setValue:val forKey:key];
  6202. }
  6203. }
  6204. [dic setValue:res forKey:valueKey];
  6205. }
  6206. }
  6207. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6208. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6209. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6210. NSString* category = [params valueForKey:@"category"];
  6211. if (!category || [category isEqualToString:@""]) {
  6212. category = @"%";
  6213. }
  6214. category = [self translateSingleQuote:category];
  6215. int limit = [[params valueForKey:@"limit"] intValue];
  6216. int offset = [[params valueForKey:@"offset"] intValue];
  6217. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6218. NSString *limit_str = @"";
  6219. if (limited) {
  6220. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6221. }
  6222. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6223. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6224. sqlite3 *db = [iSalesDB get_db];
  6225. // [iSalesDB AddExFunction:db];
  6226. int count;
  6227. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6228. 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];
  6229. double price_min = 0;
  6230. double price_max = 0;
  6231. if ([params.allKeys containsObject:@"alert"]) {
  6232. // alert
  6233. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6234. NSString *alert = params[@"alert"];
  6235. if ([alert isEqualToString:@"Display All"]) {
  6236. alert = [NSString stringWithFormat:@""];
  6237. } else {
  6238. alert = [self translateSingleQuote:alert];
  6239. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6240. }
  6241. // available
  6242. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6243. NSString *available = params[@"available"];
  6244. NSString *available_condition;
  6245. if ([available isEqualToString:@"Display All"]) {
  6246. available_condition = @"";
  6247. } else if ([available isEqualToString:@"Available Now"]) {
  6248. available_condition = @"and availability > 0";
  6249. } else {
  6250. available_condition = @"and availability == 0";
  6251. }
  6252. // best seller
  6253. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6254. NSString *best_seller = @"";
  6255. NSString *order_best_seller = @"m.name asc";
  6256. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6257. best_seller = @"and best_seller > 0";
  6258. order_best_seller = @"m.best_seller desc,m.name asc";
  6259. }
  6260. // price
  6261. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6262. NSString *price = params[@"price"];
  6263. price_min = 0;
  6264. price_max = MAXFLOAT;
  6265. if (params[@"user"] && price != nil) {
  6266. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6267. NSMutableString *priceName = [NSMutableString string];
  6268. for (int i = 0; i < priceTypeArray.count; i++) {
  6269. NSString *pricetype = priceTypeArray[i];
  6270. pricetype = [self translateSingleQuote:pricetype];
  6271. if (i == 0) {
  6272. [priceName appendFormat:@"'%@'",pricetype];
  6273. } else {
  6274. [priceName appendFormat:@",'%@'",pricetype];
  6275. }
  6276. }
  6277. if ([price isEqualToString:@"Display All"]) {
  6278. price = [NSString stringWithFormat:@""];
  6279. } else if([price containsString:@"+"]){
  6280. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6281. price_min = [price doubleValue];
  6282. 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];
  6283. } else {
  6284. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6285. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6286. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6287. 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];
  6288. }
  6289. } else {
  6290. price = @"";
  6291. }
  6292. // sold_by_qty : Sold in quantities of %@
  6293. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6294. NSString *qty = params[@"sold_by_qty"];
  6295. if ([qty isEqualToString:@"Display All"]) {
  6296. qty = @"";
  6297. } else {
  6298. qty = [self translateSingleQuote:qty];
  6299. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6300. }
  6301. // model name;
  6302. NSString* modelname =params[@"modelName"];
  6303. if(modelname.length==0)
  6304. {
  6305. modelname=@"";
  6306. }
  6307. else
  6308. {
  6309. modelname = modelname.lowercaseString;
  6310. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6311. }
  6312. //modelDescription
  6313. NSString* modelDescription =params[@"modelDescription"];
  6314. if(modelDescription.length==0)
  6315. {
  6316. modelDescription=@"";
  6317. }
  6318. else
  6319. {
  6320. modelDescription = modelDescription.lowercaseString;
  6321. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6322. }
  6323. // cate
  6324. // category = [self translateSingleQuote:category];
  6325. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6326. // cate mutiple selection
  6327. NSString *category_id = params[@"category"];
  6328. NSMutableArray *cate_id_array = nil;
  6329. NSMutableString *cateWhere = [NSMutableString string];
  6330. if ([category_id isEqualToString:@""] || !category_id) {
  6331. [cateWhere appendString:@"1 = 1"];
  6332. } else {
  6333. if ([category_id containsString:@","]) {
  6334. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6335. } else {
  6336. cate_id_array = [@[category_id] mutableCopy];
  6337. }
  6338. [cateWhere appendString:@"("];
  6339. for (int i = 0; i < cate_id_array.count; i++) {
  6340. if (i == 0) {
  6341. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6342. } else {
  6343. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6344. }
  6345. }
  6346. [cateWhere appendString:@")"];
  6347. }
  6348. // where bestseller > 0 order by bestseller desc
  6349. // sql query: alert availability(int) best_seller(int) price qty
  6350. 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];
  6351. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6352. }
  6353. DebugLog(@"offline category where: %@",where);
  6354. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6355. if (!params[@"user"]) {
  6356. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6357. }
  6358. [ret setValue:filter forKey:@"filter"];
  6359. DebugLog(@"offline_category sql:%@",sqlQuery);
  6360. sqlite3_stmt * statement;
  6361. [ret setValue:@"2" forKey:@"result"];
  6362. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6363. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6364. // int count=0;
  6365. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6366. {
  6367. int i=0;
  6368. while (sqlite3_step(statement) == SQLITE_ROW)
  6369. {
  6370. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6371. char *name = (char*)sqlite3_column_text(statement, 0);
  6372. if(name==nil)
  6373. name="";
  6374. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6375. char *description = (char*)sqlite3_column_text(statement, 1);
  6376. if(description==nil)
  6377. description="";
  6378. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6379. int product_id = sqlite3_column_int(statement, 2);
  6380. int wid = sqlite3_column_int(statement, 3);
  6381. int closeout = sqlite3_column_int(statement, 4);
  6382. int cid = sqlite3_column_int(statement, 5);
  6383. int wisdelete = sqlite3_column_int(statement, 6);
  6384. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6385. int more_color = sqlite3_column_int(statement, 8);
  6386. // Defaul Category ID
  6387. __block NSString *categoryID = nil;
  6388. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6389. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6390. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6391. if(default_category==nil)
  6392. default_category="";
  6393. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6394. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6395. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6396. categoryID = nsdefault_category;
  6397. }];
  6398. if (!categoryID.length) {
  6399. NSString *cateIDs = params[@"category"];
  6400. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6401. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6402. for (NSString *cateID in requestCategoryArr) {
  6403. BOOL needBreak = NO;
  6404. for (NSString *itemCateIDBox in itemCategoryArr) {
  6405. if (itemCateIDBox.length > 4) {
  6406. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6407. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6408. if ([itemCategoryID isEqualToString:cateID]) {
  6409. needBreak = YES;
  6410. categoryID = itemCategoryID;
  6411. break;
  6412. }
  6413. }
  6414. }
  6415. if (needBreak) {
  6416. break;
  6417. }
  6418. }
  6419. }
  6420. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6421. if(wid !=0 && wisdelete != 1)
  6422. [item setValue:@"true" forKey:@"wish_exists"];
  6423. else
  6424. [item setValue:@"false" forKey:@"wish_exists"];
  6425. if(closeout==0)
  6426. [item setValue:@"false" forKey:@"is_closeout"];
  6427. else
  6428. [item setValue:@"true" forKey:@"is_closeout"];
  6429. if(cid==0)
  6430. [item setValue:@"false" forKey:@"cart_exists"];
  6431. else
  6432. [item setValue:@"true" forKey:@"cart_exists"];
  6433. if (more_color == 0) {
  6434. [item setObject:@(false) forKey:@"more_color"];
  6435. } else if (more_color == 1) {
  6436. [item setObject:@(true) forKey:@"more_color"];
  6437. }
  6438. [item addEntriesFromDictionary:imgjson];
  6439. // [item setValue:nsurl forKey:@"img"];
  6440. [item setValue:nsname forKey:@"name"];
  6441. [item setValue:nsdescription forKey:@"description"];
  6442. if (categoryID) {
  6443. [item setValue:categoryID forKey:@"item_category_id"];
  6444. }
  6445. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6446. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6447. i++;
  6448. }
  6449. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6450. [ret setObject:items forKey:@"items"];
  6451. sqlite3_finalize(statement);
  6452. } else {
  6453. DebugLog(@"nothing...");
  6454. }
  6455. DebugLog(@"count:%d",count);
  6456. [iSalesDB close_db:db];
  6457. #ifdef DEBUG
  6458. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6459. #endif
  6460. return ret;
  6461. }
  6462. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6463. {
  6464. return [self categoryList:params limited:YES];
  6465. }
  6466. # pragma mark item search
  6467. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6468. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6469. // assert(params[@"order_code"]);
  6470. // params[@"user"]
  6471. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6472. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6473. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6474. // category
  6475. NSDictionary *category_menu = [self offline_category_menu];
  6476. [filter setValue:category_menu forKey:@"category"];
  6477. NSString* where= nil;
  6478. NSString* orderby= @"m.name";
  6479. if (!filterSearch) {
  6480. int covertype = [[params valueForKey:@"covertype"] intValue];
  6481. switch (covertype) {
  6482. case 0:
  6483. {
  6484. where=@"m.category like'%%#005#%%'";
  6485. break;
  6486. }
  6487. case 1:
  6488. {
  6489. where=@"m.alert like '%QS%'";
  6490. break;
  6491. }
  6492. case 2:
  6493. {
  6494. where=@"m.availability>0";
  6495. break;
  6496. }
  6497. case 3:
  6498. {
  6499. where=@"m.best_seller>0";
  6500. orderby=@"m.best_seller desc,m.name asc";
  6501. break;
  6502. }
  6503. default:
  6504. where=@"1=1";
  6505. break;
  6506. }
  6507. }
  6508. int limit = [[params valueForKey:@"limit"] intValue];
  6509. int offset = [[params valueForKey:@"offset"] intValue];
  6510. NSString *limit_str = @"";
  6511. if (limited) {
  6512. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6513. }
  6514. sqlite3 *db = [iSalesDB get_db];
  6515. // [iSalesDB AddExFunction:db];
  6516. int count;
  6517. NSString *sqlQuery = nil;
  6518. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6519. 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];
  6520. double price_min = 0;
  6521. double price_max = 0;
  6522. if (filterSearch) {
  6523. // alert
  6524. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6525. NSString *alert = params[@"alert"];
  6526. if ([alert isEqualToString:@"Display All"]) {
  6527. alert = [NSString stringWithFormat:@""];
  6528. } else {
  6529. alert = [self translateSingleQuote:alert];
  6530. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6531. }
  6532. // available
  6533. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6534. NSString *available = params[@"available"];
  6535. NSString *available_condition;
  6536. if ([available isEqualToString:@"Display All"]) {
  6537. available_condition = @"";
  6538. } else if ([available isEqualToString:@"Available Now"]) {
  6539. available_condition = @"and availability > 0";
  6540. } else {
  6541. available_condition = @"and availability == 0";
  6542. }
  6543. // best seller
  6544. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6545. NSString *best_seller = @"";
  6546. NSString *order_best_seller = @"m.name asc";
  6547. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6548. best_seller = @"and best_seller > 0";
  6549. order_best_seller = @"m.best_seller desc,m.name asc";
  6550. }
  6551. // price
  6552. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6553. NSString *price = params[@"price"];
  6554. price_min = 0;
  6555. price_max = MAXFLOAT;
  6556. if (params[@"user"]) {
  6557. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6558. NSMutableString *priceName = [NSMutableString string];
  6559. for (int i = 0; i < priceTypeArray.count; i++) {
  6560. NSString *pricetype = priceTypeArray[i];
  6561. pricetype = [self translateSingleQuote:pricetype];
  6562. if (i == 0) {
  6563. [priceName appendFormat:@"'%@'",pricetype];
  6564. } else {
  6565. [priceName appendFormat:@",'%@'",pricetype];
  6566. }
  6567. }
  6568. if ([price isEqualToString:@"Display All"]) {
  6569. price = [NSString stringWithFormat:@""];
  6570. } else if([price containsString:@"+"]){
  6571. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6572. price_min = [price doubleValue];
  6573. 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];
  6574. } else {
  6575. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6576. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6577. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6578. 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];
  6579. }
  6580. } else {
  6581. price = @"";
  6582. }
  6583. // sold_by_qty : Sold in quantities of %@
  6584. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6585. NSString *qty = params[@"sold_by_qty"];
  6586. if ([qty isEqualToString:@"Display All"]) {
  6587. qty = @"";
  6588. } else {
  6589. qty = [self translateSingleQuote:qty];
  6590. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6591. }
  6592. // model name;
  6593. NSString* modelname =params[@"modelName"];
  6594. if(modelname.length==0)
  6595. {
  6596. modelname=@"";
  6597. }
  6598. else
  6599. {
  6600. modelname = modelname.lowercaseString;
  6601. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6602. }
  6603. //modelDescription
  6604. NSString* modelDescription =params[@"modelDescription"];
  6605. if(modelDescription.length==0)
  6606. {
  6607. modelDescription=@"";
  6608. }
  6609. else
  6610. {
  6611. modelDescription = modelDescription.lowercaseString;
  6612. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6613. }
  6614. //lower(description) like'%%%@%%'
  6615. // category
  6616. NSString *category_id = params[@"ctgId"];
  6617. NSMutableArray *cate_id_array = nil;
  6618. NSMutableString *cateWhere = [NSMutableString string];
  6619. if ([category_id isEqualToString:@""] || !category_id) {
  6620. [cateWhere appendString:@"1 = 1"];
  6621. } else {
  6622. if ([category_id containsString:@","]) {
  6623. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6624. } else {
  6625. cate_id_array = [@[category_id] mutableCopy];
  6626. }
  6627. /*-----------*/
  6628. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6629. [cateWhere appendString:@"("];
  6630. for (int i = 0; i < cate_id_array.count; i++) {
  6631. for (NSString *key0 in cateDic.allKeys) {
  6632. if ([key0 containsString:@"category_"]) {
  6633. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6634. for (NSString *key1 in category0.allKeys) {
  6635. if ([key1 containsString:@"category_"]) {
  6636. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6637. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6638. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6639. cate_id_array[i] = [category1 objectForKey:@"id"];
  6640. if (i == 0) {
  6641. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6642. } else {
  6643. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6644. }
  6645. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6646. [category0 setValue:category1 forKey:key1];
  6647. [cateDic setValue:category0 forKey:key0];
  6648. }
  6649. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6650. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6651. cate_id_array[i] = [category0 objectForKey:@"id"];
  6652. if (i == 0) {
  6653. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6654. } else {
  6655. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6656. }
  6657. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6658. [cateDic setValue:category0 forKey:key0];
  6659. }
  6660. }
  6661. }
  6662. }
  6663. }
  6664. }
  6665. [cateWhere appendString:@")"];
  6666. [filter setValue:cateDic forKey:@"category"];
  6667. }
  6668. // where bestseller > 0 order by bestseller desc
  6669. // sql query: alert availability(int) best_seller(int) price qty
  6670. 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];
  6671. // count
  6672. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6673. }
  6674. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6675. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6676. if (!params[@"user"]) {
  6677. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6678. }
  6679. [ret setValue:filter forKey:@"filter"];
  6680. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6681. sqlite3_stmt * statement;
  6682. [ret setValue:@"2" forKey:@"result"];
  6683. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6684. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6685. // int count=0;
  6686. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6687. {
  6688. int i=0;
  6689. while (sqlite3_step(statement) == SQLITE_ROW)
  6690. {
  6691. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6692. char *name = (char*)sqlite3_column_text(statement, 0);
  6693. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6694. char *description = (char*)sqlite3_column_text(statement, 1);
  6695. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6696. int product_id = sqlite3_column_int(statement, 2);
  6697. int wid = sqlite3_column_int(statement, 3);
  6698. int closeout = sqlite3_column_int(statement, 4);
  6699. int cid = sqlite3_column_int(statement, 5);
  6700. int wisdelete = sqlite3_column_int(statement, 6);
  6701. int more_color = sqlite3_column_int(statement, 7);
  6702. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6703. if(wid !=0 && wisdelete != 1)
  6704. [item setValue:@"true" forKey:@"wish_exists"];
  6705. else
  6706. [item setValue:@"false" forKey:@"wish_exists"];
  6707. if(closeout==0)
  6708. [item setValue:@"false" forKey:@"is_closeout"];
  6709. else
  6710. [item setValue:@"true" forKey:@"is_closeout"];
  6711. if(cid==0)
  6712. [item setValue:@"false" forKey:@"cart_exists"];
  6713. else
  6714. [item setValue:@"true" forKey:@"cart_exists"];
  6715. if (more_color == 0) {
  6716. [item setObject:@(false) forKey:@"more_color"];
  6717. } else if (more_color == 1) {
  6718. [item setObject:@(true) forKey:@"more_color"];
  6719. }
  6720. [item addEntriesFromDictionary:imgjson];
  6721. // [item setValue:nsurl forKey:@"img"];
  6722. [item setValue:nsname forKey:@"fash_name"];
  6723. [item setValue:nsdescription forKey:@"description"];
  6724. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6725. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6726. i++;
  6727. }
  6728. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6729. [ret setObject:items forKey:@"items"];
  6730. sqlite3_finalize(statement);
  6731. }
  6732. [iSalesDB close_db:db];
  6733. #ifdef DEBUG
  6734. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6735. #endif
  6736. return ret;
  6737. }
  6738. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6739. {
  6740. return [self itemsearch:params limited:YES];
  6741. }
  6742. #pragma mark order detail
  6743. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6744. if (str1.length == 0) {
  6745. str1 = @"&nbsp";
  6746. }
  6747. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6748. return str;
  6749. }
  6750. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6751. {
  6752. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6753. [fromformatter setDateFormat:from];
  6754. NSDate *date = [fromformatter dateFromString:datetime];
  6755. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6756. [toformatter setDateFormat:to];
  6757. NSString * ret = [toformatter stringFromDate:date];
  6758. return ret;
  6759. }
  6760. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6761. // 把毫秒去掉
  6762. if ([dateTime containsString:@"."]) {
  6763. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6764. }
  6765. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6766. formatter.dateFormat = formate;
  6767. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6768. NSDate *date = [formatter dateFromString:dateTime];
  6769. formatter.dateFormat = newFormate;
  6770. NSString *result = [formatter stringFromDate:date];
  6771. return result ? result : @"";
  6772. }
  6773. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6774. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6775. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6776. }
  6777. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6778. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6779. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6780. }
  6781. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6782. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6783. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6784. }
  6785. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6786. {
  6787. assert(params[@"mode"]!=nil);
  6788. assert(params[@"user"]!=nil);
  6789. DebugLog(@"offline oderdetail params: %@",params);
  6790. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6791. int orderId = [params[@"orderId"] intValue];
  6792. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6793. // decrypt card number and card security code
  6794. // 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 ];
  6795. 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,discount 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,discount from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  6796. sqlite3 *db = [iSalesDB get_db];
  6797. sqlite3_stmt * statement;
  6798. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6799. NSString *nssoid = @"";
  6800. NSString* orderinfo = @"";
  6801. NSString *moreInfo = @"";
  6802. double handlingFee = 0;
  6803. double payments_and_credist = 0;
  6804. double totalPrice = 0;
  6805. double shippingFee = 0;
  6806. double lift_gate = 0;
  6807. NSString *customer_contact = @"";
  6808. NSString *customer_email = @"";
  6809. NSString *customer_fax = @"";
  6810. NSString *customer_phone = @"";
  6811. NSString *customer_city = @"";
  6812. NSString *customer_state = @"";
  6813. NSString *customer_zipcode = @"";
  6814. NSString *customer_country = @"";
  6815. BOOL must_call = NO;
  6816. double discount = 0;
  6817. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6818. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6819. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6820. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6821. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6822. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6823. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6824. {
  6825. if (sqlite3_step(statement) == SQLITE_ROW)
  6826. {
  6827. // int order_id = sqlite3_column_int(statement, 0);
  6828. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6829. // ret[@"sign_url"] = sign_url;
  6830. section_1 = @{
  6831. @"data":sign_url,
  6832. @"title":@"Signature",
  6833. @"type":@"sign_url"
  6834. }.mutableCopy;
  6835. [ret setObject:section_1 forKey:@"section_1"];
  6836. customer_contact = [self textAtColumn:52 statement:statement];
  6837. customer_email = [self textAtColumn:53 statement:statement];
  6838. customer_phone = [self textAtColumn:54 statement:statement];
  6839. customer_fax = [self textAtColumn:55 statement:statement];
  6840. customer_city = [self textAtColumn:60 statement:statement];
  6841. customer_state = [self textAtColumn:61 statement:statement];
  6842. customer_zipcode = [self textAtColumn:62 statement:statement];
  6843. customer_country = [self textAtColumn:63 statement:statement];
  6844. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6845. discount =sqlite3_column_double(statement, 65);
  6846. int offline_edit=sqlite3_column_int(statement, 56);
  6847. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6848. char *soid = (char*)sqlite3_column_text(statement, 1);
  6849. if(soid==nil)
  6850. soid= "";
  6851. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6852. // so#
  6853. ret[@"so#"] = nssoid;
  6854. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6855. if(poNumber==nil)
  6856. poNumber= "";
  6857. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6858. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6859. if(create_time==nil)
  6860. create_time= "";
  6861. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6862. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6863. int status = sqlite3_column_int(statement, 4);
  6864. int erpStatus = sqlite3_column_int(statement, 49);
  6865. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6866. // status
  6867. if (status > 1 && status != 3) {
  6868. status = erpStatus;
  6869. } else if (status == 3) {
  6870. status = 15;
  6871. }
  6872. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6873. ret[@"order_status"] = nsstatus;
  6874. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6875. if(company_name==nil)
  6876. company_name= "";
  6877. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6878. // company name
  6879. ret[@"company_name"] = nscompany_name;
  6880. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6881. if(customer_contact==nil)
  6882. customer_contact= "";
  6883. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6884. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6885. if(addr_1==nil)
  6886. addr_1="";
  6887. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6888. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6889. if(addr_2==nil)
  6890. addr_2="";
  6891. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6892. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6893. if(addr_3==nil)
  6894. addr_3="";
  6895. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6896. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6897. if(addr_4==nil)
  6898. addr_4="";
  6899. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6900. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6901. [arr_addr addObject:nsaddr_1];
  6902. [arr_addr addObject:nsaddr_2];
  6903. [arr_addr addObject:nsaddr_3];
  6904. [arr_addr addObject:nsaddr_4];
  6905. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6906. [arr_addr addObject:customer_state];
  6907. [arr_addr addObject:customer_zipcode];
  6908. [arr_addr addObject:customer_country];
  6909. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6910. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6911. char *logist = (char*)sqlite3_column_text(statement, 11);
  6912. if(logist==nil)
  6913. logist= "";
  6914. NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  6915. if (/*status == -11 || */status == 10 || status == 11) {
  6916. DebugLogist = [self textAtColumn:59 statement:statement];
  6917. };
  6918. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6919. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6920. shipping = @"Shipping To Be Quoted";
  6921. } else {
  6922. shippingFee = sqlite3_column_double(statement, 12);
  6923. }
  6924. // Shipping
  6925. // ret[@"Shipping"] = shipping;
  6926. NSDictionary *shipping_item = @{
  6927. @"title":@"Shipping",
  6928. @"value":shipping
  6929. };
  6930. [price_data setObject:shipping_item forKey:@"item_1"];
  6931. int have_lift_gate = sqlite3_column_int(statement, 17);
  6932. lift_gate = sqlite3_column_double(statement, 13);
  6933. // Liftgate Fee(No loading dock)
  6934. if (!have_lift_gate) {
  6935. lift_gate = 0;
  6936. }
  6937. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6938. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6939. if (sqlite3_column_int(statement, 57)) {
  6940. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6941. liftgate_value = @"Shipping To Be Quoted";
  6942. }
  6943. NSDictionary *liftgate_item = @{
  6944. @"title":@"Liftgate Fee(No loading dock)",
  6945. @"value":liftgate_value
  6946. };
  6947. [price_data setObject:liftgate_item forKey:@"item_2"];
  6948. // if ([DebugLogist isEqualToString:@"WILL CALL"]) {
  6949. // [ret removeObjectForKey:@"Shipping"];
  6950. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6951. // }
  6952. //
  6953. // if (have_lift_gate) {
  6954. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6955. // }
  6956. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6957. if(general_notes==nil)
  6958. general_notes= "";
  6959. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6960. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6961. if(internal_notes==nil)
  6962. internal_notes= "";
  6963. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6964. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6965. if(payment_type==nil)
  6966. payment_type= "";
  6967. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6968. // order info
  6969. orderinfo = [self textFileName:@"order_info.html"];
  6970. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6971. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6972. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6973. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6974. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6975. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6976. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6977. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  6978. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6979. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6980. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6981. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6982. NSString *payment = nil;
  6983. // id -> show
  6984. __block NSString *show_pay_type = nspayment_type;
  6985. [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) {
  6986. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6987. if (show_typ_ch != NULL) {
  6988. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6989. }
  6990. }];
  6991. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6992. {
  6993. payment = [self textFileName:@"creditcardpayment.html"];
  6994. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6995. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6996. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6997. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6998. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6999. NSString *card_type = [self textAtColumn:42 statement:statement];
  7000. if (card_type.length > 0) { // 显示星号
  7001. card_type = @"****";
  7002. }
  7003. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7004. if (card_number.length > 0 && card_number.length > 4) {
  7005. for (int i = 0; i < card_number.length - 4; i++) {
  7006. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7007. }
  7008. } else {
  7009. card_number = @"";
  7010. }
  7011. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7012. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7013. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7014. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7015. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7016. card_expiration = @"****";
  7017. }
  7018. NSString *card_city = [self textAtColumn:46 statement:statement];
  7019. NSString *card_state = [self textAtColumn:47 statement:statement];
  7020. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7021. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7022. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7023. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7024. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7025. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7026. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7027. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7028. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7029. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7030. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7031. }
  7032. else
  7033. {
  7034. payment=[self textFileName:@"normalpayment.html"];
  7035. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7036. }
  7037. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7038. ret[@"result"]= [NSNumber numberWithInt:2];
  7039. // more info
  7040. moreInfo = [self textFileName:@"more_info.html"];
  7041. /*****ship to******/
  7042. // ShipToCompany_or_&nbsp
  7043. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7044. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7045. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7046. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7047. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7048. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7049. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7050. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7051. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7052. /*****ship from******/
  7053. // ShipFromCompany_or_&nbsp
  7054. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7055. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7056. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7057. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7058. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7059. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7060. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7061. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7062. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7063. /*****freight to******/
  7064. // FreightBillToCompany_or_&nbsp
  7065. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7066. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7067. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7068. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7069. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7070. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7071. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7072. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7073. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7074. /*****merchandise to******/
  7075. // MerchandiseBillToCompany_or_&nbsp
  7076. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7077. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7078. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7079. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7080. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7081. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7082. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7083. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7084. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7085. /*****return to******/
  7086. // ReturnToCompany_or_&nbsp
  7087. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7088. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7089. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7090. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7091. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7092. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7093. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7094. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7095. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7096. //
  7097. // DebugLog(@"more info : %@",moreInfo);
  7098. // handling fee
  7099. handlingFee = sqlite3_column_double(statement, 33);
  7100. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7101. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7102. if (sqlite3_column_int(statement, 58)) {
  7103. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7104. handling_fee_value = @"Shipping To Be Quoted";
  7105. }
  7106. NSDictionary *handling_fee_item = @{
  7107. @"title":@"Handling Fee",
  7108. @"value":handling_fee_value
  7109. };
  7110. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7111. //
  7112. // customer info
  7113. customerID = [self textAtColumn:36 statement:statement];
  7114. // mode
  7115. ret[@"mode"] = params[@"mode"];
  7116. // model_count
  7117. ret[@"model_count"] = @(0);
  7118. }
  7119. sqlite3_finalize(statement);
  7120. }
  7121. [iSalesDB close_db:db];
  7122. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7123. // "customer_email" = "Shui Hu";
  7124. // "customer_fax" = "";
  7125. // "customer_first_name" = F;
  7126. // "customer_last_name" = L;
  7127. // "customer_name" = ",da He Xiang Dong Liu A";
  7128. // "customer_phone" = "Hey Xuan Feng";
  7129. contactInfo[@"customer_phone"] = customer_phone;
  7130. contactInfo[@"customer_fax"] = customer_fax;
  7131. contactInfo[@"customer_email"] = customer_email;
  7132. NSString *first_name = @"";
  7133. NSString *last_name = @"";
  7134. if ([customer_contact isEqualToString:@""]) {
  7135. } else if ([customer_contact containsString:@" "]) {
  7136. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7137. first_name = [customer_contact substringToIndex:first_space_index];
  7138. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7139. }
  7140. contactInfo[@"customer_first_name"] = first_name;
  7141. contactInfo[@"customer_last_name"] = last_name;
  7142. ret[@"customerInfo"] = contactInfo;
  7143. // models
  7144. if (nssoid) {
  7145. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7146. __block double TotalCuft = 0;
  7147. __block double TotalWeight = 0;
  7148. __block int TotalCarton = 0;
  7149. __block double allItemPrice = 0;
  7150. 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];
  7151. sqlite3 *db1 = [iSalesDB get_db];
  7152. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7153. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7154. int product_id = sqlite3_column_int(stmt, 0);
  7155. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7156. int item_id = sqlite3_column_int(stmt, 7);
  7157. NSString* Price=nil;
  7158. if(str_price==nil)
  7159. {
  7160. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7161. if(price==nil)
  7162. Price=@"No Price.";
  7163. else
  7164. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7165. }
  7166. else
  7167. {
  7168. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7169. }
  7170. double discount = sqlite3_column_double(stmt, 2);
  7171. int item_count = sqlite3_column_int(stmt, 3);
  7172. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7173. NSString *nsline_note=nil;
  7174. if(line_note!=nil)
  7175. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7176. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7177. // NSString *nsname = nil;
  7178. // if(name!=nil)
  7179. // nsname= [[NSString alloc]initWithUTF8String:name];
  7180. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7181. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7182. // NSString *nsdescription=nil;
  7183. // if(description!=nil)
  7184. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7185. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7186. // int stockUom = sqlite3_column_int(stmt, 8);
  7187. // int _id = sqlite3_column_int(stmt, 9);
  7188. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7189. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7190. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7191. int carton=[bsubtotaljson[@"carton"] intValue];
  7192. TotalCuft += cuft;
  7193. TotalWeight += weight;
  7194. TotalCarton += carton;
  7195. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7196. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7197. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7198. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7199. itemjson[@"note"]=nsline_note;
  7200. itemjson[@"origin_price"] = Price;
  7201. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7202. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7203. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7204. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7205. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7206. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7207. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7208. if(itemjson[@"combine"] != nil)
  7209. {
  7210. // int citem=0;
  7211. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7212. for(int bc=0;bc<bcount;bc++)
  7213. {
  7214. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7215. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7216. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7217. subTotal += uprice * modulus * item_count;
  7218. }
  7219. }
  7220. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7221. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7222. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7223. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7224. itemjson[@"type"] = @"order_item";
  7225. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7226. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7227. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7228. allItemPrice += subTotal;
  7229. }];
  7230. section_3[@"data"] = model_data;
  7231. section_3[@"type"] = @"sub_order";
  7232. section_3[@"title"] = @"Models";
  7233. section_3[@"switch"] = @(false);
  7234. ret[@"section_3"] = section_3;
  7235. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7236. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7237. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7238. // payments/Credits
  7239. // payments_and_credist = sqlite3_column_double(statement, 34);
  7240. payments_and_credist = allItemPrice;
  7241. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7242. NSDictionary *sub_total_item = @{
  7243. @"title":@"Sub-Total",
  7244. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7245. };
  7246. [price_data setObject:sub_total_item forKey:@"item_0"];
  7247. // // total
  7248. // totalPrice = sqlite3_column_double(statement, 35);
  7249. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7250. } else {
  7251. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7252. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7253. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7254. // payments/Credits
  7255. payments_and_credist = 0;
  7256. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7257. NSDictionary *sub_total_item = @{
  7258. @"title":@"Sub-Total",
  7259. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7260. };
  7261. [price_data setObject:sub_total_item forKey:@"item_0"];
  7262. }
  7263. // total
  7264. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7265. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7266. if(discount>0)
  7267. {
  7268. NSDictionary *total_item = @{
  7269. @"title":@"Total",
  7270. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist*(1-(discount/100.0))+ lift_gate + shippingFee + handlingFee]
  7271. };
  7272. NSDictionary *discount_item = @{
  7273. @"title":@"Order Discount",
  7274. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7275. };
  7276. [price_data setObject:discount_item forKey:@"item_4"];
  7277. [price_data setObject:total_item forKey:@"item_5"];
  7278. }
  7279. else
  7280. {
  7281. NSDictionary *total_item = @{
  7282. @"title":@"Total",
  7283. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7284. };
  7285. [price_data setObject:total_item forKey:@"item_4"];
  7286. }
  7287. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7288. section_4 = @{
  7289. @"data":price_data,
  7290. @"title":@"Price Info",
  7291. @"type":@"price_info"
  7292. }.mutableCopy;
  7293. ret[@"section_4"] = section_4;
  7294. // ret[@"order_info"]= orderinfo;
  7295. section_0 = @{
  7296. @"data":orderinfo,
  7297. @"title":@"Order Info",
  7298. @"type":@"order_info"
  7299. }.mutableCopy;
  7300. ret[@"section_0"] = section_0;
  7301. // ret[@"more_order_info"] = moreInfo;
  7302. section_2 = @{
  7303. @"data":moreInfo,
  7304. @"title":@"More Info",
  7305. @"type":@"more_order_info"
  7306. }.mutableCopy;
  7307. ret[@"section_2"] = section_2;
  7308. ret[@"count"] = @(5);
  7309. return [RAConvertor dict2data:ret];
  7310. }
  7311. #pragma mark order list
  7312. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7313. NSString *nsstatus = @"";
  7314. switch (status) {
  7315. case 0:
  7316. {
  7317. nsstatus=@"Temp Order";
  7318. break;
  7319. }
  7320. case 1:
  7321. {
  7322. nsstatus=@"Saved Order";
  7323. break;
  7324. }
  7325. case 2: {
  7326. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7327. break;
  7328. }
  7329. case 3:
  7330. case 15:
  7331. {
  7332. nsstatus=@"Cancelled";
  7333. break;
  7334. }
  7335. case 10:
  7336. {
  7337. nsstatus=@"Quote Submitted";
  7338. break;
  7339. }
  7340. case 11:
  7341. {
  7342. nsstatus=@"Sales Order Submitted";
  7343. break;
  7344. }
  7345. case 12:
  7346. {
  7347. nsstatus=@"Processing";
  7348. break;
  7349. }
  7350. case 13:
  7351. {
  7352. nsstatus=@"Shipped";
  7353. break;
  7354. }
  7355. case 14:
  7356. {
  7357. nsstatus=@"Closed";
  7358. break;
  7359. }
  7360. case -11:
  7361. {
  7362. nsstatus = @"Ready For Submit";
  7363. break;
  7364. }
  7365. default:
  7366. break;
  7367. }
  7368. return nsstatus;
  7369. }
  7370. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7371. {
  7372. double total = 0;
  7373. __block double payments_and_credist = 0;
  7374. __block double allItemPrice = 0;
  7375. // sqlite3 *db1 = [iSalesDB get_db];
  7376. if (so_id) {
  7377. // 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];
  7378. 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];
  7379. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7380. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7381. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7382. int product_id = sqlite3_column_int(stmt, 0);
  7383. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7384. int discount = sqlite3_column_int(stmt, 2);
  7385. int item_count = sqlite3_column_int(stmt, 3);
  7386. // int item_id = sqlite3_column_int(stmt, 7);
  7387. int item_id = sqlite3_column_int(stmt, 4);
  7388. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7389. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7390. NSString* Price=nil;
  7391. if(str_price==nil)
  7392. {
  7393. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7394. if(price==nil)
  7395. Price=@"No Price.";
  7396. else
  7397. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7398. }
  7399. else
  7400. {
  7401. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7402. }
  7403. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7404. itemjson[@"The unit price"]=Price;
  7405. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7406. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7407. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7408. if(itemjson[@"combine"] != nil)
  7409. {
  7410. // int citem=0;
  7411. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7412. for(int bc=0;bc<bcount;bc++)
  7413. {
  7414. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7415. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7416. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7417. subTotal += uprice * modulus * item_count;
  7418. }
  7419. }
  7420. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7421. allItemPrice += subTotal;
  7422. }];
  7423. payments_and_credist = allItemPrice;
  7424. } else {
  7425. // payments/Credits
  7426. payments_and_credist = 0;
  7427. }
  7428. // lift_gate handlingFee shippingFee
  7429. __block double lift_gate = 0;
  7430. __block double handlingFee = 0;
  7431. __block double shippingFee = 0;
  7432. __block double order_discount = 0;
  7433. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7434. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7435. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7436. if (have_lift_gate) {
  7437. lift_gate = sqlite3_column_double(stmt, 1);
  7438. }
  7439. handlingFee = sqlite3_column_double(stmt, 2);
  7440. shippingFee = sqlite3_column_double(stmt, 3);
  7441. order_discount = sqlite3_column_double(stmt, 4);
  7442. }];
  7443. if(order_discount>0)
  7444. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7445. // total
  7446. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7447. if (close) {
  7448. [iSalesDB close_db:db1];
  7449. }
  7450. return total;
  7451. }
  7452. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7453. {
  7454. assert(params[@"user"]!=nil);
  7455. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7456. int limit = [[params valueForKey:@"limit"] intValue];
  7457. int offset = [[params valueForKey:@"offset"] intValue];
  7458. NSString* keyword = [params valueForKey:@"keyWord"];
  7459. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7460. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7461. NSString* where=@"1 = 1";
  7462. if(keyword.length>0)
  7463. 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]];
  7464. if (orderStatus.length > 0) {
  7465. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7466. if (order_status_array.count == 1) {
  7467. int status_value = [[order_status_array firstObject] intValue];
  7468. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7469. if (status_value == 15 || status_value == 3) {
  7470. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7471. } else {
  7472. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7473. }
  7474. } else {
  7475. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7476. }
  7477. } else if (order_status_array.count > 1) {
  7478. for (int i = 0; i < order_status_array.count;i++) {
  7479. NSString *status = order_status_array[i];
  7480. NSString *condition = @" or";
  7481. if (i == 0) {
  7482. condition = @" and (";
  7483. }
  7484. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7485. int status_value = [status intValue];
  7486. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7487. if (status_value == 15 || status_value == 3) {
  7488. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7489. } else {
  7490. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7491. }
  7492. } else {
  7493. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7494. }
  7495. }
  7496. where = [where stringByAppendingString:@" )"];
  7497. }
  7498. }
  7499. 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];
  7500. // DebugLog(@"order list sql: %@",sqlQuery);
  7501. sqlite3 *db = [iSalesDB get_db];
  7502. sqlite3_stmt * statement;
  7503. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7504. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7505. {
  7506. int count=0;
  7507. while (sqlite3_step(statement) == SQLITE_ROW)
  7508. {
  7509. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7510. int order_id = sqlite3_column_double(statement, 0);
  7511. char *soid = (char*)sqlite3_column_text(statement, 1);
  7512. if(soid==nil)
  7513. soid= "";
  7514. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7515. int status = sqlite3_column_double(statement, 2);
  7516. int erpStatus = sqlite3_column_double(statement, 9);
  7517. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7518. if(sales_rep==nil)
  7519. sales_rep= "";
  7520. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7521. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7522. if(create_by==nil)
  7523. create_by= "";
  7524. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7525. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7526. if(company_name==nil)
  7527. company_name= "";
  7528. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7529. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7530. if(create_time==nil)
  7531. create_time= "";
  7532. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7533. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7534. // double total_price = sqlite3_column_double(statement, 7);
  7535. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7536. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7537. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7538. int offline_edit = sqlite3_column_int(statement, 10);
  7539. // ": "JH",
  7540. // "": "$8307.00",
  7541. // "": "MOB1608050001",
  7542. // "": "ArpithaT",
  7543. // "": "1st Stage Property Transformations",
  7544. // "": "JANICE SUTTON",
  7545. // "": 2255,
  7546. // "": "08/02/2016 09:49:18",
  7547. // "": 1,
  7548. // "": "Saved Order"
  7549. // "": "1470384050483",
  7550. // "model_count": "6 / 28"
  7551. item[@"sales_rep"]= nssales_rep;
  7552. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7553. item[@"so#"]= nssoid;
  7554. item[@"create_by"]= nscreate_by;
  7555. item[@"customer_name"]= nscompany_name;
  7556. item[@"customer_contact"] = customer_contact;
  7557. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7558. item[@"purchase_time"]= nscreate_time;
  7559. int statusCode = status;
  7560. if (statusCode == 2) {
  7561. statusCode = erpStatus;
  7562. } else if (statusCode == 3) {
  7563. statusCode = 15;
  7564. }
  7565. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7566. item[@"order_status"]= nsstatus;
  7567. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7568. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7569. // item[@"model_count"]
  7570. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7571. count++;
  7572. }
  7573. ret[@"count"]= [NSNumber numberWithInt:count];
  7574. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7575. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7576. ret[@"result"]= [NSNumber numberWithInt:2];
  7577. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7578. ret[@"time_zone"] = @"PST";
  7579. sqlite3_finalize(statement);
  7580. }
  7581. 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];
  7582. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7583. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7584. [iSalesDB close_db:db];
  7585. return [RAConvertor dict2data:ret];
  7586. }
  7587. #pragma mark update gnotes
  7588. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7589. {
  7590. DebugLog(@"params: %@",params);
  7591. // comments = Meyoyoyoyoyoyoy;
  7592. // orderCode = MOB1608110001;
  7593. // password = 123456;
  7594. // user = EvanK;
  7595. 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]];
  7596. int ret = [iSalesDB execSql:sql];
  7597. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7598. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7599. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7600. // [dic setValue:@"160409" forKey:@"min_ver"];
  7601. return [RAConvertor dict2data:dic];
  7602. }
  7603. #pragma mark move to wishlist
  7604. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7605. {
  7606. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7607. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7608. _id = [NSString stringWithFormat:@"(%@)",_id];
  7609. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7610. sqlite3 *db = [iSalesDB get_db];
  7611. __block NSString *product_id = @"";
  7612. __block NSString *item_count_str = @"";
  7613. // __block NSString *item_id = nil;
  7614. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7615. // product_id = [self textAtColumn:0 statement:stmt];
  7616. int item_count = sqlite3_column_int(stmt, 1);
  7617. // item_id = [self textAtColumn:2 statement:stmt];
  7618. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7619. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7620. if (p_id.length) {
  7621. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7622. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7623. }
  7624. }];
  7625. [iSalesDB close_db:db];
  7626. // 去除第一个,
  7627. if (product_id.length > 1) {
  7628. product_id = [product_id substringFromIndex:1];
  7629. }
  7630. if (item_count_str.length > 1) {
  7631. item_count_str = [item_count_str substringFromIndex:1];
  7632. }
  7633. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7634. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7635. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7636. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7637. sqlite3 *db1 = [iSalesDB get_db];
  7638. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7639. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7640. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7641. // 删除
  7642. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7643. int ret = [iSalesDB execSql:deleteSql db:db1];
  7644. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7645. [iSalesDB close_db:db1];
  7646. return [RAConvertor dict2data:dic];
  7647. }
  7648. #pragma mark cart delete
  7649. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7650. {
  7651. // cartItemId = 548;
  7652. // orderCode = MOB1608110001;
  7653. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7654. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7655. _id = [NSString stringWithFormat:@"(%@)",_id];
  7656. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7657. int ret = [iSalesDB execSql:sql];
  7658. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7659. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7660. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7661. // [dic setValue:@"160409" forKey:@"min_ver"];
  7662. return [RAConvertor dict2data:dic];
  7663. }
  7664. #pragma mark set order discount
  7665. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7666. //{
  7667. // DebugLog(@"cart set price params: %@",params);
  7668. // // "cartitem_id" = 1;
  7669. // // discount = "0.000000";
  7670. // // "item_note" = "";
  7671. // // price = "269.000000";
  7672. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7673. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7674. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7675. //// NSString *price = [self valueInParams:params key:@"price"];
  7676. //
  7677. // // bool badd_price_changed=false;
  7678. // // sqlite3* db=[iSalesDB get_db];
  7679. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7680. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7681. // // NSRange range;
  7682. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7683. // //
  7684. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7685. // // badd_price_changed=true;
  7686. // // [iSalesDB close_db:db];
  7687. // //
  7688. // // if(badd_price_changed)
  7689. // // {
  7690. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7691. // // }
  7692. // //
  7693. //
  7694. //
  7695. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7696. //
  7697. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7698. //
  7699. // int ret = [iSalesDB execSql:sql];
  7700. //
  7701. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7702. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7703. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7704. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7705. //
  7706. //// return [RAConvertor dict2data:dic];
  7707. // return dic;
  7708. //
  7709. //}
  7710. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7711. {
  7712. DebugLog(@"order set discount params: %@",params);
  7713. // "cartitem_id" = 1;
  7714. // discount = "0.000000";
  7715. // "item_note" = "";
  7716. // price = "269.000000";
  7717. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7718. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7719. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7720. // NSString *price = [self valueInParams:params key:@"price"];
  7721. // bool badd_price_changed=false;
  7722. // sqlite3* db=[iSalesDB get_db];
  7723. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7724. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7725. // NSRange range;
  7726. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7727. //
  7728. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7729. // badd_price_changed=true;
  7730. // [iSalesDB close_db:db];
  7731. //
  7732. // if(badd_price_changed)
  7733. // {
  7734. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7735. // }
  7736. //
  7737. // sqlite3 *db = [iSalesDB get_db];
  7738. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7739. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7740. //NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f, total_price=%f where so_id = '%@'",discount.doubleValue,total_price*(1-discount.doubleValue/100.0),order_code];
  7741. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7742. int ret = [iSalesDB execSql:sql];
  7743. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7744. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7745. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7746. // [dic setValue:@"160409" forKey:@"min_ver"];
  7747. // return [RAConvertor dict2data:dic];
  7748. return dic;
  7749. }
  7750. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7751. {
  7752. DebugLog(@"cart set price params: %@",params);
  7753. // "cartitem_id" = 1;
  7754. // discount = "0.000000";
  7755. // "item_note" = "";
  7756. // price = "269.000000";
  7757. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7758. NSString *notes = [self valueInParams:params key:@"item_note"];
  7759. NSString *discount = [self valueInParams:params key:@"discount"];
  7760. NSString *price = [self valueInParams:params key:@"price"];
  7761. // bool badd_price_changed=false;
  7762. // sqlite3* db=[iSalesDB get_db];
  7763. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7764. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7765. // NSRange range;
  7766. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7767. //
  7768. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7769. // badd_price_changed=true;
  7770. // [iSalesDB close_db:db];
  7771. //
  7772. // if(badd_price_changed)
  7773. // {
  7774. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7775. // }
  7776. //
  7777. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7778. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7779. int ret = [iSalesDB execSql:sql];
  7780. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7781. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7782. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7783. // [dic setValue:@"160409" forKey:@"min_ver"];
  7784. return [RAConvertor dict2data:dic];
  7785. }
  7786. #pragma mark set line notes
  7787. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7788. {
  7789. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7790. NSString *notes = [self valueInParams:params key:@"notes"];
  7791. notes = [self translateSingleQuote:notes];
  7792. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7793. int ret = [iSalesDB execSql:sql];
  7794. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7795. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7796. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7797. // [dic setValue:@"160409" forKey:@"min_ver"];
  7798. return [RAConvertor dict2data:dic];
  7799. }
  7800. #pragma mark set qty
  7801. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7802. {
  7803. assert(params[@"can_create_backorder"]!=nil);
  7804. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7805. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7806. int item_count = [params[@"inputInt"] intValue];
  7807. // 购买检查数量大于库存
  7808. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7809. if (!params[@"can_create_backorder"]) {
  7810. 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];
  7811. __block BOOL out_of_stock = NO;
  7812. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7813. int availability = sqlite3_column_int(stmt, 0);
  7814. if (availability < item_count) {
  7815. out_of_stock = YES;
  7816. }
  7817. }];
  7818. if (out_of_stock) { // 缺货
  7819. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7820. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7821. return [RAConvertor dict2data:dic];
  7822. }
  7823. }
  7824. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7825. int ret = [iSalesDB execSql:sql];
  7826. __block int item_id = 0;
  7827. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7828. item_id = sqlite3_column_int(stmt, 0);
  7829. }];
  7830. sqlite3 *db = [iSalesDB get_db];
  7831. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7832. [iSalesDB close_db:db];
  7833. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7834. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7835. // [dic setValue:@"160409" forKey:@"min_ver"];
  7836. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7837. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7838. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7839. return [RAConvertor dict2data:dic];
  7840. }
  7841. #pragma mark place order
  7842. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7843. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7844. if (aname) {
  7845. [dic setValue:aname forKey:@"aname"];
  7846. }
  7847. if (control) {
  7848. [dic setValue:control forKey:@"control"];
  7849. }
  7850. if (keyboard) {
  7851. [dic setValue:keyboard forKey:@"keyboard"];
  7852. }
  7853. if (name) {
  7854. [dic setValue:name forKey:@"name"];
  7855. }
  7856. if (value) {
  7857. [dic setValue:value forKey:@"value"];
  7858. }
  7859. return dic;
  7860. }
  7861. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7862. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7863. orderCode = [self translateSingleQuote:orderCode];
  7864. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7865. 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];
  7866. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7867. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7868. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7869. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7870. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7871. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7872. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7873. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7874. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7875. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7876. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7877. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7878. NSString *email = [self textAtColumn:11 statement:stmt];
  7879. NSString *phone = [self textAtColumn:12 statement:stmt];
  7880. NSString *fax = [self textAtColumn:13 statement:stmt];
  7881. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7882. NSString *city = [self textAtColumn:15 statement:stmt];
  7883. NSString *state = [self textAtColumn:16 statement:stmt];
  7884. NSString *country = [self textAtColumn:17 statement:stmt];
  7885. NSString *name = [self textAtColumn:18 statement:stmt];
  7886. // contact id
  7887. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7888. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7889. [contact_id_dic setValue:@"text" forKey:@"control"];
  7890. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7891. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7892. [contact_id_dic setValue:@"true" forKey:@"required"];
  7893. [contact_id_dic setValue:contact_id forKey:@"value"];
  7894. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7895. // business card
  7896. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7897. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7898. [business_card_dic setValue:@"img" forKey:@"control"];
  7899. [business_card_dic setValue:@"1" forKey:@"disable"];
  7900. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7901. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7902. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7903. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7904. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7905. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7906. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7907. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7908. // fax
  7909. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7910. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7911. // zipcode
  7912. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7913. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7914. // city
  7915. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7916. [customer_dic setValue:city_dic forKey:@"item_12"];
  7917. // state
  7918. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7919. [customer_dic setValue:state_dic forKey:@"item_13"];
  7920. // country
  7921. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7922. [customer_dic setValue:country_dic forKey:@"item_14"];
  7923. // company name
  7924. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7925. [customer_dic setValue:company_dic forKey:@"item_2"];
  7926. // addr_1
  7927. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7928. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7929. // addr_2
  7930. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7931. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7932. // addr_3
  7933. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7934. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7935. // addr_4
  7936. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7937. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7938. // Contact
  7939. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7940. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7941. // email
  7942. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7943. [customer_dic setValue:email_dic forKey:@"item_8"];
  7944. // phone
  7945. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7946. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7947. // title
  7948. [customer_dic setValue:@"Customer" forKey:@"title"];
  7949. // count
  7950. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7951. }];
  7952. // setting
  7953. NSDictionary *setting = params[@"setting"];
  7954. NSNumber *hide = setting[@"CustomerHide"];
  7955. [customer_dic setValue:hide forKey:@"hide"];
  7956. return customer_dic;
  7957. }
  7958. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7959. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7960. // setting
  7961. NSDictionary *setting = params[@"setting"];
  7962. NSNumber *hide = setting[@"ShipToHide"];
  7963. [dic setValue:hide forKey:@"hide"];
  7964. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7965. orderCode = [self translateSingleQuote:orderCode];
  7966. 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];
  7967. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7968. NSString *cid = [self textAtColumn:0 statement:stmt];
  7969. NSString *name = [self textAtColumn:1 statement:stmt];
  7970. NSString *ext = [self textAtColumn:2 statement:stmt];
  7971. NSString *contact = [self textAtColumn:3 statement:stmt];
  7972. NSString *email = [self textAtColumn:4 statement:stmt];
  7973. NSString *fax = [self textAtColumn:5 statement:stmt];
  7974. NSString *phone = [self textAtColumn:6 statement:stmt];
  7975. // count
  7976. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7977. // title
  7978. [dic setValue:@"Ship To" forKey:@"title"];
  7979. // choose
  7980. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7981. [choose_dic setValue:@"choose" forKey:@"aname"];
  7982. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7983. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7984. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7985. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7986. @"receive_contact" : @"customer_contact",
  7987. @"receive_email" : @"customer_email",
  7988. @"receive_ext" : @"customer_contact_ext",
  7989. @"receive_fax" : @"customer_fax",
  7990. @"receive_name" : @"customer_name",
  7991. @"receive_phone" : @"customer_phone"},
  7992. @"refresh" : [NSNumber numberWithInteger:1],
  7993. @"type" : @"pull"};
  7994. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7995. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7996. @"name" : @"Add new address",
  7997. @"refresh" : [NSNumber numberWithInteger:1],
  7998. @"value" : @"new_addr"
  7999. };
  8000. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8001. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8002. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8003. @"receive_contact" : @"customer_contact",
  8004. @"receive_email" : @"customer_email",
  8005. @"receive_ext" : @"customer_contact_ext",
  8006. @"receive_fax" : @"customer_fax",
  8007. @"receive_name" : @"customer_name",
  8008. @"receive_phone" : @"customer_phone"},
  8009. @"name" : @"select_ship_to",
  8010. @"refresh" : [NSNumber numberWithInteger:1],
  8011. @"value" : @"Sales_Order_Ship_To"};
  8012. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8013. [dic setValue:choose_dic forKey:@"item_0"];
  8014. // contact id
  8015. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8016. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8017. [contact_id_dic setValue:@"true" forKey:@"required"];
  8018. [dic setValue:contact_id_dic forKey:@"item_1"];
  8019. // company name
  8020. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8021. [dic setValue:company_name_dic forKey:@"item_2"];
  8022. // address
  8023. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8024. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8025. if ([required integerValue]) {
  8026. [ext_dic setValue:@"true" forKey:@"required"];
  8027. }
  8028. [dic setValue:ext_dic forKey:@"item_3"];
  8029. // contact
  8030. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8031. [dic setValue:contact_dic forKey:@"item_4"];
  8032. // phone
  8033. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8034. [dic setValue:phone_dic forKey:@"item_5"];
  8035. // fax
  8036. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8037. [dic setValue:fax_dic forKey:@"item_6"];
  8038. // email
  8039. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8040. [dic setValue:email_dic forKey:@"item_7"];
  8041. }];
  8042. return dic;
  8043. }
  8044. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8045. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8046. // setting
  8047. NSDictionary *setting = params[@"setting"];
  8048. NSNumber *hide = setting[@"ShipFromHide"];
  8049. [dic setValue:hide forKey:@"hide"];
  8050. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8051. orderCode = [self translateSingleQuote:orderCode];
  8052. 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];
  8053. 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';";
  8054. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8055. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8056. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8057. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8058. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8059. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8060. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8061. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8062. if (!cid.length) {
  8063. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8064. cid = [self textAtColumn:0 statement:statment];
  8065. name = [self textAtColumn:1 statement:statment];
  8066. ext = [self textAtColumn:2 statement:statment];
  8067. contact = [self textAtColumn:3 statement:statment];
  8068. email = [self textAtColumn:4 statement:statment];
  8069. fax = [self textAtColumn:5 statement:statment];
  8070. phone = [self textAtColumn:6 statement:statment];
  8071. }];
  8072. }
  8073. // count
  8074. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8075. // title
  8076. [dic setValue:@"Ship From" forKey:@"title"];
  8077. // hide
  8078. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8079. // choose
  8080. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8081. [choose_dic setValue:@"choose" forKey:@"aname"];
  8082. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8083. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8084. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8085. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8086. @"sender_contact" : @"customer_contact",
  8087. @"sender_email" : @"customer_email",
  8088. @"sender_ext" : @"customer_contact_ext",
  8089. @"sender_fax" : @"customer_fax",
  8090. @"sender_name" : @"customer_name",
  8091. @"sender_phone" : @"customer_phone"},
  8092. @"name" : @"select_cid",
  8093. @"refresh" : [NSNumber numberWithInteger:0],
  8094. @"value" : @"Sales_Order_Ship_From"};
  8095. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8096. [dic setValue:choose_dic forKey:@"item_0"];
  8097. // contact id
  8098. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8099. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8100. [contact_id_dic setValue:@"true" forKey:@"required"];
  8101. [dic setValue:contact_id_dic forKey:@"item_1"];
  8102. // company name
  8103. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8104. [dic setValue:company_name_dic forKey:@"item_2"];
  8105. // address
  8106. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8107. [dic setValue:ext_dic forKey:@"item_3"];
  8108. // contact
  8109. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8110. [dic setValue:contact_dic forKey:@"item_4"];
  8111. // phone
  8112. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8113. [dic setValue:phone_dic forKey:@"item_5"];
  8114. // fax
  8115. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8116. [dic setValue:fax_dic forKey:@"item_6"];
  8117. // email
  8118. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8119. [dic setValue:email_dic forKey:@"item_7"];
  8120. }];
  8121. return dic;
  8122. }
  8123. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8124. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8125. // setting
  8126. NSDictionary *setting = params[@"setting"];
  8127. NSNumber *hide = setting[@"FreightBillToHide"];
  8128. [dic setValue:hide forKey:@"hide"];
  8129. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8130. orderCode = [self translateSingleQuote:orderCode];
  8131. 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];
  8132. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8133. NSString *cid = [self textAtColumn:0 statement:stmt];
  8134. NSString *name = [self textAtColumn:1 statement:stmt];
  8135. NSString *ext = [self textAtColumn:2 statement:stmt];
  8136. NSString *contact = [self textAtColumn:3 statement:stmt];
  8137. NSString *email = [self textAtColumn:4 statement:stmt];
  8138. NSString *fax = [self textAtColumn:5 statement:stmt];
  8139. NSString *phone = [self textAtColumn:6 statement:stmt];
  8140. // count
  8141. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8142. // title
  8143. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8144. // hide
  8145. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8146. // choose
  8147. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8148. [choose_dic setValue:@"choose" forKey:@"aname"];
  8149. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8150. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8151. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8152. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8153. @"shipping_billto_contact" : @"receive_contact",
  8154. @"shipping_billto_email" : @"receive_email",
  8155. @"shipping_billto_ext" : @"receive_ext",
  8156. @"shipping_billto_fax" : @"receive_fax",
  8157. @"shipping_billto_name" : @"receive_name",
  8158. @"shipping_billto_phone" : @"receive_phone"},
  8159. @"type" : @"pull"};
  8160. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8161. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8162. @"type" : @"pull",
  8163. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8164. @"shipping_billto_contact" : @"customer_contact",
  8165. @"shipping_billto_email" : @"customer_email",
  8166. @"shipping_billto_ext" : @"customer_contact_ext",
  8167. @"shipping_billto_fax" : @"customer_fax",
  8168. @"shipping_billto_name" : @"customer_name",
  8169. @"shipping_billto_phone" : @"customer_phone"}
  8170. };
  8171. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8172. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8173. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8174. @"shipping_billto_contact" : @"sender_contact",
  8175. @"shipping_billto_email" : @"sender_email",
  8176. @"shipping_billto_ext" : @"sender_ext",
  8177. @"shipping_billto_fax" : @"sender_fax",
  8178. @"shipping_billto_name" : @"sender_name",
  8179. @"shipping_billto_phone" : @"sender_phone"},
  8180. @"type" : @"pull"
  8181. };
  8182. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8183. NSDictionary *select_freight_bill_to_dic = @{
  8184. @"aname" : @"Select freight bill to",
  8185. @"name" : @"select_cid",
  8186. @"refresh" : [NSNumber numberWithInteger:0],
  8187. @"value" : @"Sales_Order_Freight_Bill_To",
  8188. @"key_map" : @{
  8189. @"shipping_billto_cid" : @"customer_cid",
  8190. @"shipping_billto_contact" : @"customer_contact",
  8191. @"shipping_billto_email" : @"customer_email",
  8192. @"shipping_billto_ext" : @"customer_contact_ext",
  8193. @"shipping_billto_fax" : @"customer_fax",
  8194. @"shipping_billto_name" : @"customer_name",
  8195. @"shipping_billto_phone" : @"customer_phone"
  8196. }
  8197. };
  8198. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8199. [dic setValue:choose_dic forKey:@"item_0"];
  8200. // contact id
  8201. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8202. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8203. [contact_id_dic setValue:@"true" forKey:@"required"];
  8204. [dic setValue:contact_id_dic forKey:@"item_1"];
  8205. // company name
  8206. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8207. [dic setValue:company_name_dic forKey:@"item_2"];
  8208. // address
  8209. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8210. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8211. if ([ext_required integerValue]) {
  8212. [ext_dic setValue:@"true" forKey:@"required"];
  8213. }
  8214. [dic setValue:ext_dic forKey:@"item_3"];
  8215. // contact
  8216. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8217. [dic setValue:contact_dic forKey:@"item_4"];
  8218. // phone
  8219. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8220. [dic setValue:phone_dic forKey:@"item_5"];
  8221. // fax
  8222. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8223. [dic setValue:fax_dic forKey:@"item_6"];
  8224. // email
  8225. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8226. [dic setValue:email_dic forKey:@"item_7"];
  8227. }];
  8228. return dic;
  8229. }
  8230. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8231. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8232. // setting
  8233. NSDictionary *setting = params[@"setting"];
  8234. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8235. [dic setValue:hide forKey:@"hide"];
  8236. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8237. orderCode = [self translateSingleQuote:orderCode];
  8238. 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];
  8239. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8240. NSString *cid = [self textAtColumn:0 statement:stmt];
  8241. NSString *name = [self textAtColumn:1 statement:stmt];
  8242. NSString *ext = [self textAtColumn:2 statement:stmt];
  8243. NSString *contact = [self textAtColumn:3 statement:stmt];
  8244. NSString *email = [self textAtColumn:4 statement:stmt];
  8245. NSString *fax = [self textAtColumn:5 statement:stmt];
  8246. NSString *phone = [self textAtColumn:6 statement:stmt];
  8247. // count
  8248. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8249. // title
  8250. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8251. // hide
  8252. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8253. // choose
  8254. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8255. [choose_dic setValue:@"choose" forKey:@"aname"];
  8256. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8257. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8258. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8259. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8260. @"billing_contact" : @"receive_contact",
  8261. @"billing_email" : @"receive_email",
  8262. @"billing_ext" : @"receive_ext",
  8263. @"billing_fax" : @"receive_fax",
  8264. @"billing_name" : @"receive_name",
  8265. @"billing_phone" : @"receive_phone"},
  8266. @"type" : @"pull"};
  8267. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8268. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8269. @"type" : @"pull",
  8270. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8271. @"billing_contact" : @"customer_contact",
  8272. @"billing_email" : @"customer_email",
  8273. @"billing_ext" : @"customer_contact_ext",
  8274. @"billing_fax" : @"customer_fax",
  8275. @"billing_name" : @"customer_name",
  8276. @"billing_phone" : @"customer_phone"}
  8277. };
  8278. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8279. NSDictionary *select_bill_to_dic = @{
  8280. @"aname" : @"Select bill to",
  8281. @"name" : @"select_cid",
  8282. @"refresh" : [NSNumber numberWithInteger:0],
  8283. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8284. @"key_map" : @{
  8285. @"billing_cid" : @"customer_cid",
  8286. @"billing_contact" : @"customer_contact",
  8287. @"billing_email" : @"customer_email",
  8288. @"billing_ext" : @"customer_contact_ext",
  8289. @"billing_fax" : @"customer_fax",
  8290. @"billing_name" : @"customer_name",
  8291. @"billing_phone" : @"customer_phone"
  8292. }
  8293. };
  8294. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8295. [dic setValue:choose_dic forKey:@"item_0"];
  8296. // contact id
  8297. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8298. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8299. [contact_id_dic setValue:@"true" forKey:@"required"];
  8300. [dic setValue:contact_id_dic forKey:@"item_1"];
  8301. // company name
  8302. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8303. [dic setValue:company_name_dic forKey:@"item_2"];
  8304. // address
  8305. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8306. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8307. if ([ext_required integerValue]) {
  8308. [ext_dic setValue:@"true" forKey:@"required"];
  8309. }
  8310. [dic setValue:ext_dic forKey:@"item_3"];
  8311. // contact
  8312. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8313. [dic setValue:contact_dic forKey:@"item_4"];
  8314. // phone
  8315. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8316. [dic setValue:phone_dic forKey:@"item_5"];
  8317. // fax
  8318. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8319. [dic setValue:fax_dic forKey:@"item_6"];
  8320. // email
  8321. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8322. [dic setValue:email_dic forKey:@"item_7"];
  8323. }];
  8324. return dic;
  8325. }
  8326. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8327. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8328. // setting
  8329. NSDictionary *setting = params[@"setting"];
  8330. NSNumber *hide = setting[@"ReturnToHide"];
  8331. [dic setValue:hide forKey:@"hide"];
  8332. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8333. orderCode = [self translateSingleQuote:orderCode];
  8334. 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];
  8335. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8336. NSString *cid = [self textAtColumn:0 statement:stmt];
  8337. NSString *name = [self textAtColumn:1 statement:stmt];
  8338. NSString *ext = [self textAtColumn:2 statement:stmt];
  8339. NSString *contact = [self textAtColumn:3 statement:stmt];
  8340. NSString *email = [self textAtColumn:4 statement:stmt];
  8341. NSString *fax = [self textAtColumn:5 statement:stmt];
  8342. NSString *phone = [self textAtColumn:6 statement:stmt];
  8343. // count
  8344. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8345. // title
  8346. [dic setValue:@"Return To" forKey:@"title"];
  8347. // hide
  8348. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8349. // choose
  8350. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8351. [choose_dic setValue:@"choose" forKey:@"aname"];
  8352. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8353. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8354. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8355. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8356. @"returnto_contact" : @"sender_contact",
  8357. @"returnto_email" : @"sender_email",
  8358. @"returnto_ext" : @"sender_ext",
  8359. @"returnto_fax" : @"sender_fax",
  8360. @"returnto_name" : @"sender_name",
  8361. @"returnto_phone" : @"sender_phone"},
  8362. @"type" : @"pull"};
  8363. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8364. NSDictionary *select_return_to_dic = @{
  8365. @"aname" : @"Select return to",
  8366. @"name" : @"select_cid",
  8367. @"refresh" : [NSNumber numberWithInteger:0],
  8368. @"value" : @"Contact_Return_To",
  8369. @"key_map" : @{
  8370. @"returnto_cid" : @"customer_cid",
  8371. @"returnto_contact" : @"customer_contact",
  8372. @"returnto_email" : @"customer_email",
  8373. @"returnto_ext" : @"customer_contact_ext",
  8374. @"returnto_fax" : @"customer_fax",
  8375. @"returnto_name" : @"customer_name",
  8376. @"returnto_phone" : @"customer_phone"
  8377. }
  8378. };
  8379. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8380. [dic setValue:choose_dic forKey:@"item_0"];
  8381. // contact id
  8382. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8383. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8384. [contact_id_dic setValue:@"true" forKey:@"required"];
  8385. [dic setValue:contact_id_dic forKey:@"item_1"];
  8386. // company name
  8387. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8388. [dic setValue:company_name_dic forKey:@"item_2"];
  8389. // address
  8390. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8391. [dic setValue:ext_dic forKey:@"item_3"];
  8392. // contact
  8393. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8394. [dic setValue:contact_dic forKey:@"item_4"];
  8395. // phone
  8396. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8397. [dic setValue:phone_dic forKey:@"item_5"];
  8398. // fax
  8399. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8400. [dic setValue:fax_dic forKey:@"item_6"];
  8401. // email
  8402. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8403. [dic setValue:email_dic forKey:@"item_7"];
  8404. }];
  8405. return dic;
  8406. }
  8407. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8408. {
  8409. assert(params[@"user"]!=nil);
  8410. assert(params[@"contact_id"]!=nil);
  8411. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8412. orderCode = [self translateSingleQuote:orderCode];
  8413. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8414. __block double TotalCuft = 0;
  8415. __block double TotalWeight = 0;
  8416. __block int TotalCarton = 0;
  8417. __block double payments = 0;
  8418. // setting
  8419. NSDictionary *setting = params[@"setting"];
  8420. NSNumber *hide = setting[@"ModelInformationHide"];
  8421. [dic setValue:hide forKey:@"hide"];
  8422. 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];
  8423. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8424. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8425. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8426. // item id
  8427. int item_id = sqlite3_column_int(stmt, 0);
  8428. // count
  8429. int item_count = sqlite3_column_int(stmt, 1);
  8430. // stockUom
  8431. int stockUom = sqlite3_column_int(stmt, 2);
  8432. // unit price
  8433. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8434. NSString* Price=nil;
  8435. if([str_price isEqualToString:@""])
  8436. {
  8437. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8438. if(price==nil)
  8439. Price=@"No Price.";
  8440. else
  8441. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8442. }
  8443. else
  8444. {
  8445. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8446. }
  8447. // discount
  8448. double discount = sqlite3_column_double(stmt, 4);
  8449. // name
  8450. NSString *name = [self textAtColumn:5 statement:stmt];
  8451. // description
  8452. NSString *description = [self textAtColumn:6 statement:stmt];
  8453. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8454. // line note
  8455. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8456. int avaulability = sqlite3_column_int(stmt, 8);
  8457. // img
  8458. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8459. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8460. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8461. if(combine != nil)
  8462. {
  8463. // int citem=0;
  8464. int bcount=[[combine valueForKey:@"count"] intValue];
  8465. for(int bc=0;bc<bcount;bc++)
  8466. {
  8467. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8468. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8469. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8470. subTotal += uprice * modulus * item_count;
  8471. }
  8472. }
  8473. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8474. [model_dic setValue:@"model" forKey:@"control"];
  8475. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8476. [model_dic setValue:description forKey:@"description"];
  8477. [model_dic setValue:line_note forKey:@"note"];
  8478. [model_dic setValue:img forKey:@"img_url"];
  8479. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8480. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8481. [model_dic setValue:Price forKey:@"unit_price"];
  8482. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8483. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8484. if (combine) {
  8485. [model_dic setValue:combine forKey:@"combine"];
  8486. }
  8487. // well,what under the row is the info for total
  8488. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8489. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8490. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8491. int carton=[bsubtotaljson[@"carton"] intValue];
  8492. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8493. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8494. TotalCuft += cuft;
  8495. TotalWeight += weight;
  8496. TotalCarton += carton;
  8497. payments += subTotal;
  8498. //---------------------------
  8499. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8500. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8501. }];
  8502. [dic setValue:@"Model Information" forKey:@"title"];
  8503. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8504. return dic;
  8505. }
  8506. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8507. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8508. [dic setValue:@"Remarks Content" forKey:@"title"];
  8509. // setting
  8510. NSDictionary *setting = params[@"setting"];
  8511. NSNumber *hide = setting[@"RemarksContentHide"];
  8512. [dic setValue:hide forKey:@"hide"];
  8513. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8514. orderCode = [self translateSingleQuote:orderCode];
  8515. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8516. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8517. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8518. int mustCall = sqlite3_column_int(stmt, 1);
  8519. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8520. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8521. #ifdef BUILD_CONTRAST
  8522. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8523. #endif
  8524. NSDictionary *po_dic = @{
  8525. @"aname" : @"PO#",
  8526. @"control" : @"edit",
  8527. @"keyboard" : @"text",
  8528. @"name" : @"poNumber",
  8529. @"value" : poNumber
  8530. };
  8531. #ifdef BUILD_CONTRAST
  8532. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8533. NSDictionary *schedule_dict = @{
  8534. @"aname" : @"Schdule Date",
  8535. @"control" : @"datepicker",
  8536. @"type": @"date",
  8537. @"required": @"true",
  8538. @"name" : @"schedule_date_str",
  8539. @"value" : nsdate
  8540. };
  8541. #endif
  8542. NSDictionary *must_call_dic = @{
  8543. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8544. @"control" : @"switch",
  8545. @"name" : @"must_call",
  8546. @"value" : mustCall ? @"true" : @"false"
  8547. };
  8548. NSDictionary *general_notes_dic = @{
  8549. @"aname" : @"General notes",
  8550. @"control" : @"text_view",
  8551. @"keyboard" : @"text",
  8552. @"name" : @"comments",
  8553. @"value" : generalNotes
  8554. };
  8555. // NSDictionary *internal_notes_dic = @{
  8556. // @"aname" : @"Internal notes",
  8557. // @"control" : @"text_view",
  8558. // @"keyboard" : @"text",
  8559. // @"name" : @"internal_notes",
  8560. // @"value" : internalNotes
  8561. // };
  8562. #ifdef BUILD_CONTRAST
  8563. [dic setValue:po_dic forKey:@"item_0"];
  8564. [dic setValue:schedule_dict forKey:@"item_1"];
  8565. [dic setValue:must_call_dic forKey:@"item_2"];
  8566. [dic setValue:general_notes_dic forKey:@"item_3"];
  8567. #else
  8568. [dic setValue:po_dic forKey:@"item_0"];
  8569. [dic setValue:must_call_dic forKey:@"item_1"];
  8570. [dic setValue:general_notes_dic forKey:@"item_2"];
  8571. #endif
  8572. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8573. }];
  8574. #ifdef BUILD_CONTRAST
  8575. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8576. #else
  8577. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8578. #endif
  8579. return dic;
  8580. }
  8581. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8582. // params
  8583. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8584. orderCode = [self translateSingleQuote:orderCode];
  8585. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8586. // setting
  8587. NSDictionary *setting = params[@"setting"];
  8588. NSNumber *hide = setting[@"OrderTotalHide"];
  8589. [dic setValue:hide forKey:@"hide"];
  8590. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8591. __block double lift_gate_value = 0;
  8592. __block double handling_fee = 0;
  8593. __block double shipping = 0;
  8594. __block double discount = 0;
  8595. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8596. int lift_gate = sqlite3_column_int(stmt, 0);
  8597. lift_gate_value = sqlite3_column_double(stmt, 1);
  8598. shipping = sqlite3_column_double(stmt, 2);
  8599. handling_fee = sqlite3_column_double(stmt, 3);
  8600. discount = sqlite3_column_double(stmt, 4);
  8601. if (!lift_gate) {
  8602. lift_gate_value = 0;
  8603. }
  8604. }];
  8605. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8606. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8607. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8608. double payments = [[total valueForKey:@"payments"] doubleValue];
  8609. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8610. double totalPrice = payments* (1-discount/100.0);
  8611. [dic setValue:@"Order Total" forKey:@"title"];
  8612. NSDictionary *payments_dic = @{
  8613. @"align" : @"right",
  8614. @"aname" : @"Payments/Credits",
  8615. @"control" : @"text",
  8616. @"name" : @"paymentsAndCredits",
  8617. @"type" : @"price",
  8618. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8619. };
  8620. [dic setValue:payments_dic forKey:@"item_0"];
  8621. // version 1.71 remove
  8622. // NSDictionary *handling_fee_dic = @{
  8623. // @"align" : @"right",
  8624. // @"aname" : @"Handling Fee",
  8625. // @"control" : @"text",
  8626. // @"name" : @"handling_fee_value",
  8627. // @"required" : @"true",
  8628. // @"type" : @"price",
  8629. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8630. // };
  8631. NSDictionary *shipping_dic = @{
  8632. @"align" : @"right",
  8633. @"aname" : @"Shipping*",
  8634. @"control" : @"text",
  8635. @"name" : @"shipping",
  8636. @"required" : @"true",
  8637. @"type" : @"price",
  8638. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8639. };
  8640. NSDictionary *lift_gate_dic = @{
  8641. @"align" : @"right",
  8642. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8643. @"control" : @"text",
  8644. @"name" : @"lift_gate_value",
  8645. @"required" : @"true",
  8646. @"type" : @"price",
  8647. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8648. };
  8649. int item_count = 1;
  8650. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8651. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8652. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8653. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8654. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8655. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8656. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8657. } else {
  8658. }
  8659. }
  8660. // version 1.71 remove
  8661. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8662. // NSString* strdiscount;
  8663. if(discount>0)
  8664. {
  8665. NSDictionary *discount_dict = @{
  8666. @"align" : @"right",
  8667. @"aname" : @"Order Discount",
  8668. @"control" : @"text",
  8669. @"name" : @"orderDiscount",
  8670. // @"type" : @"price",
  8671. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8672. };
  8673. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8674. }
  8675. NSDictionary *total_price_dic = @{
  8676. @"align" : @"right",
  8677. @"aname" : @"Total",
  8678. @"control" : @"text",
  8679. @"name" : @"totalPrice",
  8680. @"type" : @"price",
  8681. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8682. };
  8683. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8684. NSDictionary *total_cuft_dic = @{
  8685. @"align" : @"right",
  8686. @"aname" : @"Total Cuft",
  8687. @"control" : @"text",
  8688. @"name" : @"",
  8689. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8690. };
  8691. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8692. NSDictionary *total_weight_dic = @{
  8693. @"align" : @"right",
  8694. @"aname" : @"Total Weight",
  8695. @"control" : @"text",
  8696. @"name" : @"",
  8697. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8698. };
  8699. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8700. NSDictionary *total_carton_dic = @{
  8701. @"align" : @"right",
  8702. @"aname" : @"Total Carton",
  8703. @"control" : @"text",
  8704. @"name" : @"",
  8705. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8706. };
  8707. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8708. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8709. return dic;
  8710. }
  8711. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8712. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8713. orderCode = [self translateSingleQuote:orderCode];
  8714. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8715. // setting
  8716. NSDictionary *setting = params[@"setting"];
  8717. NSNumber *hide = setting[@"SignatureHide"];
  8718. [dic setValue:hide forKey:@"hide"];
  8719. [dic setValue:@"Signature" forKey:@"title"];
  8720. __block NSString *pic_path = @"";
  8721. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8722. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8723. pic_path = [self textAtColumn:0 statement:stmt];
  8724. }];
  8725. NSDictionary *pic_dic = @{
  8726. @"aname" : @"Signature",
  8727. @"avalue" :pic_path,
  8728. @"control" : @"signature",
  8729. @"name" : @"sign_picpath",
  8730. @"value" : pic_path
  8731. };
  8732. [dic setValue:pic_dic forKey:@"item_0"];
  8733. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8734. return dic;
  8735. }
  8736. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8738. // setting
  8739. NSDictionary *setting = params[@"setting"];
  8740. NSNumber *hide = setting[@"ShippingMethodHide"];
  8741. [dic setValue:hide forKey:@"hide"];
  8742. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8743. orderCode = [self translateSingleQuote:orderCode];
  8744. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8745. __block NSString *logist = @"";
  8746. __block NSString *carrier = @"";
  8747. __block NSString *lift_gate = @"";
  8748. __block int lift_gate_integer = 0;
  8749. __block NSString *logistic_note = @"";
  8750. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8751. logist = [self textAtColumn:0 statement:stmt];
  8752. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8753. logistic_note = [self textAtColumn:2 statement:stmt];
  8754. carrier = [self textAtColumn:3 statement:stmt];
  8755. }];
  8756. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8757. [dic setValue:@"Shipping Method" forKey:@"title"];
  8758. // val_0
  8759. int PERSONAL_PICK_UP_check = 0;
  8760. int USE_MY_CARRIER_check = 0;
  8761. NSString *logistic_note_text = @"";
  8762. int will_call_check = 0;
  8763. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8764. will_call_check = 1;
  8765. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8766. PERSONAL_PICK_UP_check = 1;
  8767. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8768. USE_MY_CARRIER_check = 1;
  8769. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8770. logistic_note = [logistic_note_array firstObject];
  8771. if (logistic_note_array.count > 1) {
  8772. logistic_note_text = [logistic_note_array lastObject];
  8773. }
  8774. }
  8775. }
  8776. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8777. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8778. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8779. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8780. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8781. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8782. NSDictionary *val0_subItem_item0 = @{
  8783. @"aname" : @"Option",
  8784. @"cadedate" : @{
  8785. @"count" : [NSNumber numberWithInteger:2],
  8786. @"val_0" : @{
  8787. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8788. @"refresh" : [NSNumber numberWithInteger:0],
  8789. @"value" : @"PERSONAL PICK UP",
  8790. @"value_id" : @"PERSONAL PICK UP"
  8791. },
  8792. @"val_1" : @{
  8793. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8794. @"refresh" : [NSNumber numberWithInteger:0],
  8795. @"sub_item" : @{
  8796. @"count" : [NSNumber numberWithInteger:1],
  8797. @"item_0" : @{
  8798. @"aname" : @"BOL",
  8799. @"control" : @"edit",
  8800. @"keyboard" : @"text",
  8801. @"name" : @"logist_note_text",
  8802. @"refresh" : [NSNumber numberWithInteger:0],
  8803. @"required" : @"false",
  8804. @"value" : logistic_note_text
  8805. }
  8806. },
  8807. @"value" : @"USE MY CARRIER",
  8808. @"value_id" : @"USE MY CARRIER"
  8809. }
  8810. },
  8811. @"control" : @"enum",
  8812. @"name" : @"logistic_note",
  8813. @"required" : @"true",
  8814. @"single_select" : @"true"
  8815. };
  8816. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8817. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8818. // val_1
  8819. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8820. if([lift_gate isEqualToString:@""]) {
  8821. if (lift_gate_integer == 1) {
  8822. lift_gate = @"true";
  8823. } else {
  8824. lift_gate = @"false";
  8825. }
  8826. }
  8827. // carrier = [self valueInParams:params key:@"carrier"];
  8828. // if([carrier isEqualToString:@""]) {
  8829. //
  8830. // if (lift_gate_integer == 1) {
  8831. // lift_gate = @"true";
  8832. // } else {
  8833. // lift_gate = @"false";
  8834. // }
  8835. //
  8836. // }
  8837. int common_carrier_check = 0;
  8838. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8839. common_carrier_check = 1;
  8840. [params setValue:lift_gate forKey:@"lift_gate"];
  8841. #ifdef BUILD_CONTRAST
  8842. [params setValue:carrier forKey:@"carrier"];
  8843. #endif
  8844. }
  8845. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8846. // NSString * code = [iSalesDB jk_queryText:Sql];
  8847. // [self offline_getCarrierCode:offline_getCarrierCode];
  8848. #ifdef BUILD_CONTRAST
  8849. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8850. #endif
  8851. NSDictionary *val_1 = @{
  8852. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8853. @"sub_item" : @{
  8854. @"item_0" : @{
  8855. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8856. @"control" : @"switch",
  8857. @"name" : @"lift_gate",
  8858. @"refresh" : [NSNumber numberWithInteger:1],
  8859. @"required" : @"true",
  8860. @"value" : lift_gate
  8861. },
  8862. #ifdef BUILD_CONTRAST
  8863. @"count" : [NSNumber numberWithInteger:2],
  8864. @"item_1" : @{
  8865. @"aname" : @"Carrier",
  8866. @"cadedate" :carrierDic,
  8867. @"control" : @"enum",
  8868. @"name" : @"carrier",
  8869. @"required" : @"false",
  8870. @"value" : @"",
  8871. @"single_select" : @"true"
  8872. }
  8873. #else
  8874. @"count" : [NSNumber numberWithInteger:1]
  8875. #endif
  8876. },
  8877. @"value" : @"COMMON CARRIER",
  8878. @"value_id" : @"COMMON CARRIER"
  8879. };
  8880. // cadedate
  8881. NSDictionary *cadedate = @{
  8882. @"count" : [NSNumber numberWithInteger:2],
  8883. @"val_0" : val_0,
  8884. @"val_1" : val_1
  8885. };
  8886. // item_0
  8887. NSMutableDictionary *item_0 = @{
  8888. @"aname" : @"Shipping",
  8889. @"cadedate" : cadedate,
  8890. @"control" : @"enum",
  8891. @"name" : @"logist",
  8892. @"refresh" : [NSNumber numberWithInteger:1],
  8893. @"single_select" : @"true",
  8894. }.mutableCopy;
  8895. NSNumber *required = setting[@"ShippingMethodRequire"];
  8896. if ([required integerValue]) {
  8897. [item_0 setValue:@"true" forKey:@"required"];
  8898. }
  8899. [dic setValue:item_0 forKey:@"item_0"];
  8900. if ([logist isEqualToString:@"WILL CALL"]) {
  8901. [dic removeObjectForKey:@"lift_gate"];
  8902. }
  8903. return dic;
  8904. }
  8905. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8906. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8907. orderCode = [self translateSingleQuote:orderCode];
  8908. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8909. __block int submit_as_integer = 0;
  8910. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8911. submit_as_integer = sqlite3_column_int(stmt, 0);
  8912. }];
  8913. int check11 = 0;
  8914. int check10 = 0;
  8915. if (submit_as_integer == 11) {
  8916. check11 = 1;
  8917. }
  8918. if (submit_as_integer == 10) {
  8919. check10 = 1;
  8920. }
  8921. // section_0
  8922. NSMutableDictionary *dic = @{
  8923. @"count" : @(1),
  8924. @"item_0" : @{
  8925. @"aname" : @"Submit Order As",
  8926. @"cadedate" : @{
  8927. @"count" : @(2),
  8928. @"val_0" : @{
  8929. @"check" : [NSNumber numberWithInteger:check11],
  8930. @"value" : @"Sales Order",
  8931. @"value_id" : @(11)
  8932. },
  8933. @"val_1" : @{
  8934. @"check" : [NSNumber numberWithInteger:check10],
  8935. @"value" : @"Quote",
  8936. @"value_id" : @(10)
  8937. }
  8938. },
  8939. @"control" : @"enum",
  8940. @"name" : @"erpOrderStatus",
  8941. @"required" : @"true",
  8942. @"single_select" : @"true"
  8943. },
  8944. @"title" : @"Order Type"
  8945. }.mutableCopy;
  8946. // setting
  8947. NSDictionary *setting = params[@"setting"];
  8948. NSNumber *hide = setting[@"OrderTypeHide"];
  8949. [dic setValue:hide forKey:@"hide"];
  8950. return dic;
  8951. }
  8952. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8953. countryCode = [self translateSingleQuote:countryCode];
  8954. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8955. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8956. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8957. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8958. if (name == NULL) {
  8959. name = "";
  8960. }
  8961. if (code == NULL) {
  8962. code = "";
  8963. }
  8964. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8965. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8966. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8967. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8968. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8969. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8970. }
  8971. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8972. [container setValue:stateDic forKey:key];
  8973. }] mutableCopy];
  8974. [ret removeObjectForKey:@"result"];
  8975. // failure 可以不用了,一样的
  8976. if (ret.allKeys.count == 0) {
  8977. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8978. [stateDic setValue:@"Other" forKey:@"value"];
  8979. [stateDic setValue:@"" forKey:@"value_id"];
  8980. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8981. if (state_code && [@"" isEqualToString:state_code]) {
  8982. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8983. }
  8984. NSString *key = [NSString stringWithFormat:@"val_0"];
  8985. [ret setValue:stateDic forKey:key];
  8986. }
  8987. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8988. return ret;
  8989. }
  8990. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8991. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8992. orderCode = [self translateSingleQuote:orderCode];
  8993. 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];
  8994. __block NSString *payType = @"";
  8995. __block NSString *firstName = @"";
  8996. __block NSString *lastName = @"";
  8997. __block NSString *addr1 = @"";
  8998. __block NSString *addr2 = @"";
  8999. __block NSString *zipcode = @"";
  9000. __block NSString *cardType = @"";
  9001. __block NSString *cardNumber = @"";
  9002. __block NSString *securityCode = @"";
  9003. __block NSString *month = @"";
  9004. __block NSString *year = @"";
  9005. __block NSString *city = @"";
  9006. __block NSString *state = @"";
  9007. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9008. payType = [self textAtColumn:0 statement:stmt];
  9009. firstName = [self textAtColumn:1 statement:stmt];
  9010. lastName = [self textAtColumn:2 statement:stmt];
  9011. addr1 = [self textAtColumn:3 statement:stmt];
  9012. addr2 = [self textAtColumn:4 statement:stmt];
  9013. zipcode = [self textAtColumn:5 statement:stmt];
  9014. cardType = [self textAtColumn:6 statement:stmt];
  9015. cardNumber = [self textAtColumn:7 statement:stmt];
  9016. securityCode = [self textAtColumn:8 statement:stmt];
  9017. month = [self textAtColumn:9 statement:stmt];
  9018. year = [self textAtColumn:10 statement:stmt];
  9019. city = [self textAtColumn:11 statement:stmt];
  9020. state = [self textAtColumn:12 statement:stmt];
  9021. }];
  9022. NSString *required = @"true";
  9023. // setting
  9024. NSDictionary *setting = params[@"setting"];
  9025. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9026. if ([requiredNumber integerValue]) {
  9027. required = @"true";
  9028. } else {
  9029. required = @"false";
  9030. }
  9031. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9032. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9033. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9034. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9035. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9036. NSString *type = [self textAtColumn:1 statement:stmt];
  9037. NSMutableDictionary *val = @{
  9038. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9039. @"value" : type,
  9040. @"value_id" : type_id
  9041. }.mutableCopy;
  9042. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9043. NSDictionary *sub_item =nil;
  9044. #ifdef BUILD_CONTRAST
  9045. sub_item = @{
  9046. @"count" : @(3),
  9047. @"item_0" : @{
  9048. @"aname" : @"Number",
  9049. @"control" : @"edit",
  9050. @"keyboard" : @"int",
  9051. @"length" : @"16",
  9052. @"name" : @"credit_card_number",
  9053. @"required" : @"false",
  9054. @"value" : cardNumber
  9055. },
  9056. @"item_1" : @{
  9057. @"aname" : @"Expiration Date",
  9058. @"control" : @"monthpicker",
  9059. @"name" : @"credit_card_expiration",
  9060. @"required" : @"false",
  9061. @"type" : @"date",
  9062. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9063. },
  9064. @"item_2" : @{
  9065. @"aname" : @"Security Code",
  9066. @"control" : @"edit",
  9067. @"keyboard" : @"int",
  9068. @"length" : @"4",
  9069. @"name" : @"credit_card_security_code",
  9070. @"required" : @"false",
  9071. @"value" : securityCode
  9072. },
  9073. };
  9074. #else
  9075. sub_item= @{
  9076. @"count" : @(3),
  9077. @"item_0" : @{
  9078. @"aname" : @"choose",
  9079. @"control" : @"multi_action",
  9080. @"count" : @(1),
  9081. @"item_0" : @{
  9082. @"aname" : @"Same as customer",
  9083. @"key_map" : @{
  9084. @"credit_card_address1" : @"customer_address1",
  9085. @"credit_card_address2" : @"customer_address2",
  9086. @"credit_card_city" : @"customer_city",
  9087. @"credit_card_first_name" : @"customer_first_name",
  9088. @"credit_card_last_name" : @"customer_last_name",
  9089. @"credit_card_state" : @"customer_state",
  9090. @"credit_card_zipcode" : @"customer_zipcode"
  9091. },
  9092. @"type" : @"pull"
  9093. }
  9094. },
  9095. @"item_1" : @{
  9096. @"aname" : @"",
  9097. @"color" : @"red",
  9098. @"control" : @"text",
  9099. @"name" : @"",
  9100. @"value" : @"USA Credit cards only"
  9101. },
  9102. @"item_2" : @{
  9103. @"aname" : @"Fill",
  9104. @"cadedate" : @{
  9105. @"count" : @(2),
  9106. @"val_0" : @{
  9107. @"check" : @(1),
  9108. @"sub_item" : @{
  9109. @"count" : @(11),
  9110. @"item_0" : @{
  9111. @"aname" : @"Type",
  9112. @"cadedate" : @{
  9113. @"count" : @(2),
  9114. @"val_0" : @{
  9115. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9116. @"value" : @"VISA",
  9117. @"value_id" : @"VISA"/*@(0)*/
  9118. },
  9119. @"val_1" : @{
  9120. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9121. @"value" : @"MASTER CARD",
  9122. @"value_id" : @"MASTER CARD"/*@(1)*/
  9123. }
  9124. },
  9125. @"control" : @"enum",
  9126. @"name" : @"credit_card_type",
  9127. @"required" : @"true",
  9128. @"single_select" : @"true"
  9129. },
  9130. @"item_1" : @{
  9131. @"aname" : @"Number",
  9132. @"control" : @"edit",
  9133. @"keyboard" : @"int",
  9134. @"length" : @"16",
  9135. @"name" : @"credit_card_number",
  9136. @"required" : @"true",
  9137. @"value" : cardNumber
  9138. },
  9139. @"item_10" : @{
  9140. @"aname" : @"State",
  9141. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9142. @"control" : @"enum",
  9143. @"enum" : @"true",
  9144. @"name" : @"credit_card_state",
  9145. @"required" : @"true",
  9146. @"single_select" : @"true"
  9147. },
  9148. @"item_2" : @{
  9149. @"aname" : @"Expiration Date",
  9150. @"control" : @"monthpicker",
  9151. @"name" : @"credit_card_expiration",
  9152. @"required" : @"true",
  9153. @"type" : @"date",
  9154. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9155. },
  9156. @"item_3" : @{
  9157. @"aname" : @"Security Code",
  9158. @"control" : @"edit",
  9159. @"keyboard" : @"int",
  9160. @"length" : @"3",
  9161. @"name" : @"credit_card_security_code",
  9162. @"required" : @"true",
  9163. @"value" : securityCode
  9164. },
  9165. @"item_4" : @{
  9166. @"aname" : @"First Name",
  9167. @"control" : @"edit",
  9168. @"keyboard" : @"text",
  9169. @"name" : @"credit_card_first_name",
  9170. @"required" : @"true",
  9171. @"value" : firstName
  9172. },
  9173. @"item_5" : @{
  9174. @"aname" : @"Last Name",
  9175. @"control" : @"edit",
  9176. @"keyboard" : @"text",
  9177. @"name" : @"credit_card_last_name",
  9178. @"required" : @"true",
  9179. @"value" : lastName
  9180. },
  9181. @"item_6" : @{
  9182. @"aname" : @"Address 1",
  9183. @"control" : @"edit",
  9184. @"keyboard" : @"text",
  9185. @"name" : @"credit_card_address1",
  9186. @"required" : @"true",
  9187. @"value" : addr1
  9188. },
  9189. @"item_7" : @{
  9190. @"aname" : @"Address 2",
  9191. @"control" : @"edit",
  9192. @"keyboard" : @"text",
  9193. @"name" : @"credit_card_address2",
  9194. @"value" : addr2
  9195. },
  9196. @"item_8" : @{
  9197. @"aname" : @"zip code",
  9198. @"control" : @"edit",
  9199. @"keyboard" : @"text",
  9200. @"name" : @"credit_card_zipcode",
  9201. @"required" : @"true",
  9202. @"value" : zipcode
  9203. },
  9204. @"item_9" : @{
  9205. @"aname" : @"City",
  9206. @"control" : @"edit",
  9207. @"keyboard" : @"text",
  9208. @"name" : @"credit_card_city",
  9209. @"required" : @"true",
  9210. @"value" : city
  9211. }
  9212. },
  9213. @"value" : @"Fill Now",
  9214. @"value_id" : @""
  9215. },
  9216. @"val_1" : @{
  9217. @"check" : @(0),
  9218. @"value" : @"Fill Later",
  9219. @"value_id" : @""
  9220. }
  9221. },
  9222. @"control" : @"enum",
  9223. @"name" : @"",
  9224. @"single_select" : @"true"
  9225. }
  9226. };
  9227. #endif
  9228. [val setObject:sub_item forKey:@"sub_item"];
  9229. }
  9230. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9231. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9232. }];
  9233. // "section_2"
  9234. NSMutableDictionary *dic = @{
  9235. @"count" : @(1),
  9236. @"item_0" : @{
  9237. @"aname" : @"Payment",
  9238. @"required" : required,
  9239. @"cadedate" : cadedate,
  9240. // @{
  9241. // @"count" : @(6),
  9242. // @"val_3" : @{
  9243. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9244. // @"value" : @"Check",
  9245. // @"value_id" : @"Check"
  9246. // },
  9247. // @"val_2" : @{
  9248. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9249. // @"value" : @"Cash",
  9250. // @"value_id" : @"Cash"
  9251. // },
  9252. // @"val_1" : @{
  9253. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9254. // @"value" : @"NET 60",
  9255. // @"value_id" : @"NET 30"
  9256. // },
  9257. // @"val_4" : @{
  9258. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9259. // @"value" : @"Wire Transfer",
  9260. // @"value_id" : @"Wire Transfer"
  9261. // },
  9262. // @"val_0" : @{
  9263. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9264. // @"sub_item" : @{
  9265. // @"count" : @(3),
  9266. // @"item_0" : @{
  9267. // @"aname" : @"choose",
  9268. // @"control" : @"multi_action",
  9269. // @"count" : @(1),
  9270. // @"item_0" : @{
  9271. // @"aname" : @"Same as customer",
  9272. // @"key_map" : @{
  9273. // @"credit_card_address1" : @"customer_address1",
  9274. // @"credit_card_address2" : @"customer_address2",
  9275. // @"credit_card_city" : @"customer_city",
  9276. // @"credit_card_first_name" : @"customer_first_name",
  9277. // @"credit_card_last_name" : @"customer_last_name",
  9278. // @"credit_card_state" : @"customer_state",
  9279. // @"credit_card_zipcode" : @"customer_zipcode"
  9280. // },
  9281. // @"type" : @"pull"
  9282. // }
  9283. // },
  9284. // @"item_1" : @{
  9285. // @"aname" : @"",
  9286. // @"color" : @"red",
  9287. // @"control" : @"text",
  9288. // @"name" : @"",
  9289. // @"value" : @"USA Credit cards only"
  9290. // },
  9291. // @"item_2" : @{
  9292. // @"aname" : @"Fill",
  9293. // @"cadedate" : @{
  9294. // @"count" : @(2),
  9295. // @"val_0" : @{
  9296. // @"check" : @(1),
  9297. // @"sub_item" : @{
  9298. // @"count" : @(11),
  9299. // @"item_0" : @{
  9300. // @"aname" : @"Type",
  9301. // @"cadedate" : @{
  9302. // @"count" : @(2),
  9303. // @"val_0" : @{
  9304. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9305. // @"value" : @"VISA",
  9306. // @"value_id" : @(0)
  9307. // },
  9308. // @"val_1" : @{
  9309. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9310. // @"value" : @"MASTER CARD",
  9311. // @"value_id" : @(1)
  9312. // }
  9313. // },
  9314. // @"control" : @"enum",
  9315. // @"name" : @"credit_card_type",
  9316. // @"required" : @"true",
  9317. // @"single_select" : @"true"
  9318. // },
  9319. // @"item_1" : @{
  9320. // @"aname" : @"Number",
  9321. // @"control" : @"edit",
  9322. // @"keyboard" : @"int",
  9323. // @"length" : @"16",
  9324. // @"name" : @"credit_card_number",
  9325. // @"required" : @"true",
  9326. // @"value" : cardNumber
  9327. // },
  9328. // @"item_10" : @{
  9329. // @"aname" : @"State",
  9330. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9331. // @"control" : @"enum",
  9332. // @"enum" : @"true",
  9333. // @"name" : @"credit_card_state",
  9334. // @"required" : @"true",
  9335. // @"single_select" : @"true"
  9336. // },
  9337. // @"item_2" : @{
  9338. // @"aname" : @"Expiration Date",
  9339. // @"control" : @"monthpicker",
  9340. // @"name" : @"credit_card_expiration",
  9341. // @"required" : @"true",
  9342. // @"type" : @"date",
  9343. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9344. // },
  9345. // @"item_3" : @{
  9346. // @"aname" : @"Security Code",
  9347. // @"control" : @"edit",
  9348. // @"keyboard" : @"int",
  9349. // @"length" : @"3",
  9350. // @"name" : @"credit_card_security_code",
  9351. // @"required" : @"true",
  9352. // @"value" : securityCode
  9353. // },
  9354. // @"item_4" : @{
  9355. // @"aname" : @"First Name",
  9356. // @"control" : @"edit",
  9357. // @"keyboard" : @"text",
  9358. // @"name" : @"credit_card_first_name",
  9359. // @"required" : @"true",
  9360. // @"value" : firstName
  9361. // },
  9362. // @"item_5" : @{
  9363. // @"aname" : @"Last Name",
  9364. // @"control" : @"edit",
  9365. // @"keyboard" : @"text",
  9366. // @"name" : @"credit_card_last_name",
  9367. // @"required" : @"true",
  9368. // @"value" : lastName
  9369. // },
  9370. // @"item_6" : @{
  9371. // @"aname" : @"Address 1",
  9372. // @"control" : @"edit",
  9373. // @"keyboard" : @"text",
  9374. // @"name" : @"credit_card_address1",
  9375. // @"required" : @"true",
  9376. // @"value" : addr1
  9377. // },
  9378. // @"item_7" : @{
  9379. // @"aname" : @"Address 2",
  9380. // @"control" : @"edit",
  9381. // @"keyboard" : @"text",
  9382. // @"name" : @"credit_card_address2",
  9383. // @"value" : addr2
  9384. // },
  9385. // @"item_8" : @{
  9386. // @"aname" : @"zip code",
  9387. // @"control" : @"edit",
  9388. // @"keyboard" : @"text",
  9389. // @"name" : @"credit_card_zipcode",
  9390. // @"required" : @"true",
  9391. // @"value" : zipcode
  9392. // },
  9393. // @"item_9" : @{
  9394. // @"aname" : @"City",
  9395. // @"control" : @"edit",
  9396. // @"keyboard" : @"text",
  9397. // @"name" : @"credit_card_city",
  9398. // @"required" : @"true",
  9399. // @"value" : city
  9400. // }
  9401. // },
  9402. // @"value" : @"Fill Now",
  9403. // @"value_id" : @""
  9404. // },
  9405. // @"val_1" : @{
  9406. // @"check" : @(0),
  9407. // @"value" : @"Fill Later",
  9408. // @"value_id" : @""
  9409. // }
  9410. // },
  9411. // @"control" : @"enum",
  9412. // @"name" : @"",
  9413. // @"single_select" : @"true"
  9414. // }
  9415. // },
  9416. // @"value" : @"Visa/Master",
  9417. // @"value_id" : @"Credit Card"
  9418. // },
  9419. // @"val_5" : @{
  9420. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9421. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9422. // @"value_id" : @"FOLLOW EXISTING"
  9423. // }
  9424. // },
  9425. @"control" : @"enum",
  9426. @"name" : @"paymentType",
  9427. @"single_select" : @"true"
  9428. },
  9429. @"title" : @"Payment Information"
  9430. }.mutableCopy;
  9431. NSNumber *hide = setting[@"PaymentInformationHide"];
  9432. [dic setValue:hide forKey:@"hide"];
  9433. return dic;
  9434. }
  9435. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9436. // params
  9437. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9438. orderCode = [self translateSingleQuote:orderCode];
  9439. // 缺货检查
  9440. 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];
  9441. __block BOOL outOfStock = NO;
  9442. sqlite3 *database = db;
  9443. if (!db) {
  9444. database = [iSalesDB get_db];
  9445. }
  9446. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9447. int availability = sqlite3_column_int(stmt, 0);
  9448. int item_qty = sqlite3_column_int(stmt, 1);
  9449. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9450. }];
  9451. if (!db) {
  9452. [iSalesDB close_db:database];
  9453. }
  9454. return outOfStock;
  9455. }
  9456. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9457. {
  9458. assert(params[@"user"]!=nil);
  9459. assert(params[@"contact_id"]!=nil);
  9460. assert(params[@"can_create_backorder"]!=nil);
  9461. sqlite3 *db = [iSalesDB get_db];
  9462. // params
  9463. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9464. orderCode = [self translateSingleQuote:orderCode];
  9465. // 缺货检查
  9466. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9467. if (![params[@"can_create_backorder"] boolValue]) {
  9468. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9469. if (out_of_stock) {
  9470. [iSalesDB close_db:db];
  9471. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9472. [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"];
  9473. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9474. return [RAConvertor dict2data:resultDic];
  9475. }
  9476. }
  9477. // UISetting
  9478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9479. NSString *cachefolder = [paths objectAtIndex:0];
  9480. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9481. NSData* json =nil;
  9482. json=[NSData dataWithContentsOfFile:img_cache];
  9483. NSError *error=nil;
  9484. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9485. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9486. [params setObject:setting forKey:@"setting"];
  9487. int section_count = 0;
  9488. // 0 Order Type 1 Shipping Method 2 Payment Information
  9489. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9490. // 0 Order Type
  9491. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9492. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9493. [ret setValue:order_type_dic forKey:key0];
  9494. // 1 Shipping Method
  9495. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9496. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9497. [ret setValue:shipping_method_dic forKey:key1];
  9498. // 2 Payment Information
  9499. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9500. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9501. [ret setValue:payment_info_dic forKey:key2];
  9502. // 3 Customer
  9503. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9504. NSDictionary *customer_dic = [self customerDic:params db:db];
  9505. [ret setValue:customer_dic forKey:key3];
  9506. // 4 Ship To
  9507. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9508. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9509. [ret setValue:ship_to_dic forKey:key4];
  9510. // 5 Ship From
  9511. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9512. if (![shipFromDisable integerValue]) {
  9513. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9514. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9515. [ret setValue:ship_from_dic forKey:key5];
  9516. }
  9517. // 6 Freight Bill To
  9518. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9519. if (![freightBillToDisable integerValue]) {
  9520. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9521. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9522. [ret setValue:freight_bill_to forKey:key6];
  9523. }
  9524. // 7 Merchandise Bill To
  9525. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9526. if (![merchandiseBillToDisable integerValue]) {
  9527. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9528. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9529. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9530. }
  9531. // 8 Return To
  9532. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9533. if (![returnToDisable integerValue]) {
  9534. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9535. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9536. [ret setValue:return_to_dic forKey:key8];
  9537. }
  9538. // 9 Model Information
  9539. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9540. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9541. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9542. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9543. [ret setValue:model_info_dic forKey:key9];
  9544. // 10 Remarks Content
  9545. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9546. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9547. [ret setValue:remarks_content_dic forKey:key10];
  9548. // 11 Order Total
  9549. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9550. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9551. [ret setValue:order_total_dic forKey:key11];
  9552. // 12 Signature
  9553. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9554. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9555. [ret setValue:sign_dic forKey:key12];
  9556. [ret setValue:@(section_count) forKey:@"section_count"];
  9557. [iSalesDB close_db:db];
  9558. return [RAConvertor dict2data:ret];
  9559. // return nil;
  9560. }
  9561. #pragma mark addr editor
  9562. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9563. NSMutableDictionary *new_item = [item mutableCopy];
  9564. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9565. return new_item;
  9566. }
  9567. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9568. {
  9569. // "is_subaction" = true;
  9570. // orderCode = MOB1608240002;
  9571. // password = 123456;
  9572. // "subaction_tag" = 1;
  9573. // user = EvanK;
  9574. // {
  9575. // "is_subaction" = true;
  9576. // orderCode = MOB1608240002;
  9577. // password = 123456;
  9578. // "refresh_trigger" = zipcode;
  9579. // "subaction_tag" = 1;
  9580. // user = EvanK;
  9581. // }
  9582. NSString *country_code = nil;
  9583. NSString *zipCode = nil;
  9584. NSString *stateCode = nil;
  9585. NSString *city = nil;
  9586. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9587. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9588. NSString *code_id = params[@"country"];
  9589. country_code = [self countryCodeByid:code_id];
  9590. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9591. NSString *zip_code = params[@"zipcode"];
  9592. // 剔除全部为空格
  9593. int spaceCount = 0;
  9594. for (int i = 0; i < zip_code.length; i++) {
  9595. if ([zip_code characterAtIndex:i] == ' ') {
  9596. spaceCount++;
  9597. }
  9598. }
  9599. if (spaceCount == zip_code.length) {
  9600. zip_code = @"";
  9601. }
  9602. zipCode = zip_code;
  9603. if (zipCode.length > 0) {
  9604. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9605. country_code = [dic valueForKey:@"country_code"];
  9606. if (!country_code) {
  9607. // country_code = @"US";
  9608. NSString *code_id = params[@"country"];
  9609. country_code = [self countryCodeByid:code_id];
  9610. }
  9611. stateCode = [dic valueForKey:@"state_code"];
  9612. if(!stateCode.length) {
  9613. stateCode = params[@"state"];
  9614. }
  9615. city = [dic valueForKey:@"city"];
  9616. if (!city.length) {
  9617. city = params[@"city"];
  9618. }
  9619. // zip code
  9620. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9621. // [zipDic setValue:zipCode forKey:@"value"];
  9622. // [section_0 setValue:zipDic forKey:@"item_11"];
  9623. } else {
  9624. NSString *code_id = params[@"country"];
  9625. country_code = [self countryCodeByid:code_id];
  9626. stateCode = params[@"state"];
  9627. city = params[@"city"];
  9628. }
  9629. }
  9630. }
  9631. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9632. // [ret removeObjectForKey:@"up_params"];
  9633. [ret setObject:@"New Address" forKey:@"title"];
  9634. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9635. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9636. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9637. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9638. // [country_dic removeObjectForKey:@"refresh"];
  9639. // [country_dic removeObjectForKey:@"restore"];
  9640. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9641. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9642. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9643. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9644. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9645. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9646. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9647. // [zip_code_dic removeObjectForKey:@"refresh"];
  9648. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9649. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9650. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9651. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9652. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9653. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9654. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9655. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9656. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9657. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9658. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9659. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9660. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9661. // country
  9662. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9663. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9664. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9665. // state
  9666. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9667. // NSDictionary *tmpDic = @{
  9668. // @"value" : @"Other",
  9669. // @"value_id" : @"",
  9670. // @"check" : [NSNumber numberWithInteger:0]
  9671. // };
  9672. //
  9673. // for (int i = 0; i < allState.allKeys.count; i++) {
  9674. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9675. //
  9676. // NSDictionary *tmp = allState[key];
  9677. // [allState setValue:tmpDic forKey:key];
  9678. // tmpDic = tmp;
  9679. // }
  9680. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9681. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9682. [ret setValue:new_section_0 forKey:@"section_0"];
  9683. return [RAConvertor dict2data:ret];
  9684. }
  9685. #pragma mark save addr
  9686. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9687. {
  9688. // NSString *companyName = [self valueInParams:params key:@"company"];
  9689. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9690. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9691. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9692. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9693. // NSString *countryId = [self valueInParams:params key:@"country"];
  9694. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9695. // NSString *city = [self valueInParams:params key:@"city"];
  9696. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9697. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9698. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9699. // NSString *phone = [self valueInParams:params key:@"phone"];
  9700. // NSString *fax = [self valueInParams:params key:@"fax"];
  9701. // NSString *email = [self valueInParams:params key:@"email"];
  9702. return [self offline_saveContact:params update:NO isCustomer:NO];
  9703. }
  9704. #pragma mark cancel order
  9705. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9706. {
  9707. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9708. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9709. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9710. if (order_id.length) {
  9711. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9712. }
  9713. int ret = [iSalesDB execSql:sql];
  9714. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9715. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9716. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9717. // [dic setValue:@"160409" forKey:@"min_ver"];
  9718. return [RAConvertor dict2data:dic];
  9719. }
  9720. #pragma mark sign order
  9721. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9722. {
  9723. //参考 offline_saveBusinesscard
  9724. DebugLog(@"sign order params: %@",params);
  9725. // orderCode = MOB1608240002;
  9726. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9727. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9728. orderCode = [self translateSingleQuote:orderCode];
  9729. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9730. picPath = [self translateSingleQuote:picPath];
  9731. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9732. int ret = [iSalesDB execSql:sql];
  9733. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9734. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9735. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9736. // [dic setValue:@"160409" forKey:@"min_ver"];
  9737. return [RAConvertor dict2data:dic];
  9738. }
  9739. #pragma mark save order
  9740. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9741. NSString *ret = [self valueInParams:params key:key];
  9742. if (translate) {
  9743. ret = [self translateSingleQuote:ret];
  9744. }
  9745. return ret;
  9746. }
  9747. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9748. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9749. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9750. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9751. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9752. if (schedule_date.length==0 ) {
  9753. schedule_date = @"";
  9754. } else {
  9755. schedule_date = [self rchangeDateFormate:schedule_date];
  9756. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9757. }
  9758. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9759. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9760. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9761. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9762. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9763. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9764. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9765. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9766. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9767. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9768. if (![total_price isEqualToString:@""]) {
  9769. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9770. } else {
  9771. total_price = @"";
  9772. }
  9773. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9774. if ([paymentsAndCredits isEqualToString:@""]) {
  9775. paymentsAndCredits = @"";
  9776. } else {
  9777. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9778. }
  9779. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9780. if ([handling_fee_value isEqualToString:@""]) {
  9781. handling_fee_value = @"";
  9782. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9783. handling_fee_value = @"";
  9784. } else {
  9785. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9786. }
  9787. NSString *handling_fee_placeholder = handling_fee_value;
  9788. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9789. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9790. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9791. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9792. NSString *lift_gate_placeholder = @"";
  9793. if ([lift_gate_value isEqualToString:@""]) {
  9794. lift_gate_placeholder = @"";
  9795. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9796. lift_gate_placeholder = @"";
  9797. } else {
  9798. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9799. }
  9800. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9801. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9802. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9803. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9804. 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];
  9805. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9806. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9807. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9808. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9809. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9810. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9811. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9812. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9813. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9814. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9815. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9816. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9817. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9818. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9819. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9820. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9821. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9822. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9823. if (!number_nil) {
  9824. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9825. }
  9826. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9827. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9828. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9829. if (!security_code_nil) {
  9830. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9831. }
  9832. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9833. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9834. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9835. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9836. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9837. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9838. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9839. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9840. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9841. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9842. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9843. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9844. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9845. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9846. NSString *contact_id = customer_cid;
  9847. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9848. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9849. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9850. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9851. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9852. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9853. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9854. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9855. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9856. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9857. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9858. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9859. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9860. //
  9861. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9862. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9863. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9864. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9865. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9866. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9867. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9868. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9869. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9870. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9871. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9872. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9873. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9874. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9875. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9876. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9877. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9878. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9879. 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];
  9880. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9881. if (receive_cid.length) {
  9882. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9883. }
  9884. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9885. if (receive_name.length) {
  9886. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9887. }
  9888. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9889. if (receive_ext.length) {
  9890. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9891. }
  9892. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9893. if (receive_contact.length) {
  9894. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9895. }
  9896. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9897. if (receive_phone.length) {
  9898. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9899. }
  9900. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9901. if (receive_email.length) {
  9902. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9903. }
  9904. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9905. if (receive_fax.length) {
  9906. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9907. }
  9908. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9909. if (sender_cid.length) {
  9910. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9911. }
  9912. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9913. if (sender_name.length) {
  9914. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9915. }
  9916. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9917. if (sender_ext.length) {
  9918. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9919. }
  9920. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9921. if(sender_contact.length) {
  9922. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9923. }
  9924. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9925. if (sender_phone.length) {
  9926. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9927. }
  9928. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9929. if (sender_fax.length) {
  9930. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9931. }
  9932. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9933. if (sender_email.length) {
  9934. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9935. }
  9936. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9937. if (shipping_billto_cid.length) {
  9938. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9939. }
  9940. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9941. if (shipping_billto_name.length) {
  9942. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9943. }
  9944. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9945. if (shipping_billto_ext.length) {
  9946. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9947. }
  9948. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9949. if (shipping_billto_contact.length) {
  9950. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9951. }
  9952. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9953. if (shipping_billto_phone.length) {
  9954. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9955. }
  9956. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9957. if (shipping_billto_fax.length) {
  9958. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9959. }
  9960. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9961. if (shipping_billto_email.length) {
  9962. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9963. }
  9964. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9965. if (billing_cid.length) {
  9966. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9967. }
  9968. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9969. if (billing_name.length) {
  9970. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9971. }
  9972. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9973. if (billing_ext.length) {
  9974. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9975. }
  9976. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9977. if (billing_contact.length) {
  9978. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9979. }
  9980. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9981. if (billing_phone.length) {
  9982. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9983. }
  9984. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9985. if (billing_fax.length) {
  9986. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9987. }
  9988. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9989. if (billing_email.length) {
  9990. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9991. }
  9992. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9993. if (returnto_cid.length) {
  9994. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9995. }
  9996. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9997. if (returnto_name.length) {
  9998. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9999. }
  10000. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10001. if (returnto_ext.length) {
  10002. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10003. }
  10004. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10005. if (returnto_contact.length) {
  10006. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10007. }
  10008. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10009. if (returnto_phone.length) {
  10010. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10011. }
  10012. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10013. if (returnto_fax.length) {
  10014. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10015. }
  10016. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10017. if (returnto_email.length) {
  10018. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10019. }
  10020. NSString *order_status = @"status = 1,";
  10021. if (submit) {
  10022. order_status = @"status = -11,";
  10023. }
  10024. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10025. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10026. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10027. NSString *sync_sql = @"";
  10028. if (submit) {
  10029. param[@"truck_carrier"]=carrier;
  10030. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10031. [param setValue:sales_rep forKey:@"sales_rep"];
  10032. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10033. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10034. }
  10035. 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];
  10036. DebugLog(@"save order contactSql: %@",contactSql);
  10037. DebugLog(@"save order orderSql: %@",orderSql);
  10038. // int contact_ret = [iSalesDB execSql:contactSql];
  10039. int order_ret = [iSalesDB execSql:orderSql];
  10040. int ret = order_ret;
  10041. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10042. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10043. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10044. // [dic setValue:@"160409" forKey:@"min_ver"];
  10045. [dic setObject:so_id forKey:@"so#"];
  10046. return [RAConvertor dict2data:dic];
  10047. }
  10048. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10049. {
  10050. // id foo = nil;
  10051. // NSMutableArray *a = @[].mutableCopy;
  10052. // [a addObject:foo];
  10053. return [self saveorder:param submit:NO];
  10054. }
  10055. #pragma mark add to cart by name
  10056. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10057. {
  10058. NSString *orderCode = [params objectForKey:@"orderCode"];
  10059. NSDictionary *newParams = @{
  10060. @"product_id" : params[@"product_id_string"],
  10061. @"orderCode" : orderCode,
  10062. @"can_create_backorder":params[@"can_create_backorder"]
  10063. };
  10064. return [self offline_add2cart:[newParams mutableCopy]];
  10065. }
  10066. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10067. {
  10068. NSString *orderCode = [params objectForKey:@"orderCode"];
  10069. NSString *upccode = [params objectForKey:@"upc_code"];
  10070. // product_name = [self translateSingleQuote:product_name];
  10071. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10072. // bool search_upc = [params[@"search_upc"] boolValue];
  10073. sqlite3 *db = [iSalesDB get_db];
  10074. __block NSMutableString *product_id_string = [NSMutableString string];
  10075. // NSString *name = [product_name_array objectAtIndex:i];
  10076. NSString *sql =nil;
  10077. 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];
  10078. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10079. int product_id = sqlite3_column_int(stmt, 0);
  10080. [product_id_string appendFormat:@"%d",product_id];
  10081. }];
  10082. if (!orderCode) {
  10083. orderCode = @"";
  10084. }
  10085. NSDictionary *newParams = @{
  10086. @"product_id" : product_id_string,
  10087. @"orderCode" : orderCode,
  10088. @"can_create_backorder":params[@"can_create_backorder"]
  10089. };
  10090. [iSalesDB close_db:db];
  10091. return [self offline_add2cart:[newParams mutableCopy]];
  10092. }
  10093. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10094. {
  10095. NSString *orderCode = [params objectForKey:@"orderCode"];
  10096. NSString *product_name = [params objectForKey:@"product_name"];
  10097. product_name = [self translateSingleQuote:product_name];
  10098. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10099. bool search_upc = [params[@"search_upc"] boolValue];
  10100. sqlite3 *db = [iSalesDB get_db];
  10101. __block NSMutableString *product_id_string = [NSMutableString string];
  10102. for (int i = 0; i < product_name_array.count; i++) {
  10103. NSString *name = [product_name_array objectAtIndex:i];
  10104. NSString *sql =nil;
  10105. if(search_upc)
  10106. 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];
  10107. else
  10108. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10109. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10110. int product_id = sqlite3_column_int(stmt, 0);
  10111. if (i == product_name_array.count - 1) {
  10112. [product_id_string appendFormat:@"%d",product_id];
  10113. } else {
  10114. [product_id_string appendFormat:@"%d,",product_id];
  10115. }
  10116. }];
  10117. }
  10118. if (!orderCode) {
  10119. orderCode = @"";
  10120. }
  10121. NSDictionary *newParams = @{
  10122. @"product_id" : product_id_string,
  10123. @"orderCode" : orderCode,
  10124. @"can_create_backorder":params[@"can_create_backorder"]
  10125. };
  10126. [iSalesDB close_db:db];
  10127. return [self offline_add2cart:[newParams mutableCopy]];
  10128. }
  10129. #pragma mark reset order
  10130. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10131. {
  10132. // UIApplication * app = [UIApplication sharedApplication];
  10133. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10134. [iSalesDB disable_trigger];
  10135. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10136. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10137. [iSalesDB enable_trigger];
  10138. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10139. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10140. return [RAConvertor dict2data:dic];
  10141. }
  10142. #pragma mark submit order
  10143. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10144. {
  10145. return [self saveorder:params submit:YES];
  10146. }
  10147. #pragma mark copy order
  10148. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10149. {
  10150. NSMutableDictionary *ret = @{}.mutableCopy;
  10151. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10152. sqlite3 *db = [iSalesDB get_db];
  10153. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10154. // 首先查看联系人是否active
  10155. 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];
  10156. __block int customer_is_active = 1;
  10157. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10158. customer_is_active = sqlite3_column_int(stmt, 0);
  10159. }];
  10160. if (!customer_is_active) {
  10161. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10162. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10163. [iSalesDB close_db:db];
  10164. return [RAConvertor dict2data:ret];
  10165. }
  10166. // new order
  10167. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10168. NSString *new_order_code = [self get_offline_soid:db];
  10169. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10170. user = [self translateSingleQuote:user];
  10171. 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
  10172. __block int result = 1;
  10173. result = [iSalesDB execSql:insert_order_sql db:db];
  10174. if (!result) {
  10175. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10176. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10177. [iSalesDB close_db:db];
  10178. return [RAConvertor dict2data:ret];
  10179. }
  10180. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10181. // __block NSString *product_id = @"";
  10182. __weak typeof(self) weakSelf = self;
  10183. 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];
  10184. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10185. int is_active = sqlite3_column_int(stmt, 1);
  10186. if (is_active) {
  10187. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10188. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10189. 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];
  10190. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10191. }
  10192. }];
  10193. // product_id = [product_id substringFromIndex:1];
  10194. //
  10195. // [self offline_add2cart:@{}.mutableCopy];
  10196. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10197. [iSalesDB close_db:db];
  10198. if (result) {
  10199. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10200. } else {
  10201. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10202. }
  10203. [ret setObject:new_order_code forKey:@"so_id"];
  10204. return [RAConvertor dict2data:ret];
  10205. }
  10206. #pragma mark move wish list to cart
  10207. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10208. assert(params[@"can_create_backorder"]!=nil);
  10209. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10210. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10211. NSString *collectId = params[@"collectId"];
  10212. 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];
  10213. __block NSString *product_id = @"";
  10214. __block NSString *qty = @"";
  10215. __block int number_of_outOfStock = 0;
  10216. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10217. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10218. int productId = sqlite3_column_int(stmt, 0);
  10219. int item_qty = sqlite3_column_int(stmt, 1);
  10220. int availability = sqlite3_column_int(stmt, 2);
  10221. int _id = sqlite3_column_int(stmt, 3);
  10222. if (![params[@"can_create_backorder"] boolValue]) {
  10223. // 库存小于购买量为缺货
  10224. if (availability >= item_qty) {
  10225. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10226. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10227. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10228. } else {
  10229. number_of_outOfStock++;
  10230. }
  10231. } else {
  10232. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10233. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10234. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10235. }
  10236. }];
  10237. NSMutableDictionary *retDic = nil;
  10238. if (![params[@"can_create_backorder"] boolValue]) {
  10239. if (delete_collectId.count == 0) {
  10240. retDic = [NSMutableDictionary dictionary];
  10241. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10242. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10243. return [RAConvertor dict2data:retDic];
  10244. }
  10245. }
  10246. if (product_id.length > 1) {
  10247. product_id = [product_id substringFromIndex:1];
  10248. }
  10249. if (qty.length > 1) {
  10250. qty = [qty substringFromIndex:1];
  10251. }
  10252. NSString *orderCode = params[@"orderCode"];
  10253. if (!orderCode) {
  10254. orderCode = @"";
  10255. }
  10256. NSDictionary *newParams = @{
  10257. @"product_id" : product_id,
  10258. @"orderCode" : orderCode,
  10259. @"qty" : qty,
  10260. @"can_create_backorder":params[@"can_create_backorder"]
  10261. };
  10262. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10263. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10264. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10265. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10266. NSInteger ret = [wish_return[@"result"] intValue];
  10267. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10268. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10269. }
  10270. if (![params[@"can_create_backorder"] boolValue]) {
  10271. if (number_of_outOfStock > 0) {
  10272. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10273. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10274. }
  10275. }
  10276. return [RAConvertor dict2data:retDic];
  10277. }
  10278. #pragma mark - portfolio
  10279. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10280. // assert(params[@"contact_id"]!=nil);
  10281. assert(params[@"user"]!=nil);
  10282. assert(params[@"can_see_price"]!=nil);
  10283. int sort = [[params valueForKey:@"sort"] intValue];
  10284. int offset = [[params valueForKey:@"offset"] intValue];
  10285. int limit = [[params valueForKey:@"limit"] intValue];
  10286. NSString *orderBy = @"";
  10287. switch (sort) {
  10288. case 0:{
  10289. orderBy = @"p.modify_time desc";
  10290. }
  10291. break;
  10292. case 1:{
  10293. orderBy = @"modify_time asc";
  10294. }
  10295. break;
  10296. case 2:{
  10297. orderBy = @"p.name asc";
  10298. }
  10299. break;
  10300. case 3:{
  10301. orderBy = @"p.name desc";
  10302. }
  10303. break;
  10304. case 4:{
  10305. orderBy = @"p.description asc";
  10306. }
  10307. break;
  10308. case 5: {
  10309. orderBy = @"m.default_category asc";
  10310. }
  10311. default:
  10312. break;
  10313. }
  10314. // 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];
  10315. 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];
  10316. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10317. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10318. sqlite3 *db = [iSalesDB get_db];
  10319. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10320. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10321. int product_id = sqlite3_column_int(stmt, 0);
  10322. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10323. NSString *name = [self textAtColumn:1 statement:stmt];
  10324. NSString *description = [self textAtColumn:2 statement:stmt];
  10325. double price = sqlite3_column_double(stmt, 3);
  10326. double discount = sqlite3_column_double(stmt,4);
  10327. int qty = sqlite3_column_int(stmt, 5);
  10328. int percentage = sqlite3_column_int(stmt, 6);
  10329. int item_id = sqlite3_column_int(stmt, 7);
  10330. // int fashion_id = sqlite3_column_int(stmt, 8);
  10331. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10332. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10333. double percent = sqlite3_column_double(stmt, 11);
  10334. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10335. if ([price_null isEqualToString:@"null"]) {
  10336. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10337. }
  10338. NSMutableDictionary *item = @{
  10339. @"linenotes": line_note,
  10340. @"check": @(1),
  10341. @"product_id": product_id_string,
  10342. @"available_qty": @(qty),
  10343. @"available_percent" : @(percent),
  10344. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10345. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10346. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10347. @"img": img_path,
  10348. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10349. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10350. }.mutableCopy;
  10351. if (percentage) {
  10352. [item removeObjectForKey:@"available_qty"];
  10353. } else {
  10354. [item removeObjectForKey:@"available_percent"];
  10355. }
  10356. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10357. if ([qty_null isEqualToString:@"null"]) {
  10358. [item removeObjectForKey:@"available_qty"];
  10359. }
  10360. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10361. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10362. }];
  10363. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10364. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10365. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10366. }
  10367. [iSalesDB close_db:db];
  10368. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10369. [dic setValue:@"" forKey:@"email_content"];
  10370. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10371. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10372. return [RAConvertor dict2data:dic];
  10373. }
  10374. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10375. __block int qty = 0;
  10376. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10377. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10378. qty = sqlite3_column_int(stmt, 0);
  10379. }];
  10380. return qty;
  10381. }
  10382. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10383. {
  10384. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10385. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10386. // NSMutableDictionary * values = params[@"replaceValue"];
  10387. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10388. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10389. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10390. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10391. NSString *pdf_path = @"";
  10392. if (direct) {
  10393. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10394. } else {
  10395. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10396. }
  10397. NSString *create_user = [self valueInParams:params key:@"user"];
  10398. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10399. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10400. // model info
  10401. // 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];
  10402. // V1.90 more color
  10403. 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];
  10404. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10405. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10406. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10407. sqlite3 *db = [iSalesDB get_db];
  10408. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10409. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10410. int product_id = sqlite3_column_int(stmt, 0);
  10411. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10412. double price = sqlite3_column_double(stmt, 1);
  10413. double discount = sqlite3_column_double(stmt,2);
  10414. int qty = sqlite3_column_int(stmt, 3);
  10415. int percentage = sqlite3_column_int(stmt, 4);
  10416. int item_id = sqlite3_column_int(stmt, 5);
  10417. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10418. double percent = sqlite3_column_double(stmt, 7);
  10419. int more_color = sqlite3_column_int(stmt, 8);
  10420. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10421. /* if ([price_null isEqualToString:@"null"]) {
  10422. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10423. }
  10424. */
  10425. [product_ids_string appendFormat:@"%@,",product_id_string];
  10426. // Regular Price
  10427. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10428. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10429. // QTY
  10430. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10431. if ([qty_null isEqualToString:@"null"]) {
  10432. // 查available
  10433. qty = [self model_QTY:product_id_string db:db];
  10434. }
  10435. if (percentage) {
  10436. qty = qty * percent / 100;
  10437. }
  10438. // Special Price
  10439. if ([price_null isEqualToString:@"null"]) {
  10440. // price = regular price
  10441. price = [regular_price_str doubleValue];
  10442. }
  10443. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10444. if (![discount_null isEqualToString:@"null"]) {
  10445. price = price * (1 - discount / 100.0);
  10446. }
  10447. NSMutableDictionary *item = @{
  10448. @"line_note": line_note,
  10449. @"product_id": product_id_string,
  10450. @"available_qty": @(qty),
  10451. @"more_color":@(more_color),
  10452. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10453. @"regular_price" : regular_price_str,
  10454. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10455. }.mutableCopy;
  10456. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10457. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10458. }];
  10459. [iSalesDB close_db:db];
  10460. if (product_ids_string.length > 0) {
  10461. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10462. }
  10463. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10464. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10465. return [RAConvertor dict2data:resultDictionary];
  10466. }
  10467. NSString *model_info = [RAConvertor dict2string:dic];
  10468. // 创建PDF
  10469. // 在preview情况下保存,则不需要新建了
  10470. if (direct) {
  10471. NSMutableDictionary *tear_sheet_params = params;
  10472. // if (tear_sheet_id) {
  10473. // tear_sheet_params = values;
  10474. // }
  10475. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10476. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10477. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10478. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10479. resultDictionary = pdfInfo.mutableCopy;
  10480. } else { // 创建PDF失败
  10481. return pdfData;
  10482. }
  10483. } else {
  10484. // pdf_path 就是本地路径
  10485. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10486. }
  10487. // 将文件移动到PDF Cache文件夹
  10488. NSString *newPath = [pdf_path lastPathComponent];
  10489. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10490. NSString *cachefolder = [paths objectAtIndex:0];
  10491. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10492. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10493. NSFileManager *fileManager = [NSFileManager defaultManager];
  10494. NSError *error = nil;
  10495. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10496. if (error) { // 移动文件失败
  10497. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10498. return [RAConvertor dict2data:resultDictionary];
  10499. }
  10500. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10501. pdf_path = [newPath lastPathComponent];
  10502. // 保存信息
  10503. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10504. NSString *off_params = [RAConvertor dict2string:params];
  10505. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10506. pdf_path = [self translateSingleQuote:pdf_path];
  10507. create_user = [self translateSingleQuote:create_user];
  10508. tear_note = [self translateSingleQuote:tear_note];
  10509. tear_name = [self translateSingleQuote:tear_name];
  10510. model_info = [self translateSingleQuote:model_info];
  10511. configureParams = [self translateSingleQuote:configureParams];
  10512. off_params = [self translateSingleQuote:off_params];
  10513. 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];
  10514. if (tear_sheet_id) {
  10515. int _id = [tear_sheet_id intValue];
  10516. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10517. }
  10518. int result = [iSalesDB execSql:save_pdf_sql];
  10519. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10520. //
  10521. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10522. if (remove_Item) {
  10523. // portfolioId
  10524. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10525. }
  10526. return [RAConvertor dict2data:resultDictionary];
  10527. }
  10528. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10529. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10530. }
  10531. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10532. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10533. }
  10534. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10535. int offset = [[params valueForKey:@"offset"] intValue];
  10536. int limit = [[params valueForKey:@"limit"] intValue];
  10537. NSString *keyword = [params valueForKey:@"keyWord"];
  10538. NSString *where = @"where is_delete is null or is_delete = 0";
  10539. if (keyword.length) {
  10540. keyword = [self translateSingleQuote:keyword];
  10541. 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];
  10542. }
  10543. 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
  10544. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10545. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10546. NSString *cachefolder = [paths objectAtIndex:0];
  10547. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10548. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10549. NSString *name = [self textAtColumn:0 statement:stmt];
  10550. NSString *note = [self textAtColumn:1 statement:stmt];
  10551. NSString *time = [self textAtColumn:2 statement:stmt];
  10552. NSString *user = [self textAtColumn:3 statement:stmt];
  10553. NSString *path = [self textAtColumn:4 statement:stmt];
  10554. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10555. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10556. path = [pdfFolder stringByAppendingPathComponent:path];
  10557. BOOL bdir=NO;
  10558. NSFileManager* fileManager = [NSFileManager defaultManager];
  10559. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10560. {
  10561. //pdf文件不存在
  10562. path=nil;
  10563. }
  10564. time = [self changeDateTimeFormate:time];
  10565. time = [time stringByAppendingString:@" PST"];
  10566. int sheet_id = sqlite3_column_int(stmt, 5);
  10567. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10568. item[@"tearsheetsId"]=@(sheet_id);
  10569. item[@"pdf_path"]=path;
  10570. item[@"create_time"]=time;
  10571. item[@"create_user"]=user;
  10572. item[@"tear_note"]=note;
  10573. item[@"tear_name"]=name;
  10574. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10575. item[@"model_info"]=model_info;
  10576. item[@"off_params"]=off_params;
  10577. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10578. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10579. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10580. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10581. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10582. }];
  10583. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10584. return [RAConvertor dict2data:dic];
  10585. }
  10586. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10587. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10588. // NSString *user = [params objectForKey:@"user"];
  10589. // 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];
  10590. 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];
  10591. int result = [iSalesDB execSql:sql];
  10592. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10593. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10594. if (result == RESULT_TRUE) {
  10595. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10596. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10597. sqlite3 *db = [iSalesDB get_db];
  10598. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10599. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10600. [iSalesDB close_db:db];
  10601. }
  10602. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10603. return [RAConvertor dict2data:dic];
  10604. }
  10605. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10606. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10607. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10608. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10609. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10610. NSDictionary *company_item = @{
  10611. @"control": @"text",
  10612. @"keyboard": @"text",
  10613. @"name": @"company_name",
  10614. @"value": COMPANY_FULL_NAME,
  10615. @"aname": @"Company Name"
  10616. };
  10617. [section_0 setObject:company_item forKey:@"item_0"];
  10618. [dic setObject:section_0 forKey:@"section_0"];
  10619. // Regurlar Price
  10620. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10621. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10622. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10623. __block long val_count = 0;
  10624. NSString *sql = @"select name,type,order_by from price order by order_by";
  10625. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10626. NSString *name = [self textAtColumn:0 statement:stmt];
  10627. int type = sqlite3_column_int(stmt, 1);
  10628. int order_by = sqlite3_column_int(stmt, 2);
  10629. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10630. NSDictionary *price_dic = @{
  10631. @"value" : name,
  10632. @"value_id" : [NSNumber numberWithInteger:type],
  10633. @"check" : order_by == 0 ? @(1) : @(0)
  10634. };
  10635. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10636. val_count = ++(*count);
  10637. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10638. val_count = 0;
  10639. }];
  10640. [cadedate setObject:@{@"value" : @"None",
  10641. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10642. val_count++;
  10643. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10644. [price setObject:cadedate forKey:@"cadedate"];
  10645. [section1 setObject:price forKey:@"item_2"];
  10646. [dic setObject:section1 forKey:@"section_1"];
  10647. return [RAConvertor dict2data:dic];
  10648. }
  10649. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10650. NSString *product_id = [params objectForKey:@"fashionId"];
  10651. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10652. __block int result = RESULT_TRUE;
  10653. __block int qty = 0;
  10654. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10655. qty = sqlite3_column_int(stmt, 0);
  10656. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10657. result = RESULT_FALSE;
  10658. }];
  10659. NSMutableDictionary *dic = @{
  10660. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10661. @"mode" : @"Regular Mode",
  10662. @"quantity_available" : @(qty),
  10663. @"result" : @(result),
  10664. }.mutableCopy;
  10665. return [RAConvertor dict2data:dic];
  10666. }
  10667. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10668. NSString *item_ids = [params objectForKey:@"item_id"];
  10669. NSString *line_notes = [params objectForKey:@"notes"];
  10670. NSString *price_str = [params objectForKey:@"price"];
  10671. NSString *discount_str = [params objectForKey:@"discount"];
  10672. NSString *percent = [params objectForKey:@"available_percent"];
  10673. NSString *qty = [params objectForKey:@"available_qty"];
  10674. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10675. int dot = 0;
  10676. if (line_notes) {
  10677. line_notes = [self translateSingleQuote:line_notes];
  10678. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10679. sql = [sql stringByAppendingString:line_notes];
  10680. dot = 1;
  10681. } else {
  10682. line_notes = @"";
  10683. }
  10684. if (price_str) {
  10685. if (dot) {
  10686. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10687. } else {
  10688. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10689. dot = 1;
  10690. }
  10691. sql = [sql stringByAppendingString:price_str];
  10692. } else {
  10693. price_str = @"";
  10694. }
  10695. if (discount_str) {
  10696. if (dot) {
  10697. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10698. } else {
  10699. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10700. dot = 1;
  10701. }
  10702. sql = [sql stringByAppendingString:discount_str];
  10703. } else {
  10704. discount_str = @"";
  10705. }
  10706. if (percent) {
  10707. if (dot) {
  10708. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10709. } else {
  10710. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10711. dot = 1;
  10712. }
  10713. sql = [sql stringByAppendingString:percent];
  10714. } else {
  10715. percent = @"";
  10716. }
  10717. if (qty) {
  10718. if (dot) {
  10719. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10720. } else {
  10721. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10722. dot = 1;
  10723. }
  10724. sql = [sql stringByAppendingString:qty];
  10725. } else {
  10726. qty = @"";
  10727. }
  10728. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10729. sql = [sql stringByAppendingString:where];
  10730. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10731. int result = [iSalesDB execSql:sql];
  10732. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10733. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10734. return [RAConvertor dict2data:dic];
  10735. }
  10736. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10737. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10738. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10739. sqlite3 *db = [iSalesDB get_db];
  10740. 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];
  10741. int result = [iSalesDB execSql:sql db:db];
  10742. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10743. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10744. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10745. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10746. [iSalesDB close_db:db];
  10747. return [RAConvertor dict2data:dic];
  10748. }
  10749. + (void)offline_removePDFWithName:(NSString *)name {
  10750. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10751. NSString *cachefolder = [paths objectAtIndex:0];
  10752. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10753. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10754. NSFileManager *fileManager = [NSFileManager defaultManager];
  10755. [fileManager removeItemAtPath:path error:nil];
  10756. }
  10757. + (void)offline_clear_PDFCache {
  10758. NSFileManager *fileManager = [NSFileManager defaultManager];
  10759. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10760. NSString *cachefolder = [paths objectAtIndex:0];
  10761. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10762. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10763. for (NSString *path in pdf_files) {
  10764. [self offline_removePDFWithName:[path lastPathComponent]];
  10765. }
  10766. }
  10767. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10768. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10769. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10770. NSString *user = [params objectForKey:@"user"];
  10771. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10772. if (![create_user isEqualToString:user]) {
  10773. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10774. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10775. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10776. return [RAConvertor dict2data:dic];
  10777. }
  10778. 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]];
  10779. __block int is_local = 0;
  10780. __block NSString *path = @"";
  10781. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10782. is_local = sqlite3_column_int(stmt, 0);
  10783. path = [self textAtColumn:1 statement:stmt];
  10784. }];
  10785. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10786. if (is_local == 1) {
  10787. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10788. // 删除文件
  10789. [self offline_removePDFWithName:path];
  10790. }
  10791. int result = [iSalesDB execSql:sql];
  10792. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10793. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10794. return [RAConvertor dict2data:dic];
  10795. }
  10796. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10797. {
  10798. // assert(add_params[@"contact_id"]!=nil);
  10799. assert(add_params[@"user"]!=nil);
  10800. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10801. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10802. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10803. NSString* where=@"1=1";
  10804. if (ver!=nil) {
  10805. where=@"is_dirty=1";
  10806. }
  10807. 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];
  10808. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10809. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10810. sqlite3 *db = [iSalesDB get_db];
  10811. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10812. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10813. NSInteger _id = sqlite3_column_int(stmt, 0);
  10814. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10815. NSString *name = [self textAtColumn:2 statement:stmt];
  10816. NSString *desc = [self textAtColumn:3 statement:stmt];
  10817. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10818. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10819. NSInteger qty = sqlite3_column_int(stmt, 6);
  10820. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10821. double percent = sqlite3_column_double(stmt, 8);
  10822. double price = sqlite3_column_double(stmt, 9);
  10823. double discount = sqlite3_column_double(stmt, 10);
  10824. NSString *img = [self textAtColumn:11 statement:stmt];
  10825. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10826. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10827. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10828. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10829. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10830. if ([price_null isEqualToString:@"null"]) {
  10831. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10832. }
  10833. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10834. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10835. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10836. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10837. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10838. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10839. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10840. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10841. if ([qty_null isEqualToString:@"null"]) {
  10842. // [item setValue:@"null" forKey:@"available_qty"];
  10843. } else {
  10844. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10845. }
  10846. if ([is_percent_null isEqualToString:@"null"]) {
  10847. // [item setValue:@"null" forKey:@"percentage"];
  10848. } else {
  10849. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10850. }
  10851. if ([percent_null isEqualToString:@"null"]) {
  10852. // [item setValue:@"null" forKey:@"percent"];
  10853. } else {
  10854. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10855. }
  10856. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10857. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10858. [item setValue:name forKey:@"name"];
  10859. [item setValue:desc forKey:@"description"];
  10860. [item setValue:img forKey:@"img"];
  10861. [item setValue:line_note forKey:@"line_note"];
  10862. [item setValue:create_time forKey:@"createtime"];
  10863. [item setValue:modify_time forKey:@"modifytime"];
  10864. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10865. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10866. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10867. }];
  10868. [iSalesDB close_db:db];
  10869. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10870. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10871. }
  10872. return ret;
  10873. }
  10874. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10875. {
  10876. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10877. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10878. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10879. NSString* where=@"1=1";
  10880. if (ver!=nil) {
  10881. where=@"is_dirty=1";
  10882. }
  10883. 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];
  10884. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10885. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10886. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10887. NSInteger _id = sqlite3_column_int(stmt, 0);
  10888. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10889. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10890. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10891. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10892. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10893. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10894. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10895. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10896. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10897. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10898. NSString *uuid = [NSUUID UUID].UUIDString;
  10899. int is_delete = sqlite3_column_int(stmt, 11);
  10900. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10901. int is_local = sqlite3_column_int(stmt, 12);
  10902. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10903. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10904. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10905. }
  10906. [item setObject:pdf_path forKey:@"pdf_path"];
  10907. [item setObject:create_user forKey:@"create_user"];
  10908. [item setObject:tear_note forKey:@"tear_note"];
  10909. [item setObject:tear_name forKey:@"tear_name"];
  10910. [item setObject:model_info forKey:@"model_info"];
  10911. [item setObject:createtime forKey:@"createtime"];
  10912. [item setObject:modifytime forKey:@"modifytime"];
  10913. [item setObject:urlParams forKey:@"urlParams"];
  10914. [item setObject:off_params forKey:@"off_params"];
  10915. [item setObject:uuid forKey:@"pdf_token"];
  10916. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10917. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10918. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10919. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10920. } else {
  10921. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10922. }
  10923. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10924. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10925. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10926. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10927. }];
  10928. return ret;
  10929. }
  10930. #pragma mark 2020
  10931. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10932. {
  10933. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10934. sqlite3 *db = [iSalesDB get_db];
  10935. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10936. if (contactType) {
  10937. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10938. } else {
  10939. contactType = @"1 = 1";
  10940. }
  10941. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10942. DebugLog(@"offline contact list keyword: %@",keyword);
  10943. // advanced search
  10944. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10945. if (contact_name) {
  10946. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10947. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10948. } else {
  10949. contact_name = @"";
  10950. }
  10951. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10952. if (customer_phone) {
  10953. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10954. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10955. } else {
  10956. customer_phone = @"";
  10957. }
  10958. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10959. if (customer_fax) {
  10960. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10961. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10962. } else {
  10963. customer_fax = @"";
  10964. }
  10965. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10966. if (customer_zipcode) {
  10967. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10968. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10969. } else {
  10970. customer_zipcode = @"";
  10971. }
  10972. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10973. if (customer_sales_rep) {
  10974. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10975. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10976. } else {
  10977. customer_sales_rep = @"";
  10978. }
  10979. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10980. if (customer_state) {
  10981. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10982. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10983. } else {
  10984. customer_state = @"";
  10985. }
  10986. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10987. if (customer_name) {
  10988. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10989. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10990. } else {
  10991. customer_name = @"";
  10992. }
  10993. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10994. if (customer_country) {
  10995. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10996. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10997. } else {
  10998. customer_country = @"";
  10999. }
  11000. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11001. if (customer_cid) {
  11002. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11003. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11004. } else {
  11005. customer_cid = @"";
  11006. }
  11007. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11008. if (customer_city) {
  11009. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11010. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11011. } else {
  11012. customer_city = @"";
  11013. }
  11014. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11015. if (customer_address) {
  11016. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11017. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11018. } else {
  11019. customer_address = @"";
  11020. }
  11021. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11022. if (customer_email) {
  11023. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11024. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11025. } else {
  11026. customer_email = @"";
  11027. }
  11028. NSString *price_name = [params valueForKey:@"price_name"];
  11029. if (price_name) {
  11030. if ([price_name containsString:@","]) {
  11031. // 首先从 price表中查处name
  11032. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11033. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11034. for (int i = 1;i < pArray.count;i++) {
  11035. NSString *p = pArray[i];
  11036. [mutablePStr appendFormat:@" or type = %@ ",p];
  11037. }
  11038. [mutablePStr appendString:@";"];
  11039. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11040. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11041. char *name = (char *)sqlite3_column_text(stmt, 0);
  11042. if (!name)
  11043. name = "";
  11044. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11045. }];
  11046. // 再根据name 拼sql
  11047. NSMutableString *mutable_price_name = [NSMutableString string];
  11048. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11049. for (int i = 1; i < price_name_array.count; i++) {
  11050. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11051. }
  11052. [mutable_price_name appendString:@")"];
  11053. price_name = mutable_price_name;
  11054. } else {
  11055. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11056. if ([price_name isEqualToString:@""]) {
  11057. price_name = @"";
  11058. } else {
  11059. __block NSString *price;
  11060. price_name = [self translateSingleQuote:price_name];
  11061. [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) {
  11062. char *p = (char *)sqlite3_column_text(stmt, 0);
  11063. if (p == NULL) {
  11064. p = "";
  11065. }
  11066. price = [NSString stringWithUTF8String:p];
  11067. }];
  11068. if ([price isEqualToString:@""]) {
  11069. price_name = @"";
  11070. } else {
  11071. price = [self translateSingleQuote:price];
  11072. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11073. }
  11074. }
  11075. }
  11076. } else {
  11077. price_name = @"";
  11078. }
  11079. int limit = [[params valueForKey:@"limit"] intValue];
  11080. int offset = [[params valueForKey:@"offset"] intValue];
  11081. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11082. 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];
  11083. 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];
  11084. // int result= [iSalesDB AddExFunction:db];
  11085. int count =0;
  11086. NSString *sqlQuery = nil;
  11087. if(keyword.length==0)
  11088. {
  11089. // 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];
  11090. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11091. sqlQuery = sql;
  11092. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11093. }
  11094. else
  11095. {
  11096. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11097. keyword = keyword.lowercaseString;
  11098. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11099. 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];
  11100. 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]];
  11101. }
  11102. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11103. sqlite3_stmt * statement;
  11104. [ret setValue:@"2" forKey:@"result"];
  11105. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11106. // 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";
  11107. int i = 0;
  11108. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11109. {
  11110. while (sqlite3_step(statement) == SQLITE_ROW)
  11111. {
  11112. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11113. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11114. int editable = sqlite3_column_int(statement, 0);
  11115. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11116. NSString *nscompany_name =nil;
  11117. if(company_name==nil)
  11118. nscompany_name=@"";
  11119. else
  11120. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11121. char *country = (char*)sqlite3_column_text(statement, 2);
  11122. if(country==nil)
  11123. country="";
  11124. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11125. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11126. // if(addr==nil)
  11127. // addr="";
  11128. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11129. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11130. if(zipcode==nil)
  11131. zipcode="";
  11132. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11133. char *state = (char*)sqlite3_column_text(statement, 5);
  11134. if(state==nil)
  11135. state="";
  11136. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11137. char *city = (char*)sqlite3_column_text(statement, 6);
  11138. if(city==nil)
  11139. city="";
  11140. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11141. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11142. // NSString *nscontact_name = nil;
  11143. // if(contact_name==nil)
  11144. // nscontact_name=@"";
  11145. // else
  11146. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11147. char *phone = (char*)sqlite3_column_text(statement, 8);
  11148. NSString *nsphone = nil;
  11149. if(phone==nil)
  11150. nsphone=@"";
  11151. else
  11152. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11153. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11154. if(contact_id==nil)
  11155. contact_id="";
  11156. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11157. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11158. if(addr_1==nil)
  11159. addr_1="";
  11160. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11161. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11162. if(addr_2==nil)
  11163. addr_2="";
  11164. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11165. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11166. if(addr_3==nil)
  11167. addr_3="";
  11168. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11169. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11170. if(addr_4==nil)
  11171. addr_4="";
  11172. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11173. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11174. if(first_name==nil)
  11175. first_name="";
  11176. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11177. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11178. if(last_name==nil)
  11179. last_name="";
  11180. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11181. char *fax = (char*)sqlite3_column_text(statement, 16);
  11182. NSString *nsfax = nil;
  11183. if(fax==nil)
  11184. nsfax=@"";
  11185. else
  11186. {
  11187. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11188. if(nsfax.length>0)
  11189. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11190. }
  11191. char *email = (char*)sqlite3_column_text(statement, 17);
  11192. NSString *nsemail = nil;
  11193. if(email==nil)
  11194. nsemail=@"";
  11195. else
  11196. {
  11197. nsemail= [[NSString alloc]initWithUTF8String:email];
  11198. if(nsemail.length>0)
  11199. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11200. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11201. }
  11202. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11203. [arr_name addObject:nsfirst_name];
  11204. [arr_name addObject:nslast_name];
  11205. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11206. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11207. {
  11208. // decrypt
  11209. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11210. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11211. nsphone=[AESCrypt fastdecrypt:nsphone];
  11212. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11213. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11214. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11215. }
  11216. [arr_addr addObject:nscompany_name];
  11217. [arr_addr addObject:nscontact_name];
  11218. [arr_addr addObject:@"<br>"];
  11219. [arr_addr addObject:nsaddr_1];
  11220. [arr_addr addObject:nsaddr_2];
  11221. [arr_addr addObject:nsaddr_3];
  11222. [arr_addr addObject:nsaddr_4];
  11223. //[arr_addr addObject:nsaddr];
  11224. [arr_addr addObject:nszipcode];
  11225. [arr_addr addObject:nscity];
  11226. [arr_addr addObject:nsstate];
  11227. [arr_addr addObject:nscountry];
  11228. [arr_addr addObject:@"<br>"];
  11229. [arr_addr addObject:nsphone];
  11230. [arr_addr addObject:nsfax];
  11231. [arr_addr addObject:nsemail];
  11232. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11233. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11234. [item setValue:name forKey:@"name"];
  11235. [item setValue:nscontact_id forKey:@"contact_id"];
  11236. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11237. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11238. i++;
  11239. }
  11240. sqlite3_finalize(statement);
  11241. }
  11242. [iSalesDB close_db:db];
  11243. #ifdef DEBUG
  11244. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11245. #endif
  11246. dispatch_async(dispatch_get_main_queue(), ^{
  11247. UIApplication * app = [UIApplication sharedApplication];
  11248. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11249. [ret setValue:appDelegate.mode forKey:@"mode"];
  11250. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11251. result(ret);
  11252. });
  11253. return ;
  11254. });
  11255. }
  11256. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11257. {
  11258. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11259. NSString* order_code= appDelegate.order_code;
  11260. NSString* user = RASingleton.sharedInstance.user;
  11261. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11262. NSString* category = [params valueForKey:@"category"];
  11263. if (!category || [category isEqualToString:@""]) {
  11264. category = @"%";
  11265. }
  11266. category = [self translateSingleQuote:category];
  11267. int limit = [[params valueForKey:@"limit"] intValue];
  11268. int offset = [[params valueForKey:@"offset"] intValue];
  11269. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11270. NSString *limit_str = @"";
  11271. if (limited) {
  11272. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11273. }
  11274. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11275. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11276. sqlite3 *db = [iSalesDB get_db];
  11277. // [iSalesDB AddExFunction:db];
  11278. int count;
  11279. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11280. 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];
  11281. double price_min = 0;
  11282. double price_max = 0;
  11283. if ([params.allKeys containsObject:@"alert"]) {
  11284. // alert
  11285. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11286. NSString *alert = params[@"alert"];
  11287. if ([alert isEqualToString:@"Display All"]) {
  11288. alert = [NSString stringWithFormat:@""];
  11289. } else {
  11290. alert = [self translateSingleQuote:alert];
  11291. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11292. }
  11293. // available
  11294. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11295. NSString *available = params[@"available"];
  11296. NSString *available_condition;
  11297. if ([available isEqualToString:@"Display All"]) {
  11298. available_condition = @"";
  11299. } else if ([available isEqualToString:@"Available Now"]) {
  11300. available_condition = @"and availability > 0";
  11301. } else {
  11302. available_condition = @"and availability == 0";
  11303. }
  11304. // best seller
  11305. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11306. NSString *best_seller = @"";
  11307. NSString *order_best_seller = @"m.name asc";
  11308. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11309. best_seller = @"and best_seller > 0";
  11310. order_best_seller = @"m.best_seller desc,m.name asc";
  11311. }
  11312. // price
  11313. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11314. NSString *price = params[@"price"];
  11315. price_min = 0;
  11316. price_max = MAXFLOAT;
  11317. if (user && price != nil) {
  11318. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11319. NSMutableString *priceName = [NSMutableString string];
  11320. for (int i = 0; i < priceTypeArray.count; i++) {
  11321. NSString *pricetype = priceTypeArray[i];
  11322. pricetype = [self translateSingleQuote:pricetype];
  11323. if (i == 0) {
  11324. [priceName appendFormat:@"'%@'",pricetype];
  11325. } else {
  11326. [priceName appendFormat:@",'%@'",pricetype];
  11327. }
  11328. }
  11329. if ([price isEqualToString:@"Display All"]) {
  11330. price = [NSString stringWithFormat:@""];
  11331. } else if([price containsString:@"+"]){
  11332. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11333. price_min = [price doubleValue];
  11334. 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];
  11335. } else {
  11336. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11337. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11338. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11339. 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];
  11340. }
  11341. } else {
  11342. price = @"";
  11343. }
  11344. // sold_by_qty : Sold in quantities of %@
  11345. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11346. NSString *qty = params[@"sold_by_qty"];
  11347. if ([qty isEqualToString:@"Display All"]) {
  11348. qty = @"";
  11349. } else {
  11350. qty = [self translateSingleQuote:qty];
  11351. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11352. }
  11353. // cate
  11354. // category = [self translateSingleQuote:category];
  11355. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11356. // cate mutiple selection
  11357. NSString *category_id = params[@"category"];
  11358. NSMutableArray *cate_id_array = nil;
  11359. NSMutableString *cateWhere = [NSMutableString string];
  11360. if ([category_id isEqualToString:@""] || !category_id) {
  11361. [cateWhere appendString:@"1 = 1"];
  11362. } else {
  11363. if ([category_id containsString:@","]) {
  11364. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11365. } else {
  11366. cate_id_array = [@[category_id] mutableCopy];
  11367. }
  11368. [cateWhere appendString:@"("];
  11369. for (int i = 0; i < cate_id_array.count; i++) {
  11370. if (i == 0) {
  11371. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11372. } else {
  11373. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11374. }
  11375. }
  11376. [cateWhere appendString:@")"];
  11377. }
  11378. // where bestseller > 0 order by bestseller desc
  11379. // sql query: alert availability(int) best_seller(int) price qty
  11380. 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];
  11381. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11382. }
  11383. DebugLog(@"offline category where: %@",where);
  11384. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11385. if (!user) {
  11386. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11387. }
  11388. [ret setValue:filter forKey:@"filter"];
  11389. DebugLog(@"offline_category sql:%@",sqlQuery);
  11390. sqlite3_stmt * statement;
  11391. [ret setValue:@"2" forKey:@"result"];
  11392. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11393. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11394. // int count=0;
  11395. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11396. {
  11397. int i=0;
  11398. while (sqlite3_step(statement) == SQLITE_ROW)
  11399. {
  11400. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11401. char *name = (char*)sqlite3_column_text(statement, 0);
  11402. if(name==nil)
  11403. name="";
  11404. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11405. char *description = (char*)sqlite3_column_text(statement, 1);
  11406. if(description==nil)
  11407. description="";
  11408. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11409. int product_id = sqlite3_column_int(statement, 2);
  11410. int wid = sqlite3_column_int(statement, 3);
  11411. int closeout = sqlite3_column_int(statement, 4);
  11412. int cid = sqlite3_column_int(statement, 5);
  11413. int wisdelete = sqlite3_column_int(statement, 6);
  11414. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11415. int more_color = sqlite3_column_int(statement, 8);
  11416. // Defaul Category ID
  11417. __block NSString *categoryID = nil;
  11418. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11419. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11420. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11421. if(default_category==nil)
  11422. default_category="";
  11423. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11424. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11425. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11426. categoryID = nsdefault_category;
  11427. }];
  11428. if (!categoryID.length) {
  11429. NSString *cateIDs = params[@"category"];
  11430. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11431. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11432. for (NSString *cateID in requestCategoryArr) {
  11433. BOOL needBreak = NO;
  11434. for (NSString *itemCateIDBox in itemCategoryArr) {
  11435. if (itemCateIDBox.length > 4) {
  11436. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11437. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11438. if ([itemCategoryID isEqualToString:cateID]) {
  11439. needBreak = YES;
  11440. categoryID = itemCategoryID;
  11441. break;
  11442. }
  11443. }
  11444. }
  11445. if (needBreak) {
  11446. break;
  11447. }
  11448. }
  11449. }
  11450. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11451. if(wid !=0 && wisdelete != 1)
  11452. [item setValue:@"true" forKey:@"wish_exists"];
  11453. else
  11454. [item setValue:@"false" forKey:@"wish_exists"];
  11455. if(closeout==0)
  11456. [item setValue:@"false" forKey:@"is_closeout"];
  11457. else
  11458. [item setValue:@"true" forKey:@"is_closeout"];
  11459. if(cid==0)
  11460. [item setValue:@"false" forKey:@"cart_exists"];
  11461. else
  11462. [item setValue:@"true" forKey:@"cart_exists"];
  11463. if (more_color == 0) {
  11464. [item setObject:@(false) forKey:@"more_color"];
  11465. } else if (more_color == 1) {
  11466. [item setObject:@(true) forKey:@"more_color"];
  11467. }
  11468. [item addEntriesFromDictionary:imgjson];
  11469. // [item setValue:nsurl forKey:@"img"];
  11470. [item setValue:nsname forKey:@"name"];
  11471. [item setValue:nsdescription forKey:@"description"];
  11472. if (categoryID) {
  11473. [item setValue:categoryID forKey:@"item_category_id"];
  11474. }
  11475. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11476. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11477. i++;
  11478. }
  11479. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11480. [ret setObject:items forKey:@"items"];
  11481. sqlite3_finalize(statement);
  11482. } else {
  11483. DebugLog(@"nothing...");
  11484. }
  11485. DebugLog(@"count:%d",count);
  11486. [iSalesDB close_db:db];
  11487. #ifdef DEBUG
  11488. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11489. #endif
  11490. dispatch_async(dispatch_get_main_queue(), ^{
  11491. // UIApplication * app = [UIApplication sharedApplication];
  11492. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11493. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11494. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11495. result(ret);
  11496. });
  11497. });
  11498. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11499. }
  11500. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11501. {
  11502. [self categoryList:params limited:YES completionHandler:result];
  11503. }
  11504. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11505. {
  11506. UIApplication * app = [UIApplication sharedApplication];
  11507. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11508. NSString* order_code = appDelegate.order_code;
  11509. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11510. NSString* keyword = [params valueForKey:@"keyword"];
  11511. keyword=keyword.lowercaseString;
  11512. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11513. int limit = [[params valueForKey:@"limit"] intValue];
  11514. int offset = [[params valueForKey:@"offset"] intValue];
  11515. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11516. NSString *limit_str = @"";
  11517. if (limited) {
  11518. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11519. }
  11520. sqlite3 *db = [iSalesDB get_db];
  11521. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11522. NSString *sqlQuery = nil;
  11523. if(exactMatch )
  11524. 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 ;
  11525. else
  11526. 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
  11527. DebugLog(@"offline_search sql:%@",sqlQuery);
  11528. sqlite3_stmt * statement;
  11529. [ret setValue:@"2" forKey:@"result"];
  11530. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11531. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11532. // int count=0;
  11533. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11534. {
  11535. int i=0;
  11536. while (sqlite3_step(statement) == SQLITE_ROW)
  11537. {
  11538. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11539. // char *name = (char*)sqlite3_column_text(statement, 1);
  11540. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11541. char *name = (char*)sqlite3_column_text(statement, 0);
  11542. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11543. char *description = (char*)sqlite3_column_text(statement, 1);
  11544. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11545. int product_id = sqlite3_column_int(statement, 2);
  11546. // char *url = (char*)sqlite3_column_text(statement, 3);
  11547. // if(url==nil)
  11548. // url="";
  11549. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11550. int wid = sqlite3_column_int(statement, 3);
  11551. int closeout = sqlite3_column_int(statement, 4);
  11552. int cid = sqlite3_column_int(statement, 5);
  11553. int wisdelete = sqlite3_column_int(statement, 6);
  11554. int more_color = sqlite3_column_int(statement, 7);
  11555. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11556. if(wid !=0 && wisdelete != 1)
  11557. [item setValue:@"true" forKey:@"wish_exists"];
  11558. else
  11559. [item setValue:@"false" forKey:@"wish_exists"];
  11560. if(closeout==0)
  11561. [item setValue:@"false" forKey:@"is_closeout"];
  11562. else
  11563. [item setValue:@"true" forKey:@"is_closeout"];
  11564. if(cid==0)
  11565. [item setValue:@"false" forKey:@"cart_exists"];
  11566. else
  11567. [item setValue:@"true" forKey:@"cart_exists"];
  11568. if (more_color == 0) {
  11569. [item setObject:@(false) forKey:@"more_color"];
  11570. } else if (more_color == 1) {
  11571. [item setObject:@(true) forKey:@"more_color"];
  11572. }
  11573. [item addEntriesFromDictionary:imgjson];
  11574. // [item setValue:nsurl forKey:@"img"];
  11575. [item setValue:nsname forKey:@"fash_name"];
  11576. [item setValue:nsdescription forKey:@"description"];
  11577. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11578. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11579. i++;
  11580. }
  11581. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11582. [ret setObject:items forKey:@"items"];
  11583. sqlite3_finalize(statement);
  11584. }
  11585. DebugLog(@"count:%d",count);
  11586. [iSalesDB close_db:db];
  11587. #ifdef DEBUG
  11588. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11589. #endif
  11590. dispatch_async(dispatch_get_main_queue(), ^{
  11591. result(ret);
  11592. });
  11593. });
  11594. }
  11595. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11596. {
  11597. [self search:params limited:YES completionHandler:result];
  11598. }
  11599. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11600. {
  11601. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11602. NSString* offline_command=params[@"offline_Command"];
  11603. NSMutableDictionary* ret=nil;
  11604. if([offline_command isEqualToString:@"model_NIYMAL"])
  11605. {
  11606. NSString* category = params[@"category"];
  11607. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11608. }
  11609. dispatch_async(dispatch_get_main_queue(), ^{
  11610. result(ret);
  11611. });
  11612. });
  11613. }
  11614. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11615. {
  11616. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11617. int sort = [[params objectForKey:@"sort"] intValue];
  11618. NSString *sort_str = @"";
  11619. switch (sort) {
  11620. case 0:{
  11621. sort_str = @"order by w.modify_time desc";
  11622. }
  11623. break;
  11624. case 1:{
  11625. sort_str = @"order by w.modify_time asc";
  11626. }
  11627. break;
  11628. case 2:{
  11629. sort_str = @"order by m.name asc";
  11630. }
  11631. break;
  11632. case 3:{
  11633. sort_str = @"order by m.name desc";
  11634. }
  11635. break;
  11636. case 4:{
  11637. sort_str = @"order by m.description asc";
  11638. }
  11639. break;
  11640. default:
  11641. break;
  11642. }
  11643. // NSString* user = RASingleton.sharedInstance.user;
  11644. sqlite3 *db = [iSalesDB get_db];
  11645. // order by w.create_time
  11646. 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];
  11647. // 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];
  11648. sqlite3_stmt * statement;
  11649. NSDate *date1 = [NSDate date];
  11650. // NSDate *date2 = nil;
  11651. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11652. int count=0;
  11653. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11654. {
  11655. while (sqlite3_step(statement) == SQLITE_ROW)
  11656. {
  11657. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11658. int product_id = sqlite3_column_double(statement, 0);
  11659. char *description = (char*)sqlite3_column_text(statement, 1);
  11660. if(description==nil)
  11661. description= "";
  11662. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11663. int item_id = sqlite3_column_double(statement, 2);
  11664. NSDate *date_image = [NSDate date];
  11665. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11666. printf("image : ");
  11667. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11668. // char *url = (char*)sqlite3_column_text(statement, 3);
  11669. // if(url==nil)
  11670. // url="";
  11671. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11672. int qty = sqlite3_column_int(statement, 3);
  11673. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11674. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11675. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11676. item[@"description"]= nsdescription;
  11677. item[@"img"]= nsurl;
  11678. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11679. count++;
  11680. }
  11681. printf("total time:");
  11682. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11683. ret[@"count"]= [NSNumber numberWithInt:count];
  11684. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11685. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11686. ret[@"result"]= [NSNumber numberWithInt:2];
  11687. sqlite3_finalize(statement);
  11688. }
  11689. [iSalesDB close_db:db];
  11690. dispatch_async(dispatch_get_main_queue(), ^{
  11691. UIApplication * app = [UIApplication sharedApplication];
  11692. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11693. appDelegate.wish_count =count;
  11694. [appDelegate update_count_mark];
  11695. result(ret);
  11696. });
  11697. });
  11698. }
  11699. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11700. {
  11701. // UIApplication * app = [UIApplication sharedApplication];
  11702. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11703. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11704. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11705. sqlite3 *db = [iSalesDB get_db];
  11706. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11707. NSString* product_id=params[@"product_id"];
  11708. NSString *item_count_str = params[@"item_count"];
  11709. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11710. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11711. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11712. // NSString *sql = @"";
  11713. for(int i=0;i<arr.count;i++)
  11714. {
  11715. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11716. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11717. __block int cart_count = 0;
  11718. if (!item_count_str) {
  11719. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11720. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11721. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11722. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11723. cart_count = [[model_set_components lastObject] intValue];
  11724. }];
  11725. }
  11726. if(count==0)
  11727. {
  11728. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11729. sqlite3_stmt *stmt;
  11730. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11731. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11732. if (item_count_arr) {
  11733. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11734. } else {
  11735. sqlite3_bind_int(stmt,2,cart_count);
  11736. }
  11737. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11738. [iSalesDB execSql:@"ROLLBACK" db:db];
  11739. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11740. [iSalesDB close_db:db];
  11741. DebugLog(@"add to wishlist error");
  11742. dispatch_async(dispatch_get_main_queue(), ^{
  11743. result(ret);
  11744. });
  11745. }
  11746. } else {
  11747. int qty = 0;
  11748. if (item_count_arr) {
  11749. qty = [item_count_arr[i] intValue];
  11750. } else {
  11751. qty = cart_count;
  11752. }
  11753. 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]];
  11754. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11755. [iSalesDB execSql:@"ROLLBACK" db:db];
  11756. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11757. [iSalesDB close_db:db];
  11758. DebugLog(@"add to wishlist error");
  11759. dispatch_async(dispatch_get_main_queue(), ^{
  11760. result(ret);
  11761. });
  11762. }
  11763. }
  11764. }
  11765. // [iSalesDB execSql:sql db:db];
  11766. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11767. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11768. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11769. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11770. [iSalesDB close_db:db];
  11771. ret[@"result"]= [NSNumber numberWithInt:2];
  11772. dispatch_async(dispatch_get_main_queue(), ^{
  11773. UIApplication * app = [UIApplication sharedApplication];
  11774. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11775. appDelegate.wish_count =count;
  11776. [appDelegate update_count_mark];
  11777. result(ret);
  11778. });
  11779. });
  11780. }
  11781. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11782. {
  11783. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11784. bool bLogin = appDelegate.bLogin;
  11785. __block NSString* contact_id = appDelegate.contact_id;
  11786. __block NSString* user = RASingleton.sharedInstance.user;
  11787. __block NSString* order_code = appDelegate.order_code;
  11788. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11789. NSString* model_name = [params valueForKey:@"product_name"];
  11790. NSString* product_id = [params valueForKey:@"product_id"];
  11791. NSString* upc_code = [params valueForKey:@"upc_code"];
  11792. NSString* category = [params valueForKey:@"category"];
  11793. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11794. if(category==nil) {
  11795. category = default_category_id;
  11796. } else {
  11797. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11798. // 参数重有多个category id
  11799. if (arr_0.count > 1) {
  11800. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11801. // 取交集
  11802. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11803. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11804. [set_0 intersectSet:set_1];
  11805. if (set_0.count == 1) {
  11806. category = (NSString *)[set_0 anyObject];
  11807. } else {
  11808. if ([set_0 containsObject:default_category_id]) {
  11809. category = default_category_id;
  11810. } else {
  11811. category = (NSString *)[set_0 anyObject];
  11812. }
  11813. }
  11814. }
  11815. }
  11816. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11817. sqlite3 *db = [iSalesDB get_db];
  11818. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11819. NSString *sqlQuery = nil;
  11820. if(product_id==nil)
  11821. if(model_name==nil)
  11822. {
  11823. 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='%@';
  11824. }
  11825. else
  11826. {
  11827. 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='%@';
  11828. }
  11829. else
  11830. 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=%@;
  11831. sqlite3_stmt * statement;
  11832. // int count=0;
  11833. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11834. {
  11835. if (sqlite3_step(statement) == SQLITE_ROW)
  11836. {
  11837. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11838. [ret setValue:@"2" forKey:@"result"];
  11839. [ret setValue:@"3" forKey:@"detail_section_count"];
  11840. char *name = (char*)sqlite3_column_text(statement, 0);
  11841. if(name==nil)
  11842. name="";
  11843. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11844. char *description = (char*)sqlite3_column_text(statement, 1);
  11845. if(description==nil)
  11846. description="";
  11847. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11848. int product_id = sqlite3_column_int(statement, 2);
  11849. char *color = (char*)sqlite3_column_text(statement, 3);
  11850. if(color==nil)
  11851. color="";
  11852. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11853. //
  11854. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11855. // if(legcolor==nil)
  11856. // legcolor="";
  11857. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11858. //
  11859. //
  11860. int availability = sqlite3_column_int(statement, 5);
  11861. //
  11862. int incoming_stock = sqlite3_column_int(statement, 6);
  11863. char *demension = (char*)sqlite3_column_text(statement, 7);
  11864. if(demension==nil)
  11865. demension="";
  11866. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11867. // ,,,,,,,,,
  11868. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11869. if(seat_height==nil)
  11870. seat_height="";
  11871. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11872. char *material = (char*)sqlite3_column_text(statement, 9);
  11873. if(material==nil)
  11874. material="";
  11875. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11876. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11877. if(box_dim==nil)
  11878. box_dim="";
  11879. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11880. char *volume = (char*)sqlite3_column_text(statement, 11);
  11881. if(volume==nil)
  11882. volume="";
  11883. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11884. double weight = sqlite3_column_double(statement, 12);
  11885. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11886. if(model_set==nil)
  11887. model_set="";
  11888. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11889. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11890. if(load_ability==nil)
  11891. load_ability="";
  11892. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11893. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11894. // if(default_category==nil)
  11895. // default_category="";
  11896. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11897. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11898. if(fabric_content==nil)
  11899. fabric_content="";
  11900. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11901. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11902. if(assembling==nil)
  11903. assembling="";
  11904. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11905. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11906. if(made_in==nil)
  11907. made_in="";
  11908. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11909. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11910. if(special_remarks==nil)
  11911. special_remarks="";
  11912. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11913. int stockUcom = sqlite3_column_double(statement, 20);
  11914. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11915. if(product_group==nil)
  11916. product_group="";
  11917. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11918. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11919. // if(fashion_selector==nil)
  11920. // fashion_selector="";
  11921. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11922. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11923. if(selector_field==nil)
  11924. selector_field="";
  11925. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11926. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11927. if(property_field==nil)
  11928. property_field="";
  11929. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11930. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11931. if(packaging==nil)
  11932. packaging="";
  11933. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11934. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11935. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11936. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11937. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11938. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11939. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11940. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11941. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11942. NSString* Availability=nil;
  11943. if(availability>0)
  11944. Availability=[NSString stringWithFormat:@"%d",availability];
  11945. else
  11946. Availability = @"Out of Stock";
  11947. [img_section setValue:Availability forKey:@"Availability"];
  11948. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11949. char *eta = (char*)sqlite3_column_text(statement, 25);
  11950. if(eta==nil)
  11951. eta="";
  11952. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11953. if ([nseta isEqualToString:@"null"]) {
  11954. nseta = @"";
  11955. }
  11956. if (availability <= 0) {
  11957. [img_section setValue:nseta forKey:@"ETA"];
  11958. }
  11959. int item_id = sqlite3_column_int(statement, 26);
  11960. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11961. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11962. NSString* Price=nil;
  11963. if(bLogin==false)
  11964. Price=@"Must Sign in.";
  11965. else
  11966. {
  11967. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11968. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11969. if(price==nil)
  11970. Price=@"No Price.";
  11971. else
  11972. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11973. }
  11974. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11975. [img_section setValue:Price forKey:@"price"];
  11976. [img_section setValue:nsname forKey:@"model_name"];
  11977. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11978. if (order_code) { // 离线order code即so#
  11979. 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];
  11980. __block int cartQTY = 0;
  11981. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11982. cartQTY = sqlite3_column_int(stmt, 0);
  11983. }];
  11984. if (cartQTY > 0) {
  11985. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11986. }
  11987. }
  11988. [ret setObject:img_section forKey:@"img_section"];
  11989. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11990. int detail0_item_count=0;
  11991. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11992. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11993. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11994. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11995. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11996. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11997. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11998. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11999. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12000. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12001. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12002. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12003. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12004. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12005. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12006. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12007. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12008. {
  12009. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12010. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12011. }
  12012. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12013. [detail0_section setValue:@"kv" forKey:@"type"];
  12014. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12015. [ret setObject:detail0_section forKey:@"detail_0"];
  12016. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12017. // [detail1_section setValue:@"detail" forKey:@"target"];
  12018. // [detail1_section setValue:@"popup" forKey:@"action"];
  12019. // [detail1_section setValue:@"content" forKey:@"type"];
  12020. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12021. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12022. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12023. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12024. [ret setObject:detail1_section forKey:@"detail_1"];
  12025. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12026. [detail2_section setValue:@"detail" forKey:@"target"];
  12027. [detail2_section setValue:@"popup" forKey:@"action"];
  12028. [detail2_section setValue:@"content" forKey:@"type"];
  12029. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12030. [detail2_section setValue:@"true" forKey:@"single_row"];
  12031. [detail2_section setValue:@"local" forKey:@"data"];
  12032. [ret setObject:detail2_section forKey:@"detail_2"];
  12033. }
  12034. else
  12035. {
  12036. [ret setValue:@"8" forKey:@"result"];
  12037. [ret setValue:@"No such model." forKey:@"err_msg"];
  12038. }
  12039. sqlite3_finalize(statement);
  12040. }
  12041. // DebugLog(@"count:%d",count);
  12042. [iSalesDB close_db:db];
  12043. #ifdef DEBUG
  12044. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12045. #endif
  12046. dispatch_async(dispatch_get_main_queue(), ^{
  12047. result(ret);
  12048. });
  12049. });
  12050. }
  12051. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12052. //{
  12053. //
  12054. // UIApplication * app = [UIApplication sharedApplication];
  12055. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12056. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12057. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12058. // sqlite3 *db = [iSalesDB get_db];
  12059. // NSString* collectId=params[@"collectId"];
  12060. //
  12061. //
  12062. //
  12063. //
  12064. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12065. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12066. // [iSalesDB execSql:sqlQuery db:db];
  12067. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12068. // [iSalesDB close_db:db];
  12069. //
  12070. //
  12071. // ret[@"result"]= [NSNumber numberWithInt:2];
  12072. // dispatch_async(dispatch_get_main_queue(), ^{
  12073. //
  12074. // appDelegate.wish_count =count;
  12075. //
  12076. // [appDelegate update_count_mark];
  12077. // result(ret);
  12078. // });
  12079. //
  12080. // });
  12081. //}
  12082. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12083. {
  12084. UIApplication * app = [UIApplication sharedApplication];
  12085. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12086. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12087. [iSalesDB disable_trigger];
  12088. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12089. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12090. [iSalesDB enable_trigger];
  12091. dispatch_async(dispatch_get_main_queue(), ^{
  12092. //
  12093. // NSString* user = [params valueForKey:@"user"];
  12094. //
  12095. // NSString* password = [params valueForKey:@"password"];
  12096. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12097. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12098. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12099. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12100. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12101. [appDelegate update_count_mark];
  12102. appDelegate.can_show_price =false;
  12103. appDelegate.can_see_price =false;
  12104. appDelegate.can_create_portfolio =false;
  12105. appDelegate.can_create_order =false;
  12106. appDelegate.can_cancel_order =false;
  12107. appDelegate.can_set_cart_price =false;
  12108. appDelegate.can_delete_order =false;
  12109. appDelegate.can_submit_order =false;
  12110. appDelegate.can_set_tearsheet_price =false;
  12111. appDelegate.can_update_contact_info = false;
  12112. appDelegate.save_order_logout = false;
  12113. appDelegate.submit_order_logout = false;
  12114. appDelegate.alert_sold_in_quantities = false;
  12115. appDelegate.ipad_perm =nil ;
  12116. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12117. appDelegate.OrderFilter= nil;
  12118. [appDelegate SetSo:nil];
  12119. [appDelegate set_main_button_panel];
  12120. result(ret);
  12121. });
  12122. });
  12123. }
  12124. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12125. {
  12126. // iSalesDB.
  12127. // UIApplication * app = [UIApplication sharedApplication];
  12128. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12129. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12130. [iSalesDB disable_trigger];
  12131. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12132. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12133. [iSalesDB enable_trigger];
  12134. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12135. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12136. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12137. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12138. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12139. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12140. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12141. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12142. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12143. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12144. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12145. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12146. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12147. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12148. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12149. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12150. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12151. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12152. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12153. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12154. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12155. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12156. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12157. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12158. [arr_name addObject:customer_first_name];
  12159. [arr_name addObject:customer_last_name];
  12160. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12161. // default ship from
  12162. 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';";
  12163. __block NSString *cid = @"";
  12164. __block NSString *name = @"";
  12165. __block NSString *ext = @"";
  12166. __block NSString *contact = @"";
  12167. __block NSString *email = @"";
  12168. __block NSString *fax = @"";
  12169. __block NSString *phone = @"";
  12170. sqlite3 *db = [iSalesDB get_db];
  12171. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12172. cid = [self textAtColumn:0 statement:statment];
  12173. name = [self textAtColumn:1 statement:statment];
  12174. ext = [self textAtColumn:2 statement:statment];
  12175. contact = [self textAtColumn:3 statement:statment];
  12176. email = [self textAtColumn:4 statement:statment];
  12177. fax = [self textAtColumn:5 statement:statment];
  12178. phone = [self textAtColumn:6 statement:statment];
  12179. }];
  12180. // default carrier
  12181. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12182. __block NSString *carrier = @"";
  12183. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12184. carrier = [self textAtColumn:0 statement:statment];
  12185. }];
  12186. NSString* so_id = [self get_offline_soid:db];
  12187. if(so_id==nil)
  12188. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12189. 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];
  12190. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12191. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12192. //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'
  12193. //soId
  12194. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12195. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12196. sqlite3_stmt * statement;
  12197. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12198. {
  12199. if (sqlite3_step(statement) == SQLITE_ROW)
  12200. {
  12201. // char *name = (char*)sqlite3_column_text(statement, 1);
  12202. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12203. //ret = sqlite3_column_int(statement, 0);
  12204. char *soId = (char*)sqlite3_column_text(statement, 0);
  12205. if(soId==nil)
  12206. soId="";
  12207. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12208. [ret setValue:nssoId forKey:@"soId"];
  12209. [ret setValue:nssoId forKey:@"orderCode"];
  12210. }
  12211. sqlite3_finalize(statement);
  12212. }
  12213. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12214. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12215. [iSalesDB close_db:db];
  12216. dispatch_async(dispatch_get_main_queue(), ^{
  12217. result(ret);
  12218. });
  12219. });
  12220. }
  12221. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12222. {
  12223. UIApplication * app = [UIApplication sharedApplication];
  12224. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12225. bool can_create_backorder= appDelegate.can_create_backorder;
  12226. NSString* user = RASingleton.sharedInstance.user;
  12227. NSString* contact_id = appDelegate.contact_id;
  12228. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12229. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12230. sqlite3 *db = [iSalesDB get_db];
  12231. NSString* orderCode=params[@"orderCode"];
  12232. int sort = [[params objectForKey:@"sort"] intValue];
  12233. NSString *sort_str = @"";
  12234. switch (sort) {
  12235. case 0:{
  12236. sort_str = @"order by c.modify_time desc";
  12237. }
  12238. break;
  12239. case 1:{
  12240. sort_str = @"order by c.modify_time asc";
  12241. }
  12242. break;
  12243. case 2:{
  12244. sort_str = @"order by m.name asc";
  12245. }
  12246. break;
  12247. case 3:{
  12248. sort_str = @"order by m.name desc";
  12249. }
  12250. break;
  12251. case 4:{
  12252. sort_str = @"order by m.description asc";
  12253. }
  12254. break;
  12255. default:
  12256. break;
  12257. }
  12258. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  12259. sqlite3_stmt * statement1;
  12260. double orderdiscount=0;
  12261. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  12262. if ( dbresult1== SQLITE_OK)
  12263. {
  12264. if (sqlite3_step(statement1) == SQLITE_ROW)
  12265. {
  12266. // NSDate *row_date = [NSDate date];
  12267. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12268. orderdiscount = sqlite3_column_double(statement1, 0);
  12269. }
  12270. sqlite3_finalize(statement1);
  12271. }
  12272. 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 ];
  12273. // 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 ];
  12274. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12275. DebugLog(@"offline_login sql:%@",sqlQuery);
  12276. sqlite3_stmt * statement;
  12277. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12278. NSDate *date1 = [NSDate date];
  12279. int count=0;
  12280. int cart_count=0;
  12281. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12282. if ( dbresult== SQLITE_OK)
  12283. {
  12284. while (sqlite3_step(statement) == SQLITE_ROW)
  12285. {
  12286. // NSDate *row_date = [NSDate date];
  12287. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12288. int product_id = sqlite3_column_int(statement, 0);
  12289. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12290. int item_id = sqlite3_column_int(statement, 7);
  12291. NSString* Price=nil;
  12292. if(str_price==nil)
  12293. {
  12294. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12295. // NSDate *price_date = [NSDate date];
  12296. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12297. // DebugLog(@"price time interval");
  12298. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12299. if(price==nil)
  12300. Price=@"No Price.";
  12301. else
  12302. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12303. }
  12304. else
  12305. {
  12306. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12307. }
  12308. double discount = sqlite3_column_double(statement, 2);
  12309. int item_count = sqlite3_column_int(statement, 3);
  12310. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12311. NSString *nsline_note=nil;
  12312. if(line_note!=nil)
  12313. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12314. char *name = (char*)sqlite3_column_text(statement, 5);
  12315. NSString *nsname=nil;
  12316. if(name!=nil)
  12317. nsname= [[NSString alloc]initWithUTF8String:name];
  12318. char *description = (char*)sqlite3_column_text(statement, 6);
  12319. NSString *nsdescription=nil;
  12320. if(description!=nil)
  12321. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12322. int stockUom = sqlite3_column_int(statement, 8);
  12323. int _id = sqlite3_column_int(statement, 9);
  12324. int availability = sqlite3_column_int(statement, 10);
  12325. // NSDate *subtotal_date = [NSDate date];
  12326. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12327. // DebugLog(@"subtotal_date time interval");
  12328. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12329. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12330. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12331. int carton=[bsubtotaljson[@"carton"] intValue];
  12332. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12333. int mpack =[bsubtotaljson[@"mpack"] intValue];
  12334. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12335. // NSDate *img_date = [NSDate date];
  12336. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12337. // DebugLog(@"img_date time interval");
  12338. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12339. itemjson[@"model"]=nsname;
  12340. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12341. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12342. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12343. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12344. itemjson[@"check"]=@"true";
  12345. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12346. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12347. itemjson[@"unit_price"]=Price;
  12348. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12349. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12350. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12351. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  12352. itemjson[@"note"]=nsline_note;
  12353. if (!can_create_backorder) {
  12354. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12355. }
  12356. // NSDate *date2 = [NSDate date];
  12357. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12358. // DebugLog(@"model_bundle time interval");
  12359. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12360. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12361. count++;
  12362. // DebugLog(@"row time interval");
  12363. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12364. }
  12365. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12366. sqlite3_finalize(statement);
  12367. }
  12368. DebugLog(@"request cart total time interval");
  12369. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12370. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12371. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12372. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12373. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12374. ret[@"count"]=[NSNumber numberWithInt:count ];
  12375. if(orderdiscount>0)
  12376. ret[@"orderDiscount"]=@(orderdiscount);
  12377. ret[@"mode"]=@"Regular Mode";
  12378. [iSalesDB close_db:db];
  12379. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12380. DebugLog(@"general notes :%@",general_note);
  12381. ret[@"general_note"]= general_note;
  12382. dispatch_async(dispatch_get_main_queue(), ^{
  12383. result(ret);
  12384. });
  12385. });
  12386. }
  12387. @end