OLDataProvider.m 684 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295
  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. float order_discount=sqlite3_column_double(statement, 35);
  633. data[@"company"]=sender_name;
  634. data[@"shipfrom_addr"]=sender_ext;
  635. data[@"shipfrom_phone"]=sender_phone;
  636. data[@"shipfrom_fax"]=sender_fax;
  637. data[@"shipfrom_email"]=sender_email;
  638. data[@"warehouse_name"]=warehouse_name;
  639. data[@"bill_to_name"]=billing_name;
  640. data[@"bill_to_ext"]=billto;
  641. data[@"ship_to_name"]=receive_name;
  642. data[@"ship_to_ext"]=receive;
  643. data[@"po_no"]=poNumber;
  644. data[@"rep"]=sales_rep;
  645. data[@"e_schdule"]=etd;
  646. data[@"sales_terms"]=sales_terms;
  647. data[@"port_destination"]=port_destination;
  648. data[@"terms"]=terms;
  649. data[@"truck_carrier"]=carrier;
  650. data[@"vendor_no"]=vendor_no;
  651. data[@"customer_sign"]=sign_picpath;
  652. data[@"print_status"]=print_status;
  653. data[@"create_date"]=create_date;
  654. 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];;
  655. sqlite3_stmt * statement_cart;
  656. // NSDate *date1 = [NSDate date];
  657. int count=0;
  658. // int cart_count=0;
  659. float credits=0;
  660. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  661. if ( dbresult== SQLITE_OK)
  662. {
  663. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  664. int row1count=0;
  665. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  666. {
  667. // NSDate *row_date = [NSDate date];
  668. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  669. // int product_id = sqlite3_column_int(statement_cart, 0);
  670. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  671. int item_id = sqlite3_column_int(statement_cart, 7);
  672. double discount = sqlite3_column_double(statement_cart, 2);
  673. NSString* Price=nil;
  674. if(str_price==nil)
  675. {
  676. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  677. // NSDate *price_date = [NSDate date];
  678. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  679. // DebugLog(@"price time interval");
  680. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  681. if(price==nil)
  682. Price=@"No Price.";
  683. else
  684. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  685. }
  686. else
  687. {
  688. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  689. }
  690. float dunitprice=0;
  691. int item_count = sqlite3_column_int(statement_cart, 3);
  692. NSString* Amount=@"";
  693. if(![Price isEqualToString:@"No Price."])
  694. {
  695. dunitprice=[Price floatValue];
  696. dunitprice=dunitprice* (1-discount/100.0);
  697. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  698. credits+=dunitprice*item_count;
  699. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  700. }
  701. else
  702. {
  703. Price=@"";
  704. }
  705. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  706. // NSString *nsline_note=nil;
  707. // if(line_note!=nil)
  708. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  709. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  710. NSString *nsname=nil;
  711. if(name!=nil)
  712. nsname= [[NSString alloc]initWithUTF8String:name];
  713. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  714. NSString *nsdescription=nil;
  715. if(description!=nil)
  716. nsdescription= [[NSString alloc]initWithUTF8String:description];
  717. // int stockUom = sqlite3_column_int(statement_cart, 8);
  718. // int _id = sqlite3_column_int(statement_cart, 9);
  719. // NSDate *subtotal_date = [NSDate date];
  720. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  721. // DebugLog(@"subtotal_date time interval");
  722. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  723. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  724. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  725. // int carton=[bsubtotaljson[@"carton"] intValue];
  726. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  727. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  728. // NSDate *img_date = [NSDate date];
  729. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  730. // DebugLog(@"img_date time interval");
  731. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  732. itemjson[@"model"]=nsname;
  733. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  734. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  735. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  736. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  737. // itemjson[@"check"]=@"true";
  738. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  739. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  740. itemjson[@"unit_price"]=Price;
  741. itemjson[@"amount"]=Amount;
  742. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  743. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  744. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  745. // itemjson[@"note"]=nsline_note;
  746. // NSDate *date2 = [NSDate date];
  747. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  748. // DebugLog(@"model_bundle time interval");
  749. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  750. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  751. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  752. row1count++;
  753. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  754. for(int c=0;c<[combine[@"count"] intValue];c++)
  755. {
  756. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  757. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  758. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  759. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  760. credits+=combine_amount;
  761. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  762. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  763. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  764. row1count++;
  765. }
  766. count++;
  767. // DebugLog(@"row time interval");
  768. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  769. }
  770. row1[@"count"]=[NSNumber numberWithInt:row1count];
  771. datasource[@"row1"]=row1;
  772. data[@"table_model"]=datasource;
  773. sqlite3_finalize(statement_cart);
  774. }
  775. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  776. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  777. // if(credits>0)
  778. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  779. // else
  780. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  781. if(shipping>0)
  782. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  783. else
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  785. if(lift_gate_value>0)
  786. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  787. else
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  789. if(handling_fee_value>0)
  790. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  791. else
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  793. #ifdef BUILD_CONTRAST
  794. if(order_discount>0)
  795. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": [NSString stringWithFormat:@"-%.2f",credits*(order_discount/100.0)]};
  796. else
  797. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": @""};
  798. float total = credits*(1-order_discount/100.0)+shipping+lift_gate_value+handling_fee_value;
  799. if(total>0)
  800. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  801. else
  802. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": @""};
  803. fee_row0[@"count"]=[NSNumber numberWithInt:6];
  804. #else
  805. float total = credits+shipping+lift_gate_value+handling_fee_value;
  806. if(total>0)
  807. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  808. else
  809. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  810. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  811. #endif
  812. fee[@"row0"]=fee_row0;
  813. data[@"table_fee"]=fee;
  814. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  815. // ret[@"sign_url"] = sign_url;
  816. //
  817. // customer_contact = [self textAtColumn:52 statement:statement];
  818. // customer_email = [self textAtColumn:53 statement:statement];
  819. // customer_phone = [self textAtColumn:54 statement:statement];
  820. // customer_fax = [self textAtColumn:55 statement:statement];
  821. //
  822. //
  823. // int offline_edit=sqlite3_column_int(statement, 56);
  824. //
  825. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  826. //
  827. // char *soid = (char*)sqlite3_column_text(statement, 1);
  828. // if(soid==nil)
  829. // soid= "";
  830. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  831. // // so#
  832. // ret[@"so#"] = nssoid;
  833. //
  834. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  835. // if(poNumber==nil)
  836. // poNumber= "";
  837. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  838. //
  839. //
  840. //
  841. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  842. // if(create_time==nil)
  843. // create_time= "";
  844. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  845. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  846. //
  847. // int status = sqlite3_column_int(statement, 4);
  848. // int erpStatus = sqlite3_column_int(statement, 49);
  849. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  850. //
  851. // // status
  852. // if (status > 1 && status != 3) {
  853. // status = erpStatus;
  854. // } else if (status == 3) {
  855. // status = 15;
  856. // }
  857. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  858. // ret[@"order_status"] = nsstatus;
  859. //
  860. //
  861. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  862. // if(company_name==nil)
  863. // company_name= "";
  864. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  865. // // company name
  866. // ret[@"company_name"] = nscompany_name;
  867. //
  868. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  869. // if(customer_contact==nil)
  870. // customer_contact= "";
  871. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  872. //
  873. //
  874. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  875. // if(addr_1==nil)
  876. // addr_1="";
  877. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  878. //
  879. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  880. // if(addr_2==nil)
  881. // addr_2="";
  882. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  883. //
  884. //
  885. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  886. // if(addr_3==nil)
  887. // addr_3="";
  888. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  889. //
  890. //
  891. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  892. // if(addr_4==nil)
  893. // addr_4="";
  894. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  895. //
  896. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  897. // [arr_addr addObject:nsaddr_1];
  898. // [arr_addr addObject:nsaddr_2];
  899. // [arr_addr addObject:nsaddr_3];
  900. // [arr_addr addObject:nsaddr_4];
  901. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  902. //
  903. //
  904. // char *logist = (char*)sqlite3_column_text(statement, 11);
  905. // if(logist==nil)
  906. // logist= "";
  907. // NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  908. // if (status == -11 || status == 10 || status == 11) {
  909. // DebugLogist = [self textAtColumn:59 statement:statement];
  910. // };
  911. //
  912. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  913. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  914. // shipping = @"Shipping To Be Quoted";
  915. // } else {
  916. // shippingFee = sqlite3_column_double(statement, 12);
  917. // }
  918. //
  919. // // Shipping
  920. // ret[@"Shipping"] = shipping;
  921. //
  922. // int have_lift_gate = sqlite3_column_int(statement, 17);
  923. // lift_gate = sqlite3_column_double(statement, 13);
  924. // // Liftgate Fee(No loading dock)
  925. // if (!have_lift_gate) {
  926. // lift_gate = 0;
  927. // }
  928. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  929. // if (sqlite3_column_int(statement, 57)) {
  930. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  931. // }
  932. //
  933. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  934. // if(general_notes==nil)
  935. // general_notes= "";
  936. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  937. //
  938. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  939. // if(internal_notes==nil)
  940. // internal_notes= "";
  941. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  942. //
  943. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  944. // if(payment_type==nil)
  945. // payment_type= "";
  946. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  947. //
  948. //
  949. //
  950. // // order info
  951. // orderinfo = [self textFileName:@"order_info.html"];
  952. //
  953. //
  954. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  955. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  956. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  957. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  958. //
  959. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  960. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  964. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  965. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  967. //
  968. // NSString *payment = nil;
  969. // if([nspayment_type isEqualToString:@"Credit Card"])
  970. // {
  971. // payment = [self textFileName:@"creditcardpayment.html"];
  972. //
  973. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  974. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  975. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  976. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  977. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  978. // NSString *card_type = [self textAtColumn:42 statement:statement];
  979. // if (card_type.length > 0) { // 显示星号
  980. // card_type = @"****";
  981. // }
  982. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  983. // if (card_number.length > 0 && card_number.length > 4) {
  984. // for (int i = 0; i < card_number.length - 4; i++) {
  985. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  986. // }
  987. // } else {
  988. // card_number = @"";
  989. // }
  990. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  991. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  992. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  993. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  994. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  995. // card_expiration = @"****";
  996. // }
  997. //
  998. //
  999. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1000. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1001. //
  1002. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1003. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1004. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1005. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1006. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1007. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1008. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1009. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1010. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1011. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1012. //
  1013. //
  1014. // }
  1015. // else
  1016. // {
  1017. // payment=[self textFileName:@"normalpayment.html"];
  1018. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1019. // }
  1020. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1021. //
  1022. // ret[@"result"]= [NSNumber numberWithInt:2];
  1023. //
  1024. // // more info
  1025. // moreInfo = [self textFileName:@"more_info.html"];
  1026. //
  1027. //
  1028. // // ShipToCompany_or_&nbsp
  1029. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1031. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1032. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1033. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1034. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1035. //
  1036. //
  1037. // // ShipFromCompany_or_&nbsp
  1038. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1040. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1041. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1042. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1043. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1044. //
  1045. //
  1046. // // FreightBillToCompany_or_&nbsp
  1047. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1049. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1051. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1053. //
  1054. //
  1055. // // MerchandiseBillToCompany_or_&nbsp
  1056. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1058. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1060. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1062. //
  1063. //
  1064. // // ReturnToCompany_or_&nbsp
  1065. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1067. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1069. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1071. // //
  1072. //
  1073. // // DebugLog(@"more info : %@",moreInfo);
  1074. //
  1075. // // handling fee
  1076. // handlingFee = sqlite3_column_double(statement, 33);
  1077. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1078. // if (sqlite3_column_int(statement, 58)) {
  1079. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1080. // }
  1081. // //
  1082. //
  1083. // // customer info
  1084. // customerID = [self textAtColumn:36 statement:statement];
  1085. //
  1086. // // mode
  1087. // ret[@"mode"] = appDelegate.mode;
  1088. //
  1089. // // model_count
  1090. // ret[@"model_count"] = @(0);
  1091. }
  1092. sqlite3_finalize(statement);
  1093. }
  1094. [iSalesDB close_db:db];
  1095. data[@"order_type"]=order_type;
  1096. #ifdef DEBUG
  1097. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1098. #endif
  1099. return data;
  1100. }
  1101. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1102. {
  1103. assert(params[@"user"]!=nil);
  1104. NSString* template_file=nil;
  1105. switch([params[@"thumb"] intValue])
  1106. {
  1107. case 0:
  1108. template_file= @"so.json";
  1109. break;
  1110. case 1:
  1111. template_file= @"so_thumb.json";
  1112. break;
  1113. default:
  1114. template_file= @"so.json";
  1115. }
  1116. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1117. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1118. if(data[@"table_model"][@"row2"]==nil)
  1119. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1120. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1121. DebugLog(@"%@",file);
  1122. // return nil;
  1123. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1124. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1125. if (file) {
  1126. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1127. [dic setObject:file forKey:@"pdf_path"];
  1128. dic[@"isLocalFile"]=@"true";
  1129. return [RAConvertor dict2data:dic];
  1130. }
  1131. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1132. dic[@"isLocalFile"]=@"true";
  1133. return [RAConvertor dict2data:dic];
  1134. }
  1135. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1136. {
  1137. // assert(params[@"contact_id"]!=nil);
  1138. // assert(params[@"user"]!=nil);
  1139. if(params[@"user"]==nil)
  1140. {
  1141. AppDelegate *appDelegate = nil;
  1142. //some UI methods ej
  1143. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1144. if(appDelegate.user!=nil)
  1145. [params setValue:appDelegate.user forKey:@"user"];
  1146. }
  1147. NSString* template_file=nil;
  1148. switch([params[@"pdf_style"] intValue])
  1149. {
  1150. case 0:
  1151. template_file= @"portfolio_2x3.json";
  1152. break;
  1153. case 1:
  1154. template_file= @"portfolio_3x2.json";
  1155. break;
  1156. default:
  1157. template_file= @"portfolio_3x2.json";
  1158. }
  1159. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1160. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1161. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1162. DebugLog(@"%@",file);
  1163. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1164. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1165. if (file) {
  1166. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1167. [dic setObject:file forKey:@"pdf_path"];
  1168. dic[@"isLocalFile"]=@"true";
  1169. return [RAConvertor dict2data:dic];
  1170. }
  1171. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1172. dic[@"isLocalFile"]=@"true";
  1173. return [RAConvertor dict2data:dic];
  1174. }
  1175. +(NSString*) get_offline_soid:(sqlite3*)db
  1176. {
  1177. NSString* soid=nil;
  1178. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1179. [formatter setDateFormat:@"yyMMdd"];
  1180. NSString* date = [formatter stringFromDate:[NSDate date]];
  1181. for(int i=1;i<999;i++)
  1182. {
  1183. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1184. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1185. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1186. if(count==0)
  1187. return soid;
  1188. }
  1189. return nil;
  1190. }
  1191. +(NSArray*) enumOfflineOrder
  1192. {
  1193. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1194. sqlite3 *db = [iSalesDB get_db];
  1195. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1196. sqlite3_stmt * statement;
  1197. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1198. if ( dbresult== SQLITE_OK)
  1199. {
  1200. while (sqlite3_step(statement) == SQLITE_ROW)
  1201. {
  1202. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1203. NSString* nsso_id=nil;
  1204. if(so_id!=nil)
  1205. {
  1206. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1207. }
  1208. [ret addObject:nsso_id];
  1209. }
  1210. sqlite3_finalize(statement);
  1211. }
  1212. [iSalesDB close_db:db];
  1213. return ret;
  1214. }
  1215. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1216. {
  1217. if(submitlist==nil)
  1218. return;
  1219. NSString *userdir = [OLDataProvider getUserPath];
  1220. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1221. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1222. }
  1223. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1224. {
  1225. if(orderlist==nil)
  1226. return;
  1227. NSString *userdir = [OLDataProvider getUserPath];
  1228. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1229. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1230. }
  1231. +(NSMutableDictionary*) getScanSubmitList
  1232. {
  1233. NSString *userdir = [OLDataProvider getUserPath];
  1234. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1235. NSMutableDictionary* submitlist = nil;
  1236. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1237. submitlist = [NSMutableDictionary new];
  1238. }
  1239. else
  1240. {
  1241. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1242. }
  1243. return submitlist;
  1244. }
  1245. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1246. //// NSString *userdir = [self getUserPath:user];
  1247. // NSMutableDictionary* orderlist = nil;
  1248. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1249. //
  1250. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1251. //
  1252. //
  1253. // orderlist = [NSMutableDictionary new];
  1254. //
  1255. // // [orderlist addObject:appDelegate.order_code];
  1256. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1257. //
  1258. // }
  1259. // else
  1260. // {
  1261. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1262. //
  1263. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1264. //
  1265. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1266. // }
  1267. // return orderlist;
  1268. //}
  1269. +(NSMutableDictionary*) getScanOrderList{
  1270. NSString *userdir = [OLDataProvider getUserPath];
  1271. NSMutableDictionary* orderlist = nil;
  1272. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1273. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1274. orderlist = [NSMutableDictionary new];
  1275. // [orderlist addObject:appDelegate.order_code];
  1276. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1277. }
  1278. else
  1279. {
  1280. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1281. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1282. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1283. }
  1284. return orderlist;
  1285. }
  1286. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1287. {
  1288. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1289. // NSString *documents = [paths objectAtIndex:0];
  1290. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1291. // NSString *userdir = [OLDataProvider getUserPath];
  1292. NSMutableDictionary* orderlist = [self getScanOrderList];
  1293. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1294. int count = [orderlist[@"count"] intValue];
  1295. if(count==0)
  1296. {
  1297. // order list 为空,不用维护
  1298. #ifdef RA_NOTIFICATION
  1299. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1300. #else
  1301. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1302. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1303. #endif
  1304. return;
  1305. }
  1306. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1307. //
  1308. // // order list 为空,不用维护
  1309. //
  1310. //
  1311. // // [orderlist addObject:appDelegate.order_code];
  1312. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1313. //
  1314. // }
  1315. // else
  1316. // {
  1317. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1318. //
  1319. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1320. //
  1321. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1322. // }
  1323. //==================== order list 删除
  1324. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1325. NSMutableDictionary* submititem =nil;
  1326. int newcount = 0;
  1327. for(int i=0;i<count;i++)
  1328. {
  1329. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1330. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1331. {
  1332. submititem = item;
  1333. continue;
  1334. }
  1335. else
  1336. {
  1337. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1338. newcount++;
  1339. }
  1340. }
  1341. if(submititem==nil)
  1342. {
  1343. //order list 里没有和当前打开的order code一致的order
  1344. //这种情况往往是因为重复点提交按钮,order已关闭。
  1345. return;
  1346. }
  1347. newlist[@"count"] = @(newcount);
  1348. [self saveScanOrderList:newlist];
  1349. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1350. //////////////////---=============================== submitlist 添加
  1351. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1352. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1353. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1354. //
  1355. // submitlist = [NSMutableDictionary new];
  1356. //
  1357. //
  1358. // }
  1359. // else
  1360. // {
  1361. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1362. //
  1363. //
  1364. // }
  1365. int submitcount =[submitlist[@"count"] intValue];
  1366. submititem[@"order_status"] = soid;//@"Submitted Order";
  1367. submititem[@"customer_name"] = company;//@"Submitted Order";
  1368. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1369. submitlist[@"count"] = @(submitcount+1);
  1370. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1371. [self saveScanSubmitList:submitlist];
  1372. #ifdef RA_NOTIFICATION
  1373. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1374. #else
  1375. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1376. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1377. #endif
  1378. }
  1379. +(void) save2submitScanOrder1
  1380. {
  1381. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1382. // NSString *documents = [paths objectAtIndex:0];
  1383. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1384. // NSString *userdir = [OLDataProvider getUserPath];
  1385. NSMutableDictionary* orderlist = [self getScanOrderList];
  1386. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1387. int count = [orderlist[@"count"] intValue];
  1388. if(count==0)
  1389. {
  1390. // order list 为空,不用维护
  1391. #ifdef RA_NOTIFICATION
  1392. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1393. #else
  1394. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1395. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1396. #endif
  1397. return;
  1398. return;
  1399. }
  1400. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1401. int newcount = 0;
  1402. for(int i=0;i<count;i++)
  1403. {
  1404. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1405. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1406. continue;
  1407. else
  1408. {
  1409. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1410. newcount++;
  1411. }
  1412. }
  1413. newlist[@"count"] = @(newcount);
  1414. [self saveScanOrderList:newlist];
  1415. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1416. #ifdef RA_NOTIFICATION
  1417. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1418. #else
  1419. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1420. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1421. #endif
  1422. }
  1423. //+(void) createScanTempFolder
  1424. //{
  1425. // return;
  1426. //// NSString* scanFilePath = [self getScanTempPath];
  1427. //// BOOL bdir=YES;
  1428. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1429. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1430. //// {
  1431. ////
  1432. //// NSError *error = nil;
  1433. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1434. ////
  1435. //// if(!bsuccess)
  1436. //// DebugLog(@"Create cache folder failed");
  1437. ////
  1438. //// // if(bsuccess)
  1439. //// // {
  1440. //// // sqlite3 *db = [self get_db];
  1441. //// //
  1442. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1443. //// // [iSalesDB close_db:db];
  1444. //// // }
  1445. //// }
  1446. //}
  1447. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1448. {
  1449. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1450. NSString *documents = [paths objectAtIndex:0];
  1451. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1452. NSString* servername = addressDic[@"name"];
  1453. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1454. NSString* user = appDelegate.user;
  1455. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1456. orderpath = [orderpath stringByAppendingPathComponent:user];
  1457. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1458. BOOL bdir=YES;
  1459. NSFileManager* fileManager = [NSFileManager defaultManager];
  1460. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1461. {
  1462. NSError *error = nil;
  1463. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1464. if(!bsuccess)
  1465. DebugLog(@"Create cache folder failed");
  1466. // if(bsuccess)
  1467. // {
  1468. // sqlite3 *db = [self get_db];
  1469. //
  1470. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1471. // [iSalesDB close_db:db];
  1472. // }
  1473. }
  1474. return orderpath;
  1475. }
  1476. +(NSString*) getScanOrderPath
  1477. {
  1478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1479. NSString *documents = [paths objectAtIndex:0];
  1480. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1481. NSString* servername = addressDic[@"name"];
  1482. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1483. NSString* user = appDelegate.user;
  1484. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1485. orderpath = [orderpath stringByAppendingPathComponent:user];
  1486. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1487. BOOL bdir=YES;
  1488. NSFileManager* fileManager = [NSFileManager defaultManager];
  1489. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1490. {
  1491. NSError *error = nil;
  1492. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1493. if(!bsuccess)
  1494. DebugLog(@"Create cache folder failed");
  1495. // if(bsuccess)
  1496. // {
  1497. // sqlite3 *db = [self get_db];
  1498. //
  1499. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1500. // [iSalesDB close_db:db];
  1501. // }
  1502. }
  1503. return orderpath;
  1504. }
  1505. //+(NSString*) getScanTempPath
  1506. //{
  1507. // return nil;
  1508. ////
  1509. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1510. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1511. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1512. //}
  1513. //+(void) moveScanTemp2Order
  1514. //{
  1515. // return;
  1516. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1517. ////
  1518. //// NSError *error = nil;
  1519. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1520. ////
  1521. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1522. //// // for(int i=0;i<arr_files.count;i++)
  1523. //// // {
  1524. //// // NSString* file=arr_files[i];
  1525. //// //
  1526. //// //
  1527. //// // }
  1528. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1529. ////
  1530. //
  1531. //}
  1532. //+(NSString*) getUserPath: (NSString*) user
  1533. //{
  1534. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1535. // NSString *documents = [paths objectAtIndex:0];
  1536. //
  1537. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1538. //
  1539. // NSString* servername = addressDic[@"name"];
  1540. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1541. //// NSString* user = appDelegate.user;
  1542. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1543. // userpath = [userpath stringByAppendingPathComponent:user];
  1544. // return userpath;
  1545. //}
  1546. +(NSString*) getServerPath
  1547. {
  1548. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1549. NSString *documents = [paths objectAtIndex:0];
  1550. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1551. NSString* servername = addressDic[@"name"];
  1552. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1553. // NSString* user = appDelegate.user;
  1554. // NSString* user = RASingleton.sharedInstance.user;
  1555. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1556. // userpath = [userpath stringByAppendingPathComponent:user];
  1557. NSFileManager* fileManager = [NSFileManager defaultManager];
  1558. BOOL bdir=YES;
  1559. if(! [fileManager fileExistsAtPath:serverpath isDirectory:&bdir])
  1560. {
  1561. NSError *error = nil;
  1562. [fileManager createDirectoryAtPath:serverpath withIntermediateDirectories:YES attributes:nil error:&error];
  1563. }
  1564. return serverpath;
  1565. }
  1566. +(NSString*) getUserPath
  1567. {
  1568. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1569. NSString *documents = [paths objectAtIndex:0];
  1570. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1571. NSString* servername = addressDic[@"name"];
  1572. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1573. // NSString* user = appDelegate.user;
  1574. NSString* user = RASingleton.sharedInstance.user;
  1575. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1576. userpath = [userpath stringByAppendingPathComponent:user];
  1577. NSFileManager* fileManager = [NSFileManager defaultManager];
  1578. BOOL bdir=YES;
  1579. if(! [fileManager fileExistsAtPath:userpath isDirectory:&bdir])
  1580. {
  1581. NSError *error = nil;
  1582. [fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:&error];
  1583. }
  1584. return userpath;
  1585. }
  1586. +(bool) isSavedScanOrder:(NSString*) order_code
  1587. {
  1588. if(order_code == nil)
  1589. return false;
  1590. NSMutableDictionary * orderlist=[self getScanOrderList];
  1591. int count = [orderlist[@"count"] intValue];
  1592. for(int i=0;i<count;i++)
  1593. {
  1594. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1595. if([item[@"order_code"] isEqualToString: order_code ])
  1596. {
  1597. return true;
  1598. }
  1599. }
  1600. return false;
  1601. }
  1602. +(NSString*) getScanPath
  1603. {
  1604. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1605. if(appDelegate.order_code==nil)
  1606. {
  1607. appDelegate.order_code=[[NSUUID new] UUIDString];
  1608. }
  1609. return [self getScanOrderPath];
  1610. // else
  1611. // {
  1612. // [self createScanTempFolder];
  1613. // return [self getScanTempPath];
  1614. // }
  1615. }
  1616. +(void) scanCloseOrder
  1617. {
  1618. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1619. {
  1620. // 当前订单先关闭
  1621. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1622. appDelegate.order_code = nil;
  1623. // RASingleton.sharedInstance.scan_temp_code = nil;
  1624. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1625. { appDelegate.customerInfo = nil;
  1626. appDelegate.contact_id = nil;
  1627. }
  1628. RASingleton.sharedInstance.scan_cart = nil;
  1629. RASingleton.sharedInstance.price_type = 1;
  1630. // [appDelegate updateScanButton:false];
  1631. [appDelegate update_count_mark];
  1632. [appDelegate closeOrder];
  1633. }
  1634. }
  1635. +(NSString*) createScanTempCode
  1636. {
  1637. return [[NSUUID new] UUIDString];
  1638. }
  1639. +(NSString*) scanTemplatePath:(NSString*)file
  1640. {
  1641. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1642. NSString *documents = [paths objectAtIndex:0];
  1643. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1644. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1645. return templatefile;
  1646. }
  1647. +(void) realguestLogin
  1648. {
  1649. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1650. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1651. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1652. }
  1653. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1654. {
  1655. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1656. NSString *documents = [paths objectAtIndex:0];
  1657. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1658. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1659. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1660. return [dict mutableCopy];
  1661. }
  1662. +(void) deleteSubmittedOrder:(NSString*) order_code
  1663. {
  1664. NSString *userdir = [OLDataProvider getUserPath];
  1665. if(order_code.length==0)
  1666. return;
  1667. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1668. // NSString *documents = [paths objectAtIndex:0];
  1669. // if([appDelegate.order_code isEqualToString:order_code])
  1670. // {
  1671. // //要删除的订单是当前打开的订单;
  1672. // appDelegate.order_code = nil;
  1673. // appDelegate.customerInfo = nil;
  1674. // RASingleton.sharedInstance.scan_cart = nil;
  1675. // appDelegate.contact_id = nil;
  1676. // // [appDelegate updateScanButton:false];
  1677. //
  1678. // [appDelegate update_count_mark];
  1679. // }
  1680. //删除订单目录
  1681. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1682. //维护订单列表
  1683. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1684. int count = [submitlist[@"count"] intValue];
  1685. if(count==0)
  1686. {
  1687. // order list 为空,不用维护
  1688. #ifdef RA_NOTIFICATION
  1689. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1690. #else
  1691. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1692. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1693. #endif
  1694. return;
  1695. }
  1696. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1697. //
  1698. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1699. //
  1700. //
  1701. //
  1702. // // [orderlist addObject:appDelegate.order_code];
  1703. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1704. //
  1705. // }
  1706. // else
  1707. // {
  1708. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1709. //
  1710. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1711. //
  1712. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1713. // }
  1714. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1715. int newcount = 0;
  1716. for(int i=0;i<count;i++)
  1717. {
  1718. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1719. if([item[@"order_code"] isEqualToString: order_code ])
  1720. continue;
  1721. else
  1722. {
  1723. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1724. newcount++;
  1725. }
  1726. }
  1727. newlist[@"count"] = @(newcount);
  1728. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1729. [self saveScanSubmitList:newlist];
  1730. #ifdef RA_NOTIFICATION
  1731. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1732. #else
  1733. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1734. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1735. #endif
  1736. }
  1737. +(void) updateScanOrder:(NSString*) order_code
  1738. {
  1739. #ifdef SCANNER_ORDER
  1740. NSString *userdir = [OLDataProvider getUserPath];
  1741. if(order_code.length==0)
  1742. return;
  1743. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1744. // NSString *documents = [paths objectAtIndex:0];
  1745. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1746. NSMutableDictionary* orderlist = [self getScanOrderList];
  1747. int count = [orderlist[@"count"] intValue];
  1748. if(count==0)
  1749. {
  1750. // order list 为空,不用维护
  1751. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1752. return;
  1753. }
  1754. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1755. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1756. //
  1757. //
  1758. //
  1759. // // [orderlist addObject:appDelegate.order_code];
  1760. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1761. //
  1762. // }
  1763. // else
  1764. // {
  1765. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1766. //
  1767. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1768. //
  1769. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1770. // }
  1771. //
  1772. //
  1773. // int count = [orderlist[@"count"] intValue];
  1774. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1775. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1776. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1777. float p =0;
  1778. float s =0;
  1779. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1780. {
  1781. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1782. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1783. int mcount = [cartitem[@"count"] intValue];
  1784. double msurcharge =0;
  1785. // remove net price
  1786. // if(RASingleton.sharedInstance.price_type==1)
  1787. // {
  1788. // if([cartitem[@"special_price"] boolValue])
  1789. // {
  1790. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1791. // }
  1792. // else
  1793. // {
  1794. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1795. // }
  1796. // }
  1797. s+=msurcharge;
  1798. }
  1799. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1800. int order_qty=0;
  1801. for(int c=0;c<[section[@"count"] intValue];c++)
  1802. {
  1803. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1804. order_qty+=[item[@"count"] intValue];
  1805. }
  1806. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1807. // int newcount = 0;
  1808. for(int i=0;i<count;i++)
  1809. {
  1810. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1811. if([item[@"order_code"] isEqualToString: order_code ])
  1812. {
  1813. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1814. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1815. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1816. }
  1817. }
  1818. // newlist[@"count"] = @(newcount);
  1819. [self saveScanOrderList:orderlist];
  1820. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1821. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1822. #endif
  1823. }
  1824. +(void) deleteScanOrder:(NSString*) order_code
  1825. {
  1826. NSString *userdir = [OLDataProvider getUserPath];
  1827. if(order_code.length==0)
  1828. return;
  1829. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1830. // NSString *documents = [paths objectAtIndex:0];
  1831. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1832. if([appDelegate.order_code isEqualToString:order_code])
  1833. {
  1834. //要删除的订单是当前打开的订单;
  1835. // appDelegate.order_code = nil;
  1836. // appDelegate.customerInfo = nil;
  1837. // RASingleton.sharedInstance.scan_cart = nil;
  1838. // appDelegate.contact_id = nil;
  1839. [self scanCloseOrder];
  1840. // [appDelegate updateScanButton:false];
  1841. [appDelegate update_count_mark];
  1842. }
  1843. //删除订单目录
  1844. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1845. //维护订单列表
  1846. NSMutableDictionary* orderlist = [self getScanOrderList];
  1847. int count = [orderlist[@"count"] intValue];
  1848. if(count==0)
  1849. {
  1850. // order list 为空,不用维护
  1851. #ifdef RA_NOTIFICATION
  1852. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1853. #else
  1854. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1855. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1856. #endif
  1857. return;
  1858. }
  1859. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1860. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1861. //
  1862. //
  1863. //
  1864. // // [orderlist addObject:appDelegate.order_code];
  1865. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1866. //
  1867. // }
  1868. // else
  1869. // {
  1870. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1871. //
  1872. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1873. //
  1874. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1875. // }
  1876. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1877. int newcount = 0;
  1878. for(int i=0;i<count;i++)
  1879. {
  1880. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1881. if([item[@"order_code"] isEqualToString: order_code ])
  1882. continue;
  1883. else
  1884. {
  1885. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1886. newcount++;
  1887. }
  1888. }
  1889. newlist[@"count"] = @(newcount);
  1890. [self saveScanOrderList:newlist];
  1891. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1892. #ifdef RA_NOTIFICATION
  1893. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1894. #else
  1895. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1896. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1897. #endif
  1898. }
  1899. #ifdef SCANNER_ORDER
  1900. +(void) scanRefreshCart
  1901. {
  1902. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1903. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  1904. NSMutableDictionary * section = cart[@"section_0"];
  1905. NSMutableDictionary* newsection = [NSMutableDictionary new];
  1906. int count = [section[@"count"] intValue];
  1907. int newcount = 0;
  1908. for(int i=0;i<count;i++)
  1909. {
  1910. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1911. NSString* model = item[@"model"];
  1912. NSDictionary* modeljson=appDelegate.scan_model[model];
  1913. if(modeljson==nil)
  1914. continue;
  1915. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  1916. cartitem[@"note"]=item[@"note"];
  1917. cartitem[@"discount"]=item[@"discount"];
  1918. // [item removeObjectForKey:@"discount"];
  1919. // [item removeObjectForKey:@"note"];
  1920. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  1921. newcount++;
  1922. }
  1923. newsection[@"count"]=@(newcount);
  1924. newsection[@"available"] = @(1);
  1925. newsection[@"title"] = @"Available List";
  1926. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  1927. }
  1928. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  1929. {
  1930. // _modelJson = modelJson;
  1931. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1932. // self.labelPrice0.text = appDelegate.price0_name;
  1933. // self.labelPrice1.text = appDelegate.price1_name;
  1934. // self.labelPrice2.text = appDelegate.price2_name;
  1935. // self.labelPrice3.text = appDelegate.price3_name;
  1936. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1937. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  1938. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  1939. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  1940. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  1941. #ifdef DEBUG
  1942. DebugLog( [RAConvertor dict2string:_modelJson]);
  1943. #endif
  1944. NSString * port=_modelJson[@"port"];
  1945. if(port.length==0)
  1946. port = @"N/A";
  1947. NSString * origin=_modelJson[@"origin"];
  1948. if(origin.length==0)
  1949. origin = @"N/A";
  1950. NSString * dimension=_modelJson[@"dimension"];
  1951. if(dimension.length==0)
  1952. dimension = @"N/A";
  1953. // _labelModel.text=_modelJson[@"model"];
  1954. // _labelDescription.text=_modelJson[@"description"];
  1955. // _labelDimension.text=dimension;
  1956. // _labelCuft.text=_modelJson[@"unit_cuft"];
  1957. // _labelOrigin.text=origin;
  1958. // _labelPort.text=port;
  1959. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  1960. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  1961. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  1962. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  1963. if(![price0 isEqualToString: @"N/A"])
  1964. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  1965. if(![price1 isEqualToString: @"N/A"])
  1966. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  1967. if(![price2 isEqualToString: @"N/A"])
  1968. {
  1969. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  1970. // if(RASingleton.sharedInstance.price_type==1)
  1971. _modelJson [@"special_price"] = @true;
  1972. }
  1973. if(![price3 isEqualToString: @"N/A"])
  1974. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  1975. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  1976. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  1977. {
  1978. // if(RASingleton.sharedInstance.price_type==1)
  1979. _modelJson [@"net_price"] = @true;
  1980. // remove net price
  1981. _modelJson [@"net_price"] = @false;
  1982. }
  1983. //
  1984. // if(![price1 isEqualToString:@"N/A"])
  1985. // {
  1986. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  1987. // _modelJson[@"price2"]= price2;
  1988. // }
  1989. // else
  1990. // {
  1991. // price2=@"N/A";
  1992. // }
  1993. //
  1994. // if([price0 isEqualToString:@"N/A"])
  1995. // _labelPriceCTNR.text= @"";//price0;
  1996. // else
  1997. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  1998. //
  1999. // if([price3 isEqualToString:@"N/A"])
  2000. // _labelPriceNCA.text= price3;
  2001. // else
  2002. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2003. // _labelPrice25p.text=price2;
  2004. //
  2005. // if([_modelJson [@"special_price"] boolValue])
  2006. // {
  2007. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2008. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2009. // else
  2010. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2011. // }
  2012. // else
  2013. // {
  2014. // _labelPriceSpecial.text=@"";//@"N/A";
  2015. // }
  2016. //
  2017. // if([_modelJson [@"net_price"] boolValue])
  2018. // {
  2019. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2020. //// {
  2021. //// _labelPriceNet.text=_modelJson[@"price1"];
  2022. ////// _labelPriceNCA.text=@"N/A";
  2023. //// }
  2024. //// else
  2025. //// {
  2026. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2027. ////// _labelPriceNCA.text=@"N/A";
  2028. //// }
  2029. //
  2030. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2031. // {
  2032. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2033. //// _labelPriceNCA.text=@"N/A";
  2034. // }
  2035. // else
  2036. // {
  2037. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2038. //// _labelPriceNCA.text=@"N/A";
  2039. // }
  2040. //
  2041. // }
  2042. // else
  2043. // {
  2044. // _labelPriceNet.text=@"";//@"N/A";
  2045. // }
  2046. //
  2047. // {
  2048. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2049. // {
  2050. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2051. //// _labelPriceNCA.text=@"N/A";
  2052. // }
  2053. // else
  2054. // {
  2055. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2056. //// _labelPriceNet.text=@"N/A";
  2057. // }
  2058. // }
  2059. NSString * available=_modelJson[@"available"];
  2060. if(available.length==0)
  2061. available = @"N/A";
  2062. // _labelAvailable.text=available;
  2063. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2064. if(unit_cuft.length==0)
  2065. unit_cuft = @"N/A";
  2066. else
  2067. {
  2068. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2069. }
  2070. NSString * cell_price;
  2071. if(RASingleton.sharedInstance.price_type==1)
  2072. {
  2073. // if(_modelJson [@"net_price"])
  2074. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2075. // else if(_modelJson [@"special_price"])
  2076. // {
  2077. // unit_price= _modelJson[@"price2"];
  2078. // }
  2079. // else
  2080. // {
  2081. // unit_price= _modelJson[@"price1"];
  2082. // }
  2083. }
  2084. else
  2085. {
  2086. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2087. }
  2088. NSString * mpack=_modelJson[@"stockUom"];
  2089. if([mpack isEqualToString:@"N/A"])
  2090. mpack=@"1";
  2091. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2092. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2093. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2094. NSMutableDictionary* jitem = nil;
  2095. jitem = [_modelJson mutableCopy];
  2096. int stockUom =[jitem[@"stockUom"] intValue];
  2097. if(stockUom==0)
  2098. stockUom=1;
  2099. bool newitem = true;
  2100. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2101. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2102. // for(int i=0;i<count;i++)
  2103. // {
  2104. //
  2105. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2106. //
  2107. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2108. // {
  2109. //
  2110. //// int oldcount = [litem[@"stockUom"] intValue];
  2111. // newitem = false;
  2112. //
  2113. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2114. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2115. //
  2116. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2117. // break;
  2118. //
  2119. //
  2120. // }
  2121. //
  2122. //
  2123. //
  2124. // }
  2125. // if(true)
  2126. // {
  2127. jitem[@"count"]=@(stockUom);
  2128. jitem[@"check"]=@(true);
  2129. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2130. NSString * unit_price;
  2131. if(RASingleton.sharedInstance.price_type==0)
  2132. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2133. else if(_modelJson [@"special_price"])
  2134. {
  2135. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2136. }
  2137. else if(_modelJson [@"net_price"])
  2138. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2139. else
  2140. {
  2141. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2142. }
  2143. if([unit_price isEqualToString:@"N/A"])
  2144. unit_price = @"0";
  2145. else
  2146. {
  2147. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2148. }
  2149. jitem[@"unit_price"] = unit_price;
  2150. jitem[@"erp_unit_price"] = unit_price;
  2151. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2152. // section[@"count"]= @(count+1);
  2153. // count++;
  2154. // }
  2155. return jitem;
  2156. }
  2157. +(void) saveScanCart:(NSMutableDictionary*) cart
  2158. {
  2159. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2160. if(appDelegate.order_code.length<=0)
  2161. return;
  2162. if(cart==nil)
  2163. return;
  2164. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2165. // NSString *documents = [paths objectAtIndex:0];
  2166. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2167. if(RASingleton.sharedInstance.price_type==0)
  2168. {
  2169. NSMutableDictionary * section = cart[@"section_0"];
  2170. int count = [section[@"count"] intValue];
  2171. for(int i=0;i<count;i++)
  2172. {
  2173. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2174. [item removeObjectForKey:@"discount"];
  2175. [item removeObjectForKey:@"note"];
  2176. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2177. }
  2178. cart[@"section_0"]=section;
  2179. }
  2180. NSString *orderdir = [self getScanPath];
  2181. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2182. cart[@"price_group"] = appDelegate.price_group;
  2183. [RAUtils dicttofile:cartpath dict:cart];
  2184. }
  2185. #endif
  2186. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2187. {
  2188. assert(add_params[@"user"]!=nil);
  2189. // assert(add_params[@"contact_id"]!=nil);
  2190. // assert(add_params[@"password"]!=nil);
  2191. NSString* serial= [[NSUUID UUID] UUIDString];
  2192. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2193. NSString *cachefolder = [paths objectAtIndex:0];
  2194. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2195. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2196. NSFileManager* fileManager = [NSFileManager defaultManager];
  2197. BOOL bdir=YES;
  2198. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2199. {
  2200. NSError *error = nil;
  2201. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2202. if(!bsuccess)
  2203. DebugLog(@"Create UPLOAD folder failed");
  2204. }
  2205. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2206. ret[@"contact"]=[self prepareContact:serial];
  2207. ret[@"wishlist"]=[self prepareWishlist:serial];
  2208. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2209. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2210. ret[@"view_portfolio"] = [self preparePDF:serial];
  2211. NSString* str= [RAConvertor dict2string:ret];
  2212. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2213. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2214. NSError *error=nil;
  2215. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2216. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2217. ZipArchive* zip = [[ZipArchive alloc] init];
  2218. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2219. for(int i=0;i<arr_files.count;i++)
  2220. {
  2221. NSString* file=arr_files[i];
  2222. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2223. }
  2224. if( ![zip CloseZipFile2] )
  2225. {
  2226. zippath = @"";
  2227. }
  2228. ret[@"file"]=zippath;
  2229. return ret;
  2230. }
  2231. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2232. {
  2233. if(filename.length==0)
  2234. return false;
  2235. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2236. bool ret=false;
  2237. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2238. NSString *cachefolder = [paths objectAtIndex:0];
  2239. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2240. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2241. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2242. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2243. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2244. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2245. NSFileManager* fileManager = [NSFileManager defaultManager];
  2246. BOOL bdir=NO;
  2247. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2248. {
  2249. NSError *error = nil;
  2250. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2251. {
  2252. ret=false;
  2253. }
  2254. else
  2255. {
  2256. ret=true;
  2257. }
  2258. }
  2259. return ret;
  2260. }
  2261. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2262. {
  2263. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2264. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2265. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2266. NSString* where=@" 1=1";
  2267. if (ver!=nil) {
  2268. where=@"is_dirty=1";
  2269. }
  2270. sqlite3 *db = [iSalesDB get_db];
  2271. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2272. sqlite3_stmt * statement;
  2273. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2274. int count=0;
  2275. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2276. if ( dbresult== SQLITE_OK)
  2277. {
  2278. while (sqlite3_step(statement) == SQLITE_ROW)
  2279. {
  2280. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2281. int _id = sqlite3_column_int(statement, 0);
  2282. int product_id = sqlite3_column_int(statement, 1);
  2283. int item_id = sqlite3_column_int(statement, 2);
  2284. int qty = sqlite3_column_int(statement, 3);
  2285. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2286. NSString* nscreate_time=nil;
  2287. if(create_time!=nil)
  2288. {
  2289. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2290. }
  2291. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2292. NSString* nsmodify_time=nil;
  2293. if(modify_time!=nil)
  2294. {
  2295. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2296. }
  2297. int is_delete = sqlite3_column_int(statement, 6);
  2298. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2299. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2300. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2301. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2302. itemjson[@"create_time"]=nscreate_time;
  2303. itemjson[@"modify_time"]=nsmodify_time;
  2304. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2305. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2306. count++;
  2307. }
  2308. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2309. sqlite3_finalize(statement);
  2310. }
  2311. ret[@"count"]=[NSNumber numberWithInt:count ];
  2312. [iSalesDB close_db:db];
  2313. return ret;
  2314. }
  2315. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2316. {
  2317. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2318. // UIApplication * app = [UIApplication sharedApplication];
  2319. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2320. assert(add_params[@"user"]!=nil);
  2321. // assert(add_params[@"password"]!=nil);
  2322. 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 ];
  2323. // 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 ];
  2324. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2325. DebugLog(@"offline_login sql:%@",sqlQuery);
  2326. sqlite3_stmt * statement;
  2327. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2328. int count=0;
  2329. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2330. if ( dbresult== SQLITE_OK)
  2331. {
  2332. while (sqlite3_step(statement) == SQLITE_ROW)
  2333. {
  2334. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2335. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2336. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2337. int product_id = sqlite3_column_int(statement, 0);
  2338. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2339. int item_id = sqlite3_column_int(statement, 7);
  2340. NSString* Price=nil;
  2341. if(str_price==nil)
  2342. {
  2343. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2344. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2345. if(price==nil)
  2346. Price=@"No Price.";
  2347. else
  2348. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2349. }
  2350. else
  2351. {
  2352. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2353. }
  2354. double discount = sqlite3_column_double(statement, 2);
  2355. int item_count = sqlite3_column_int(statement, 3);
  2356. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2357. NSString *nsline_note=nil;
  2358. if(line_note!=nil)
  2359. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2360. char *name = (char*)sqlite3_column_text(statement, 5);
  2361. NSString *nsname=nil;
  2362. if(name!=nil)
  2363. nsname= [[NSString alloc]initWithUTF8String:name];
  2364. char *description = (char*)sqlite3_column_text(statement, 6);
  2365. NSString *nsdescription=nil;
  2366. if(description!=nil)
  2367. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2368. // int stockUom = sqlite3_column_int(statement, 8);
  2369. // int _id = sqlite3_column_int(statement, 9);
  2370. //
  2371. //
  2372. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2373. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2374. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2375. // int carton=[bsubtotaljson[@"carton"] intValue];
  2376. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2377. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2378. itemjson[@"model"]=nsname;
  2379. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2380. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2381. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2382. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2383. // itemjson[@"check"]=@"true";
  2384. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2385. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2386. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2387. itemjson[@"unit_price"]=Price;
  2388. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2389. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2390. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2391. itemjson[@"note"]=nsline_note;
  2392. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2393. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2394. count++;
  2395. }
  2396. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2397. sqlite3_finalize(statement);
  2398. }
  2399. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2400. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2401. return ret;
  2402. }
  2403. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2404. {
  2405. assert(add_params[@"user"]!=nil);
  2406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2407. sqlite3 *db = [iSalesDB get_db];
  2408. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id,discount from offline_order where order_id is null";
  2409. // for(int i=0;i<arr_soid.count;i++)
  2410. sqlite3_stmt * statement;
  2411. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2412. //int count=0;
  2413. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2414. if ( dbresult== SQLITE_OK)
  2415. {
  2416. int count=0;
  2417. while (sqlite3_step(statement) == SQLITE_ROW)
  2418. {
  2419. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2420. int _id = sqlite3_column_int(statement, 0);
  2421. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2422. NSString* nssync_data=nil;
  2423. if(sync_data!=nil)
  2424. {
  2425. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2426. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2427. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2428. }
  2429. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2430. NSString* nsimg_1=nil;
  2431. if(img_1!=nil)
  2432. {
  2433. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2434. [self copy_upImg:serial file:nsimg_1];
  2435. }
  2436. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2437. NSString* nsso_no=nil;
  2438. if(so_no!=nil)
  2439. {
  2440. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2441. }
  2442. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2443. itemjson[@"order_type"]=@"submit order";
  2444. else
  2445. itemjson[@"order_type"]=@"archive order";
  2446. double discount =sqlite3_column_double(statement, 4);
  2447. itemjson[@"discount"]=@(discount);
  2448. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2449. itemjson[@"json_data"]= nssync_data;
  2450. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2451. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2452. count++;
  2453. }
  2454. ret[@"count"]=[NSNumber numberWithInt:count ];
  2455. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2456. }
  2457. sqlite3_finalize(statement);
  2458. [iSalesDB close_db:db];
  2459. return ret;
  2460. }
  2461. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2462. {
  2463. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2464. sqlite3 *db = [iSalesDB get_db];
  2465. // UIApplication * app = [UIApplication sharedApplication];
  2466. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2467. 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";
  2468. sqlite3_stmt * statement;
  2469. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2470. int count=0;
  2471. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2472. if ( dbresult== SQLITE_OK)
  2473. {
  2474. while (sqlite3_step(statement) == SQLITE_ROW)
  2475. {
  2476. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2477. int _id = sqlite3_column_int(statement, 0);
  2478. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2479. NSString* nsimg_0=nil;
  2480. if(img_0!=nil)
  2481. {
  2482. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2483. [self copy_upImg:serial file:nsimg_0];
  2484. }
  2485. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2486. NSString* nsimg_1=nil;
  2487. if(img_1!=nil)
  2488. {
  2489. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2490. [self copy_upImg:serial file:nsimg_1];
  2491. }
  2492. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2493. NSString* nsimg_2=nil;
  2494. if(img_2!=nil)
  2495. {
  2496. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2497. [self copy_upImg:serial file:nsimg_2];
  2498. }
  2499. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2500. NSString* nssync_data=nil;
  2501. if(sync_data!=nil)
  2502. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2503. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2504. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2505. itemjson[@"json_data"]= nssync_data;
  2506. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2507. count++;
  2508. }
  2509. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2510. sqlite3_finalize(statement);
  2511. }
  2512. ret[@"count"]=[NSNumber numberWithInt:count ];
  2513. [iSalesDB close_db:db];
  2514. return ret;
  2515. }
  2516. +(bool) check_offlinedata
  2517. {
  2518. // UIApplication * app = [UIApplication sharedApplication];
  2519. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2520. sqlite3 *db = [iSalesDB get_db];
  2521. NSString * where=@"1=1";
  2522. // if(appDelegate.user!=nil)
  2523. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2524. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2525. [iSalesDB close_db:db];
  2526. if(count==0)
  2527. {
  2528. return false;
  2529. }
  2530. return true;
  2531. //
  2532. //[iSalesDB close_db:db];
  2533. }
  2534. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2535. {
  2536. // UIApplication * app = [UIApplication sharedApplication];
  2537. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2538. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2539. sqlite3 *db = [iSalesDB get_db];
  2540. NSString* collectId=params[@"collectId"];
  2541. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2542. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2543. [iSalesDB execSql:sqlQuery db:db];
  2544. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2545. [iSalesDB close_db:db];
  2546. // appDelegate.wish_count =count;
  2547. //
  2548. // [appDelegate update_count_mark];
  2549. ret[@"result"]= [NSNumber numberWithInt:2];
  2550. ret[@"wish_count"]=@(count);
  2551. return ret;
  2552. }
  2553. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2554. //{
  2555. //
  2556. // UIApplication * app = [UIApplication sharedApplication];
  2557. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2558. //
  2559. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2560. // sqlite3 *db = [iSalesDB get_db];
  2561. // NSString* product_id=params[@"product_id"];
  2562. //
  2563. //
  2564. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2565. //
  2566. //
  2567. // for(int i=0;i<arr.count;i++)
  2568. // {
  2569. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2570. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2571. // if(count==0)
  2572. // {
  2573. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2574. // [iSalesDB execSql:sqlQuery db:db];
  2575. // }
  2576. // }
  2577. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2578. // [iSalesDB close_db:db];
  2579. //
  2580. // appDelegate.wish_count =count;
  2581. //
  2582. // [appDelegate update_count_mark];
  2583. // ret[@"result"]= [NSNumber numberWithInt:2];
  2584. // return ret;
  2585. // //
  2586. // //return ret;
  2587. //}
  2588. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2589. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2590. DebugLog(@"time interval: %lf",interval);
  2591. }
  2592. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2593. //{
  2594. // UIApplication * app = [UIApplication sharedApplication];
  2595. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2596. //
  2597. // int sort = [[params objectForKey:@"sort"] intValue];
  2598. // NSString *sort_str = @"";
  2599. // switch (sort) {
  2600. // case 0:{
  2601. // sort_str = @"order by w.modify_time desc";
  2602. // }
  2603. // break;
  2604. // case 1:{
  2605. // sort_str = @"order by w.modify_time asc";
  2606. // }
  2607. // break;
  2608. // case 2:{
  2609. // sort_str = @"order by m.name asc";
  2610. // }
  2611. // break;
  2612. // case 3:{
  2613. // sort_str = @"order by m.name desc";
  2614. // }
  2615. // break;
  2616. // case 4:{
  2617. // sort_str = @"order by m.description asc";
  2618. // }
  2619. // break;
  2620. //
  2621. // default:
  2622. // break;
  2623. //
  2624. // }
  2625. //
  2626. //
  2627. //// NSString* user = appDelegate.user;
  2628. //
  2629. // sqlite3 *db = [iSalesDB get_db];
  2630. //
  2631. // // order by w.create_time
  2632. // 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];
  2633. //
  2634. //// 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];
  2635. //
  2636. //
  2637. // sqlite3_stmt * statement;
  2638. //
  2639. // NSDate *date1 = [NSDate date];
  2640. //// NSDate *date2 = nil;
  2641. //
  2642. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2643. // int count=0;
  2644. //
  2645. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2646. // {
  2647. //
  2648. //
  2649. // while (sqlite3_step(statement) == SQLITE_ROW)
  2650. // {
  2651. //
  2652. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2653. //
  2654. // int product_id = sqlite3_column_double(statement, 0);
  2655. //
  2656. //
  2657. //
  2658. //
  2659. //
  2660. // char *description = (char*)sqlite3_column_text(statement, 1);
  2661. // if(description==nil)
  2662. // description= "";
  2663. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2664. //
  2665. // int item_id = sqlite3_column_double(statement, 2);
  2666. //
  2667. // NSDate *date_image = [NSDate date];
  2668. //
  2669. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2670. //
  2671. // printf("image : ");
  2672. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2673. //
  2674. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2675. // // if(url==nil)
  2676. // // url="";
  2677. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2678. //
  2679. // int qty = sqlite3_column_int(statement, 3);
  2680. //
  2681. //
  2682. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2683. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2684. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2685. // item[@"description"]= nsdescription;
  2686. // item[@"img"]= nsurl;
  2687. //
  2688. //
  2689. //
  2690. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2691. // count++;
  2692. //
  2693. // }
  2694. // printf("total time:");
  2695. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2696. //
  2697. // ret[@"count"]= [NSNumber numberWithInt:count];
  2698. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2699. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2700. // ret[@"result"]= [NSNumber numberWithInt:2];
  2701. //
  2702. //
  2703. // appDelegate.wish_count =count;
  2704. //
  2705. // [appDelegate update_count_mark];
  2706. // sqlite3_finalize(statement);
  2707. //
  2708. //
  2709. //
  2710. //
  2711. // }
  2712. //
  2713. // [iSalesDB close_db:db];
  2714. //
  2715. // return ret;
  2716. //}
  2717. +(NSDictionary*) offline_notimpl
  2718. {
  2719. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2720. ret[@"result"]=@"8";
  2721. ret[@"err_msg"]=@"offline mode does not support this function.";
  2722. return ret;
  2723. }
  2724. +(NSDictionary*) offline_home
  2725. {
  2726. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2727. NSString *cachefolder = [paths objectAtIndex:0];
  2728. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2729. NSData* json =nil;
  2730. json=[NSData dataWithContentsOfFile:img_cache];
  2731. if(json==nil)
  2732. return nil;
  2733. NSError *error=nil;
  2734. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2735. return menu;
  2736. }
  2737. +(NSDictionary*) offline_category_menu
  2738. {
  2739. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2740. NSString *cachefolder = [paths objectAtIndex:0];
  2741. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2742. NSData* json =nil;
  2743. json=[NSData dataWithContentsOfFile:img_cache];
  2744. if(json==nil)
  2745. return nil;
  2746. NSError *error=nil;
  2747. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2748. return menu;
  2749. }
  2750. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2751. {
  2752. NSString* offline_command=params[@"offline_Command"];
  2753. NSDictionary* ret=nil;
  2754. if([offline_command isEqualToString:@"model_NIYMAL"])
  2755. {
  2756. NSString* category = params[@"category"];
  2757. ret = [self refresh_model_NIYMAL:category];
  2758. }
  2759. return ret;
  2760. }
  2761. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2762. {
  2763. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2764. [ret setValue:@"2" forKey:@"result"];
  2765. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2766. sqlite3* db= [iSalesDB get_db];
  2767. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2768. [iSalesDB close_db:db];
  2769. [ret setObject:detail1_section forKey:@"detail_1"];
  2770. return ret;
  2771. }
  2772. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2773. {
  2774. //assert(user!=nil);
  2775. // UIApplication * app = [UIApplication sharedApplication];
  2776. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2777. // NSArray* arr1 = [self get_user_all_price_type];
  2778. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2779. // NSSet *set1 = [NSSet setWithArray:arr1];
  2780. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2781. // if(appDelegate.contact_id==nil)
  2782. // set2=[set1 mutableCopy];
  2783. // else
  2784. // [set2 intersectsSet:set1];
  2785. // NSArray *retarr = [set2 allObjects];
  2786. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2787. // sqlite3 *db = [iSalesDB get_db];
  2788. NSString* sqlQuery = nil;
  2789. if(contact_id==nil)
  2790. {
  2791. if(!blogin)
  2792. return nil;
  2793. 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];
  2794. }
  2795. else
  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 and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  2797. sqlite3_stmt * statement;
  2798. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2799. int count=0;
  2800. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2801. {
  2802. while (sqlite3_step(statement) == SQLITE_ROW)
  2803. {
  2804. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2805. char *name = (char*)sqlite3_column_text(statement, 0);
  2806. if(name==nil)
  2807. name="";
  2808. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2809. // double price = sqlite3_column_double(statement, 1);
  2810. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2811. // if(isnull==nil)
  2812. // item[nsname]= @"No Price";
  2813. // else
  2814. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2815. char *price = (char*)sqlite3_column_text(statement, 1);
  2816. if(price!=nil)
  2817. {
  2818. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2819. nsprice=[AESCrypt fastdecrypt:nsprice];
  2820. if(nsprice.length>0)
  2821. {
  2822. double dp= [nsprice doubleValue];
  2823. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2824. }
  2825. }
  2826. else
  2827. {
  2828. item[nsname]= @"No Price";
  2829. }
  2830. // int type= sqlite3_column_int(statement, 2);
  2831. //item[@"type"]=@"price";
  2832. // item[nsname]= nsprice;
  2833. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2834. count++;
  2835. }
  2836. ret[@"count"]= [NSNumber numberWithInt:count];
  2837. sqlite3_finalize(statement);
  2838. }
  2839. // [iSalesDB close_db:db];
  2840. return ret;
  2841. }
  2842. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2843. //{
  2844. // NSArray* arr1 = [self get_user_all_price_type:db];
  2845. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2846. //
  2847. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2848. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2849. // // [set2 intersectsSet:set1];
  2850. // //
  2851. // //
  2852. // // NSArray *retarr = [set2 allObjects];
  2853. //
  2854. // NSString* whereprice=nil;
  2855. // if(contact_id==nil)
  2856. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2857. // else
  2858. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2859. //
  2860. //
  2861. // // sqlite3 *db = [iSalesDB get_db];
  2862. //
  2863. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2864. // sqlite3_stmt * statement;
  2865. //
  2866. //
  2867. // NSNumber* ret = nil;
  2868. // double dprice=DBL_MAX;
  2869. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2870. // {
  2871. //
  2872. //
  2873. // while (sqlite3_step(statement) == SQLITE_ROW)
  2874. // {
  2875. //
  2876. // // double val = sqlite3_column_double(statement, 0);
  2877. // char *price = (char*)sqlite3_column_text(statement, 0);
  2878. // if(price!=nil)
  2879. // {
  2880. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2881. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2882. // if(nsprice.length>0)
  2883. // {
  2884. // double dp= [nsprice doubleValue];
  2885. // if(dp<dprice)
  2886. // dprice=dp;
  2887. // }
  2888. // }
  2889. // }
  2890. //
  2891. //
  2892. //
  2893. //
  2894. // sqlite3_finalize(statement);
  2895. //
  2896. //
  2897. //
  2898. //
  2899. // }
  2900. //
  2901. // // [iSalesDB close_db:db];
  2902. //
  2903. // if(dprice==DBL_MAX)
  2904. // ret= nil;
  2905. // else
  2906. // ret= [NSNumber numberWithDouble:dprice];
  2907. // return ret;
  2908. //}
  2909. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2910. {
  2911. assert(user!=nil);
  2912. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2913. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2914. NSString* whereprice=nil;
  2915. if(contact_id==nil)
  2916. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2917. else
  2918. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2919. // sqlite3 *db = [iSalesDB get_db];
  2920. NSString *productIdCondition = @"1 = 1";
  2921. if (product_id) {
  2922. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2923. }
  2924. NSString *itemIdCondition = @"";
  2925. if (item_id) {
  2926. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2927. }
  2928. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2929. sqlite3_stmt * statement;
  2930. NSNumber* ret = nil;
  2931. double dprice=DBL_MAX;
  2932. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2933. {
  2934. while (sqlite3_step(statement) == SQLITE_ROW)
  2935. {
  2936. // double val = sqlite3_column_double(statement, 0);
  2937. char *price = (char*)sqlite3_column_text(statement, 0);
  2938. if(price!=nil)
  2939. {
  2940. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2941. nsprice=[AESCrypt fastdecrypt:nsprice];
  2942. if(nsprice.length>0)
  2943. {
  2944. double dp= [nsprice doubleValue];
  2945. if(dp<dprice)
  2946. dprice=dp;
  2947. }
  2948. }
  2949. }
  2950. sqlite3_finalize(statement);
  2951. }
  2952. // [iSalesDB close_db:db];
  2953. if(dprice==DBL_MAX)
  2954. ret= nil;
  2955. else
  2956. ret= [NSNumber numberWithDouble:dprice];
  2957. return ret;
  2958. }
  2959. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2960. {
  2961. assert(user!=nil);
  2962. NSArray* ret=nil;
  2963. // sqlite3 *db = [iSalesDB get_db];
  2964. // no customer assigned , use login user contact_id
  2965. // UIApplication * app = [UIApplication sharedApplication];
  2966. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2967. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2968. sqlite3_stmt * statement;
  2969. // int count=0;
  2970. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2971. {
  2972. if (sqlite3_step(statement) == SQLITE_ROW)
  2973. {
  2974. char *val = (char*)sqlite3_column_text(statement, 0);
  2975. if(val==nil)
  2976. val="";
  2977. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2978. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2979. }
  2980. sqlite3_finalize(statement);
  2981. }
  2982. // [iSalesDB close_db:db];
  2983. return ret;
  2984. }
  2985. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2986. {
  2987. // sqlite3 *db = [iSalesDB get_db];
  2988. if(contact_id==nil)
  2989. {
  2990. // no customer assigned , use login user contact_id
  2991. // UIApplication * app = [UIApplication sharedApplication];
  2992. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2993. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2994. sqlite3_stmt * statement;
  2995. // int count=0;
  2996. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2997. {
  2998. if (sqlite3_step(statement) == SQLITE_ROW)
  2999. {
  3000. char *val = (char*)sqlite3_column_text(statement, 0);
  3001. if(val==nil)
  3002. val="";
  3003. contact_id = [[NSString alloc]initWithUTF8String:val];
  3004. }
  3005. sqlite3_finalize(statement);
  3006. }
  3007. if(contact_id.length<=0)
  3008. {
  3009. // [iSalesDB close_db:db];
  3010. return nil;
  3011. }
  3012. }
  3013. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3014. sqlite3_stmt * statement;
  3015. NSArray* ret=nil;
  3016. // int count=0;
  3017. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3018. {
  3019. if (sqlite3_step(statement) == SQLITE_ROW)
  3020. {
  3021. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3022. if(price_type==nil)
  3023. price_type="";
  3024. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3025. if(nsprice_type.length>0)
  3026. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3027. else
  3028. ret=nil;
  3029. }
  3030. sqlite3_finalize(statement);
  3031. }
  3032. // [iSalesDB close_db:db];
  3033. return ret;
  3034. }
  3035. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3036. {
  3037. NSString* ret= nil;
  3038. // sqlite3 *db = [iSalesDB get_db];
  3039. NSString *sqlQuery = nil;
  3040. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3041. // 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;
  3042. // 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
  3043. if(product_id==nil && model_name)
  3044. 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;
  3045. else if (product_id)
  3046. 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
  3047. sqlite3_stmt * statement;
  3048. // int count=0;
  3049. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3050. {
  3051. if (sqlite3_step(statement) == SQLITE_ROW)
  3052. {
  3053. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3054. if(imgurl==nil)
  3055. imgurl="";
  3056. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3057. ret=nsimgurl;
  3058. }
  3059. sqlite3_finalize(statement);
  3060. }
  3061. else
  3062. {
  3063. [ret setValue:@"8" forKey:@"result"];
  3064. }
  3065. // [iSalesDB close_db:db];
  3066. DebugLog(@"data string: %@",ret );
  3067. return ret;
  3068. }
  3069. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3070. {
  3071. NSString* ret= nil;
  3072. sqlite3 *db = [iSalesDB get_db];
  3073. NSString *sqlQuery = nil;
  3074. if(product_id==nil)
  3075. if(upc_code==nil)
  3076. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3077. else
  3078. 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='%@';
  3079. else
  3080. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3081. sqlite3_stmt * statement;
  3082. // int count=0;
  3083. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3084. {
  3085. if (sqlite3_step(statement) == SQLITE_ROW)
  3086. {
  3087. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3088. if(default_category==nil)
  3089. default_category="";
  3090. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3091. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3092. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3093. ret=nsdefault_category;
  3094. }
  3095. sqlite3_finalize(statement);
  3096. }
  3097. else
  3098. {
  3099. [ret setValue:@"8" forKey:@"result"];
  3100. }
  3101. [iSalesDB close_db:db];
  3102. DebugLog(@"data string: %@",ret );
  3103. return ret;
  3104. }
  3105. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3106. NSString *sql = nil;
  3107. if (product_id != nil) {
  3108. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3109. } else {
  3110. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3111. }
  3112. NSString *result_set = [iSalesDB jk_queryText:sql];
  3113. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3114. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3115. return result_set;
  3116. }
  3117. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3118. //{
  3119. //
  3120. //
  3121. // assert(params[@"user"]!=nil);
  3122. //
  3123. //
  3124. //
  3125. // NSString* model_name = [params valueForKey:@"product_name"];
  3126. //
  3127. // NSString* product_id = [params valueForKey:@"product_id"];
  3128. //
  3129. // NSString* category = [params valueForKey:@"category"];
  3130. //
  3131. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3132. // if(category==nil) {
  3133. // category = default_category_id;
  3134. // } else {
  3135. //
  3136. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3137. // // 参数重有多个category id
  3138. // if (arr_0.count > 1) {
  3139. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3140. //
  3141. // // 取交集
  3142. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3143. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3144. // [set_0 intersectSet:set_1];
  3145. //
  3146. // if (set_0.count == 1) {
  3147. // category = (NSString *)[set_0 anyObject];
  3148. // } else {
  3149. // if ([set_0 containsObject:default_category_id]) {
  3150. // category = default_category_id;
  3151. // } else {
  3152. // category = (NSString *)[set_0 anyObject];
  3153. // }
  3154. // }
  3155. // }
  3156. // }
  3157. //
  3158. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3159. //
  3160. //
  3161. //
  3162. //
  3163. //
  3164. //
  3165. // sqlite3 *db = [iSalesDB get_db];
  3166. //
  3167. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3168. //
  3169. //
  3170. // NSString *sqlQuery = nil;
  3171. //
  3172. // if(product_id==nil)
  3173. // 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='%@';
  3174. // else
  3175. //
  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 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=%@;
  3177. //
  3178. //
  3179. // sqlite3_stmt * statement;
  3180. // [ret setValue:@"2" forKey:@"result"];
  3181. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3182. //
  3183. // // int count=0;
  3184. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3185. // {
  3186. //
  3187. //
  3188. // if (sqlite3_step(statement) == SQLITE_ROW)
  3189. // {
  3190. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3191. //
  3192. //
  3193. // char *name = (char*)sqlite3_column_text(statement, 0);
  3194. // if(name==nil)
  3195. // name="";
  3196. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3197. //
  3198. // char *description = (char*)sqlite3_column_text(statement, 1);
  3199. // if(description==nil)
  3200. // description="";
  3201. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3202. //
  3203. //
  3204. // int product_id = sqlite3_column_int(statement, 2);
  3205. //
  3206. //
  3207. // char *color = (char*)sqlite3_column_text(statement, 3);
  3208. // if(color==nil)
  3209. // color="";
  3210. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3211. // //
  3212. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3213. // // if(legcolor==nil)
  3214. // // legcolor="";
  3215. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3216. // //
  3217. // //
  3218. // int availability = sqlite3_column_int(statement, 5);
  3219. // //
  3220. // int incoming_stock = sqlite3_column_int(statement, 6);
  3221. //
  3222. //
  3223. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3224. // if(demension==nil)
  3225. // demension="";
  3226. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3227. //
  3228. //
  3229. //
  3230. // // ,,,,,,,,,
  3231. //
  3232. //
  3233. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3234. // if(seat_height==nil)
  3235. // seat_height="";
  3236. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3237. //
  3238. //
  3239. // char *material = (char*)sqlite3_column_text(statement, 9);
  3240. // if(material==nil)
  3241. // material="";
  3242. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3243. //
  3244. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3245. // if(box_dim==nil)
  3246. // box_dim="";
  3247. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3248. //
  3249. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3250. // if(volume==nil)
  3251. // volume="";
  3252. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3253. //
  3254. // double weight = sqlite3_column_double(statement, 12);
  3255. //
  3256. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3257. // if(model_set==nil)
  3258. // model_set="";
  3259. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3260. //
  3261. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3262. // if(load_ability==nil)
  3263. // load_ability="";
  3264. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3265. //
  3266. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3267. //// if(default_category==nil)
  3268. //// default_category="";
  3269. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3270. //
  3271. //
  3272. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3273. // if(fabric_content==nil)
  3274. // fabric_content="";
  3275. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3276. //
  3277. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3278. // if(assembling==nil)
  3279. // assembling="";
  3280. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3281. //
  3282. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3283. // if(made_in==nil)
  3284. // made_in="";
  3285. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3286. //
  3287. //
  3288. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3289. // if(special_remarks==nil)
  3290. // special_remarks="";
  3291. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3292. //
  3293. //
  3294. // int stockUcom = sqlite3_column_double(statement, 20);
  3295. //
  3296. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3297. // if(product_group==nil)
  3298. // product_group="";
  3299. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3300. //
  3301. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3302. // // if(fashion_selector==nil)
  3303. // // fashion_selector="";
  3304. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3305. //
  3306. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3307. // if(selector_field==nil)
  3308. // selector_field="";
  3309. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3310. //
  3311. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3312. // if(property_field==nil)
  3313. // property_field="";
  3314. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3315. //
  3316. //
  3317. //
  3318. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3319. // if(packaging==nil)
  3320. // packaging="";
  3321. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3322. //
  3323. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3324. //
  3325. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3326. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3327. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3328. //
  3329. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3330. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3331. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3332. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3333. //
  3334. //
  3335. //
  3336. //
  3337. //
  3338. // NSString* Availability=nil;
  3339. // if(availability>0)
  3340. // Availability=[NSString stringWithFormat:@"%d",availability];
  3341. // else
  3342. // Availability = @"Out of Stock";
  3343. //
  3344. // [img_section setValue:Availability forKey:@"Availability"];
  3345. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3346. //
  3347. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3348. //
  3349. //
  3350. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3351. // if(eta==nil)
  3352. // eta="";
  3353. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3354. // if ([nseta isEqualToString:@"null"]) {
  3355. // nseta = @"";
  3356. // }
  3357. // if (availability <= 0) {
  3358. // [img_section setValue:nseta forKey:@"ETA"];
  3359. // }
  3360. //
  3361. //
  3362. // int item_id = sqlite3_column_int(statement, 26);
  3363. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3364. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3365. //
  3366. // NSString* Price=nil;
  3367. // if(appDelegate.bLogin==false)
  3368. // Price=@"Must Sign in.";
  3369. // else
  3370. // {
  3371. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3372. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3373. // if(price==nil)
  3374. // Price=@"No Price.";
  3375. // else
  3376. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3377. // }
  3378. //
  3379. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3380. // [img_section setValue:Price forKey:@"price"];
  3381. // [img_section setValue:nsname forKey:@"model_name"];
  3382. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3383. //
  3384. // if (appDelegate.order_code) { // 离线order code即so#
  3385. //
  3386. // 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];
  3387. // __block int cartQTY = 0;
  3388. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3389. //
  3390. // cartQTY = sqlite3_column_int(stmt, 0);
  3391. //
  3392. // }];
  3393. //
  3394. // if (cartQTY > 0) {
  3395. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3396. // }
  3397. // }
  3398. //
  3399. // [ret setObject:img_section forKey:@"img_section"];
  3400. //
  3401. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3402. // int detail0_item_count=0;
  3403. //
  3404. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3405. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3406. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3407. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3408. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3409. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3410. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3411. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3412. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3413. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3414. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3415. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3416. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3417. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3418. //
  3419. //
  3420. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3421. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3422. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3423. // {
  3424. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3425. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3426. // }
  3427. //
  3428. //
  3429. //
  3430. //
  3431. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3432. // [detail0_section setValue:@"kv" forKey:@"type"];
  3433. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3434. //
  3435. // [ret setObject:detail0_section forKey:@"detail_0"];
  3436. //
  3437. //
  3438. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3439. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3440. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3441. // // [detail1_section setValue:@"content" forKey:@"type"];
  3442. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3443. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3444. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3445. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3446. // [ret setObject:detail1_section forKey:@"detail_1"];
  3447. //
  3448. //
  3449. //
  3450. //
  3451. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3452. // [detail2_section setValue:@"detail" forKey:@"target"];
  3453. // [detail2_section setValue:@"popup" forKey:@"action"];
  3454. // [detail2_section setValue:@"content" forKey:@"type"];
  3455. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3456. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3457. // [detail2_section setValue:@"local" forKey:@"data"];
  3458. // [ret setObject:detail2_section forKey:@"detail_2"];
  3459. // }
  3460. //
  3461. //
  3462. //
  3463. //
  3464. // sqlite3_finalize(statement);
  3465. // }
  3466. // else
  3467. // {
  3468. // [ret setValue:@"8" forKey:@"result"];
  3469. // }
  3470. //// DebugLog(@"count:%d",count);
  3471. //
  3472. //
  3473. // [iSalesDB close_db:db];
  3474. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3475. //
  3476. // return ret;
  3477. //}
  3478. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3479. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3480. NSString* keyword = [params valueForKey:@"keyword"];
  3481. keyword=keyword.lowercaseString;
  3482. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3483. int limit = [[params valueForKey:@"limit"] intValue];
  3484. int offset = [[params valueForKey:@"offset"] intValue];
  3485. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3486. NSString *limit_str = @"";
  3487. if (limited) {
  3488. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3489. }
  3490. sqlite3 *db = [iSalesDB get_db];
  3491. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3492. // UIApplication * app = [UIApplication sharedApplication];
  3493. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3494. NSString *sqlQuery = nil;
  3495. if(exactMatch )
  3496. 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 ;
  3497. else
  3498. 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
  3499. DebugLog(@"offline_search sql:%@",sqlQuery);
  3500. sqlite3_stmt * statement;
  3501. [ret setValue:@"2" forKey:@"result"];
  3502. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3503. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3504. // int count=0;
  3505. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3506. {
  3507. int i=0;
  3508. while (sqlite3_step(statement) == SQLITE_ROW)
  3509. {
  3510. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3511. // char *name = (char*)sqlite3_column_text(statement, 1);
  3512. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3513. char *name = (char*)sqlite3_column_text(statement, 0);
  3514. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3515. char *description = (char*)sqlite3_column_text(statement, 1);
  3516. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3517. int product_id = sqlite3_column_int(statement, 2);
  3518. // char *url = (char*)sqlite3_column_text(statement, 3);
  3519. // if(url==nil)
  3520. // url="";
  3521. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3522. int wid = sqlite3_column_int(statement, 3);
  3523. int closeout = sqlite3_column_int(statement, 4);
  3524. int cid = sqlite3_column_int(statement, 5);
  3525. int wisdelete = sqlite3_column_int(statement, 6);
  3526. int more_color = sqlite3_column_int(statement, 7);
  3527. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3528. if(wid !=0 && wisdelete != 1)
  3529. [item setValue:@"true" forKey:@"wish_exists"];
  3530. else
  3531. [item setValue:@"false" forKey:@"wish_exists"];
  3532. if(closeout==0)
  3533. [item setValue:@"false" forKey:@"is_closeout"];
  3534. else
  3535. [item setValue:@"true" forKey:@"is_closeout"];
  3536. if(cid==0)
  3537. [item setValue:@"false" forKey:@"cart_exists"];
  3538. else
  3539. [item setValue:@"true" forKey:@"cart_exists"];
  3540. if (more_color == 0) {
  3541. [item setObject:@(false) forKey:@"more_color"];
  3542. } else if (more_color == 1) {
  3543. [item setObject:@(true) forKey:@"more_color"];
  3544. }
  3545. [item addEntriesFromDictionary:imgjson];
  3546. // [item setValue:nsurl forKey:@"img"];
  3547. [item setValue:nsname forKey:@"fash_name"];
  3548. [item setValue:nsdescription forKey:@"description"];
  3549. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3550. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3551. i++;
  3552. }
  3553. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3554. [ret setObject:items forKey:@"items"];
  3555. sqlite3_finalize(statement);
  3556. }
  3557. DebugLog(@"count:%d",count);
  3558. [iSalesDB close_db:db];
  3559. #ifdef DEBUG
  3560. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3561. #endif
  3562. return ret;
  3563. }
  3564. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3565. {
  3566. return [self search:params limited:YES];
  3567. }
  3568. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3569. //{
  3570. // UIApplication * app = [UIApplication sharedApplication];
  3571. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3572. //
  3573. //// [iSalesDB disable_trigger]
  3574. // [iSalesDB disable_trigger];
  3575. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3576. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3577. //
  3578. // [iSalesDB enable_trigger];
  3579. //// [iSalesDB enable_trigger]
  3580. // //
  3581. // // NSString* user = [params valueForKey:@"user"];
  3582. // //
  3583. // // NSString* password = [params valueForKey:@"password"];
  3584. //
  3585. //
  3586. //
  3587. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3588. //
  3589. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3590. //
  3591. //
  3592. //
  3593. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3594. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3595. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3596. //
  3597. // [appDelegate update_count_mark];
  3598. //
  3599. //
  3600. // appDelegate.can_show_price =false;
  3601. // appDelegate.can_see_price =false;
  3602. // appDelegate.can_create_portfolio =false;
  3603. // appDelegate.can_create_order =false;
  3604. //
  3605. //
  3606. // appDelegate.can_cancel_order =false;
  3607. // appDelegate.can_set_cart_price =false;
  3608. // appDelegate.can_delete_order =false;
  3609. // appDelegate.can_submit_order =false;
  3610. // appDelegate.can_set_tearsheet_price =false;
  3611. // appDelegate.can_update_contact_info = false;
  3612. //
  3613. // appDelegate.save_order_logout = false;
  3614. // appDelegate.submit_order_logout = false;
  3615. // appDelegate.alert_sold_in_quantities = false;
  3616. //
  3617. // appDelegate.ipad_perm =nil ;
  3618. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3619. // appDelegate.OrderFilter= nil;
  3620. // [appDelegate SetSo:nil];
  3621. // [appDelegate set_main_button_panel];
  3622. //
  3623. //
  3624. // // sqlite3 *db = [iSalesDB get_db];
  3625. // //
  3626. // //
  3627. // //
  3628. // //
  3629. // //
  3630. // // 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"]];
  3631. // //
  3632. // //
  3633. // //
  3634. // //
  3635. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3636. // // sqlite3_stmt * statement;
  3637. // //
  3638. // //
  3639. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3640. // //
  3641. // //
  3642. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3643. // // {
  3644. // //
  3645. // //
  3646. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3647. // // {
  3648. // //
  3649. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3650. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3651. // //
  3652. // //
  3653. // //
  3654. // // int can_show_price = sqlite3_column_int(statement, 0);
  3655. // // int can_see_price = sqlite3_column_int(statement, 1);
  3656. // //
  3657. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3658. // // if(contact_id==nil)
  3659. // // contact_id="";
  3660. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3661. // //
  3662. // // int user_type = sqlite3_column_int(statement, 3);
  3663. // //
  3664. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3665. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3666. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3667. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3668. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3669. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3670. // // int can_create_order = sqlite3_column_int(statement, 10);
  3671. // //
  3672. // //
  3673. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3674. // // if(mode==nil)
  3675. // // mode="";
  3676. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3677. // //
  3678. // //
  3679. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3680. // // if(username==nil)
  3681. // // username="";
  3682. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3683. // //
  3684. // //
  3685. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3686. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3687. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3688. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3689. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3690. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3691. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3692. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3693. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3694. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3695. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3696. // //
  3697. // // [header setValue:nsusername forKey:@"username"];
  3698. // //
  3699. // //
  3700. // // [ret setObject:header forKey:@"header"];
  3701. // // [ret setValue:nsmode forKey:@"mode"];
  3702. // //
  3703. // //
  3704. // // }
  3705. // //
  3706. // //
  3707. // //
  3708. // // sqlite3_finalize(statement);
  3709. // // }
  3710. // //
  3711. // //
  3712. // //
  3713. // // [iSalesDB close_db:db];
  3714. // //
  3715. // //
  3716. // //
  3717. // //
  3718. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3719. //
  3720. // return ret;
  3721. //}
  3722. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3723. //{
  3724. //
  3725. // UIApplication * app = [UIApplication sharedApplication];
  3726. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3727. //
  3728. // [iSalesDB disable_trigger];
  3729. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3730. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3731. // [iSalesDB enable_trigger];
  3732. //
  3733. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3734. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3735. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3736. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3737. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3738. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3739. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3740. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3741. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3742. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3743. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3744. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3745. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3746. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3747. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3748. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3749. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3750. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3751. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3752. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3753. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3754. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3755. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3756. //
  3757. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3758. //
  3759. // [arr_name addObject:customer_first_name];
  3760. // [arr_name addObject:customer_last_name];
  3761. //
  3762. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3763. //
  3764. // // default ship from
  3765. // 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';";
  3766. //
  3767. // __block NSString *cid = @"";
  3768. // __block NSString *name = @"";
  3769. // __block NSString *ext = @"";
  3770. // __block NSString *contact = @"";
  3771. // __block NSString *email = @"";
  3772. // __block NSString *fax = @"";
  3773. // __block NSString *phone = @"";
  3774. //
  3775. // sqlite3 *db = [iSalesDB get_db];
  3776. //
  3777. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3778. //
  3779. // cid = [self textAtColumn:0 statement:statment];
  3780. // name = [self textAtColumn:1 statement:statment];
  3781. // ext = [self textAtColumn:2 statement:statment];
  3782. // contact = [self textAtColumn:3 statement:statment];
  3783. // email = [self textAtColumn:4 statement:statment];
  3784. // fax = [self textAtColumn:5 statement:statment];
  3785. // phone = [self textAtColumn:6 statement:statment];
  3786. //
  3787. // }];
  3788. //
  3789. // NSString* so_id = [self get_offline_soid:db];
  3790. // if(so_id==nil)
  3791. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3792. //
  3793. // 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];
  3794. //
  3795. //
  3796. //
  3797. // int result =[iSalesDB execSql:sql_neworder db:db];
  3798. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3799. //
  3800. //
  3801. //
  3802. // //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'
  3803. // //soId
  3804. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3805. //
  3806. //
  3807. //
  3808. //
  3809. //
  3810. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3811. // sqlite3_stmt * statement;
  3812. //
  3813. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3814. // {
  3815. // if (sqlite3_step(statement) == SQLITE_ROW)
  3816. // {
  3817. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3818. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3819. //
  3820. // //ret = sqlite3_column_int(statement, 0);
  3821. //
  3822. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3823. // if(soId==nil)
  3824. // soId="";
  3825. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3826. // [ret setValue:nssoId forKey:@"soId"];
  3827. // [ret setValue:nssoId forKey:@"orderCode"];
  3828. //
  3829. // }
  3830. // sqlite3_finalize(statement);
  3831. // }
  3832. //
  3833. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3834. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3835. //
  3836. //
  3837. // [iSalesDB close_db:db];
  3838. //
  3839. // return [RAConvertor dict2data:ret];
  3840. //
  3841. //}
  3842. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3843. {
  3844. //assert(params[@"order_code"]);
  3845. // assert(params[@"order_code"]);
  3846. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3847. NSString* orderCode = [params valueForKey:@"orderCode"];
  3848. NSString* app_order_code= params[@"appDelegate.order_code"];
  3849. // UIApplication * app = [UIApplication sharedApplication];
  3850. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3851. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3852. {
  3853. [iSalesDB disable_trigger];
  3854. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3855. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3856. [iSalesDB enable_trigger];
  3857. }
  3858. sqlite3 *db = [iSalesDB get_db];
  3859. int cart_count=[self query_ordercartcount:orderCode db:db];
  3860. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3861. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3862. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3863. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3864. [iSalesDB close_db:db];
  3865. return [RAConvertor dict2data:ret];
  3866. }
  3867. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3868. {
  3869. assert(params[@"can_create_backorder"]!=nil);
  3870. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3871. sqlite3 *db = [iSalesDB get_db];
  3872. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3873. int count =0;
  3874. if(params[@"count"]!=nil)
  3875. {
  3876. count = [params[@"count"] intValue];
  3877. }
  3878. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3879. NSString* product_id=params[@"product_id"];
  3880. NSString* orderCode=params[@"orderCode"];
  3881. NSString *qty = params[@"qty"];
  3882. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3883. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3884. __block int number_of_outOfStock = 0;
  3885. for(int i=0;i<arr_id.count;i++)
  3886. {
  3887. NSInteger item_qty= count;
  3888. if (qty) {
  3889. item_qty = [qty_arr[i] integerValue];
  3890. }
  3891. if(item_qty==0)
  3892. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3893. // 检查新加Model数量是否大于库存
  3894. if (![params[@"can_create_backorder"] boolValue]) {
  3895. __block BOOL needContinue = NO;
  3896. [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) {
  3897. int availability = sqlite3_column_int(stmt, 0);
  3898. // 库存小于购买量为缺货
  3899. if (availability < item_qty || availability <= 0) {
  3900. number_of_outOfStock++;
  3901. needContinue = YES;
  3902. }
  3903. }];
  3904. if (needContinue) {
  3905. continue;
  3906. }
  3907. }
  3908. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3909. NSString* sql=nil;
  3910. sqlite3_stmt *stmt = nil;
  3911. BOOL shouldStep = NO;
  3912. if(_id<0)
  3913. {
  3914. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3915. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3916. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3917. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3918. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3919. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3920. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3921. shouldStep = YES;
  3922. }
  3923. else
  3924. {
  3925. if (qty) { // wish list move to cart
  3926. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3927. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3928. sqlite3_bind_int(stmt, 1, _id);
  3929. shouldStep = YES;
  3930. } else {
  3931. 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];
  3932. __block BOOL update = YES;
  3933. if (![params[@"can_create_backorder"] boolValue]) {
  3934. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3935. int newQTY = sqlite3_column_int(stmt, 0);
  3936. int availability = sqlite3_column_int(stmt, 1);
  3937. if (newQTY > availability) { // 库存不够
  3938. update = NO;
  3939. number_of_outOfStock++;
  3940. }
  3941. }];
  3942. }
  3943. if (update) {
  3944. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3945. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3946. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3947. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3948. shouldStep = YES;
  3949. }
  3950. }
  3951. }
  3952. if (shouldStep) {
  3953. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3954. [iSalesDB execSql:@"ROLLBACK" db:db];
  3955. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3956. [iSalesDB close_db:db];
  3957. DebugLog(@"add to cart error");
  3958. return [RAConvertor dict2data:ret];
  3959. }
  3960. }
  3961. }
  3962. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3963. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3964. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3965. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3966. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3967. if (![params[@"can_create_backorder"] boolValue]) {
  3968. if (number_of_outOfStock > 0) {
  3969. ret[@"result"]=[NSNumber numberWithInt:8];
  3970. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3971. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3972. }
  3973. }
  3974. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3975. [iSalesDB close_db:db];
  3976. return [RAConvertor dict2data:ret];
  3977. }
  3978. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3979. {
  3980. // UIApplication * app = [UIApplication sharedApplication];
  3981. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3982. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3983. 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];
  3984. sqlite3_stmt * statement;
  3985. int count=0;
  3986. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3987. {
  3988. while (sqlite3_step(statement) == SQLITE_ROW)
  3989. {
  3990. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3991. int bitem_id = sqlite3_column_int(statement, 0);
  3992. int bitem_qty = sqlite3_column_int(statement, 1);
  3993. char *name = (char*)sqlite3_column_text(statement, 2);
  3994. if(name==nil)
  3995. name="";
  3996. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3997. char *description = (char*)sqlite3_column_text(statement, 3);
  3998. if(description==nil)
  3999. description="";
  4000. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4001. double unit_price = sqlite3_column_double(statement, 4);
  4002. int use_unitprice = sqlite3_column_int(statement, 5);
  4003. if(use_unitprice!=1)
  4004. {
  4005. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4006. }
  4007. itemjson[@"model"]=nsname;
  4008. itemjson[@"description"]=nsdescription;
  4009. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4010. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4011. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4012. if(compute)
  4013. {
  4014. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4015. }
  4016. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4017. count++;
  4018. }
  4019. sqlite3_finalize(statement);
  4020. }
  4021. ret[@"count"]=@(count);
  4022. if(count==0)
  4023. return nil;
  4024. else
  4025. return ret;
  4026. }
  4027. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4028. {
  4029. //compute: add part to subtotal;
  4030. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4031. dict_item[@(item_id)]=@"1";
  4032. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4033. double cuft=0;
  4034. double weight=0;
  4035. int carton=0;
  4036. int impack=0;
  4037. 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];
  4038. sqlite3_stmt * statement;
  4039. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4040. {
  4041. if (sqlite3_step(statement) == SQLITE_ROW)
  4042. {
  4043. double ulength = sqlite3_column_double(statement, 0);
  4044. double uwidth = sqlite3_column_double(statement, 1);
  4045. double uheight = sqlite3_column_double(statement, 2);
  4046. double uweight = sqlite3_column_double(statement, 3);
  4047. double mlength = sqlite3_column_double(statement, 4);
  4048. double mwidth = sqlite3_column_double(statement, 5);
  4049. double mheight = sqlite3_column_double(statement, 6);
  4050. double mweight = sqlite3_column_double(statement, 7);
  4051. double ilength = sqlite3_column_double(statement, 8);
  4052. double iwidth = sqlite3_column_double(statement, 9);
  4053. double iheight = sqlite3_column_double(statement, 10);
  4054. double iweight = sqlite3_column_double(statement, 11);
  4055. // int pcs = sqlite3_column_int(statement,12);
  4056. int mpack = sqlite3_column_int(statement, 13);
  4057. impack = mpack;
  4058. int ipack = sqlite3_column_int(statement, 14);
  4059. double ucbf = sqlite3_column_double(statement, 15);
  4060. // double icbf = sqlite3_column_double(statement, 16);
  4061. // double mcbf = sqlite3_column_double(statement, 17);
  4062. if(ipack==0)
  4063. {
  4064. carton= count/mpack ;
  4065. weight = mweight*carton;
  4066. cuft= carton*(mlength*mwidth*mheight);
  4067. int remain=count%mpack;
  4068. if(remain==0)
  4069. {
  4070. //do nothing;
  4071. }
  4072. else
  4073. {
  4074. carton++;
  4075. weight += uweight*remain;
  4076. cuft += (ulength*uwidth*uheight)*remain;
  4077. }
  4078. }
  4079. else
  4080. {
  4081. carton = count/(mpack*ipack);
  4082. weight = mweight*carton;
  4083. cuft= carton*(mlength*mwidth*mheight);
  4084. int remain=count%(mpack*ipack);
  4085. if(remain==0)
  4086. {
  4087. // do nothing;
  4088. }
  4089. else
  4090. {
  4091. carton++;
  4092. int icarton =remain/ipack;
  4093. int iremain=remain%ipack;
  4094. weight += iweight*icarton;
  4095. cuft += (ilength*iwidth*iheight)*icarton;
  4096. if(iremain==0)
  4097. {
  4098. //do nothing;
  4099. }
  4100. else
  4101. {
  4102. weight += uweight*iremain;
  4103. cuft += (ulength*uwidth*uheight)*iremain;
  4104. }
  4105. }
  4106. }
  4107. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4108. cuft=ucbf*count;
  4109. weight= uweight*count;
  4110. #endif
  4111. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4112. cuft=ucbf*count;
  4113. weight= uweight*count;
  4114. #endif
  4115. }
  4116. sqlite3_finalize(statement);
  4117. }
  4118. if(compute)
  4119. {
  4120. NSArray * arr_count=nil;
  4121. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4122. for(int i=0;i<arr_bundle.count;i++)
  4123. {
  4124. dict_item[arr_bundle[i]]=@"1";
  4125. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4126. cuft+=[bundlejson[@"cuft"] doubleValue];
  4127. weight+=[bundlejson[@"weight"] doubleValue];
  4128. carton+=[bundlejson[@"carton"] intValue];
  4129. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4130. }
  4131. }
  4132. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4133. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4134. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4135. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4136. ret[@"items"]=dict_item;
  4137. return ret;
  4138. }
  4139. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4140. {
  4141. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4142. // 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 ];
  4143. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4144. DebugLog(@"offline_login sql:%@",sqlQuery);
  4145. sqlite3_stmt * statement;
  4146. int cart_count=0;
  4147. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4148. if ( dbresult== SQLITE_OK)
  4149. {
  4150. while (sqlite3_step(statement) == SQLITE_ROW)
  4151. {
  4152. int item_id = sqlite3_column_int(statement, 0);
  4153. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4154. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4155. }
  4156. sqlite3_finalize(statement);
  4157. }
  4158. return cart_count;
  4159. }
  4160. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4161. //{
  4162. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4163. // sqlite3 *db = [iSalesDB get_db];
  4164. // UIApplication * app = [UIApplication sharedApplication];
  4165. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4166. //
  4167. // NSString* orderCode=params[@"orderCode"];
  4168. //
  4169. // int sort = [[params objectForKey:@"sort"] intValue];
  4170. // NSString *sort_str = @"";
  4171. // switch (sort) {
  4172. // case 0:{
  4173. // sort_str = @"order by c.modify_time desc";
  4174. // }
  4175. // break;
  4176. // case 1:{
  4177. // sort_str = @"order by c.modify_time asc";
  4178. // }
  4179. // break;
  4180. // case 2:{
  4181. // sort_str = @"order by m.name asc";
  4182. // }
  4183. // break;
  4184. // case 3:{
  4185. // sort_str = @"order by m.name desc";
  4186. // }
  4187. // break;
  4188. // case 4:{
  4189. // sort_str = @"order by m.description asc";
  4190. // }
  4191. // break;
  4192. //
  4193. // default:
  4194. // break;
  4195. //
  4196. // }
  4197. //
  4198. //
  4199. //
  4200. // 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 ];
  4201. //
  4202. //
  4203. //// 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 ];
  4204. //
  4205. //
  4206. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4207. //
  4208. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4209. // sqlite3_stmt * statement;
  4210. //
  4211. //
  4212. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4213. //
  4214. // NSDate *date1 = [NSDate date];
  4215. //
  4216. // int count=0;
  4217. // int cart_count=0;
  4218. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4219. // if ( dbresult== SQLITE_OK)
  4220. // {
  4221. //
  4222. //
  4223. // while (sqlite3_step(statement) == SQLITE_ROW)
  4224. // {
  4225. //// NSDate *row_date = [NSDate date];
  4226. //
  4227. //
  4228. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4229. //
  4230. //
  4231. //
  4232. //
  4233. // int product_id = sqlite3_column_int(statement, 0);
  4234. //
  4235. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4236. //
  4237. // int item_id = sqlite3_column_int(statement, 7);
  4238. //
  4239. // NSString* Price=nil;
  4240. // if(str_price==nil)
  4241. // {
  4242. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4243. //// NSDate *price_date = [NSDate date];
  4244. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4245. //// DebugLog(@"price time interval");
  4246. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4247. //
  4248. // if(price==nil)
  4249. // Price=@"No Price.";
  4250. // else
  4251. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4252. // }
  4253. // else
  4254. // {
  4255. //
  4256. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4257. // }
  4258. //
  4259. //
  4260. // double discount = sqlite3_column_double(statement, 2);
  4261. // int item_count = sqlite3_column_int(statement, 3);
  4262. //
  4263. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4264. // NSString *nsline_note=nil;
  4265. // if(line_note!=nil)
  4266. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4267. //
  4268. //
  4269. // char *name = (char*)sqlite3_column_text(statement, 5);
  4270. // NSString *nsname=nil;
  4271. // if(name!=nil)
  4272. // nsname= [[NSString alloc]initWithUTF8String:name];
  4273. //
  4274. // char *description = (char*)sqlite3_column_text(statement, 6);
  4275. // NSString *nsdescription=nil;
  4276. // if(description!=nil)
  4277. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4278. //
  4279. //
  4280. //
  4281. // int stockUom = sqlite3_column_int(statement, 8);
  4282. // int _id = sqlite3_column_int(statement, 9);
  4283. // int availability = sqlite3_column_int(statement, 10);
  4284. //
  4285. //// NSDate *subtotal_date = [NSDate date];
  4286. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4287. //// DebugLog(@"subtotal_date time interval");
  4288. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4289. //
  4290. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4291. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4292. // int carton=[bsubtotaljson[@"carton"] intValue];
  4293. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4294. //
  4295. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4296. //// NSDate *img_date = [NSDate date];
  4297. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4298. //// DebugLog(@"img_date time interval");
  4299. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4300. //
  4301. // itemjson[@"model"]=nsname;
  4302. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4303. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4304. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4305. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4306. // itemjson[@"check"]=@"true";
  4307. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4308. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4309. // itemjson[@"unit_price"]=Price;
  4310. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4311. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4312. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4313. // itemjson[@"note"]=nsline_note;
  4314. // if (!appDelegate.can_create_backorder) {
  4315. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4316. // }
  4317. //// NSDate *date2 = [NSDate date];
  4318. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4319. //// DebugLog(@"model_bundle time interval");
  4320. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4321. //
  4322. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4323. // count++;
  4324. //
  4325. //// DebugLog(@"row time interval");
  4326. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4327. // }
  4328. //
  4329. //
  4330. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4331. // sqlite3_finalize(statement);
  4332. // }
  4333. //
  4334. //
  4335. // DebugLog(@"request cart total time interval");
  4336. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4337. //
  4338. //
  4339. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4340. //
  4341. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4342. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4343. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4344. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4345. //
  4346. // ret[@"mode"]=@"Regular Mode";
  4347. //
  4348. // [iSalesDB close_db:db];
  4349. //
  4350. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4351. //
  4352. // DebugLog(@"general notes :%@",general_note);
  4353. //
  4354. // ret[@"general_note"]= general_note;
  4355. //
  4356. // return [RAConvertor dict2data:ret];
  4357. //}
  4358. +(NSData*) offline_login :(NSMutableDictionary *) params
  4359. {
  4360. NSString* user = [params valueForKey:@"user"];
  4361. NSString* password = [params valueForKey:@"password"];
  4362. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4363. sqlite3 *db = [iSalesDB get_db];
  4364. 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"]];
  4365. DebugLog(@"offline_login sql:%@",sqlQuery);
  4366. sqlite3_stmt * statement;
  4367. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4368. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4369. {
  4370. if (sqlite3_step(statement) == SQLITE_ROW)
  4371. {
  4372. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4373. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4374. int can_show_price = sqlite3_column_int(statement, 0);
  4375. int can_see_price = sqlite3_column_int(statement, 1);
  4376. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4377. if(contact_id==nil)
  4378. contact_id="";
  4379. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4380. int user_type = sqlite3_column_int(statement, 3);
  4381. int can_cancel_order = sqlite3_column_int(statement, 4);
  4382. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4383. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4384. int can_delete_order = sqlite3_column_int(statement, 7);
  4385. int can_submit_order = sqlite3_column_int(statement, 8);
  4386. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4387. int can_create_order = sqlite3_column_int(statement, 10);
  4388. char *mode = (char*)sqlite3_column_text(statement, 11);
  4389. if(mode==nil)
  4390. mode="";
  4391. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4392. char *username = (char*)sqlite3_column_text(statement, 12);
  4393. if(username==nil)
  4394. username="";
  4395. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4396. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4397. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4398. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4399. [header setValue:nscontact_id forKey:@"contact_id"];
  4400. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4401. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4402. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4403. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4404. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4405. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4406. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4407. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4408. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4409. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4410. [header setValue:nsusername forKey:@"username"];
  4411. NSError* error=nil;
  4412. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4413. [header setValue:statusFilter forKey:@"statusFilter"];
  4414. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4415. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4416. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4417. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4418. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4419. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4420. [ret setObject:header forKey:@"header"];
  4421. [ret setValue:nsmode forKey:@"mode"];
  4422. }
  4423. sqlite3_finalize(statement);
  4424. }
  4425. [iSalesDB close_db:db];
  4426. #ifdef DEBUG
  4427. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4428. #endif
  4429. return [RAConvertor dict2data:ret];
  4430. }
  4431. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4432. {
  4433. assert(params[@"mode"]!=nil);
  4434. NSString* contactId = [params valueForKey:@"contactId"];
  4435. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4436. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4437. sqlite3 *db = [iSalesDB get_db];
  4438. NSString *sqlQuery = nil;
  4439. {
  4440. 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];
  4441. }
  4442. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4443. sqlite3_stmt * statement;
  4444. [ret setValue:@"2" forKey:@"result"];
  4445. int carrier = -1;
  4446. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4447. {
  4448. //int i = 0;
  4449. if (sqlite3_step(statement) == SQLITE_ROW)
  4450. {
  4451. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4452. // int editable = sqlite3_column_int(statement, 0);
  4453. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4454. NSString *nscompany_name =nil;
  4455. if(company_name==nil)
  4456. nscompany_name=@"";
  4457. else
  4458. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4459. char *country = (char*)sqlite3_column_text(statement, 2);
  4460. if(country==nil)
  4461. country="";
  4462. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4463. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4464. // if(addr==nil)
  4465. // addr="";
  4466. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4467. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4468. if(zipcode==nil)
  4469. zipcode="";
  4470. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4471. char *state = (char*)sqlite3_column_text(statement, 5);
  4472. if(state==nil)
  4473. state="";
  4474. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4475. char *city = (char*)sqlite3_column_text(statement, 6);
  4476. if(city==nil)
  4477. city="";
  4478. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4479. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4480. // NSString *nscontact_name = nil;
  4481. // if(contact_name==nil)
  4482. // nscontact_name=@"";
  4483. // else
  4484. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4485. char *phone = (char*)sqlite3_column_text(statement, 8);
  4486. NSString *nsphone = nil;
  4487. if(phone==nil)
  4488. nsphone=@"";
  4489. else
  4490. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4491. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4492. // if(contact_id==nil)
  4493. // contact_id="";
  4494. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4495. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4496. if(addr_1==nil)
  4497. addr_1="";
  4498. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4499. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4500. if(addr_2==nil)
  4501. addr_2="";
  4502. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4503. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4504. if(addr_3==nil)
  4505. addr_3="";
  4506. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4507. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4508. if(addr_4==nil)
  4509. addr_4="";
  4510. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4511. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4512. if(first_name==nil)
  4513. first_name="";
  4514. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4515. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4516. if(last_name==nil)
  4517. last_name="";
  4518. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4519. char *fax = (char*)sqlite3_column_text(statement, 16);
  4520. NSString *nsfax = nil;
  4521. if(fax==nil)
  4522. nsfax=@"";
  4523. else
  4524. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4525. char *email = (char*)sqlite3_column_text(statement, 17);
  4526. NSString *nsemail = nil;
  4527. if(email==nil)
  4528. nsemail=@"";
  4529. else
  4530. nsemail= [[NSString alloc]initWithUTF8String:email];
  4531. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4532. NSString *nsimg_0 = nil;
  4533. if(img_0==nil)
  4534. nsimg_0=@"";
  4535. else
  4536. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4537. [self copy_bcardImg:nsimg_0];
  4538. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4539. NSString *nsimg_1 = nil;
  4540. if(img_1==nil)
  4541. nsimg_1=@"";
  4542. else
  4543. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4544. [self copy_bcardImg:nsimg_1];
  4545. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4546. NSString *nsimg_2 = nil;
  4547. if(img_2==nil)
  4548. nsimg_2=@"";
  4549. else
  4550. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4551. [self copy_bcardImg:nsimg_2];
  4552. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4553. NSString *nsprice_type = nil;
  4554. if(price_type==nil)
  4555. nsprice_type=@"";
  4556. else
  4557. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4558. char *notes = (char*)sqlite3_column_text(statement, 22);
  4559. NSString *nsnotes = nil;
  4560. if(notes==nil)
  4561. nsnotes=@"";
  4562. else
  4563. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4564. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4565. NSString *nssalesrep = nil;
  4566. if(salesrep==nil)
  4567. nssalesrep=@"";
  4568. else
  4569. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4570. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4571. carrier = sqlite3_column_int(statement, 25);
  4572. {
  4573. // decrypt
  4574. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4575. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4576. nsphone=[AESCrypt fastdecrypt:nsphone];
  4577. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4578. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4579. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4580. }
  4581. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4582. [arr_name addObject:nsfirst_name];
  4583. [arr_name addObject:nslast_name];
  4584. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4585. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4586. [arr_ext addObject:nsaddr_1];
  4587. [arr_ext addObject:nsaddr_2];
  4588. [arr_ext addObject:nsaddr_3];
  4589. [arr_ext addObject:nsaddr_4];
  4590. [arr_ext addObject:@"\r\n"];
  4591. [arr_ext addObject:nscity];
  4592. [arr_ext addObject:nsstate];
  4593. [arr_ext addObject:nszipcode];
  4594. [arr_ext addObject:nscountry];
  4595. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4596. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4597. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4598. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4599. [item setValue:nscountry forKey:@"customer_country"];
  4600. [item setValue:nsphone forKey:@"customer_phone"];
  4601. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4602. [item setValue:nscompany_name forKey:@"customer_name"];
  4603. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4604. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4605. [item setValue:nsext forKey:@"customer_contact_ext"];
  4606. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4607. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4608. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4609. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4610. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4611. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4612. [item setValue:nslast_name forKey:@"customer_last_name"];
  4613. [item setValue:nscity forKey:@"customer_city"];
  4614. [item setValue:nsstate forKey:@"customer_state"];
  4615. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4616. [item setValue:contactId forKey:@"customer_cid"];
  4617. [item setValue:nscontact_name forKey:@"customer_contact"];
  4618. [item setValue:nsfax forKey:@"customer_fax"];
  4619. [item setValue:nsemail forKey:@"customer_email"];
  4620. [item setValue:contact_type forKey:@"customer_contact_type"];
  4621. // i++;
  4622. }
  4623. // UIApplication * app = [UIApplication sharedApplication];
  4624. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4625. [ret setValue:params[@"mode"] forKey:@"mode"];
  4626. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4627. sqlite3_finalize(statement);
  4628. }
  4629. [iSalesDB close_db:db];
  4630. NSString* scarrier = [self offline_getCarrier:carrier];
  4631. item[@"customer_truck_carrier"] = scarrier;
  4632. [ret setObject:item forKey:@"customerInfo"];
  4633. #ifdef DEBUG
  4634. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4635. #endif
  4636. return ret;
  4637. }
  4638. + (bool) copy_bcardImg:(NSString*) filename
  4639. {
  4640. if(filename.length==0)
  4641. return false;
  4642. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4643. bool ret=false;
  4644. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4645. NSString *cachefolder = [paths objectAtIndex:0];
  4646. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4647. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4648. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4649. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4650. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4651. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4652. //
  4653. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4654. NSFileManager* fileManager = [NSFileManager defaultManager];
  4655. BOOL bdir=NO;
  4656. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4657. {
  4658. NSError *error = nil;
  4659. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4660. {
  4661. ret=false;
  4662. }
  4663. else
  4664. {
  4665. ret=true;
  4666. }
  4667. // NSError *error = nil;
  4668. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4669. //
  4670. // if(!bsuccess)
  4671. // {
  4672. // DebugLog(@"Create offline_createimg folder failed");
  4673. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4674. // return [RAConvertor dict2data:ret];
  4675. // }
  4676. // if(bsuccess)
  4677. // {
  4678. // sqlite3 *db = [self get_db];
  4679. //
  4680. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4681. // [iSalesDB close_db:db];
  4682. // }
  4683. }
  4684. return ret;
  4685. //
  4686. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4687. // if(bsuccess)
  4688. // {
  4689. // NSError *error = nil;
  4690. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4691. // {
  4692. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4693. // }
  4694. // else
  4695. // {
  4696. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4697. //
  4698. // ret[@"img_url_aname"]=filename;
  4699. // ret[@"img_url"]=savedImagePath;
  4700. // }
  4701. // }
  4702. }
  4703. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4704. {
  4705. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4706. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4707. NSString *cachefolder = [paths objectAtIndex:0];
  4708. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4709. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4710. NSFileManager* fileManager = [NSFileManager defaultManager];
  4711. BOOL bdir=YES;
  4712. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4713. {
  4714. NSError *error = nil;
  4715. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4716. if(!bsuccess)
  4717. {
  4718. DebugLog(@"Create offline_createimg folder failed");
  4719. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4720. return [RAConvertor dict2data:ret];
  4721. }
  4722. // if(bsuccess)
  4723. // {
  4724. // sqlite3 *db = [self get_db];
  4725. //
  4726. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4727. // [iSalesDB close_db:db];
  4728. // }
  4729. }
  4730. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4731. {
  4732. NSError *error = nil;
  4733. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4734. if(!bsuccess)
  4735. {
  4736. DebugLog(@"Create img_cache folder failed");
  4737. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4738. return [RAConvertor dict2data:ret];
  4739. }
  4740. // if(bsuccess)
  4741. // {
  4742. // sqlite3 *db = [self get_db];
  4743. //
  4744. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4745. // [iSalesDB close_db:db];
  4746. // }
  4747. }
  4748. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4749. //JEPG格式
  4750. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4751. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4752. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4753. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4754. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4755. if(bsuccess)
  4756. {
  4757. NSError *error = nil;
  4758. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4759. {
  4760. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4761. }
  4762. else
  4763. {
  4764. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4765. ret[@"img_url_aname"]=filename;
  4766. ret[@"img_url"]=filename;
  4767. }
  4768. }
  4769. else
  4770. {
  4771. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4772. }
  4773. return [RAConvertor dict2data:ret];
  4774. }
  4775. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4776. {
  4777. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4778. category = [category substringToIndex:3];
  4779. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4780. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4781. params[@"category"]= category;
  4782. ret[@"params"]= params;
  4783. [ret setValue:@"detail" forKey:@"target"];
  4784. [ret setValue:@"popup" forKey:@"action"];
  4785. [ret setValue:@"content" forKey:@"type"];
  4786. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4787. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4788. [ret setValue:@"true" forKey:@"single_row"];
  4789. [ret setValue:@"true" forKey:@"partial_refresh"];
  4790. // sqlite3 *db = [iSalesDB get_db];
  4791. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4792. NSString *sqlQuery =nil;
  4793. #ifdef BUILD_NPD
  4794. 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 ;
  4795. #else
  4796. 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];
  4797. #endif
  4798. sqlite3_stmt * statement;
  4799. int count = 0;
  4800. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4801. // int count=0;
  4802. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4803. {
  4804. int i=0;
  4805. while (sqlite3_step(statement) == SQLITE_ROW)
  4806. {
  4807. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4808. // char *name = (char*)sqlite3_column_text(statement, 1);
  4809. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4810. char *name = (char*)sqlite3_column_text(statement, 0);
  4811. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4812. int product_id = sqlite3_column_int(statement, 1);
  4813. char *url = (char*)sqlite3_column_text(statement, 2);
  4814. if(url==nil)
  4815. url="";
  4816. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4817. [item setValue:nsurl forKey:@"picture_path"];
  4818. [item setValue:nsname forKey:@"fash_name"];
  4819. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4820. [item setValue:category forKey:@"category"];
  4821. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4822. i++;
  4823. }
  4824. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4825. sqlite3_finalize(statement);
  4826. }
  4827. DebugLog(@"count:%d",count);
  4828. // [iSalesDB close_db:db];
  4829. return ret;
  4830. }
  4831. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4832. {
  4833. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4834. [ret setValue:key forKey:@"key"];
  4835. [ret setValue:value forKey:@"val"];
  4836. [ret setValue:@"price" forKey:@"type"];
  4837. return ret;
  4838. }
  4839. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4840. {
  4841. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4842. [ret setValue:key forKey:@"key"];
  4843. [ret setValue:value forKey:@"val"];
  4844. return ret;
  4845. }
  4846. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4847. {
  4848. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4849. [ret setValue:@"0" forKey:@"img_count"];
  4850. // sqlite3 *db = [iSalesDB get_db];
  4851. 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 ;
  4852. sqlite3_stmt * statement;
  4853. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4854. {
  4855. int i=0;
  4856. if (sqlite3_step(statement) == SQLITE_ROW)
  4857. {
  4858. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4859. // char *name = (char*)sqlite3_column_text(statement, 1);
  4860. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4861. char *value = (char*)sqlite3_column_text(statement, 0);
  4862. if(value==nil)
  4863. value="";
  4864. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4865. char *key = (char*)sqlite3_column_text(statement, 1);
  4866. if(key==nil)
  4867. key="";
  4868. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4869. [item setValue:nsvalue forKey:@"val"];
  4870. [item setValue:nskey forKey:@"key"];
  4871. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4872. [ret setValue:@"1" forKey:@"count"];
  4873. i++;
  4874. }
  4875. sqlite3_finalize(statement);
  4876. }
  4877. // [iSalesDB close_db:db];
  4878. return ret;
  4879. }
  4880. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4881. {
  4882. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4883. [ret setValue:@"0" forKey:@"count"];
  4884. // sqlite3 *db = [iSalesDB get_db];
  4885. 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;
  4886. sqlite3_stmt * statement;
  4887. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4888. {
  4889. int i=0;
  4890. while (sqlite3_step(statement) == SQLITE_ROW)
  4891. {
  4892. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4893. // char *name = (char*)sqlite3_column_text(statement, 1);
  4894. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4895. char *value = (char*)sqlite3_column_text(statement, 0);
  4896. if(value==nil)
  4897. value="";
  4898. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4899. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4900. if(selector_display==nil)
  4901. selector_display="";
  4902. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4903. int product_id = sqlite3_column_int(statement, 2);
  4904. char *category = (char*)sqlite3_column_text(statement, 3);
  4905. if(category==nil)
  4906. category="";
  4907. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4908. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4909. [item setValue:nsvalue forKey:@"title"];
  4910. [item setValue:url forKey:@"pic_url"];
  4911. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4912. [params setValue:@"2" forKey:@"count"];
  4913. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4914. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4915. [param0 setValue:@"product_id" forKey:@"name"];
  4916. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4917. [param1 setValue:nscategory forKey:@"val"];
  4918. [param1 setValue:@"category" forKey:@"name"];
  4919. [params setObject:param0 forKey:@"param_0"];
  4920. [params setObject:param1 forKey:@"param_1"];
  4921. [item setObject:params forKey:@"params"];
  4922. [ret setValue:nsselector_display forKey:@"name"];
  4923. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4924. i++;
  4925. }
  4926. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4927. [ret setValue:@"switch" forKey:@"action"];
  4928. sqlite3_finalize(statement);
  4929. }
  4930. // [iSalesDB close_db:db];
  4931. return ret;
  4932. }
  4933. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4934. {
  4935. // model 在 category search 显示的图片。
  4936. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4937. // sqlite3 *db = [iSalesDB get_db];
  4938. 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];
  4939. sqlite3_stmt * statement;
  4940. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4941. {
  4942. while (sqlite3_step(statement) == SQLITE_ROW)
  4943. {
  4944. char *url = (char*)sqlite3_column_text(statement, 0);
  4945. if(url==nil)
  4946. url="";
  4947. int type = sqlite3_column_int(statement, 1);
  4948. if(type==0)
  4949. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4950. else
  4951. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4952. }
  4953. sqlite3_finalize(statement);
  4954. }
  4955. // [iSalesDB close_db:db];
  4956. return ret;
  4957. }
  4958. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4959. {
  4960. int item_id=-1;
  4961. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4962. sqlite3_stmt * statement;
  4963. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4964. {
  4965. if (sqlite3_step(statement) == SQLITE_ROW)
  4966. {
  4967. item_id = sqlite3_column_int(statement, 0);
  4968. }
  4969. sqlite3_finalize(statement);
  4970. }
  4971. return item_id;
  4972. }
  4973. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4974. {
  4975. // NSString* ret = @"";
  4976. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4977. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4978. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4979. sqlite3_stmt * statement;
  4980. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4981. {
  4982. while (sqlite3_step(statement) == SQLITE_ROW)
  4983. {
  4984. int bitem_id = sqlite3_column_int(statement, 0);
  4985. int bitem_qty = sqlite3_column_int(statement, 1);
  4986. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4987. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4988. }
  4989. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4990. sqlite3_finalize(statement);
  4991. }
  4992. // if(ret==nil)
  4993. // ret=@"";
  4994. *count=arr_count;
  4995. return arr_bundle;
  4996. }
  4997. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4998. {
  4999. // get default sold qty, return -1 if model not found;
  5000. int ret = -1;
  5001. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5002. sqlite3_stmt * statement;
  5003. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5004. {
  5005. if (sqlite3_step(statement) == SQLITE_ROW)
  5006. {
  5007. ret = sqlite3_column_int(statement, 0);
  5008. }
  5009. sqlite3_finalize(statement);
  5010. }
  5011. return ret;
  5012. }
  5013. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5014. {
  5015. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5016. [ret setValue:@"0" forKey:@"img_count"];
  5017. // sqlite3 *db = [iSalesDB get_db];
  5018. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5019. sqlite3_stmt * statement;
  5020. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5021. {
  5022. int i=0;
  5023. while (sqlite3_step(statement) == SQLITE_ROW)
  5024. {
  5025. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5026. // char *name = (char*)sqlite3_column_text(statement, 1);
  5027. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5028. char *url = (char*)sqlite3_column_text(statement, 0);
  5029. if(url==nil)
  5030. url="";
  5031. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5032. [item setValue:nsurl forKey:@"s"];
  5033. [item setValue:nsurl forKey:@"l"];
  5034. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5035. i++;
  5036. }
  5037. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5038. sqlite3_finalize(statement);
  5039. }
  5040. // [iSalesDB close_db:db];
  5041. return ret;
  5042. }
  5043. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5044. // UIApplication * app = [UIApplication sharedApplication];
  5045. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5046. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5047. sqlite3 *db = [iSalesDB get_db];
  5048. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5049. NSString* product_id=params[@"product_id"];
  5050. NSString *item_count_str = params[@"item_count"];
  5051. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5052. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5053. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5054. // NSString *sql = @"";
  5055. for(int i=0;i<arr.count;i++)
  5056. {
  5057. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5058. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5059. __block int cart_count = 0;
  5060. if (!item_count_str) {
  5061. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5062. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5063. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5064. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5065. cart_count = [[model_set_components lastObject] intValue];
  5066. }];
  5067. }
  5068. if(count==0)
  5069. {
  5070. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5071. sqlite3_stmt *stmt;
  5072. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5073. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5074. if (item_count_arr) {
  5075. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5076. } else {
  5077. sqlite3_bind_int(stmt,2,cart_count);
  5078. }
  5079. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5080. [iSalesDB execSql:@"ROLLBACK" db:db];
  5081. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5082. [iSalesDB close_db:db];
  5083. DebugLog(@"add to wishlist error");
  5084. return ret;
  5085. }
  5086. } else {
  5087. int qty = 0;
  5088. if (item_count_arr) {
  5089. qty = [item_count_arr[i] intValue];
  5090. } else {
  5091. qty = cart_count;
  5092. }
  5093. 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]];
  5094. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5095. [iSalesDB execSql:@"ROLLBACK" db:db];
  5096. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5097. [iSalesDB close_db:db];
  5098. DebugLog(@"add to wishlist error");
  5099. return ret;
  5100. }
  5101. }
  5102. }
  5103. // [iSalesDB execSql:sql db:db];
  5104. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5105. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5106. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5107. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5108. [iSalesDB close_db:db];
  5109. ret[@"wish_count"]=@(count);
  5110. ret[@"result"]= [NSNumber numberWithInt:2];
  5111. return ret;
  5112. }
  5113. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5114. // 0 category
  5115. // 1 search
  5116. // 2 itemsearch
  5117. NSData *ret = nil;
  5118. NSDictionary *items = nil;
  5119. switch (from) {
  5120. case 0:{
  5121. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5122. }
  5123. break;
  5124. case 1:{
  5125. items = [[self search:params limited:NO] objectForKey:@"items"];
  5126. }
  5127. break;
  5128. case 2:{
  5129. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5130. }
  5131. break;
  5132. default:
  5133. break;
  5134. }
  5135. if (!items) {
  5136. return ret;
  5137. }
  5138. int count = [[items objectForKey:@"count"] intValue];
  5139. NSMutableString *product_id_str = [@"" mutableCopy];
  5140. for (int i = 0; i < count; i++) {
  5141. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5142. NSDictionary *item = [items objectForKey:key];
  5143. NSString *product_id = [item objectForKey:@"product_id"];
  5144. if (i == 0) {
  5145. [product_id_str appendString:product_id];
  5146. } else {
  5147. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5148. }
  5149. }
  5150. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5151. if ([add_to isEqualToString:@"cart"]) {
  5152. NSString *order_code = [params objectForKey:@"orderCode"];
  5153. if (order_code.length) {
  5154. NSDictionary *newParams = @{
  5155. @"product_id" : product_id_str,
  5156. @"orderCode" : order_code,
  5157. @"can_create_backorder":params[@"can_create_backorder"]
  5158. };
  5159. ret = [self offline_add2cart:newParams.mutableCopy];
  5160. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5161. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5162. ret = [RAConvertor dict2data:retDic];
  5163. }
  5164. } else if([add_to isEqualToString:@"wishlist"]) {
  5165. NSDictionary *newParams = @{
  5166. @"product_id" : product_id_str
  5167. };
  5168. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5169. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5170. ret = [RAConvertor dict2data:retDic];
  5171. } else if([add_to isEqualToString:@"portfolio"]) {
  5172. NSDictionary *newParams = @{
  5173. @"product_id" : product_id_str
  5174. };
  5175. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5176. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5177. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5178. ret = [RAConvertor dict2data:retDic];
  5179. }
  5180. return ret;
  5181. }
  5182. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5183. {
  5184. return [self addAll:params from:0];
  5185. }
  5186. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5187. {
  5188. return [self addAll:params from:1];
  5189. }
  5190. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5191. {
  5192. return [self addAll:params from:2];
  5193. }
  5194. #pragma mark - Jack
  5195. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5196. // "val_227" : {
  5197. // "check" : 1,
  5198. // "value" : "US United States",
  5199. // "value_id" : "228"
  5200. // },
  5201. if (!countryCode) {
  5202. countryCode = @"US";
  5203. }
  5204. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5205. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5206. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5207. int code_id = sqlite3_column_int(stmt, 3); // id
  5208. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5209. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5210. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5211. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5212. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5213. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5214. }
  5215. long n = *count;
  5216. *count = n + 1;
  5217. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5218. [container setValue:countryDic forKey:key];
  5219. }] mutableCopy];
  5220. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5221. return ret;
  5222. }
  5223. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5224. // "val_227" : {
  5225. // "check" : 1,
  5226. // "value" : "US United States",
  5227. // "value_id" : "228"
  5228. // },
  5229. __block NSString* OrderCarrier =@"";
  5230. // sqlite3 *db = [iSalesDB get_db];
  5231. // sqlite3_stmt * statement;
  5232. [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) {
  5233. OrderCarrier = [self textAtColumn:0 statement:statment];
  5234. // sqlite3_finalize(statement);
  5235. }];
  5236. // [iSalesDB close_db:db];
  5237. __block int c=0;
  5238. 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) {
  5239. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5240. int code_id = sqlite3_column_int(stmt, 2); // id
  5241. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5242. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5243. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5244. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5245. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5246. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5247. }
  5248. c++;
  5249. long n = *count;
  5250. *count = n + 1;
  5251. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5252. [container setValue:carrierDic forKey:key];
  5253. }] mutableCopy];
  5254. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5255. return ret;
  5256. }
  5257. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5258. // "val_227" : {
  5259. // "check" : 1,
  5260. // "value" : "US United States",
  5261. // "value_id" : "228"
  5262. // },
  5263. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5264. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5265. int code_id = sqlite3_column_int(stmt, 2); // id
  5266. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5267. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5268. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5269. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5270. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5271. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5272. }
  5273. long n = *count;
  5274. *count = n + 1;
  5275. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5276. [container setValue:carrierDic forKey:key];
  5277. }] mutableCopy];
  5278. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5279. return ret;
  5280. }
  5281. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5282. // // "val_227" : {
  5283. // // "check" : 1,
  5284. // // "value" : "US United States",
  5285. // // "value_id" : "228"
  5286. // // },
  5287. //
  5288. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5289. // NSString * code = [iSalesDB jk_queryText:Sql];
  5290. //
  5291. // return [self offline_getCarrier:code];
  5292. //
  5293. //}
  5294. + (NSString *)offline_getCarrier:(int)code {
  5295. // "val_227" : {
  5296. // "check" : 1,
  5297. // "value" : "US United States",
  5298. // "value_id" : "228"
  5299. // },
  5300. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5301. NSString * ret = [iSalesDB jk_queryText:Sql];
  5302. return ret;
  5303. }
  5304. + (int )offline_getCarrierCode:(NSString*)name {
  5305. // "val_227" : {
  5306. // "check" : 1,
  5307. // "value" : "US United States",
  5308. // "value_id" : "228"
  5309. // },
  5310. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5311. NSString * ret = [iSalesDB jk_queryText:Sql];
  5312. return [ret intValue];
  5313. }
  5314. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5315. countryCode = [self translateSingleQuote:countryCode];
  5316. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5317. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5318. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5319. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5320. if (name == NULL) {
  5321. name = "";
  5322. }
  5323. if (code == NULL) {
  5324. code = "";
  5325. }
  5326. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5327. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5328. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5329. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5330. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5331. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5332. }
  5333. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5334. [container setValue:stateDic forKey:key];
  5335. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5336. DebugLog(@"query all state error: %@",err_msg);
  5337. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5338. // [stateDic setValue:@"Other" forKey:@"value"];
  5339. // [stateDic setValue:@"" forKey:@"value_id"];
  5340. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5341. //
  5342. // if (state_code && [@"" isEqualToString:state_code]) {
  5343. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5344. // }
  5345. //
  5346. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5347. // [container setValue:stateDic forKey:key];
  5348. }] mutableCopy];
  5349. [ret removeObjectForKey:@"result"];
  5350. // failure 可以不用了,一样的
  5351. if (ret.allKeys.count == 0) {
  5352. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5353. [stateDic setValue:@"Other" forKey:@"value"];
  5354. [stateDic setValue:@"" forKey:@"value_id"];
  5355. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5356. if (state_code && [@"" isEqualToString:state_code]) {
  5357. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5358. }
  5359. NSString *key = [NSString stringWithFormat:@"val_0"];
  5360. [ret setValue:stateDic forKey:key];
  5361. }
  5362. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5363. return ret;
  5364. }
  5365. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5366. codeId = [self translateSingleQuote:codeId];
  5367. 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];
  5368. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5369. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5370. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5371. if (name == NULL) {
  5372. name = "";
  5373. }
  5374. if (code == NULL) {
  5375. code = "";
  5376. }
  5377. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5378. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5379. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5380. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5381. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5382. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5383. }
  5384. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5385. [container setValue:stateDic forKey:key];
  5386. }] mutableCopy];
  5387. [ret removeObjectForKey:@"result"];
  5388. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5389. return ret;
  5390. }
  5391. + (NSDictionary *)offline_getPrice {
  5392. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5393. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5394. char *name = (char *) sqlite3_column_text(stmt, 1);
  5395. int type = sqlite3_column_int(stmt, 2);
  5396. int orderBy = sqlite3_column_int(stmt, 3);
  5397. if (name == NULL) {
  5398. name = "";
  5399. }
  5400. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5401. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5402. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5403. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5404. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5405. if (orderBy == 0) {
  5406. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5407. }
  5408. [container setValue:priceDic forKey:key];
  5409. }] mutableCopy];
  5410. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5411. return ret;
  5412. }
  5413. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5414. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5415. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5416. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5417. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5418. // int _id = sqlite3_column_int(stmt, 0);
  5419. NSString *name = [self textAtColumn:1 statement:stmt];
  5420. NSDictionary *typeDic = @{
  5421. @"value_id" : name,
  5422. @"value" : name,
  5423. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5424. };
  5425. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5426. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5427. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5428. }];
  5429. return ret;
  5430. }
  5431. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5432. // 首先从offline_login表中取出sales_code
  5433. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5434. // NSString *user = app.user;
  5435. user = [self translateSingleQuote:user];
  5436. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5437. __block NSString *user_code = @"";
  5438. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5439. char *code = (char *)sqlite3_column_text(stmt, 0);
  5440. if (code == NULL) {
  5441. code = "";
  5442. }
  5443. user_code = [NSString stringWithUTF8String:code];
  5444. }];
  5445. // 再取所有salesRep
  5446. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5447. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5448. // 1 name 2 code 3 salesrep_id
  5449. char *name = (char *)sqlite3_column_text(stmt, 1);
  5450. char *code = (char *)sqlite3_column_text(stmt, 2);
  5451. int salesrep_id = sqlite3_column_int(stmt, 3);
  5452. if (name == NULL) {
  5453. name = "";
  5454. }
  5455. if (code == NULL) {
  5456. code = "";
  5457. }
  5458. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5459. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5460. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5461. // 比较code 相等则check
  5462. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5463. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5464. }
  5465. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5466. }] mutableCopy];
  5467. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5468. return ret;
  5469. }
  5470. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5471. zipcode = [self translateSingleQuote:zipcode];
  5472. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5473. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5474. char *country = (char *)sqlite3_column_text(stmt, 0);
  5475. char *state = (char *)sqlite3_column_text(stmt, 1);
  5476. char *city = (char *)sqlite3_column_text(stmt, 2);
  5477. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5478. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5479. if (country == NULL) {
  5480. country = "";
  5481. }
  5482. if (state == NULL) {
  5483. state = "";
  5484. }
  5485. if (city == NULL) {
  5486. city = "";
  5487. }
  5488. if (country_code == NULL) {
  5489. country_code = "";
  5490. }
  5491. if (state_code == NULL) {
  5492. state_code = "";
  5493. }
  5494. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5495. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5496. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5497. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5498. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5499. }] mutableCopy];
  5500. return ret;
  5501. }
  5502. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5503. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5504. [item setValue:value forKey:valueKey];
  5505. [dic setValue:item forKey:itemKey];
  5506. }
  5507. + (NSString *)countryCodeByid:(NSString *)code_id {
  5508. NSString *ret = nil;
  5509. code_id = [self translateSingleQuote:code_id];
  5510. sqlite3 *db = [iSalesDB get_db];
  5511. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5512. sqlite3_stmt * statement;
  5513. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5514. while (sqlite3_step(statement) == SQLITE_ROW) {
  5515. char *code = (char *)sqlite3_column_text(statement, 0);
  5516. if (code == NULL) {
  5517. code = "";
  5518. }
  5519. ret = [NSString stringWithUTF8String:code];
  5520. }
  5521. sqlite3_finalize(statement);
  5522. }
  5523. [iSalesDB close_db:db];
  5524. return ret;
  5525. }
  5526. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5527. NSString *ret = nil;
  5528. code = [self translateSingleQuote:code];
  5529. sqlite3 *db = [iSalesDB get_db];
  5530. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5531. sqlite3_stmt * statement;
  5532. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5533. while (sqlite3_step(statement) == SQLITE_ROW) {
  5534. char *_id = (char *)sqlite3_column_text(statement, 0);
  5535. if (_id == NULL) {
  5536. _id = "";
  5537. }
  5538. ret = [NSString stringWithFormat:@"%s",_id];
  5539. }
  5540. sqlite3_finalize(statement);
  5541. }
  5542. [iSalesDB close_db:db];
  5543. return ret;
  5544. }
  5545. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5546. NSString *name = nil;
  5547. codeId = [self translateSingleQuote:codeId];
  5548. sqlite3 *db = [iSalesDB get_db];
  5549. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5550. sqlite3_stmt * statement;
  5551. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5552. while (sqlite3_step(statement) == SQLITE_ROW) {
  5553. char *value = (char *)sqlite3_column_text(statement, 0);
  5554. if (value == NULL) {
  5555. value = "";
  5556. }
  5557. name = [NSString stringWithUTF8String:value];
  5558. }
  5559. sqlite3_finalize(statement);
  5560. }
  5561. [iSalesDB close_db:db];
  5562. return name;
  5563. }
  5564. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5565. NSString *ret = nil;
  5566. sqlite3 *db = [iSalesDB get_db];
  5567. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5568. sqlite3_stmt * statement;
  5569. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5570. while (sqlite3_step(statement) == SQLITE_ROW) {
  5571. char *name = (char *)sqlite3_column_text(statement, 0);
  5572. if (name == NULL) {
  5573. name = "";
  5574. }
  5575. ret = [NSString stringWithUTF8String:name];
  5576. }
  5577. sqlite3_finalize(statement);
  5578. }
  5579. [iSalesDB close_db:db];
  5580. return ret;
  5581. }
  5582. + (NSString *)salesRepCodeById:(NSString *)_id {
  5583. NSString *ret = nil;
  5584. _id = [self translateSingleQuote:_id];
  5585. sqlite3 *db = [iSalesDB get_db];
  5586. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5587. sqlite3_stmt * statement;
  5588. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5589. while (sqlite3_step(statement) == SQLITE_ROW) {
  5590. char *rep = (char *)sqlite3_column_text(statement, 0);
  5591. if (rep == NULL) {
  5592. rep = "";
  5593. }
  5594. ret = [NSString stringWithUTF8String:rep];
  5595. }
  5596. sqlite3_finalize(statement);
  5597. }
  5598. [iSalesDB close_db:db];
  5599. return ret;
  5600. }
  5601. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5602. char *tx = (char *)sqlite3_column_text(stmt, col);
  5603. if (tx == NULL) {
  5604. tx = "";
  5605. }
  5606. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5607. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5608. if (!text) {
  5609. text = @"";
  5610. }
  5611. // 将字符全部为' '的字符串干掉
  5612. int spaceCount = 0;
  5613. for (int i = 0; i < text.length; i++) {
  5614. if ([text characterAtIndex:i] == ' ') {
  5615. spaceCount++;
  5616. }
  5617. }
  5618. if (spaceCount == text.length) {
  5619. text = @"";
  5620. }
  5621. return text;
  5622. }
  5623. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5624. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5625. NSData *data = [NSData dataWithContentsOfFile:path];
  5626. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5627. return ret;
  5628. }
  5629. + (NSString *)textFileName:(NSString *)name {
  5630. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5631. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5632. if (!text) {
  5633. text = @"";
  5634. }
  5635. return text;
  5636. }
  5637. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5638. return [[dic objectForKey:key] mutableCopy];
  5639. }
  5640. + (id)translateSingleQuote:(NSString *)string {
  5641. if ([string isKindOfClass:[NSString class]])
  5642. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5643. return string;
  5644. }
  5645. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5646. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5647. }
  5648. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5649. NSString* ret= nil;
  5650. NSString *sqlQuery = nil;
  5651. // 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
  5652. 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];
  5653. sqlite3_stmt * statement;
  5654. // int count=0;
  5655. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5656. {
  5657. if (sqlite3_step(statement) == SQLITE_ROW)
  5658. {
  5659. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5660. if(imgurl==nil)
  5661. imgurl="";
  5662. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5663. ret=nsimgurl;
  5664. }
  5665. sqlite3_finalize(statement);
  5666. }
  5667. else
  5668. {
  5669. [ret setValue:@"8" forKey:@"result"];
  5670. }
  5671. // [iSalesDB close_db:db];
  5672. // DebugLog(@"data string: %@",ret );
  5673. return ret;
  5674. }
  5675. #pragma mark contact Advanced search
  5676. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5677. {
  5678. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5679. return [RAConvertor dict2data:contactAdvanceDic];
  5680. }
  5681. #pragma mark create new contact
  5682. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5683. assert(params[@"user"]!=nil);
  5684. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5685. NSData *data = [NSData dataWithContentsOfFile:path];
  5686. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5687. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5688. NSString *countryCode = nil;
  5689. NSString *countryCode_id = nil;
  5690. NSString *stateCode = nil;
  5691. NSString *city = nil;
  5692. NSString *zipCode = nil;
  5693. // NSString *carrierCode = nil;
  5694. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5695. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5696. NSString *code_id = params[@"country"];
  5697. countryCode_id = code_id;
  5698. countryCode = [self countryCodeByid:code_id];
  5699. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5700. NSString *zip_code = params[@"zipcode"];
  5701. // 剔除全部为空格
  5702. int spaceCount = 0;
  5703. for (int i = 0; i < zip_code.length; i++) {
  5704. if ([zip_code characterAtIndex:i] == ' ') {
  5705. spaceCount++;
  5706. }
  5707. }
  5708. if (spaceCount == zip_code.length) {
  5709. zip_code = @"";
  5710. }
  5711. zipCode = zip_code;
  5712. if (zipCode.length > 0) {
  5713. countryCode_id = params[@"country"];
  5714. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5715. countryCode = [dic valueForKey:@"country_code"];
  5716. if (!countryCode) {
  5717. // countryCode = @"US";
  5718. NSString *code_id = params[@"country"];
  5719. countryCode = [self countryCodeByid:code_id];
  5720. }
  5721. stateCode = [dic valueForKey:@"state_code"];
  5722. if (!stateCode.length) {
  5723. stateCode = params[@"state"];
  5724. }
  5725. city = [dic valueForKey:@"city"];
  5726. if (!city.length) {
  5727. city = params[@"city"];
  5728. }
  5729. // zip code
  5730. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5731. [zipDic setValue:zipCode forKey:@"value"];
  5732. [section_0 setValue:zipDic forKey:@"item_11"];
  5733. } else {
  5734. NSString *code_id = params[@"country"];
  5735. countryCode = [self countryCodeByid:code_id];
  5736. stateCode = params[@"state"];
  5737. city = params[@"city"];
  5738. }
  5739. }
  5740. } else {
  5741. // default: US United States
  5742. countryCode = @"US";
  5743. countryCode_id = @"228";
  5744. }
  5745. // country
  5746. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5747. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5748. // state
  5749. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5750. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5751. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5752. // city
  5753. if (city) {
  5754. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5755. [cityDic setValue:city forKey:@"value"];
  5756. [section_0 setValue:cityDic forKey:@"item_13"];
  5757. }
  5758. // price type
  5759. NSDictionary *priceDic = [self offline_getPrice];
  5760. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5761. // contact type
  5762. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5763. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5764. // Sales Rep
  5765. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5766. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5767. #ifdef BUILD_CONTRAST
  5768. //carrier
  5769. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5770. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5771. #endif
  5772. [ret setValue:section_0 forKey:@"section_0"];
  5773. return [RAConvertor dict2data:ret];
  5774. }
  5775. #pragma mark save
  5776. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5777. NSString *addr = nil;
  5778. NSString *contact_name = nil;
  5779. // int carrier = [params[@"carrier"] intValue];
  5780. NSString *companyName = [params objectForKey:@"company"];
  5781. if (companyName) {
  5782. companyName = [AESCrypt fastencrypt:companyName];
  5783. } else {
  5784. companyName = @"";
  5785. }
  5786. DebugLog(@"company");
  5787. companyName = [self translateSingleQuote:companyName];
  5788. NSString *addr1 = [params objectForKey:@"address"];
  5789. NSString *addr2 = [params objectForKey:@"address2"];
  5790. NSString *addr3 = [params objectForKey:@"address_3"];
  5791. NSString *addr4 = [params objectForKey:@"address_4"];
  5792. if (!addr2) {
  5793. addr2 = @"";
  5794. }
  5795. if (!addr3) {
  5796. addr3 = @"";
  5797. }
  5798. if (!addr4) {
  5799. addr4 = @"";
  5800. }
  5801. if (!addr1) {
  5802. addr1 = @"";
  5803. }
  5804. DebugLog(@"addr");
  5805. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5806. addr = [AESCrypt fastencrypt:addr];
  5807. addr = [self translateSingleQuote:addr];
  5808. if (addr1 && ![addr1 isEqualToString:@""]) {
  5809. addr1 = [AESCrypt fastencrypt:addr1];
  5810. }
  5811. addr1 = [self translateSingleQuote:addr1];
  5812. addr2 = [self translateSingleQuote:addr2];
  5813. addr3 = [self translateSingleQuote:addr3];
  5814. addr4 = [self translateSingleQuote:addr4];
  5815. NSString *country = [params objectForKey:@"country"];
  5816. if (country) {
  5817. country = [self countryNameByCountryCodeId:country];
  5818. } else {
  5819. country = @"";
  5820. }
  5821. int carrier = [params[@"carrier"] intValue];
  5822. DebugLog(@"country");
  5823. country = [self translateSingleQuote:country];
  5824. NSString *state = [params objectForKey:@"state"];
  5825. if (!state) {
  5826. state = @"";
  5827. }
  5828. DebugLog(@"state");
  5829. state = [self translateSingleQuote:state];
  5830. NSString *city = [params objectForKey:@"city"];
  5831. if (!city) {
  5832. city = @"";
  5833. }
  5834. city = [self translateSingleQuote:city];
  5835. NSString *zipcode = [params objectForKey:@"zipcode"];
  5836. if (!zipcode) {
  5837. zipcode = @"";
  5838. }
  5839. DebugLog(@"zip");
  5840. zipcode = [self translateSingleQuote:zipcode];
  5841. NSString *fistName = [params objectForKey:@"firstname"];
  5842. if (!fistName) {
  5843. fistName = @"";
  5844. }
  5845. NSString *lastName = [params objectForKey:@"lastname"];
  5846. if (!lastName) {
  5847. lastName = @"";
  5848. }
  5849. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5850. DebugLog(@"contact_name");
  5851. contact_name = [self translateSingleQuote:contact_name];
  5852. fistName = [self translateSingleQuote:fistName];
  5853. lastName = [self translateSingleQuote:lastName];
  5854. NSString *phone = [params objectForKey:@"phone"];
  5855. if (phone) {
  5856. phone = [AESCrypt fastencrypt:phone];
  5857. } else {
  5858. phone = @"";
  5859. }
  5860. DebugLog(@"PHONE");
  5861. phone = [self translateSingleQuote:phone];
  5862. NSString *fax = [params objectForKey:@"fax"];
  5863. if (!fax) {
  5864. fax = @"";
  5865. }
  5866. DebugLog(@"FAX");
  5867. fax = [self translateSingleQuote:fax];
  5868. NSString *email = [params objectForKey:@"email"];
  5869. if (!email) {
  5870. email = @"";
  5871. }
  5872. DebugLog(@"EMAIL:%@",email);
  5873. email = [self translateSingleQuote:email];
  5874. NSString *notes = [params objectForKey:@"contact_notes"];
  5875. if (!notes) {
  5876. notes = @"";
  5877. }
  5878. DebugLog(@"NOTE:%@",notes);
  5879. notes = [self translateSingleQuote:notes];
  5880. NSString *price = [params objectForKey:@"price_name"];
  5881. if (price) {
  5882. price = [self priceNameByPriceId:price];
  5883. } else {
  5884. price = @"";
  5885. }
  5886. DebugLog(@"PRICE");
  5887. price = [self translateSingleQuote:price];
  5888. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5889. if (salesRep) {
  5890. salesRep = [self salesRepCodeById:salesRep];
  5891. } else {
  5892. salesRep = @"";
  5893. }
  5894. salesRep = [self translateSingleQuote:salesRep];
  5895. NSString *img = [params objectForKey:@"business_card"];
  5896. NSArray *array = [img componentsSeparatedByString:@","];
  5897. NSString *img_0 = array[0];
  5898. if (!img_0) {
  5899. img_0 = @"";
  5900. }
  5901. img_0 = [self translateSingleQuote:img_0];
  5902. NSString *img_1 = array[1];
  5903. if (!img_1) {
  5904. img_1 = @"";
  5905. }
  5906. img_1 = [self translateSingleQuote:img_1];
  5907. NSString *img_2 = array[2];
  5908. if (!img_2) {
  5909. img_2 = @"";
  5910. }
  5911. img_2 = [self translateSingleQuote:img_2];
  5912. NSString *contact_id = [NSUUID UUID].UUIDString;
  5913. NSString *contact_type = [params objectForKey:@"type_name"];
  5914. if (!contact_type) {
  5915. contact_type = @"";
  5916. }
  5917. contact_type = [self translateSingleQuote:contact_type];
  5918. // 判断更新时是否为customer
  5919. if (update) {
  5920. contact_id = [params objectForKey:@"contact_id"];
  5921. if (!contact_id) {
  5922. contact_id = @"";
  5923. }
  5924. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5925. __block int customer = 0;
  5926. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5927. customer = sqlite3_column_int(stmt, 0);
  5928. }];
  5929. isCustomer = customer ? YES : NO;
  5930. }
  5931. NSMutableDictionary *sync_dic = [params mutableCopy];
  5932. if (isCustomer) {
  5933. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5934. } else {
  5935. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5936. }
  5937. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  5938. NSString *sync_data = nil;
  5939. NSString *sql = nil;
  5940. if (update){
  5941. contact_id = [params objectForKey:@"contact_id"];
  5942. if (!contact_id) {
  5943. contact_id = @"";
  5944. }
  5945. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5946. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5947. sync_data = [RAConvertor dict2string:sync_dic];
  5948. sync_data = [self translateSingleQuote:sync_data];
  5949. 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];
  5950. } else {
  5951. contact_id = [self translateSingleQuote:contact_id];
  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:@"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];
  5957. }
  5958. int result = [iSalesDB execSql:sql];
  5959. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5960. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5961. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5962. }
  5963. #pragma mark save new contact
  5964. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5965. {
  5966. return [self offline_saveContact:params update:NO isCustomer:YES];
  5967. }
  5968. #pragma mark edit contact
  5969. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5970. {
  5971. assert(params[@"user"]!=nil);
  5972. // {
  5973. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5974. // password = 123456;
  5975. // user = EvanK;
  5976. // }
  5977. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5978. NSData *data = [NSData dataWithContentsOfFile:path];
  5979. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5980. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5981. NSString *countryCode = nil;
  5982. NSString *countryCode_id = nil;
  5983. NSString *stateCode = nil;
  5984. __block int carrierCode = -1;
  5985. /*------contact infor------*/
  5986. __block NSString *country = nil;
  5987. __block NSString *company_name = nil;
  5988. __block NSString *contact_id = params[@"contact_id"];
  5989. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5990. __block NSString *zipcode = nil;
  5991. __block NSString *state = nil; // state_code
  5992. __block NSString *city = nil; //
  5993. __block NSString *firt_name,*last_name;
  5994. __block NSString *phone,*fax,*email;
  5995. __block NSString *notes,*price_type,*sales_rep;
  5996. __block NSString *img_0,*img_1,*img_2;
  5997. __block NSString *contact_type;
  5998. contact_id = [self translateSingleQuote:contact_id];
  5999. 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];
  6000. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6001. country = [self textAtColumn:0 statement:stmt]; // country name
  6002. company_name = [self textAtColumn:1 statement:stmt];
  6003. addr_1 = [self textAtColumn:2 statement:stmt];
  6004. addr_2 = [self textAtColumn:3 statement:stmt];
  6005. addr_3 = [self textAtColumn:4 statement:stmt];
  6006. addr_4 = [self textAtColumn:5 statement:stmt];
  6007. zipcode = [self textAtColumn:6 statement:stmt];
  6008. state = [self textAtColumn:7 statement:stmt]; // state code
  6009. city = [self textAtColumn:8 statement:stmt];
  6010. firt_name = [self textAtColumn:9 statement:stmt];
  6011. last_name = [self textAtColumn:10 statement:stmt];
  6012. phone = [self textAtColumn:11 statement:stmt];
  6013. fax = [self textAtColumn:12 statement:stmt];
  6014. email = [self textAtColumn:13 statement:stmt];
  6015. notes = [self textAtColumn:14 statement:stmt];
  6016. price_type = [self textAtColumn:15 statement:stmt]; // name
  6017. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6018. img_0 = [self textAtColumn:17 statement:stmt];
  6019. img_1 = [self textAtColumn:18 statement:stmt];
  6020. img_2 = [self textAtColumn:19 statement:stmt];
  6021. contact_type = [self textAtColumn:20 statement:stmt];
  6022. carrierCode = sqlite3_column_int(stmt, 21);
  6023. }];
  6024. // decrypt
  6025. if (company_name) {
  6026. company_name = [AESCrypt fastdecrypt:company_name];
  6027. }
  6028. if (addr_1) {
  6029. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6030. }
  6031. if (phone) {
  6032. phone = [AESCrypt fastdecrypt:phone];
  6033. }
  6034. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6035. countryCode = [iSalesDB jk_queryText:countrySql];
  6036. stateCode = state;
  6037. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6038. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6039. NSString *code_id = params[@"country"];
  6040. countryCode_id = code_id;
  6041. countryCode = [self countryCodeByid:code_id];
  6042. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6043. NSString *zip_code = params[@"zipcode"];
  6044. // 剔除全部为空格
  6045. int spaceCount = 0;
  6046. for (int i = 0; i < zip_code.length; i++) {
  6047. if ([zip_code characterAtIndex:i] == ' ') {
  6048. spaceCount++;
  6049. }
  6050. }
  6051. if (spaceCount == zip_code.length) {
  6052. zip_code = @"";
  6053. }
  6054. if (zipcode.length > 0) {
  6055. zipcode = zip_code;
  6056. countryCode_id = params[@"country"];
  6057. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6058. countryCode = [dic valueForKey:@"country_code"];
  6059. if (!countryCode) {
  6060. NSString *code_id = params[@"country"];
  6061. countryCode = [self countryCodeByid:code_id];
  6062. }
  6063. stateCode = [dic valueForKey:@"state_code"];
  6064. if (!stateCode.length) {
  6065. stateCode = params[@"state"];
  6066. }
  6067. city = [dic valueForKey:@"city"];
  6068. if (!city.length) {
  6069. city = params[@"city"];
  6070. }
  6071. // zip code
  6072. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6073. [zipDic setValue:zipcode forKey:@"value"];
  6074. [section_0 setValue:zipDic forKey:@"item_8"];
  6075. } else {
  6076. NSString *code_id = params[@"country"];
  6077. countryCode = [self countryCodeByid:code_id];
  6078. stateCode = params[@"state"];
  6079. city = params[@"city"];
  6080. }
  6081. }
  6082. }
  6083. // 0 Country
  6084. // 1 Company Name
  6085. // 2 Contact ID
  6086. // 3 Picture
  6087. // 4 Address 1
  6088. // 5 Address 2
  6089. // 6 Address 3
  6090. // 7 Address 4
  6091. // 8 Zip Code
  6092. // 9 State/Province
  6093. // 10 City
  6094. // 11 Contact First Name
  6095. // 12 Contact Last Name
  6096. // 13 Phone
  6097. // 14 Fax
  6098. // 15 Email
  6099. // 16 Contact Notes
  6100. // 17 Price Type
  6101. // 18 Contact Type
  6102. // 19 Sales Rep
  6103. // country
  6104. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6105. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6106. // company
  6107. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6108. // contact_id
  6109. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6110. // picture
  6111. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6112. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6113. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6114. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6115. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6116. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6117. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6118. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6119. // addr 1 2 3 4
  6120. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6121. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6122. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6123. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6124. // zip code
  6125. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6126. // state
  6127. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6128. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6129. // city
  6130. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6131. // first last
  6132. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6133. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6134. // phone fax email
  6135. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6136. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6137. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6138. // notes
  6139. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6140. // price
  6141. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6142. for (NSString *key in priceDic.allKeys) {
  6143. if ([key containsString:@"val_"]) {
  6144. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6145. if ([dic[@"value"] isEqualToString:price_type]) {
  6146. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6147. [priceDic setValue:dic forKey:key];
  6148. }
  6149. }
  6150. }
  6151. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6152. // Contact Rep
  6153. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6154. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6155. // Sales Rep
  6156. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6157. for (NSString *key in repDic.allKeys) {
  6158. if ([key containsString:@"val_"]) {
  6159. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6160. NSString *value = dic[@"value"];
  6161. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6162. if (code && [code isEqualToString:sales_rep]) {
  6163. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6164. [repDic setValue:dic forKey:key];
  6165. }
  6166. }
  6167. }
  6168. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6169. #ifdef BUILD_CONTRAST
  6170. //carrier
  6171. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6172. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6173. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6174. #endif
  6175. [ret setValue:section_0 forKey:@"section_0"];
  6176. return [RAConvertor dict2data:ret];
  6177. }
  6178. #pragma mark save contact
  6179. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6180. {
  6181. return [self offline_saveContact:params update:YES isCustomer:YES];
  6182. }
  6183. #pragma mark category
  6184. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6185. if (ck) {
  6186. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6187. for (NSString *key in res.allKeys) {
  6188. if (![key isEqualToString:@"count"]) {
  6189. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6190. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6191. if ([val[@"value"] isEqualToString:ck]) {
  6192. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6193. }
  6194. [res setValue:val forKey:key];
  6195. }
  6196. }
  6197. [dic setValue:res forKey:valueKey];
  6198. }
  6199. }
  6200. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6201. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6202. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6203. NSString* category = [params valueForKey:@"category"];
  6204. if (!category || [category isEqualToString:@""]) {
  6205. category = @"%";
  6206. }
  6207. category = [self translateSingleQuote:category];
  6208. int limit = [[params valueForKey:@"limit"] intValue];
  6209. int offset = [[params valueForKey:@"offset"] intValue];
  6210. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6211. NSString *limit_str = @"";
  6212. if (limited) {
  6213. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6214. }
  6215. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6216. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6217. sqlite3 *db = [iSalesDB get_db];
  6218. // [iSalesDB AddExFunction:db];
  6219. int count;
  6220. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6221. 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];
  6222. double price_min = 0;
  6223. double price_max = 0;
  6224. if ([params.allKeys containsObject:@"alert"]) {
  6225. // alert
  6226. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6227. NSString *alert = params[@"alert"];
  6228. if ([alert isEqualToString:@"Display All"]) {
  6229. alert = [NSString stringWithFormat:@""];
  6230. } else {
  6231. alert = [self translateSingleQuote:alert];
  6232. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6233. }
  6234. // available
  6235. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6236. NSString *available = params[@"available"];
  6237. NSString *available_condition;
  6238. if ([available isEqualToString:@"Display All"]) {
  6239. available_condition = @"";
  6240. } else if ([available isEqualToString:@"Available Now"]) {
  6241. available_condition = @"and availability > 0";
  6242. } else {
  6243. available_condition = @"and availability == 0";
  6244. }
  6245. // best seller
  6246. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6247. NSString *best_seller = @"";
  6248. NSString *order_best_seller = @"m.name asc";
  6249. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6250. best_seller = @"and best_seller > 0";
  6251. order_best_seller = @"m.best_seller desc,m.name asc";
  6252. }
  6253. // price
  6254. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6255. NSString *price = params[@"price"];
  6256. price_min = 0;
  6257. price_max = MAXFLOAT;
  6258. if (params[@"user"] && price != nil) {
  6259. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6260. NSMutableString *priceName = [NSMutableString string];
  6261. for (int i = 0; i < priceTypeArray.count; i++) {
  6262. NSString *pricetype = priceTypeArray[i];
  6263. pricetype = [self translateSingleQuote:pricetype];
  6264. if (i == 0) {
  6265. [priceName appendFormat:@"'%@'",pricetype];
  6266. } else {
  6267. [priceName appendFormat:@",'%@'",pricetype];
  6268. }
  6269. }
  6270. if ([price isEqualToString:@"Display All"]) {
  6271. price = [NSString stringWithFormat:@""];
  6272. } else if([price containsString:@"+"]){
  6273. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6274. price_min = [price doubleValue];
  6275. 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];
  6276. } else {
  6277. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6278. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6279. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6280. 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];
  6281. }
  6282. } else {
  6283. price = @"";
  6284. }
  6285. // sold_by_qty : Sold in quantities of %@
  6286. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6287. NSString *qty = params[@"sold_by_qty"];
  6288. if ([qty isEqualToString:@"Display All"]) {
  6289. qty = @"";
  6290. } else {
  6291. qty = [self translateSingleQuote:qty];
  6292. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6293. }
  6294. // model name;
  6295. NSString* modelname =params[@"modelName"];
  6296. if(modelname.length==0)
  6297. {
  6298. modelname=@"";
  6299. }
  6300. else
  6301. {
  6302. modelname = modelname.lowercaseString;
  6303. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6304. }
  6305. //modelDescription
  6306. NSString* modelDescription =params[@"modelDescription"];
  6307. if(modelDescription.length==0)
  6308. {
  6309. modelDescription=@"";
  6310. }
  6311. else
  6312. {
  6313. modelDescription = modelDescription.lowercaseString;
  6314. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6315. }
  6316. // cate
  6317. // category = [self translateSingleQuote:category];
  6318. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6319. // cate mutiple selection
  6320. NSString *category_id = params[@"category"];
  6321. NSMutableArray *cate_id_array = nil;
  6322. NSMutableString *cateWhere = [NSMutableString string];
  6323. if ([category_id isEqualToString:@""] || !category_id) {
  6324. [cateWhere appendString:@"1 = 1"];
  6325. } else {
  6326. if ([category_id containsString:@","]) {
  6327. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6328. } else {
  6329. cate_id_array = [@[category_id] mutableCopy];
  6330. }
  6331. [cateWhere appendString:@"("];
  6332. for (int i = 0; i < cate_id_array.count; i++) {
  6333. if (i == 0) {
  6334. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6335. } else {
  6336. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6337. }
  6338. }
  6339. [cateWhere appendString:@")"];
  6340. }
  6341. // where bestseller > 0 order by bestseller desc
  6342. // sql query: alert availability(int) best_seller(int) price qty
  6343. 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];
  6344. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6345. }
  6346. DebugLog(@"offline category where: %@",where);
  6347. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6348. if (!params[@"user"]) {
  6349. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6350. }
  6351. [ret setValue:filter forKey:@"filter"];
  6352. DebugLog(@"offline_category sql:%@",sqlQuery);
  6353. sqlite3_stmt * statement;
  6354. [ret setValue:@"2" forKey:@"result"];
  6355. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6356. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6357. // int count=0;
  6358. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6359. {
  6360. int i=0;
  6361. while (sqlite3_step(statement) == SQLITE_ROW)
  6362. {
  6363. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6364. char *name = (char*)sqlite3_column_text(statement, 0);
  6365. if(name==nil)
  6366. name="";
  6367. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6368. char *description = (char*)sqlite3_column_text(statement, 1);
  6369. if(description==nil)
  6370. description="";
  6371. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6372. int product_id = sqlite3_column_int(statement, 2);
  6373. int wid = sqlite3_column_int(statement, 3);
  6374. int closeout = sqlite3_column_int(statement, 4);
  6375. int cid = sqlite3_column_int(statement, 5);
  6376. int wisdelete = sqlite3_column_int(statement, 6);
  6377. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6378. int more_color = sqlite3_column_int(statement, 8);
  6379. // Defaul Category ID
  6380. __block NSString *categoryID = nil;
  6381. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6382. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6383. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6384. if(default_category==nil)
  6385. default_category="";
  6386. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6387. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6388. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6389. categoryID = nsdefault_category;
  6390. }];
  6391. if (!categoryID.length) {
  6392. NSString *cateIDs = params[@"category"];
  6393. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6394. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6395. for (NSString *cateID in requestCategoryArr) {
  6396. BOOL needBreak = NO;
  6397. for (NSString *itemCateIDBox in itemCategoryArr) {
  6398. if (itemCateIDBox.length > 4) {
  6399. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6400. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6401. if ([itemCategoryID isEqualToString:cateID]) {
  6402. needBreak = YES;
  6403. categoryID = itemCategoryID;
  6404. break;
  6405. }
  6406. }
  6407. }
  6408. if (needBreak) {
  6409. break;
  6410. }
  6411. }
  6412. }
  6413. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6414. if(wid !=0 && wisdelete != 1)
  6415. [item setValue:@"true" forKey:@"wish_exists"];
  6416. else
  6417. [item setValue:@"false" forKey:@"wish_exists"];
  6418. if(closeout==0)
  6419. [item setValue:@"false" forKey:@"is_closeout"];
  6420. else
  6421. [item setValue:@"true" forKey:@"is_closeout"];
  6422. if(cid==0)
  6423. [item setValue:@"false" forKey:@"cart_exists"];
  6424. else
  6425. [item setValue:@"true" forKey:@"cart_exists"];
  6426. if (more_color == 0) {
  6427. [item setObject:@(false) forKey:@"more_color"];
  6428. } else if (more_color == 1) {
  6429. [item setObject:@(true) forKey:@"more_color"];
  6430. }
  6431. [item addEntriesFromDictionary:imgjson];
  6432. // [item setValue:nsurl forKey:@"img"];
  6433. [item setValue:nsname forKey:@"name"];
  6434. [item setValue:nsdescription forKey:@"description"];
  6435. if (categoryID) {
  6436. [item setValue:categoryID forKey:@"item_category_id"];
  6437. }
  6438. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6439. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6440. i++;
  6441. }
  6442. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6443. [ret setObject:items forKey:@"items"];
  6444. sqlite3_finalize(statement);
  6445. } else {
  6446. DebugLog(@"nothing...");
  6447. }
  6448. DebugLog(@"count:%d",count);
  6449. [iSalesDB close_db:db];
  6450. #ifdef DEBUG
  6451. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6452. #endif
  6453. return ret;
  6454. }
  6455. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6456. {
  6457. return [self categoryList:params limited:YES];
  6458. }
  6459. # pragma mark item search
  6460. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6461. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6462. // assert(params[@"order_code"]);
  6463. // params[@"user"]
  6464. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6465. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6466. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6467. // category
  6468. NSDictionary *category_menu = [self offline_category_menu];
  6469. [filter setValue:category_menu forKey:@"category"];
  6470. NSString* where= nil;
  6471. NSString* orderby= @"m.name";
  6472. if (!filterSearch) {
  6473. int covertype = [[params valueForKey:@"covertype"] intValue];
  6474. switch (covertype) {
  6475. case 0:
  6476. {
  6477. where=@"m.category like'%%#005#%%'";
  6478. break;
  6479. }
  6480. case 1:
  6481. {
  6482. where=@"m.alert like '%QS%'";
  6483. break;
  6484. }
  6485. case 2:
  6486. {
  6487. where=@"m.availability>0";
  6488. break;
  6489. }
  6490. case 3:
  6491. {
  6492. where=@"m.best_seller>0";
  6493. orderby=@"m.best_seller desc,m.name asc";
  6494. break;
  6495. }
  6496. default:
  6497. where=@"1=1";
  6498. break;
  6499. }
  6500. }
  6501. int limit = [[params valueForKey:@"limit"] intValue];
  6502. int offset = [[params valueForKey:@"offset"] intValue];
  6503. NSString *limit_str = @"";
  6504. if (limited) {
  6505. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6506. }
  6507. sqlite3 *db = [iSalesDB get_db];
  6508. // [iSalesDB AddExFunction:db];
  6509. int count;
  6510. NSString *sqlQuery = nil;
  6511. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6512. 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];
  6513. double price_min = 0;
  6514. double price_max = 0;
  6515. if (filterSearch) {
  6516. // alert
  6517. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6518. NSString *alert = params[@"alert"];
  6519. if ([alert isEqualToString:@"Display All"]) {
  6520. alert = [NSString stringWithFormat:@""];
  6521. } else {
  6522. alert = [self translateSingleQuote:alert];
  6523. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6524. }
  6525. // available
  6526. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6527. NSString *available = params[@"available"];
  6528. NSString *available_condition;
  6529. if ([available isEqualToString:@"Display All"]) {
  6530. available_condition = @"";
  6531. } else if ([available isEqualToString:@"Available Now"]) {
  6532. available_condition = @"and availability > 0";
  6533. } else {
  6534. available_condition = @"and availability == 0";
  6535. }
  6536. // best seller
  6537. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6538. NSString *best_seller = @"";
  6539. NSString *order_best_seller = @"m.name asc";
  6540. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6541. best_seller = @"and best_seller > 0";
  6542. order_best_seller = @"m.best_seller desc,m.name asc";
  6543. }
  6544. // price
  6545. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6546. NSString *price = params[@"price"];
  6547. price_min = 0;
  6548. price_max = MAXFLOAT;
  6549. if (params[@"user"]) {
  6550. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6551. NSMutableString *priceName = [NSMutableString string];
  6552. for (int i = 0; i < priceTypeArray.count; i++) {
  6553. NSString *pricetype = priceTypeArray[i];
  6554. pricetype = [self translateSingleQuote:pricetype];
  6555. if (i == 0) {
  6556. [priceName appendFormat:@"'%@'",pricetype];
  6557. } else {
  6558. [priceName appendFormat:@",'%@'",pricetype];
  6559. }
  6560. }
  6561. if ([price isEqualToString:@"Display All"]) {
  6562. price = [NSString stringWithFormat:@""];
  6563. } else if([price containsString:@"+"]){
  6564. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6565. price_min = [price doubleValue];
  6566. 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];
  6567. } else {
  6568. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6569. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6570. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6571. 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];
  6572. }
  6573. } else {
  6574. price = @"";
  6575. }
  6576. // sold_by_qty : Sold in quantities of %@
  6577. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6578. NSString *qty = params[@"sold_by_qty"];
  6579. if ([qty isEqualToString:@"Display All"]) {
  6580. qty = @"";
  6581. } else {
  6582. qty = [self translateSingleQuote:qty];
  6583. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6584. }
  6585. // model name;
  6586. NSString* modelname =params[@"modelName"];
  6587. if(modelname.length==0)
  6588. {
  6589. modelname=@"";
  6590. }
  6591. else
  6592. {
  6593. modelname = modelname.lowercaseString;
  6594. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6595. }
  6596. //modelDescription
  6597. NSString* modelDescription =params[@"modelDescription"];
  6598. if(modelDescription.length==0)
  6599. {
  6600. modelDescription=@"";
  6601. }
  6602. else
  6603. {
  6604. modelDescription = modelDescription.lowercaseString;
  6605. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6606. }
  6607. //lower(description) like'%%%@%%'
  6608. // category
  6609. NSString *category_id = params[@"ctgId"];
  6610. NSMutableArray *cate_id_array = nil;
  6611. NSMutableString *cateWhere = [NSMutableString string];
  6612. if ([category_id isEqualToString:@""] || !category_id) {
  6613. [cateWhere appendString:@"1 = 1"];
  6614. } else {
  6615. if ([category_id containsString:@","]) {
  6616. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6617. } else {
  6618. cate_id_array = [@[category_id] mutableCopy];
  6619. }
  6620. /*-----------*/
  6621. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6622. [cateWhere appendString:@"("];
  6623. for (int i = 0; i < cate_id_array.count; i++) {
  6624. for (NSString *key0 in cateDic.allKeys) {
  6625. if ([key0 containsString:@"category_"]) {
  6626. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6627. for (NSString *key1 in category0.allKeys) {
  6628. if ([key1 containsString:@"category_"]) {
  6629. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6630. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6631. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6632. cate_id_array[i] = [category1 objectForKey:@"id"];
  6633. if (i == 0) {
  6634. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6635. } else {
  6636. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6637. }
  6638. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6639. [category0 setValue:category1 forKey:key1];
  6640. [cateDic setValue:category0 forKey:key0];
  6641. }
  6642. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6643. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6644. cate_id_array[i] = [category0 objectForKey:@"id"];
  6645. if (i == 0) {
  6646. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6647. } else {
  6648. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6649. }
  6650. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6651. [cateDic setValue:category0 forKey:key0];
  6652. }
  6653. }
  6654. }
  6655. }
  6656. }
  6657. }
  6658. [cateWhere appendString:@")"];
  6659. [filter setValue:cateDic forKey:@"category"];
  6660. }
  6661. // where bestseller > 0 order by bestseller desc
  6662. // sql query: alert availability(int) best_seller(int) price qty
  6663. 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];
  6664. // count
  6665. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6666. }
  6667. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6668. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6669. if (!params[@"user"]) {
  6670. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6671. }
  6672. [ret setValue:filter forKey:@"filter"];
  6673. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6674. sqlite3_stmt * statement;
  6675. [ret setValue:@"2" forKey:@"result"];
  6676. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6677. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6678. // int count=0;
  6679. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6680. {
  6681. int i=0;
  6682. while (sqlite3_step(statement) == SQLITE_ROW)
  6683. {
  6684. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6685. char *name = (char*)sqlite3_column_text(statement, 0);
  6686. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6687. char *description = (char*)sqlite3_column_text(statement, 1);
  6688. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6689. int product_id = sqlite3_column_int(statement, 2);
  6690. int wid = sqlite3_column_int(statement, 3);
  6691. int closeout = sqlite3_column_int(statement, 4);
  6692. int cid = sqlite3_column_int(statement, 5);
  6693. int wisdelete = sqlite3_column_int(statement, 6);
  6694. int more_color = sqlite3_column_int(statement, 7);
  6695. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6696. if(wid !=0 && wisdelete != 1)
  6697. [item setValue:@"true" forKey:@"wish_exists"];
  6698. else
  6699. [item setValue:@"false" forKey:@"wish_exists"];
  6700. if(closeout==0)
  6701. [item setValue:@"false" forKey:@"is_closeout"];
  6702. else
  6703. [item setValue:@"true" forKey:@"is_closeout"];
  6704. if(cid==0)
  6705. [item setValue:@"false" forKey:@"cart_exists"];
  6706. else
  6707. [item setValue:@"true" forKey:@"cart_exists"];
  6708. if (more_color == 0) {
  6709. [item setObject:@(false) forKey:@"more_color"];
  6710. } else if (more_color == 1) {
  6711. [item setObject:@(true) forKey:@"more_color"];
  6712. }
  6713. [item addEntriesFromDictionary:imgjson];
  6714. // [item setValue:nsurl forKey:@"img"];
  6715. [item setValue:nsname forKey:@"fash_name"];
  6716. [item setValue:nsdescription forKey:@"description"];
  6717. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6718. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6719. i++;
  6720. }
  6721. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6722. [ret setObject:items forKey:@"items"];
  6723. sqlite3_finalize(statement);
  6724. }
  6725. [iSalesDB close_db:db];
  6726. #ifdef DEBUG
  6727. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6728. #endif
  6729. return ret;
  6730. }
  6731. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6732. {
  6733. return [self itemsearch:params limited:YES];
  6734. }
  6735. #pragma mark order detail
  6736. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6737. if (str1.length == 0) {
  6738. str1 = @"&nbsp";
  6739. }
  6740. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6741. return str;
  6742. }
  6743. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6744. {
  6745. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6746. [fromformatter setDateFormat:from];
  6747. NSDate *date = [fromformatter dateFromString:datetime];
  6748. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6749. [toformatter setDateFormat:to];
  6750. NSString * ret = [toformatter stringFromDate:date];
  6751. return ret;
  6752. }
  6753. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6754. // 把毫秒去掉
  6755. if ([dateTime containsString:@"."]) {
  6756. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6757. }
  6758. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6759. formatter.dateFormat = formate;
  6760. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6761. NSDate *date = [formatter dateFromString:dateTime];
  6762. formatter.dateFormat = newFormate;
  6763. NSString *result = [formatter stringFromDate:date];
  6764. return result ? result : @"";
  6765. }
  6766. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6767. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6768. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6769. }
  6770. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6771. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6772. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6773. }
  6774. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6775. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6776. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6777. }
  6778. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6779. {
  6780. assert(params[@"mode"]!=nil);
  6781. assert(params[@"user"]!=nil);
  6782. DebugLog(@"offline oderdetail params: %@",params);
  6783. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6784. int orderId = [params[@"orderId"] intValue];
  6785. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6786. // decrypt card number and card security code
  6787. // 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 ];
  6788. 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];
  6789. sqlite3 *db = [iSalesDB get_db];
  6790. sqlite3_stmt * statement;
  6791. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6792. NSString *nssoid = @"";
  6793. NSString* orderinfo = @"";
  6794. NSString *moreInfo = @"";
  6795. double handlingFee = 0;
  6796. double payments_and_credist = 0;
  6797. double totalPrice = 0;
  6798. double shippingFee = 0;
  6799. double lift_gate = 0;
  6800. NSString *customer_contact = @"";
  6801. NSString *customer_email = @"";
  6802. NSString *customer_fax = @"";
  6803. NSString *customer_phone = @"";
  6804. NSString *customer_city = @"";
  6805. NSString *customer_state = @"";
  6806. NSString *customer_zipcode = @"";
  6807. NSString *customer_country = @"";
  6808. BOOL must_call = NO;
  6809. double discount = 0;
  6810. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6811. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6812. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6813. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6814. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6815. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6816. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6817. {
  6818. if (sqlite3_step(statement) == SQLITE_ROW)
  6819. {
  6820. // int order_id = sqlite3_column_int(statement, 0);
  6821. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6822. // ret[@"sign_url"] = sign_url;
  6823. section_1 = @{
  6824. @"data":sign_url,
  6825. @"title":@"Signature",
  6826. @"type":@"sign_url"
  6827. }.mutableCopy;
  6828. [ret setObject:section_1 forKey:@"section_1"];
  6829. customer_contact = [self textAtColumn:52 statement:statement];
  6830. customer_email = [self textAtColumn:53 statement:statement];
  6831. customer_phone = [self textAtColumn:54 statement:statement];
  6832. customer_fax = [self textAtColumn:55 statement:statement];
  6833. customer_city = [self textAtColumn:60 statement:statement];
  6834. customer_state = [self textAtColumn:61 statement:statement];
  6835. customer_zipcode = [self textAtColumn:62 statement:statement];
  6836. customer_country = [self textAtColumn:63 statement:statement];
  6837. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6838. discount =sqlite3_column_double(statement, 65);
  6839. int offline_edit=sqlite3_column_int(statement, 56);
  6840. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6841. char *soid = (char*)sqlite3_column_text(statement, 1);
  6842. if(soid==nil)
  6843. soid= "";
  6844. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6845. // so#
  6846. ret[@"so#"] = nssoid;
  6847. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6848. if(poNumber==nil)
  6849. poNumber= "";
  6850. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6851. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6852. if(create_time==nil)
  6853. create_time= "";
  6854. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6855. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6856. int status = sqlite3_column_int(statement, 4);
  6857. int erpStatus = sqlite3_column_int(statement, 49);
  6858. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6859. // status
  6860. if (status > 1 && status != 3) {
  6861. status = erpStatus;
  6862. } else if (status == 3) {
  6863. status = 15;
  6864. }
  6865. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6866. ret[@"order_status"] = nsstatus;
  6867. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6868. if(company_name==nil)
  6869. company_name= "";
  6870. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6871. // company name
  6872. ret[@"company_name"] = nscompany_name;
  6873. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6874. if(customer_contact==nil)
  6875. customer_contact= "";
  6876. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6877. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6878. if(addr_1==nil)
  6879. addr_1="";
  6880. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6881. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6882. if(addr_2==nil)
  6883. addr_2="";
  6884. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6885. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6886. if(addr_3==nil)
  6887. addr_3="";
  6888. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6889. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6890. if(addr_4==nil)
  6891. addr_4="";
  6892. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6893. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6894. [arr_addr addObject:nsaddr_1];
  6895. [arr_addr addObject:nsaddr_2];
  6896. [arr_addr addObject:nsaddr_3];
  6897. [arr_addr addObject:nsaddr_4];
  6898. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6899. [arr_addr addObject:customer_state];
  6900. [arr_addr addObject:customer_zipcode];
  6901. [arr_addr addObject:customer_country];
  6902. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6903. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6904. char *logist = (char*)sqlite3_column_text(statement, 11);
  6905. if(logist==nil)
  6906. logist= "";
  6907. NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  6908. if (/*status == -11 || */status == 10 || status == 11) {
  6909. DebugLogist = [self textAtColumn:59 statement:statement];
  6910. };
  6911. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6912. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6913. shipping = @"Shipping To Be Quoted";
  6914. } else {
  6915. shippingFee = sqlite3_column_double(statement, 12);
  6916. }
  6917. // Shipping
  6918. // ret[@"Shipping"] = shipping;
  6919. NSDictionary *shipping_item = @{
  6920. @"title":@"Shipping",
  6921. @"value":shipping
  6922. };
  6923. [price_data setObject:shipping_item forKey:@"item_1"];
  6924. int have_lift_gate = sqlite3_column_int(statement, 17);
  6925. lift_gate = sqlite3_column_double(statement, 13);
  6926. // Liftgate Fee(No loading dock)
  6927. if (!have_lift_gate) {
  6928. lift_gate = 0;
  6929. }
  6930. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6931. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6932. if (sqlite3_column_int(statement, 57)) {
  6933. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6934. liftgate_value = @"Shipping To Be Quoted";
  6935. }
  6936. NSDictionary *liftgate_item = @{
  6937. @"title":@"Liftgate Fee(No loading dock)",
  6938. @"value":liftgate_value
  6939. };
  6940. [price_data setObject:liftgate_item forKey:@"item_2"];
  6941. // if ([DebugLogist isEqualToString:@"WILL CALL"]) {
  6942. // [ret removeObjectForKey:@"Shipping"];
  6943. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6944. // }
  6945. //
  6946. // if (have_lift_gate) {
  6947. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6948. // }
  6949. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6950. if(general_notes==nil)
  6951. general_notes= "";
  6952. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6953. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6954. if(internal_notes==nil)
  6955. internal_notes= "";
  6956. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6957. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6958. if(payment_type==nil)
  6959. payment_type= "";
  6960. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6961. // order info
  6962. orderinfo = [self textFileName:@"order_info.html"];
  6963. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6964. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6965. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6966. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6967. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6968. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6969. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6970. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  6971. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6972. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6973. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6974. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6975. NSString *payment = nil;
  6976. // id -> show
  6977. __block NSString *show_pay_type = nspayment_type;
  6978. [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) {
  6979. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6980. if (show_typ_ch != NULL) {
  6981. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6982. }
  6983. }];
  6984. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6985. {
  6986. payment = [self textFileName:@"creditcardpayment.html"];
  6987. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6988. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6989. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6990. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6991. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6992. NSString *card_type = [self textAtColumn:42 statement:statement];
  6993. if (card_type.length > 0) { // 显示星号
  6994. card_type = @"****";
  6995. }
  6996. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6997. if (card_number.length > 0 && card_number.length > 4) {
  6998. for (int i = 0; i < card_number.length - 4; i++) {
  6999. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7000. }
  7001. } else {
  7002. card_number = @"";
  7003. }
  7004. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7005. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7006. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7007. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7008. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7009. card_expiration = @"****";
  7010. }
  7011. NSString *card_city = [self textAtColumn:46 statement:statement];
  7012. NSString *card_state = [self textAtColumn:47 statement:statement];
  7013. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7014. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7015. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7016. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7017. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7018. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7019. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7020. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7021. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7022. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7023. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7024. }
  7025. else
  7026. {
  7027. payment=[self textFileName:@"normalpayment.html"];
  7028. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7029. }
  7030. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7031. ret[@"result"]= [NSNumber numberWithInt:2];
  7032. // more info
  7033. moreInfo = [self textFileName:@"more_info.html"];
  7034. /*****ship to******/
  7035. // ShipToCompany_or_&nbsp
  7036. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7037. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7038. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7039. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7040. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7041. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7042. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7043. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7044. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7045. /*****ship from******/
  7046. // ShipFromCompany_or_&nbsp
  7047. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7048. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7049. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7050. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7051. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7052. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7053. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7054. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7055. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7056. /*****freight to******/
  7057. // FreightBillToCompany_or_&nbsp
  7058. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7059. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7060. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7061. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7062. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7063. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7064. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7065. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7066. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7067. /*****merchandise to******/
  7068. // MerchandiseBillToCompany_or_&nbsp
  7069. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7070. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7071. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7072. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7073. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7074. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7075. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7076. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7077. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7078. /*****return to******/
  7079. // ReturnToCompany_or_&nbsp
  7080. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7081. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7082. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7083. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7084. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7085. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7086. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7087. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7088. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7089. //
  7090. // DebugLog(@"more info : %@",moreInfo);
  7091. // handling fee
  7092. handlingFee = sqlite3_column_double(statement, 33);
  7093. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7094. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7095. if (sqlite3_column_int(statement, 58)) {
  7096. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7097. handling_fee_value = @"Shipping To Be Quoted";
  7098. }
  7099. NSDictionary *handling_fee_item = @{
  7100. @"title":@"Handling Fee",
  7101. @"value":handling_fee_value
  7102. };
  7103. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7104. //
  7105. // customer info
  7106. customerID = [self textAtColumn:36 statement:statement];
  7107. // mode
  7108. ret[@"mode"] = params[@"mode"];
  7109. // model_count
  7110. ret[@"model_count"] = @(0);
  7111. }
  7112. sqlite3_finalize(statement);
  7113. }
  7114. [iSalesDB close_db:db];
  7115. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7116. // "customer_email" = "Shui Hu";
  7117. // "customer_fax" = "";
  7118. // "customer_first_name" = F;
  7119. // "customer_last_name" = L;
  7120. // "customer_name" = ",da He Xiang Dong Liu A";
  7121. // "customer_phone" = "Hey Xuan Feng";
  7122. contactInfo[@"customer_phone"] = customer_phone;
  7123. contactInfo[@"customer_fax"] = customer_fax;
  7124. contactInfo[@"customer_email"] = customer_email;
  7125. NSString *first_name = @"";
  7126. NSString *last_name = @"";
  7127. if ([customer_contact isEqualToString:@""]) {
  7128. } else if ([customer_contact containsString:@" "]) {
  7129. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7130. first_name = [customer_contact substringToIndex:first_space_index];
  7131. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7132. }
  7133. contactInfo[@"customer_first_name"] = first_name;
  7134. contactInfo[@"customer_last_name"] = last_name;
  7135. ret[@"customerInfo"] = contactInfo;
  7136. // models
  7137. if (nssoid) {
  7138. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7139. __block double TotalCuft = 0;
  7140. __block double TotalWeight = 0;
  7141. __block int TotalCarton = 0;
  7142. __block double allItemPrice = 0;
  7143. 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];
  7144. sqlite3 *db1 = [iSalesDB get_db];
  7145. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7146. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7147. int product_id = sqlite3_column_int(stmt, 0);
  7148. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7149. int item_id = sqlite3_column_int(stmt, 7);
  7150. NSString* Price=nil;
  7151. if(str_price==nil)
  7152. {
  7153. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7154. if(price==nil)
  7155. Price=@"No Price.";
  7156. else
  7157. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7158. }
  7159. else
  7160. {
  7161. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7162. }
  7163. double discount = sqlite3_column_double(stmt, 2);
  7164. int item_count = sqlite3_column_int(stmt, 3);
  7165. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7166. NSString *nsline_note=nil;
  7167. if(line_note!=nil)
  7168. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7169. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7170. // NSString *nsname = nil;
  7171. // if(name!=nil)
  7172. // nsname= [[NSString alloc]initWithUTF8String:name];
  7173. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7174. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7175. // NSString *nsdescription=nil;
  7176. // if(description!=nil)
  7177. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7178. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7179. // int stockUom = sqlite3_column_int(stmt, 8);
  7180. // int _id = sqlite3_column_int(stmt, 9);
  7181. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7182. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7183. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7184. int carton=[bsubtotaljson[@"carton"] intValue];
  7185. TotalCuft += cuft;
  7186. TotalWeight += weight;
  7187. TotalCarton += carton;
  7188. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7189. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7190. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7191. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7192. itemjson[@"note"]=nsline_note;
  7193. itemjson[@"origin_price"] = Price;
  7194. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7195. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7196. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7197. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7198. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7199. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7200. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7201. if(itemjson[@"combine"] != nil)
  7202. {
  7203. // int citem=0;
  7204. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7205. for(int bc=0;bc<bcount;bc++)
  7206. {
  7207. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7208. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7209. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7210. subTotal += uprice * modulus * item_count;
  7211. }
  7212. }
  7213. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7214. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7215. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7216. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7217. itemjson[@"type"] = @"order_item";
  7218. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7219. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7220. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7221. allItemPrice += subTotal;
  7222. }];
  7223. section_3[@"data"] = model_data;
  7224. section_3[@"type"] = @"sub_order";
  7225. section_3[@"title"] = @"Models";
  7226. section_3[@"switch"] = @(false);
  7227. ret[@"section_3"] = section_3;
  7228. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7229. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7230. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7231. // payments/Credits
  7232. // payments_and_credist = sqlite3_column_double(statement, 34);
  7233. payments_and_credist = allItemPrice;
  7234. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7235. NSDictionary *sub_total_item = @{
  7236. @"title":@"Sub-Total",
  7237. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7238. };
  7239. [price_data setObject:sub_total_item forKey:@"item_0"];
  7240. // // total
  7241. // totalPrice = sqlite3_column_double(statement, 35);
  7242. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7243. } else {
  7244. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7245. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7246. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7247. // payments/Credits
  7248. payments_and_credist = 0;
  7249. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7250. NSDictionary *sub_total_item = @{
  7251. @"title":@"Sub-Total",
  7252. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7253. };
  7254. [price_data setObject:sub_total_item forKey:@"item_0"];
  7255. }
  7256. // total
  7257. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7258. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7259. if(discount>0)
  7260. {
  7261. NSDictionary *total_item = @{
  7262. @"title":@"Total",
  7263. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist*(1-(discount/100.0))+ lift_gate + shippingFee + handlingFee]
  7264. };
  7265. NSDictionary *discount_item = @{
  7266. @"title":@"Order Discount",
  7267. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7268. };
  7269. [price_data setObject:discount_item forKey:@"item_4"];
  7270. [price_data setObject:total_item forKey:@"item_5"];
  7271. }
  7272. else
  7273. {
  7274. NSDictionary *total_item = @{
  7275. @"title":@"Total",
  7276. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7277. };
  7278. [price_data setObject:total_item forKey:@"item_4"];
  7279. }
  7280. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7281. section_4 = @{
  7282. @"data":price_data,
  7283. @"title":@"Price Info",
  7284. @"type":@"price_info"
  7285. }.mutableCopy;
  7286. ret[@"section_4"] = section_4;
  7287. // ret[@"order_info"]= orderinfo;
  7288. section_0 = @{
  7289. @"data":orderinfo,
  7290. @"title":@"Order Info",
  7291. @"type":@"order_info"
  7292. }.mutableCopy;
  7293. ret[@"section_0"] = section_0;
  7294. // ret[@"more_order_info"] = moreInfo;
  7295. section_2 = @{
  7296. @"data":moreInfo,
  7297. @"title":@"More Info",
  7298. @"type":@"more_order_info"
  7299. }.mutableCopy;
  7300. ret[@"section_2"] = section_2;
  7301. ret[@"count"] = @(5);
  7302. return [RAConvertor dict2data:ret];
  7303. }
  7304. #pragma mark order list
  7305. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7306. NSString *nsstatus = @"";
  7307. switch (status) {
  7308. case 0:
  7309. {
  7310. nsstatus=@"Temp Order";
  7311. break;
  7312. }
  7313. case 1:
  7314. {
  7315. nsstatus=@"Saved Order";
  7316. break;
  7317. }
  7318. case 2: {
  7319. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7320. break;
  7321. }
  7322. case 3:
  7323. case 15:
  7324. {
  7325. nsstatus=@"Cancelled";
  7326. break;
  7327. }
  7328. case 10:
  7329. {
  7330. nsstatus=@"Quote Submitted";
  7331. break;
  7332. }
  7333. case 11:
  7334. {
  7335. nsstatus=@"Sales Order Submitted";
  7336. break;
  7337. }
  7338. case 12:
  7339. {
  7340. nsstatus=@"Processing";
  7341. break;
  7342. }
  7343. case 13:
  7344. {
  7345. nsstatus=@"Shipped";
  7346. break;
  7347. }
  7348. case 14:
  7349. {
  7350. nsstatus=@"Closed";
  7351. break;
  7352. }
  7353. case -11:
  7354. {
  7355. nsstatus = @"Ready For Submit";
  7356. break;
  7357. }
  7358. default:
  7359. break;
  7360. }
  7361. return nsstatus;
  7362. }
  7363. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7364. {
  7365. double total = 0;
  7366. __block double payments_and_credist = 0;
  7367. __block double allItemPrice = 0;
  7368. // sqlite3 *db1 = [iSalesDB get_db];
  7369. if (so_id) {
  7370. // 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];
  7371. 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];
  7372. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7373. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7374. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7375. int product_id = sqlite3_column_int(stmt, 0);
  7376. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7377. int discount = sqlite3_column_int(stmt, 2);
  7378. int item_count = sqlite3_column_int(stmt, 3);
  7379. // int item_id = sqlite3_column_int(stmt, 7);
  7380. int item_id = sqlite3_column_int(stmt, 4);
  7381. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7382. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7383. NSString* Price=nil;
  7384. if(str_price==nil)
  7385. {
  7386. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7387. if(price==nil)
  7388. Price=@"No Price.";
  7389. else
  7390. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7391. }
  7392. else
  7393. {
  7394. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7395. }
  7396. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7397. itemjson[@"The unit price"]=Price;
  7398. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7399. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7400. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7401. if(itemjson[@"combine"] != nil)
  7402. {
  7403. // int citem=0;
  7404. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7405. for(int bc=0;bc<bcount;bc++)
  7406. {
  7407. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7408. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7409. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7410. subTotal += uprice * modulus * item_count;
  7411. }
  7412. }
  7413. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7414. allItemPrice += subTotal;
  7415. }];
  7416. payments_and_credist = allItemPrice;
  7417. } else {
  7418. // payments/Credits
  7419. payments_and_credist = 0;
  7420. }
  7421. // lift_gate handlingFee shippingFee
  7422. __block double lift_gate = 0;
  7423. __block double handlingFee = 0;
  7424. __block double shippingFee = 0;
  7425. __block double order_discount = 0;
  7426. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7427. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7428. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7429. if (have_lift_gate) {
  7430. lift_gate = sqlite3_column_double(stmt, 1);
  7431. }
  7432. handlingFee = sqlite3_column_double(stmt, 2);
  7433. shippingFee = sqlite3_column_double(stmt, 3);
  7434. order_discount = sqlite3_column_double(stmt, 4);
  7435. }];
  7436. if(order_discount>0)
  7437. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7438. // total
  7439. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7440. if (close) {
  7441. [iSalesDB close_db:db1];
  7442. }
  7443. return total;
  7444. }
  7445. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7446. {
  7447. assert(params[@"user"]!=nil);
  7448. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7449. int limit = [[params valueForKey:@"limit"] intValue];
  7450. int offset = [[params valueForKey:@"offset"] intValue];
  7451. NSString* keyword = [params valueForKey:@"keyWord"];
  7452. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7453. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7454. NSString* where=@"1 = 1";
  7455. if(keyword.length>0)
  7456. 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]];
  7457. if (orderStatus.length > 0) {
  7458. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7459. if (order_status_array.count == 1) {
  7460. int status_value = [[order_status_array firstObject] intValue];
  7461. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7462. if (status_value == 15 || status_value == 3) {
  7463. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7464. } else {
  7465. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7466. }
  7467. } else {
  7468. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7469. }
  7470. } else if (order_status_array.count > 1) {
  7471. for (int i = 0; i < order_status_array.count;i++) {
  7472. NSString *status = order_status_array[i];
  7473. NSString *condition = @" or";
  7474. if (i == 0) {
  7475. condition = @" and (";
  7476. }
  7477. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7478. int status_value = [status intValue];
  7479. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7480. if (status_value == 15 || status_value == 3) {
  7481. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7482. } else {
  7483. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7484. }
  7485. } else {
  7486. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7487. }
  7488. }
  7489. where = [where stringByAppendingString:@" )"];
  7490. }
  7491. }
  7492. 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];
  7493. // DebugLog(@"order list sql: %@",sqlQuery);
  7494. sqlite3 *db = [iSalesDB get_db];
  7495. sqlite3_stmt * statement;
  7496. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7497. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7498. {
  7499. int count=0;
  7500. while (sqlite3_step(statement) == SQLITE_ROW)
  7501. {
  7502. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7503. int order_id = sqlite3_column_double(statement, 0);
  7504. char *soid = (char*)sqlite3_column_text(statement, 1);
  7505. if(soid==nil)
  7506. soid= "";
  7507. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7508. int status = sqlite3_column_double(statement, 2);
  7509. int erpStatus = sqlite3_column_double(statement, 9);
  7510. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7511. if(sales_rep==nil)
  7512. sales_rep= "";
  7513. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7514. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7515. if(create_by==nil)
  7516. create_by= "";
  7517. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7518. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7519. if(company_name==nil)
  7520. company_name= "";
  7521. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7522. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7523. if(create_time==nil)
  7524. create_time= "";
  7525. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7526. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7527. // double total_price = sqlite3_column_double(statement, 7);
  7528. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7529. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7530. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7531. int offline_edit = sqlite3_column_int(statement, 10);
  7532. // ": "JH",
  7533. // "": "$8307.00",
  7534. // "": "MOB1608050001",
  7535. // "": "ArpithaT",
  7536. // "": "1st Stage Property Transformations",
  7537. // "": "JANICE SUTTON",
  7538. // "": 2255,
  7539. // "": "08/02/2016 09:49:18",
  7540. // "": 1,
  7541. // "": "Saved Order"
  7542. // "": "1470384050483",
  7543. // "model_count": "6 / 28"
  7544. item[@"sales_rep"]= nssales_rep;
  7545. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7546. item[@"so#"]= nssoid;
  7547. item[@"create_by"]= nscreate_by;
  7548. item[@"customer_name"]= nscompany_name;
  7549. item[@"customer_contact"] = customer_contact;
  7550. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7551. item[@"purchase_time"]= nscreate_time;
  7552. int statusCode = status;
  7553. if (statusCode == 2) {
  7554. statusCode = erpStatus;
  7555. } else if (statusCode == 3) {
  7556. statusCode = 15;
  7557. }
  7558. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7559. item[@"order_status"]= nsstatus;
  7560. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7561. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7562. // item[@"model_count"]
  7563. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7564. count++;
  7565. }
  7566. ret[@"count"]= [NSNumber numberWithInt:count];
  7567. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7568. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7569. ret[@"result"]= [NSNumber numberWithInt:2];
  7570. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7571. ret[@"time_zone"] = @"PST";
  7572. sqlite3_finalize(statement);
  7573. }
  7574. 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];
  7575. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7576. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7577. [iSalesDB close_db:db];
  7578. return [RAConvertor dict2data:ret];
  7579. }
  7580. #pragma mark update gnotes
  7581. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7582. {
  7583. DebugLog(@"params: %@",params);
  7584. // comments = Meyoyoyoyoyoyoy;
  7585. // orderCode = MOB1608110001;
  7586. // password = 123456;
  7587. // user = EvanK;
  7588. 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]];
  7589. int ret = [iSalesDB execSql:sql];
  7590. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7591. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7592. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7593. // [dic setValue:@"160409" forKey:@"min_ver"];
  7594. return [RAConvertor dict2data:dic];
  7595. }
  7596. #pragma mark move to wishlist
  7597. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7598. {
  7599. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7600. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7601. _id = [NSString stringWithFormat:@"(%@)",_id];
  7602. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7603. sqlite3 *db = [iSalesDB get_db];
  7604. __block NSString *product_id = @"";
  7605. __block NSString *item_count_str = @"";
  7606. // __block NSString *item_id = nil;
  7607. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7608. // product_id = [self textAtColumn:0 statement:stmt];
  7609. int item_count = sqlite3_column_int(stmt, 1);
  7610. // item_id = [self textAtColumn:2 statement:stmt];
  7611. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7612. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7613. if (p_id.length) {
  7614. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7615. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7616. }
  7617. }];
  7618. [iSalesDB close_db:db];
  7619. // 去除第一个,
  7620. if (product_id.length > 1) {
  7621. product_id = [product_id substringFromIndex:1];
  7622. }
  7623. if (item_count_str.length > 1) {
  7624. item_count_str = [item_count_str substringFromIndex:1];
  7625. }
  7626. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7627. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7628. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7629. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7630. sqlite3 *db1 = [iSalesDB get_db];
  7631. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7632. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7633. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7634. // 删除
  7635. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7636. int ret = [iSalesDB execSql:deleteSql db:db1];
  7637. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7638. [iSalesDB close_db:db1];
  7639. return [RAConvertor dict2data:dic];
  7640. }
  7641. #pragma mark cart delete
  7642. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7643. {
  7644. // cartItemId = 548;
  7645. // orderCode = MOB1608110001;
  7646. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7647. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7648. _id = [NSString stringWithFormat:@"(%@)",_id];
  7649. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7650. int ret = [iSalesDB execSql:sql];
  7651. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7652. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7653. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7654. // [dic setValue:@"160409" forKey:@"min_ver"];
  7655. return [RAConvertor dict2data:dic];
  7656. }
  7657. #pragma mark set order discount
  7658. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7659. //{
  7660. // DebugLog(@"cart set price params: %@",params);
  7661. // // "cartitem_id" = 1;
  7662. // // discount = "0.000000";
  7663. // // "item_note" = "";
  7664. // // price = "269.000000";
  7665. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7666. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7667. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7668. //// NSString *price = [self valueInParams:params key:@"price"];
  7669. //
  7670. // // bool badd_price_changed=false;
  7671. // // sqlite3* db=[iSalesDB get_db];
  7672. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7673. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7674. // // NSRange range;
  7675. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7676. // //
  7677. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7678. // // badd_price_changed=true;
  7679. // // [iSalesDB close_db:db];
  7680. // //
  7681. // // if(badd_price_changed)
  7682. // // {
  7683. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7684. // // }
  7685. // //
  7686. //
  7687. //
  7688. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7689. //
  7690. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7691. //
  7692. // int ret = [iSalesDB execSql:sql];
  7693. //
  7694. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7695. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7696. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7697. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7698. //
  7699. //// return [RAConvertor dict2data:dic];
  7700. // return dic;
  7701. //
  7702. //}
  7703. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7704. {
  7705. DebugLog(@"order set discount params: %@",params);
  7706. // "cartitem_id" = 1;
  7707. // discount = "0.000000";
  7708. // "item_note" = "";
  7709. // price = "269.000000";
  7710. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7711. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7712. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7713. // NSString *price = [self valueInParams:params key:@"price"];
  7714. // bool badd_price_changed=false;
  7715. // sqlite3* db=[iSalesDB get_db];
  7716. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7717. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7718. // NSRange range;
  7719. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7720. //
  7721. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7722. // badd_price_changed=true;
  7723. // [iSalesDB close_db:db];
  7724. //
  7725. // if(badd_price_changed)
  7726. // {
  7727. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7728. // }
  7729. //
  7730. // sqlite3 *db = [iSalesDB get_db];
  7731. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7732. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7733. //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];
  7734. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7735. int ret = [iSalesDB execSql:sql];
  7736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7737. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7738. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7739. // [dic setValue:@"160409" forKey:@"min_ver"];
  7740. // return [RAConvertor dict2data:dic];
  7741. return dic;
  7742. }
  7743. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7744. {
  7745. DebugLog(@"cart set price params: %@",params);
  7746. // "cartitem_id" = 1;
  7747. // discount = "0.000000";
  7748. // "item_note" = "";
  7749. // price = "269.000000";
  7750. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7751. NSString *notes = [self valueInParams:params key:@"item_note"];
  7752. NSString *discount = [self valueInParams:params key:@"discount"];
  7753. NSString *price = [self valueInParams:params key:@"price"];
  7754. // bool badd_price_changed=false;
  7755. // sqlite3* db=[iSalesDB get_db];
  7756. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7757. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7758. // NSRange range;
  7759. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7760. //
  7761. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7762. // badd_price_changed=true;
  7763. // [iSalesDB close_db:db];
  7764. //
  7765. // if(badd_price_changed)
  7766. // {
  7767. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7768. // }
  7769. //
  7770. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7771. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7772. int ret = [iSalesDB execSql:sql];
  7773. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7774. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7775. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7776. // [dic setValue:@"160409" forKey:@"min_ver"];
  7777. return [RAConvertor dict2data:dic];
  7778. }
  7779. #pragma mark set line notes
  7780. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7781. {
  7782. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7783. NSString *notes = [self valueInParams:params key:@"notes"];
  7784. notes = [self translateSingleQuote:notes];
  7785. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7786. int ret = [iSalesDB execSql:sql];
  7787. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7788. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7789. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7790. // [dic setValue:@"160409" forKey:@"min_ver"];
  7791. return [RAConvertor dict2data:dic];
  7792. }
  7793. #pragma mark set qty
  7794. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7795. {
  7796. assert(params[@"can_create_backorder"]!=nil);
  7797. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7798. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7799. int item_count = [params[@"inputInt"] intValue];
  7800. // 购买检查数量大于库存
  7801. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7802. if (!params[@"can_create_backorder"]) {
  7803. 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];
  7804. __block BOOL out_of_stock = NO;
  7805. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7806. int availability = sqlite3_column_int(stmt, 0);
  7807. if (availability < item_count) {
  7808. out_of_stock = YES;
  7809. }
  7810. }];
  7811. if (out_of_stock) { // 缺货
  7812. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7813. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7814. return [RAConvertor dict2data:dic];
  7815. }
  7816. }
  7817. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7818. int ret = [iSalesDB execSql:sql];
  7819. __block int item_id = 0;
  7820. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7821. item_id = sqlite3_column_int(stmt, 0);
  7822. }];
  7823. sqlite3 *db = [iSalesDB get_db];
  7824. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7825. [iSalesDB close_db:db];
  7826. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7827. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7828. // [dic setValue:@"160409" forKey:@"min_ver"];
  7829. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7830. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7831. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7832. return [RAConvertor dict2data:dic];
  7833. }
  7834. #pragma mark place order
  7835. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7836. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7837. if (aname) {
  7838. [dic setValue:aname forKey:@"aname"];
  7839. }
  7840. if (control) {
  7841. [dic setValue:control forKey:@"control"];
  7842. }
  7843. if (keyboard) {
  7844. [dic setValue:keyboard forKey:@"keyboard"];
  7845. }
  7846. if (name) {
  7847. [dic setValue:name forKey:@"name"];
  7848. }
  7849. if (value) {
  7850. [dic setValue:value forKey:@"value"];
  7851. }
  7852. return dic;
  7853. }
  7854. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7855. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7856. orderCode = [self translateSingleQuote:orderCode];
  7857. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7858. 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];
  7859. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7860. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7861. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7862. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7863. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7864. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7865. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7866. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7867. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7868. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7869. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7870. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7871. NSString *email = [self textAtColumn:11 statement:stmt];
  7872. NSString *phone = [self textAtColumn:12 statement:stmt];
  7873. NSString *fax = [self textAtColumn:13 statement:stmt];
  7874. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7875. NSString *city = [self textAtColumn:15 statement:stmt];
  7876. NSString *state = [self textAtColumn:16 statement:stmt];
  7877. NSString *country = [self textAtColumn:17 statement:stmt];
  7878. NSString *name = [self textAtColumn:18 statement:stmt];
  7879. // contact id
  7880. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7881. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7882. [contact_id_dic setValue:@"text" forKey:@"control"];
  7883. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7884. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7885. [contact_id_dic setValue:@"true" forKey:@"required"];
  7886. [contact_id_dic setValue:contact_id forKey:@"value"];
  7887. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7888. // business card
  7889. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7890. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7891. [business_card_dic setValue:@"img" forKey:@"control"];
  7892. [business_card_dic setValue:@"1" forKey:@"disable"];
  7893. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7894. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7895. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7896. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7897. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7898. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7899. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7900. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7901. // fax
  7902. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7903. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7904. // zipcode
  7905. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7906. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7907. // city
  7908. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7909. [customer_dic setValue:city_dic forKey:@"item_12"];
  7910. // state
  7911. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7912. [customer_dic setValue:state_dic forKey:@"item_13"];
  7913. // country
  7914. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7915. [customer_dic setValue:country_dic forKey:@"item_14"];
  7916. // company name
  7917. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7918. [customer_dic setValue:company_dic forKey:@"item_2"];
  7919. // addr_1
  7920. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7921. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7922. // addr_2
  7923. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7924. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7925. // addr_3
  7926. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7927. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7928. // addr_4
  7929. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7930. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7931. // Contact
  7932. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7933. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7934. // email
  7935. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7936. [customer_dic setValue:email_dic forKey:@"item_8"];
  7937. // phone
  7938. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7939. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7940. // title
  7941. [customer_dic setValue:@"Customer" forKey:@"title"];
  7942. // count
  7943. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7944. }];
  7945. // setting
  7946. NSDictionary *setting = params[@"setting"];
  7947. NSNumber *hide = setting[@"CustomerHide"];
  7948. [customer_dic setValue:hide forKey:@"hide"];
  7949. return customer_dic;
  7950. }
  7951. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7952. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7953. // setting
  7954. NSDictionary *setting = params[@"setting"];
  7955. NSNumber *hide = setting[@"ShipToHide"];
  7956. [dic setValue:hide forKey:@"hide"];
  7957. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7958. orderCode = [self translateSingleQuote:orderCode];
  7959. 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];
  7960. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7961. NSString *cid = [self textAtColumn:0 statement:stmt];
  7962. NSString *name = [self textAtColumn:1 statement:stmt];
  7963. NSString *ext = [self textAtColumn:2 statement:stmt];
  7964. NSString *contact = [self textAtColumn:3 statement:stmt];
  7965. NSString *email = [self textAtColumn:4 statement:stmt];
  7966. NSString *fax = [self textAtColumn:5 statement:stmt];
  7967. NSString *phone = [self textAtColumn:6 statement:stmt];
  7968. // count
  7969. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7970. // title
  7971. [dic setValue:@"Ship To" forKey:@"title"];
  7972. // choose
  7973. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7974. [choose_dic setValue:@"choose" forKey:@"aname"];
  7975. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7976. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7977. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7978. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7979. @"receive_contact" : @"customer_contact",
  7980. @"receive_email" : @"customer_email",
  7981. @"receive_ext" : @"customer_contact_ext",
  7982. @"receive_fax" : @"customer_fax",
  7983. @"receive_name" : @"customer_name",
  7984. @"receive_phone" : @"customer_phone"},
  7985. @"refresh" : [NSNumber numberWithInteger:1],
  7986. @"type" : @"pull"};
  7987. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7988. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7989. @"name" : @"Add new address",
  7990. @"refresh" : [NSNumber numberWithInteger:1],
  7991. @"value" : @"new_addr"
  7992. };
  7993. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7994. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7995. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7996. @"receive_contact" : @"customer_contact",
  7997. @"receive_email" : @"customer_email",
  7998. @"receive_ext" : @"customer_contact_ext",
  7999. @"receive_fax" : @"customer_fax",
  8000. @"receive_name" : @"customer_name",
  8001. @"receive_phone" : @"customer_phone"},
  8002. @"name" : @"select_ship_to",
  8003. @"refresh" : [NSNumber numberWithInteger:1],
  8004. @"value" : @"Sales_Order_Ship_To"};
  8005. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8006. [dic setValue:choose_dic forKey:@"item_0"];
  8007. // contact id
  8008. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8009. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8010. [contact_id_dic setValue:@"true" forKey:@"required"];
  8011. [dic setValue:contact_id_dic forKey:@"item_1"];
  8012. // company name
  8013. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8014. [dic setValue:company_name_dic forKey:@"item_2"];
  8015. // address
  8016. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8017. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8018. if ([required integerValue]) {
  8019. [ext_dic setValue:@"true" forKey:@"required"];
  8020. }
  8021. [dic setValue:ext_dic forKey:@"item_3"];
  8022. // contact
  8023. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8024. [dic setValue:contact_dic forKey:@"item_4"];
  8025. // phone
  8026. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8027. [dic setValue:phone_dic forKey:@"item_5"];
  8028. // fax
  8029. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8030. [dic setValue:fax_dic forKey:@"item_6"];
  8031. // email
  8032. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8033. [dic setValue:email_dic forKey:@"item_7"];
  8034. }];
  8035. return dic;
  8036. }
  8037. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8038. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8039. // setting
  8040. NSDictionary *setting = params[@"setting"];
  8041. NSNumber *hide = setting[@"ShipFromHide"];
  8042. [dic setValue:hide forKey:@"hide"];
  8043. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8044. orderCode = [self translateSingleQuote:orderCode];
  8045. 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];
  8046. 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';";
  8047. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8048. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8049. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8050. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8051. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8052. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8053. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8054. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8055. if (!cid.length) {
  8056. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8057. cid = [self textAtColumn:0 statement:statment];
  8058. name = [self textAtColumn:1 statement:statment];
  8059. ext = [self textAtColumn:2 statement:statment];
  8060. contact = [self textAtColumn:3 statement:statment];
  8061. email = [self textAtColumn:4 statement:statment];
  8062. fax = [self textAtColumn:5 statement:statment];
  8063. phone = [self textAtColumn:6 statement:statment];
  8064. }];
  8065. }
  8066. // count
  8067. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8068. // title
  8069. [dic setValue:@"Ship From" forKey:@"title"];
  8070. // hide
  8071. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8072. // choose
  8073. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8074. [choose_dic setValue:@"choose" forKey:@"aname"];
  8075. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8076. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8077. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8078. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8079. @"sender_contact" : @"customer_contact",
  8080. @"sender_email" : @"customer_email",
  8081. @"sender_ext" : @"customer_contact_ext",
  8082. @"sender_fax" : @"customer_fax",
  8083. @"sender_name" : @"customer_name",
  8084. @"sender_phone" : @"customer_phone"},
  8085. @"name" : @"select_cid",
  8086. @"refresh" : [NSNumber numberWithInteger:0],
  8087. @"value" : @"Sales_Order_Ship_From"};
  8088. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8089. [dic setValue:choose_dic forKey:@"item_0"];
  8090. // contact id
  8091. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8092. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8093. [contact_id_dic setValue:@"true" forKey:@"required"];
  8094. [dic setValue:contact_id_dic forKey:@"item_1"];
  8095. // company name
  8096. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8097. [dic setValue:company_name_dic forKey:@"item_2"];
  8098. // address
  8099. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8100. [dic setValue:ext_dic forKey:@"item_3"];
  8101. // contact
  8102. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8103. [dic setValue:contact_dic forKey:@"item_4"];
  8104. // phone
  8105. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8106. [dic setValue:phone_dic forKey:@"item_5"];
  8107. // fax
  8108. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8109. [dic setValue:fax_dic forKey:@"item_6"];
  8110. // email
  8111. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8112. [dic setValue:email_dic forKey:@"item_7"];
  8113. }];
  8114. return dic;
  8115. }
  8116. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8117. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8118. // setting
  8119. NSDictionary *setting = params[@"setting"];
  8120. NSNumber *hide = setting[@"FreightBillToHide"];
  8121. [dic setValue:hide forKey:@"hide"];
  8122. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8123. orderCode = [self translateSingleQuote:orderCode];
  8124. 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];
  8125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8126. NSString *cid = [self textAtColumn:0 statement:stmt];
  8127. NSString *name = [self textAtColumn:1 statement:stmt];
  8128. NSString *ext = [self textAtColumn:2 statement:stmt];
  8129. NSString *contact = [self textAtColumn:3 statement:stmt];
  8130. NSString *email = [self textAtColumn:4 statement:stmt];
  8131. NSString *fax = [self textAtColumn:5 statement:stmt];
  8132. NSString *phone = [self textAtColumn:6 statement:stmt];
  8133. // count
  8134. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8135. // title
  8136. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8137. // hide
  8138. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8139. // choose
  8140. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8141. [choose_dic setValue:@"choose" forKey:@"aname"];
  8142. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8143. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8144. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8145. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8146. @"shipping_billto_contact" : @"receive_contact",
  8147. @"shipping_billto_email" : @"receive_email",
  8148. @"shipping_billto_ext" : @"receive_ext",
  8149. @"shipping_billto_fax" : @"receive_fax",
  8150. @"shipping_billto_name" : @"receive_name",
  8151. @"shipping_billto_phone" : @"receive_phone"},
  8152. @"type" : @"pull"};
  8153. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8154. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8155. @"type" : @"pull",
  8156. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8157. @"shipping_billto_contact" : @"customer_contact",
  8158. @"shipping_billto_email" : @"customer_email",
  8159. @"shipping_billto_ext" : @"customer_contact_ext",
  8160. @"shipping_billto_fax" : @"customer_fax",
  8161. @"shipping_billto_name" : @"customer_name",
  8162. @"shipping_billto_phone" : @"customer_phone"}
  8163. };
  8164. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8165. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8166. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8167. @"shipping_billto_contact" : @"sender_contact",
  8168. @"shipping_billto_email" : @"sender_email",
  8169. @"shipping_billto_ext" : @"sender_ext",
  8170. @"shipping_billto_fax" : @"sender_fax",
  8171. @"shipping_billto_name" : @"sender_name",
  8172. @"shipping_billto_phone" : @"sender_phone"},
  8173. @"type" : @"pull"
  8174. };
  8175. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8176. NSDictionary *select_freight_bill_to_dic = @{
  8177. @"aname" : @"Select freight bill to",
  8178. @"name" : @"select_cid",
  8179. @"refresh" : [NSNumber numberWithInteger:0],
  8180. @"value" : @"Sales_Order_Freight_Bill_To",
  8181. @"key_map" : @{
  8182. @"shipping_billto_cid" : @"customer_cid",
  8183. @"shipping_billto_contact" : @"customer_contact",
  8184. @"shipping_billto_email" : @"customer_email",
  8185. @"shipping_billto_ext" : @"customer_contact_ext",
  8186. @"shipping_billto_fax" : @"customer_fax",
  8187. @"shipping_billto_name" : @"customer_name",
  8188. @"shipping_billto_phone" : @"customer_phone"
  8189. }
  8190. };
  8191. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8192. [dic setValue:choose_dic forKey:@"item_0"];
  8193. // contact id
  8194. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8195. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8196. [contact_id_dic setValue:@"true" forKey:@"required"];
  8197. [dic setValue:contact_id_dic forKey:@"item_1"];
  8198. // company name
  8199. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8200. [dic setValue:company_name_dic forKey:@"item_2"];
  8201. // address
  8202. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8203. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8204. if ([ext_required integerValue]) {
  8205. [ext_dic setValue:@"true" forKey:@"required"];
  8206. }
  8207. [dic setValue:ext_dic forKey:@"item_3"];
  8208. // contact
  8209. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8210. [dic setValue:contact_dic forKey:@"item_4"];
  8211. // phone
  8212. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8213. [dic setValue:phone_dic forKey:@"item_5"];
  8214. // fax
  8215. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8216. [dic setValue:fax_dic forKey:@"item_6"];
  8217. // email
  8218. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8219. [dic setValue:email_dic forKey:@"item_7"];
  8220. }];
  8221. return dic;
  8222. }
  8223. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8224. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8225. // setting
  8226. NSDictionary *setting = params[@"setting"];
  8227. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8228. [dic setValue:hide forKey:@"hide"];
  8229. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8230. orderCode = [self translateSingleQuote:orderCode];
  8231. 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];
  8232. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8233. NSString *cid = [self textAtColumn:0 statement:stmt];
  8234. NSString *name = [self textAtColumn:1 statement:stmt];
  8235. NSString *ext = [self textAtColumn:2 statement:stmt];
  8236. NSString *contact = [self textAtColumn:3 statement:stmt];
  8237. NSString *email = [self textAtColumn:4 statement:stmt];
  8238. NSString *fax = [self textAtColumn:5 statement:stmt];
  8239. NSString *phone = [self textAtColumn:6 statement:stmt];
  8240. // count
  8241. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8242. // title
  8243. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8244. // hide
  8245. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8246. // choose
  8247. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8248. [choose_dic setValue:@"choose" forKey:@"aname"];
  8249. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8250. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8251. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8252. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8253. @"billing_contact" : @"receive_contact",
  8254. @"billing_email" : @"receive_email",
  8255. @"billing_ext" : @"receive_ext",
  8256. @"billing_fax" : @"receive_fax",
  8257. @"billing_name" : @"receive_name",
  8258. @"billing_phone" : @"receive_phone"},
  8259. @"type" : @"pull"};
  8260. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8261. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8262. @"type" : @"pull",
  8263. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8264. @"billing_contact" : @"customer_contact",
  8265. @"billing_email" : @"customer_email",
  8266. @"billing_ext" : @"customer_contact_ext",
  8267. @"billing_fax" : @"customer_fax",
  8268. @"billing_name" : @"customer_name",
  8269. @"billing_phone" : @"customer_phone"}
  8270. };
  8271. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8272. NSDictionary *select_bill_to_dic = @{
  8273. @"aname" : @"Select bill to",
  8274. @"name" : @"select_cid",
  8275. @"refresh" : [NSNumber numberWithInteger:0],
  8276. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8277. @"key_map" : @{
  8278. @"billing_cid" : @"customer_cid",
  8279. @"billing_contact" : @"customer_contact",
  8280. @"billing_email" : @"customer_email",
  8281. @"billing_ext" : @"customer_contact_ext",
  8282. @"billing_fax" : @"customer_fax",
  8283. @"billing_name" : @"customer_name",
  8284. @"billing_phone" : @"customer_phone"
  8285. }
  8286. };
  8287. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8288. [dic setValue:choose_dic forKey:@"item_0"];
  8289. // contact id
  8290. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8291. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8292. [contact_id_dic setValue:@"true" forKey:@"required"];
  8293. [dic setValue:contact_id_dic forKey:@"item_1"];
  8294. // company name
  8295. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8296. [dic setValue:company_name_dic forKey:@"item_2"];
  8297. // address
  8298. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8299. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8300. if ([ext_required integerValue]) {
  8301. [ext_dic setValue:@"true" forKey:@"required"];
  8302. }
  8303. [dic setValue:ext_dic forKey:@"item_3"];
  8304. // contact
  8305. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8306. [dic setValue:contact_dic forKey:@"item_4"];
  8307. // phone
  8308. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8309. [dic setValue:phone_dic forKey:@"item_5"];
  8310. // fax
  8311. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8312. [dic setValue:fax_dic forKey:@"item_6"];
  8313. // email
  8314. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8315. [dic setValue:email_dic forKey:@"item_7"];
  8316. }];
  8317. return dic;
  8318. }
  8319. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8320. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8321. // setting
  8322. NSDictionary *setting = params[@"setting"];
  8323. NSNumber *hide = setting[@"ReturnToHide"];
  8324. [dic setValue:hide forKey:@"hide"];
  8325. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8326. orderCode = [self translateSingleQuote:orderCode];
  8327. 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];
  8328. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8329. NSString *cid = [self textAtColumn:0 statement:stmt];
  8330. NSString *name = [self textAtColumn:1 statement:stmt];
  8331. NSString *ext = [self textAtColumn:2 statement:stmt];
  8332. NSString *contact = [self textAtColumn:3 statement:stmt];
  8333. NSString *email = [self textAtColumn:4 statement:stmt];
  8334. NSString *fax = [self textAtColumn:5 statement:stmt];
  8335. NSString *phone = [self textAtColumn:6 statement:stmt];
  8336. // count
  8337. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8338. // title
  8339. [dic setValue:@"Return To" forKey:@"title"];
  8340. // hide
  8341. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8342. // choose
  8343. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8344. [choose_dic setValue:@"choose" forKey:@"aname"];
  8345. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8346. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8347. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8348. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8349. @"returnto_contact" : @"sender_contact",
  8350. @"returnto_email" : @"sender_email",
  8351. @"returnto_ext" : @"sender_ext",
  8352. @"returnto_fax" : @"sender_fax",
  8353. @"returnto_name" : @"sender_name",
  8354. @"returnto_phone" : @"sender_phone"},
  8355. @"type" : @"pull"};
  8356. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8357. NSDictionary *select_return_to_dic = @{
  8358. @"aname" : @"Select return to",
  8359. @"name" : @"select_cid",
  8360. @"refresh" : [NSNumber numberWithInteger:0],
  8361. @"value" : @"Contact_Return_To",
  8362. @"key_map" : @{
  8363. @"returnto_cid" : @"customer_cid",
  8364. @"returnto_contact" : @"customer_contact",
  8365. @"returnto_email" : @"customer_email",
  8366. @"returnto_ext" : @"customer_contact_ext",
  8367. @"returnto_fax" : @"customer_fax",
  8368. @"returnto_name" : @"customer_name",
  8369. @"returnto_phone" : @"customer_phone"
  8370. }
  8371. };
  8372. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8373. [dic setValue:choose_dic forKey:@"item_0"];
  8374. // contact id
  8375. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8376. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8377. [contact_id_dic setValue:@"true" forKey:@"required"];
  8378. [dic setValue:contact_id_dic forKey:@"item_1"];
  8379. // company name
  8380. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8381. [dic setValue:company_name_dic forKey:@"item_2"];
  8382. // address
  8383. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8384. [dic setValue:ext_dic forKey:@"item_3"];
  8385. // contact
  8386. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8387. [dic setValue:contact_dic forKey:@"item_4"];
  8388. // phone
  8389. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8390. [dic setValue:phone_dic forKey:@"item_5"];
  8391. // fax
  8392. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8393. [dic setValue:fax_dic forKey:@"item_6"];
  8394. // email
  8395. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8396. [dic setValue:email_dic forKey:@"item_7"];
  8397. }];
  8398. return dic;
  8399. }
  8400. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8401. {
  8402. assert(params[@"user"]!=nil);
  8403. assert(params[@"contact_id"]!=nil);
  8404. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8405. orderCode = [self translateSingleQuote:orderCode];
  8406. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8407. __block double TotalCuft = 0;
  8408. __block double TotalWeight = 0;
  8409. __block int TotalCarton = 0;
  8410. __block double payments = 0;
  8411. // setting
  8412. NSDictionary *setting = params[@"setting"];
  8413. NSNumber *hide = setting[@"ModelInformationHide"];
  8414. [dic setValue:hide forKey:@"hide"];
  8415. 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];
  8416. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8417. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8418. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8419. // item id
  8420. int item_id = sqlite3_column_int(stmt, 0);
  8421. // count
  8422. int item_count = sqlite3_column_int(stmt, 1);
  8423. // stockUom
  8424. int stockUom = sqlite3_column_int(stmt, 2);
  8425. // unit price
  8426. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8427. NSString* Price=nil;
  8428. if([str_price isEqualToString:@""])
  8429. {
  8430. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8431. if(price==nil)
  8432. Price=@"No Price.";
  8433. else
  8434. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8435. }
  8436. else
  8437. {
  8438. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8439. }
  8440. // discount
  8441. double discount = sqlite3_column_double(stmt, 4);
  8442. // name
  8443. NSString *name = [self textAtColumn:5 statement:stmt];
  8444. // description
  8445. NSString *description = [self textAtColumn:6 statement:stmt];
  8446. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8447. // line note
  8448. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8449. int avaulability = sqlite3_column_int(stmt, 8);
  8450. // img
  8451. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8452. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8453. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8454. if(combine != nil)
  8455. {
  8456. // int citem=0;
  8457. int bcount=[[combine valueForKey:@"count"] intValue];
  8458. for(int bc=0;bc<bcount;bc++)
  8459. {
  8460. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8461. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8462. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8463. subTotal += uprice * modulus * item_count;
  8464. }
  8465. }
  8466. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8467. [model_dic setValue:@"model" forKey:@"control"];
  8468. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8469. [model_dic setValue:description forKey:@"description"];
  8470. [model_dic setValue:line_note forKey:@"note"];
  8471. [model_dic setValue:img forKey:@"img_url"];
  8472. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8473. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8474. [model_dic setValue:Price forKey:@"unit_price"];
  8475. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8476. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8477. if (combine) {
  8478. [model_dic setValue:combine forKey:@"combine"];
  8479. }
  8480. // well,what under the row is the info for total
  8481. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8482. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8483. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8484. int carton=[bsubtotaljson[@"carton"] intValue];
  8485. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8486. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8487. TotalCuft += cuft;
  8488. TotalWeight += weight;
  8489. TotalCarton += carton;
  8490. payments += subTotal;
  8491. //---------------------------
  8492. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8493. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8494. }];
  8495. [dic setValue:@"Model Information" forKey:@"title"];
  8496. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8497. return dic;
  8498. }
  8499. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8500. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8501. [dic setValue:@"Remarks Content" forKey:@"title"];
  8502. // setting
  8503. NSDictionary *setting = params[@"setting"];
  8504. NSNumber *hide = setting[@"RemarksContentHide"];
  8505. [dic setValue:hide forKey:@"hide"];
  8506. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8507. orderCode = [self translateSingleQuote:orderCode];
  8508. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8509. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8510. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8511. int mustCall = sqlite3_column_int(stmt, 1);
  8512. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8513. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8514. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8515. NSDictionary *po_dic = @{
  8516. @"aname" : @"PO#",
  8517. @"control" : @"edit",
  8518. @"keyboard" : @"text",
  8519. @"name" : @"poNumber",
  8520. @"value" : poNumber
  8521. };
  8522. #ifdef BUILD_CONTRAST
  8523. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8524. NSDictionary *schedule_dict = @{
  8525. @"aname" : @"Schdule Date",
  8526. @"control" : @"datepicker",
  8527. @"type": @"date",
  8528. @"required": @"true",
  8529. @"name" : @"schedule_date_str",
  8530. @"value" : nsdate
  8531. };
  8532. #endif
  8533. NSDictionary *must_call_dic = @{
  8534. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8535. @"control" : @"switch",
  8536. @"name" : @"must_call",
  8537. @"value" : mustCall ? @"true" : @"false"
  8538. };
  8539. NSDictionary *general_notes_dic = @{
  8540. @"aname" : @"General notes",
  8541. @"control" : @"text_view",
  8542. @"keyboard" : @"text",
  8543. @"name" : @"comments",
  8544. @"value" : generalNotes
  8545. };
  8546. // NSDictionary *internal_notes_dic = @{
  8547. // @"aname" : @"Internal notes",
  8548. // @"control" : @"text_view",
  8549. // @"keyboard" : @"text",
  8550. // @"name" : @"internal_notes",
  8551. // @"value" : internalNotes
  8552. // };
  8553. #ifdef BUILD_CONTRAST
  8554. [dic setValue:po_dic forKey:@"item_0"];
  8555. [dic setValue:schedule_dict forKey:@"item_1"];
  8556. [dic setValue:must_call_dic forKey:@"item_2"];
  8557. [dic setValue:general_notes_dic forKey:@"item_3"];
  8558. #else
  8559. [dic setValue:po_dic forKey:@"item_0"];
  8560. [dic setValue:must_call_dic forKey:@"item_1"];
  8561. [dic setValue:general_notes_dic forKey:@"item_2"];
  8562. #endif
  8563. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8564. }];
  8565. #ifdef BUILD_CONTRAST
  8566. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8567. #else
  8568. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8569. #endif
  8570. return dic;
  8571. }
  8572. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8573. // params
  8574. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8575. orderCode = [self translateSingleQuote:orderCode];
  8576. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8577. // setting
  8578. NSDictionary *setting = params[@"setting"];
  8579. NSNumber *hide = setting[@"OrderTotalHide"];
  8580. [dic setValue:hide forKey:@"hide"];
  8581. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8582. __block double lift_gate_value = 0;
  8583. __block double handling_fee = 0;
  8584. __block double shipping = 0;
  8585. __block double discount = 0;
  8586. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8587. int lift_gate = sqlite3_column_int(stmt, 0);
  8588. lift_gate_value = sqlite3_column_double(stmt, 1);
  8589. shipping = sqlite3_column_double(stmt, 2);
  8590. handling_fee = sqlite3_column_double(stmt, 3);
  8591. discount = sqlite3_column_double(stmt, 4);
  8592. if (!lift_gate) {
  8593. lift_gate_value = 0;
  8594. }
  8595. }];
  8596. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8597. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8598. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8599. double payments = [[total valueForKey:@"payments"] doubleValue];
  8600. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8601. double totalPrice = payments* (1-discount/100.0);
  8602. [dic setValue:@"Order Total" forKey:@"title"];
  8603. NSDictionary *payments_dic = @{
  8604. @"align" : @"right",
  8605. @"aname" : @"Payments/Credits",
  8606. @"control" : @"text",
  8607. @"name" : @"paymentsAndCredits",
  8608. @"type" : @"price",
  8609. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8610. };
  8611. [dic setValue:payments_dic forKey:@"item_0"];
  8612. // version 1.71 remove
  8613. // NSDictionary *handling_fee_dic = @{
  8614. // @"align" : @"right",
  8615. // @"aname" : @"Handling Fee",
  8616. // @"control" : @"text",
  8617. // @"name" : @"handling_fee_value",
  8618. // @"required" : @"true",
  8619. // @"type" : @"price",
  8620. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8621. // };
  8622. NSDictionary *shipping_dic = @{
  8623. @"align" : @"right",
  8624. @"aname" : @"Shipping*",
  8625. @"control" : @"text",
  8626. @"name" : @"shipping",
  8627. @"required" : @"true",
  8628. @"type" : @"price",
  8629. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8630. };
  8631. NSDictionary *lift_gate_dic = @{
  8632. @"align" : @"right",
  8633. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8634. @"control" : @"text",
  8635. @"name" : @"lift_gate_value",
  8636. @"required" : @"true",
  8637. @"type" : @"price",
  8638. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8639. };
  8640. int item_count = 1;
  8641. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8642. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8643. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8644. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8645. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8646. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8647. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8648. } else {
  8649. }
  8650. }
  8651. // version 1.71 remove
  8652. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8653. // NSString* strdiscount;
  8654. if(discount>0)
  8655. {
  8656. NSDictionary *discount_dict = @{
  8657. @"align" : @"right",
  8658. @"aname" : @"Order Discount",
  8659. @"control" : @"text",
  8660. @"name" : @"orderDiscount",
  8661. // @"type" : @"price",
  8662. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8663. };
  8664. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8665. }
  8666. NSDictionary *total_price_dic = @{
  8667. @"align" : @"right",
  8668. @"aname" : @"Total",
  8669. @"control" : @"text",
  8670. @"name" : @"totalPrice",
  8671. @"type" : @"price",
  8672. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8673. };
  8674. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8675. NSDictionary *total_cuft_dic = @{
  8676. @"align" : @"right",
  8677. @"aname" : @"Total Cuft",
  8678. @"control" : @"text",
  8679. @"name" : @"",
  8680. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8681. };
  8682. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8683. NSDictionary *total_weight_dic = @{
  8684. @"align" : @"right",
  8685. @"aname" : @"Total Weight",
  8686. @"control" : @"text",
  8687. @"name" : @"",
  8688. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8689. };
  8690. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8691. NSDictionary *total_carton_dic = @{
  8692. @"align" : @"right",
  8693. @"aname" : @"Total Carton",
  8694. @"control" : @"text",
  8695. @"name" : @"",
  8696. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8697. };
  8698. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8699. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8700. return dic;
  8701. }
  8702. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8703. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8704. orderCode = [self translateSingleQuote:orderCode];
  8705. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8706. // setting
  8707. NSDictionary *setting = params[@"setting"];
  8708. NSNumber *hide = setting[@"SignatureHide"];
  8709. [dic setValue:hide forKey:@"hide"];
  8710. [dic setValue:@"Signature" forKey:@"title"];
  8711. __block NSString *pic_path = @"";
  8712. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8713. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8714. pic_path = [self textAtColumn:0 statement:stmt];
  8715. }];
  8716. NSDictionary *pic_dic = @{
  8717. @"aname" : @"Signature",
  8718. @"avalue" :pic_path,
  8719. @"control" : @"signature",
  8720. @"name" : @"sign_picpath",
  8721. @"value" : pic_path
  8722. };
  8723. [dic setValue:pic_dic forKey:@"item_0"];
  8724. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8725. return dic;
  8726. }
  8727. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8729. // setting
  8730. NSDictionary *setting = params[@"setting"];
  8731. NSNumber *hide = setting[@"ShippingMethodHide"];
  8732. [dic setValue:hide forKey:@"hide"];
  8733. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8734. orderCode = [self translateSingleQuote:orderCode];
  8735. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8736. __block NSString *logist = @"";
  8737. __block NSString *carrier = @"";
  8738. __block NSString *lift_gate = @"";
  8739. __block int lift_gate_integer = 0;
  8740. __block NSString *logistic_note = @"";
  8741. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8742. logist = [self textAtColumn:0 statement:stmt];
  8743. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8744. logistic_note = [self textAtColumn:2 statement:stmt];
  8745. carrier = [self textAtColumn:3 statement:stmt];
  8746. }];
  8747. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8748. [dic setValue:@"Shipping Method" forKey:@"title"];
  8749. // val_0
  8750. int PERSONAL_PICK_UP_check = 0;
  8751. int USE_MY_CARRIER_check = 0;
  8752. NSString *logistic_note_text = @"";
  8753. int will_call_check = 0;
  8754. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8755. will_call_check = 1;
  8756. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8757. PERSONAL_PICK_UP_check = 1;
  8758. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8759. USE_MY_CARRIER_check = 1;
  8760. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8761. logistic_note = [logistic_note_array firstObject];
  8762. if (logistic_note_array.count > 1) {
  8763. logistic_note_text = [logistic_note_array lastObject];
  8764. }
  8765. }
  8766. }
  8767. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8768. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8769. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8770. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8771. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8772. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8773. NSDictionary *val0_subItem_item0 = @{
  8774. @"aname" : @"Option",
  8775. @"cadedate" : @{
  8776. @"count" : [NSNumber numberWithInteger:2],
  8777. @"val_0" : @{
  8778. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8779. @"refresh" : [NSNumber numberWithInteger:0],
  8780. @"value" : @"PERSONAL PICK UP",
  8781. @"value_id" : @"PERSONAL PICK UP"
  8782. },
  8783. @"val_1" : @{
  8784. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8785. @"refresh" : [NSNumber numberWithInteger:0],
  8786. @"sub_item" : @{
  8787. @"count" : [NSNumber numberWithInteger:1],
  8788. @"item_0" : @{
  8789. @"aname" : @"BOL",
  8790. @"control" : @"edit",
  8791. @"keyboard" : @"text",
  8792. @"name" : @"logist_note_text",
  8793. @"refresh" : [NSNumber numberWithInteger:0],
  8794. @"required" : @"false",
  8795. @"value" : logistic_note_text
  8796. }
  8797. },
  8798. @"value" : @"USE MY CARRIER",
  8799. @"value_id" : @"USE MY CARRIER"
  8800. }
  8801. },
  8802. @"control" : @"enum",
  8803. @"name" : @"logistic_note",
  8804. @"required" : @"true",
  8805. @"single_select" : @"true"
  8806. };
  8807. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8808. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8809. // val_1
  8810. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8811. if([lift_gate isEqualToString:@""]) {
  8812. if (lift_gate_integer == 1) {
  8813. lift_gate = @"true";
  8814. } else {
  8815. lift_gate = @"false";
  8816. }
  8817. }
  8818. // carrier = [self valueInParams:params key:@"carrier"];
  8819. // if([carrier isEqualToString:@""]) {
  8820. //
  8821. // if (lift_gate_integer == 1) {
  8822. // lift_gate = @"true";
  8823. // } else {
  8824. // lift_gate = @"false";
  8825. // }
  8826. //
  8827. // }
  8828. int common_carrier_check = 0;
  8829. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8830. common_carrier_check = 1;
  8831. [params setValue:lift_gate forKey:@"lift_gate"];
  8832. #ifdef BUILD_CONTRAST
  8833. [params setValue:carrier forKey:@"carrier"];
  8834. #endif
  8835. }
  8836. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8837. // NSString * code = [iSalesDB jk_queryText:Sql];
  8838. // [self offline_getCarrierCode:offline_getCarrierCode];
  8839. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8840. NSDictionary *val_1 = @{
  8841. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8842. @"sub_item" : @{
  8843. @"item_0" : @{
  8844. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8845. @"control" : @"switch",
  8846. @"name" : @"lift_gate",
  8847. @"refresh" : [NSNumber numberWithInteger:1],
  8848. @"required" : @"true",
  8849. @"value" : lift_gate
  8850. },
  8851. #ifdef BUILD_CONTRAST
  8852. @"count" : [NSNumber numberWithInteger:2],
  8853. @"item_1" : @{
  8854. @"aname" : @"Carrier",
  8855. @"cadedate" :carrierDic,
  8856. @"control" : @"enum",
  8857. @"name" : @"carrier",
  8858. @"required" : @"false",
  8859. @"value" : @"",
  8860. @"single_select" : @"true"
  8861. }
  8862. #else
  8863. @"count" : [NSNumber numberWithInteger:1]
  8864. #endif
  8865. },
  8866. @"value" : @"COMMON CARRIER",
  8867. @"value_id" : @"COMMON CARRIER"
  8868. };
  8869. // cadedate
  8870. NSDictionary *cadedate = @{
  8871. @"count" : [NSNumber numberWithInteger:2],
  8872. @"val_0" : val_0,
  8873. @"val_1" : val_1
  8874. };
  8875. // item_0
  8876. NSMutableDictionary *item_0 = @{
  8877. @"aname" : @"Shipping",
  8878. @"cadedate" : cadedate,
  8879. @"control" : @"enum",
  8880. @"name" : @"logist",
  8881. @"refresh" : [NSNumber numberWithInteger:1],
  8882. @"single_select" : @"true",
  8883. }.mutableCopy;
  8884. NSNumber *required = setting[@"ShippingMethodRequire"];
  8885. if ([required integerValue]) {
  8886. [item_0 setValue:@"true" forKey:@"required"];
  8887. }
  8888. [dic setValue:item_0 forKey:@"item_0"];
  8889. if ([logist isEqualToString:@"WILL CALL"]) {
  8890. [dic removeObjectForKey:@"lift_gate"];
  8891. }
  8892. return dic;
  8893. }
  8894. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8895. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8896. orderCode = [self translateSingleQuote:orderCode];
  8897. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8898. __block int submit_as_integer = 0;
  8899. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8900. submit_as_integer = sqlite3_column_int(stmt, 0);
  8901. }];
  8902. int check11 = 0;
  8903. int check10 = 0;
  8904. if (submit_as_integer == 11) {
  8905. check11 = 1;
  8906. }
  8907. if (submit_as_integer == 10) {
  8908. check10 = 1;
  8909. }
  8910. // section_0
  8911. NSMutableDictionary *dic = @{
  8912. @"count" : @(1),
  8913. @"item_0" : @{
  8914. @"aname" : @"Submit Order As",
  8915. @"cadedate" : @{
  8916. @"count" : @(2),
  8917. @"val_0" : @{
  8918. @"check" : [NSNumber numberWithInteger:check11],
  8919. @"value" : @"Sales Order",
  8920. @"value_id" : @(11)
  8921. },
  8922. @"val_1" : @{
  8923. @"check" : [NSNumber numberWithInteger:check10],
  8924. @"value" : @"Quote",
  8925. @"value_id" : @(10)
  8926. }
  8927. },
  8928. @"control" : @"enum",
  8929. @"name" : @"erpOrderStatus",
  8930. @"required" : @"true",
  8931. @"single_select" : @"true"
  8932. },
  8933. @"title" : @"Order Type"
  8934. }.mutableCopy;
  8935. // setting
  8936. NSDictionary *setting = params[@"setting"];
  8937. NSNumber *hide = setting[@"OrderTypeHide"];
  8938. [dic setValue:hide forKey:@"hide"];
  8939. return dic;
  8940. }
  8941. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8942. countryCode = [self translateSingleQuote:countryCode];
  8943. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8944. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8945. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8946. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8947. if (name == NULL) {
  8948. name = "";
  8949. }
  8950. if (code == NULL) {
  8951. code = "";
  8952. }
  8953. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8954. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8955. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8956. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8957. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8958. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8959. }
  8960. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8961. [container setValue:stateDic forKey:key];
  8962. }] mutableCopy];
  8963. [ret removeObjectForKey:@"result"];
  8964. // failure 可以不用了,一样的
  8965. if (ret.allKeys.count == 0) {
  8966. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8967. [stateDic setValue:@"Other" forKey:@"value"];
  8968. [stateDic setValue:@"" forKey:@"value_id"];
  8969. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8970. if (state_code && [@"" isEqualToString:state_code]) {
  8971. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8972. }
  8973. NSString *key = [NSString stringWithFormat:@"val_0"];
  8974. [ret setValue:stateDic forKey:key];
  8975. }
  8976. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8977. return ret;
  8978. }
  8979. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8980. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8981. orderCode = [self translateSingleQuote:orderCode];
  8982. 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];
  8983. __block NSString *payType = @"";
  8984. __block NSString *firstName = @"";
  8985. __block NSString *lastName = @"";
  8986. __block NSString *addr1 = @"";
  8987. __block NSString *addr2 = @"";
  8988. __block NSString *zipcode = @"";
  8989. __block NSString *cardType = @"";
  8990. __block NSString *cardNumber = @"";
  8991. __block NSString *securityCode = @"";
  8992. __block NSString *month = @"";
  8993. __block NSString *year = @"";
  8994. __block NSString *city = @"";
  8995. __block NSString *state = @"";
  8996. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8997. payType = [self textAtColumn:0 statement:stmt];
  8998. firstName = [self textAtColumn:1 statement:stmt];
  8999. lastName = [self textAtColumn:2 statement:stmt];
  9000. addr1 = [self textAtColumn:3 statement:stmt];
  9001. addr2 = [self textAtColumn:4 statement:stmt];
  9002. zipcode = [self textAtColumn:5 statement:stmt];
  9003. cardType = [self textAtColumn:6 statement:stmt];
  9004. cardNumber = [self textAtColumn:7 statement:stmt];
  9005. securityCode = [self textAtColumn:8 statement:stmt];
  9006. month = [self textAtColumn:9 statement:stmt];
  9007. year = [self textAtColumn:10 statement:stmt];
  9008. city = [self textAtColumn:11 statement:stmt];
  9009. state = [self textAtColumn:12 statement:stmt];
  9010. }];
  9011. NSString *required = @"true";
  9012. // setting
  9013. NSDictionary *setting = params[@"setting"];
  9014. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9015. if ([requiredNumber integerValue]) {
  9016. required = @"true";
  9017. } else {
  9018. required = @"false";
  9019. }
  9020. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9021. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9022. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9023. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9024. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9025. NSString *type = [self textAtColumn:1 statement:stmt];
  9026. NSMutableDictionary *val = @{
  9027. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9028. @"value" : type,
  9029. @"value_id" : type_id
  9030. }.mutableCopy;
  9031. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9032. NSDictionary *sub_item =nil;
  9033. #ifdef BUILD_CONTRAST
  9034. sub_item = @{
  9035. @"count" : @(3),
  9036. @"item_0" : @{
  9037. @"aname" : @"Number",
  9038. @"control" : @"edit",
  9039. @"keyboard" : @"int",
  9040. @"length" : @"16",
  9041. @"name" : @"credit_card_number",
  9042. @"required" : @"false",
  9043. @"value" : cardNumber
  9044. },
  9045. @"item_1" : @{
  9046. @"aname" : @"Expiration Date",
  9047. @"control" : @"monthpicker",
  9048. @"name" : @"credit_card_expiration",
  9049. @"required" : @"false",
  9050. @"type" : @"date",
  9051. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9052. },
  9053. @"item_2" : @{
  9054. @"aname" : @"Security Code",
  9055. @"control" : @"edit",
  9056. @"keyboard" : @"int",
  9057. @"length" : @"4",
  9058. @"name" : @"credit_card_security_code",
  9059. @"required" : @"false",
  9060. @"value" : securityCode
  9061. },
  9062. };
  9063. #else
  9064. sub_item= @{
  9065. @"count" : @(3),
  9066. @"item_0" : @{
  9067. @"aname" : @"choose",
  9068. @"control" : @"multi_action",
  9069. @"count" : @(1),
  9070. @"item_0" : @{
  9071. @"aname" : @"Same as customer",
  9072. @"key_map" : @{
  9073. @"credit_card_address1" : @"customer_address1",
  9074. @"credit_card_address2" : @"customer_address2",
  9075. @"credit_card_city" : @"customer_city",
  9076. @"credit_card_first_name" : @"customer_first_name",
  9077. @"credit_card_last_name" : @"customer_last_name",
  9078. @"credit_card_state" : @"customer_state",
  9079. @"credit_card_zipcode" : @"customer_zipcode"
  9080. },
  9081. @"type" : @"pull"
  9082. }
  9083. },
  9084. @"item_1" : @{
  9085. @"aname" : @"",
  9086. @"color" : @"red",
  9087. @"control" : @"text",
  9088. @"name" : @"",
  9089. @"value" : @"USA Credit cards only"
  9090. },
  9091. @"item_2" : @{
  9092. @"aname" : @"Fill",
  9093. @"cadedate" : @{
  9094. @"count" : @(2),
  9095. @"val_0" : @{
  9096. @"check" : @(1),
  9097. @"sub_item" : @{
  9098. @"count" : @(11),
  9099. @"item_0" : @{
  9100. @"aname" : @"Type",
  9101. @"cadedate" : @{
  9102. @"count" : @(2),
  9103. @"val_0" : @{
  9104. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9105. @"value" : @"VISA",
  9106. @"value_id" : @"VISA"/*@(0)*/
  9107. },
  9108. @"val_1" : @{
  9109. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9110. @"value" : @"MASTER CARD",
  9111. @"value_id" : @"MASTER CARD"/*@(1)*/
  9112. }
  9113. },
  9114. @"control" : @"enum",
  9115. @"name" : @"credit_card_type",
  9116. @"required" : @"true",
  9117. @"single_select" : @"true"
  9118. },
  9119. @"item_1" : @{
  9120. @"aname" : @"Number",
  9121. @"control" : @"edit",
  9122. @"keyboard" : @"int",
  9123. @"length" : @"16",
  9124. @"name" : @"credit_card_number",
  9125. @"required" : @"true",
  9126. @"value" : cardNumber
  9127. },
  9128. @"item_10" : @{
  9129. @"aname" : @"State",
  9130. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9131. @"control" : @"enum",
  9132. @"enum" : @"true",
  9133. @"name" : @"credit_card_state",
  9134. @"required" : @"true",
  9135. @"single_select" : @"true"
  9136. },
  9137. @"item_2" : @{
  9138. @"aname" : @"Expiration Date",
  9139. @"control" : @"monthpicker",
  9140. @"name" : @"credit_card_expiration",
  9141. @"required" : @"true",
  9142. @"type" : @"date",
  9143. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9144. },
  9145. @"item_3" : @{
  9146. @"aname" : @"Security Code",
  9147. @"control" : @"edit",
  9148. @"keyboard" : @"int",
  9149. @"length" : @"3",
  9150. @"name" : @"credit_card_security_code",
  9151. @"required" : @"true",
  9152. @"value" : securityCode
  9153. },
  9154. @"item_4" : @{
  9155. @"aname" : @"First Name",
  9156. @"control" : @"edit",
  9157. @"keyboard" : @"text",
  9158. @"name" : @"credit_card_first_name",
  9159. @"required" : @"true",
  9160. @"value" : firstName
  9161. },
  9162. @"item_5" : @{
  9163. @"aname" : @"Last Name",
  9164. @"control" : @"edit",
  9165. @"keyboard" : @"text",
  9166. @"name" : @"credit_card_last_name",
  9167. @"required" : @"true",
  9168. @"value" : lastName
  9169. },
  9170. @"item_6" : @{
  9171. @"aname" : @"Address 1",
  9172. @"control" : @"edit",
  9173. @"keyboard" : @"text",
  9174. @"name" : @"credit_card_address1",
  9175. @"required" : @"true",
  9176. @"value" : addr1
  9177. },
  9178. @"item_7" : @{
  9179. @"aname" : @"Address 2",
  9180. @"control" : @"edit",
  9181. @"keyboard" : @"text",
  9182. @"name" : @"credit_card_address2",
  9183. @"value" : addr2
  9184. },
  9185. @"item_8" : @{
  9186. @"aname" : @"zip code",
  9187. @"control" : @"edit",
  9188. @"keyboard" : @"text",
  9189. @"name" : @"credit_card_zipcode",
  9190. @"required" : @"true",
  9191. @"value" : zipcode
  9192. },
  9193. @"item_9" : @{
  9194. @"aname" : @"City",
  9195. @"control" : @"edit",
  9196. @"keyboard" : @"text",
  9197. @"name" : @"credit_card_city",
  9198. @"required" : @"true",
  9199. @"value" : city
  9200. }
  9201. },
  9202. @"value" : @"Fill Now",
  9203. @"value_id" : @""
  9204. },
  9205. @"val_1" : @{
  9206. @"check" : @(0),
  9207. @"value" : @"Fill Later",
  9208. @"value_id" : @""
  9209. }
  9210. },
  9211. @"control" : @"enum",
  9212. @"name" : @"",
  9213. @"single_select" : @"true"
  9214. }
  9215. };
  9216. #endif
  9217. [val setObject:sub_item forKey:@"sub_item"];
  9218. }
  9219. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9220. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9221. }];
  9222. // "section_2"
  9223. NSMutableDictionary *dic = @{
  9224. @"count" : @(1),
  9225. @"item_0" : @{
  9226. @"aname" : @"Payment",
  9227. @"required" : required,
  9228. @"cadedate" : cadedate,
  9229. // @{
  9230. // @"count" : @(6),
  9231. // @"val_3" : @{
  9232. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9233. // @"value" : @"Check",
  9234. // @"value_id" : @"Check"
  9235. // },
  9236. // @"val_2" : @{
  9237. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9238. // @"value" : @"Cash",
  9239. // @"value_id" : @"Cash"
  9240. // },
  9241. // @"val_1" : @{
  9242. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9243. // @"value" : @"NET 60",
  9244. // @"value_id" : @"NET 30"
  9245. // },
  9246. // @"val_4" : @{
  9247. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9248. // @"value" : @"Wire Transfer",
  9249. // @"value_id" : @"Wire Transfer"
  9250. // },
  9251. // @"val_0" : @{
  9252. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9253. // @"sub_item" : @{
  9254. // @"count" : @(3),
  9255. // @"item_0" : @{
  9256. // @"aname" : @"choose",
  9257. // @"control" : @"multi_action",
  9258. // @"count" : @(1),
  9259. // @"item_0" : @{
  9260. // @"aname" : @"Same as customer",
  9261. // @"key_map" : @{
  9262. // @"credit_card_address1" : @"customer_address1",
  9263. // @"credit_card_address2" : @"customer_address2",
  9264. // @"credit_card_city" : @"customer_city",
  9265. // @"credit_card_first_name" : @"customer_first_name",
  9266. // @"credit_card_last_name" : @"customer_last_name",
  9267. // @"credit_card_state" : @"customer_state",
  9268. // @"credit_card_zipcode" : @"customer_zipcode"
  9269. // },
  9270. // @"type" : @"pull"
  9271. // }
  9272. // },
  9273. // @"item_1" : @{
  9274. // @"aname" : @"",
  9275. // @"color" : @"red",
  9276. // @"control" : @"text",
  9277. // @"name" : @"",
  9278. // @"value" : @"USA Credit cards only"
  9279. // },
  9280. // @"item_2" : @{
  9281. // @"aname" : @"Fill",
  9282. // @"cadedate" : @{
  9283. // @"count" : @(2),
  9284. // @"val_0" : @{
  9285. // @"check" : @(1),
  9286. // @"sub_item" : @{
  9287. // @"count" : @(11),
  9288. // @"item_0" : @{
  9289. // @"aname" : @"Type",
  9290. // @"cadedate" : @{
  9291. // @"count" : @(2),
  9292. // @"val_0" : @{
  9293. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9294. // @"value" : @"VISA",
  9295. // @"value_id" : @(0)
  9296. // },
  9297. // @"val_1" : @{
  9298. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9299. // @"value" : @"MASTER CARD",
  9300. // @"value_id" : @(1)
  9301. // }
  9302. // },
  9303. // @"control" : @"enum",
  9304. // @"name" : @"credit_card_type",
  9305. // @"required" : @"true",
  9306. // @"single_select" : @"true"
  9307. // },
  9308. // @"item_1" : @{
  9309. // @"aname" : @"Number",
  9310. // @"control" : @"edit",
  9311. // @"keyboard" : @"int",
  9312. // @"length" : @"16",
  9313. // @"name" : @"credit_card_number",
  9314. // @"required" : @"true",
  9315. // @"value" : cardNumber
  9316. // },
  9317. // @"item_10" : @{
  9318. // @"aname" : @"State",
  9319. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9320. // @"control" : @"enum",
  9321. // @"enum" : @"true",
  9322. // @"name" : @"credit_card_state",
  9323. // @"required" : @"true",
  9324. // @"single_select" : @"true"
  9325. // },
  9326. // @"item_2" : @{
  9327. // @"aname" : @"Expiration Date",
  9328. // @"control" : @"monthpicker",
  9329. // @"name" : @"credit_card_expiration",
  9330. // @"required" : @"true",
  9331. // @"type" : @"date",
  9332. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9333. // },
  9334. // @"item_3" : @{
  9335. // @"aname" : @"Security Code",
  9336. // @"control" : @"edit",
  9337. // @"keyboard" : @"int",
  9338. // @"length" : @"3",
  9339. // @"name" : @"credit_card_security_code",
  9340. // @"required" : @"true",
  9341. // @"value" : securityCode
  9342. // },
  9343. // @"item_4" : @{
  9344. // @"aname" : @"First Name",
  9345. // @"control" : @"edit",
  9346. // @"keyboard" : @"text",
  9347. // @"name" : @"credit_card_first_name",
  9348. // @"required" : @"true",
  9349. // @"value" : firstName
  9350. // },
  9351. // @"item_5" : @{
  9352. // @"aname" : @"Last Name",
  9353. // @"control" : @"edit",
  9354. // @"keyboard" : @"text",
  9355. // @"name" : @"credit_card_last_name",
  9356. // @"required" : @"true",
  9357. // @"value" : lastName
  9358. // },
  9359. // @"item_6" : @{
  9360. // @"aname" : @"Address 1",
  9361. // @"control" : @"edit",
  9362. // @"keyboard" : @"text",
  9363. // @"name" : @"credit_card_address1",
  9364. // @"required" : @"true",
  9365. // @"value" : addr1
  9366. // },
  9367. // @"item_7" : @{
  9368. // @"aname" : @"Address 2",
  9369. // @"control" : @"edit",
  9370. // @"keyboard" : @"text",
  9371. // @"name" : @"credit_card_address2",
  9372. // @"value" : addr2
  9373. // },
  9374. // @"item_8" : @{
  9375. // @"aname" : @"zip code",
  9376. // @"control" : @"edit",
  9377. // @"keyboard" : @"text",
  9378. // @"name" : @"credit_card_zipcode",
  9379. // @"required" : @"true",
  9380. // @"value" : zipcode
  9381. // },
  9382. // @"item_9" : @{
  9383. // @"aname" : @"City",
  9384. // @"control" : @"edit",
  9385. // @"keyboard" : @"text",
  9386. // @"name" : @"credit_card_city",
  9387. // @"required" : @"true",
  9388. // @"value" : city
  9389. // }
  9390. // },
  9391. // @"value" : @"Fill Now",
  9392. // @"value_id" : @""
  9393. // },
  9394. // @"val_1" : @{
  9395. // @"check" : @(0),
  9396. // @"value" : @"Fill Later",
  9397. // @"value_id" : @""
  9398. // }
  9399. // },
  9400. // @"control" : @"enum",
  9401. // @"name" : @"",
  9402. // @"single_select" : @"true"
  9403. // }
  9404. // },
  9405. // @"value" : @"Visa/Master",
  9406. // @"value_id" : @"Credit Card"
  9407. // },
  9408. // @"val_5" : @{
  9409. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9410. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9411. // @"value_id" : @"FOLLOW EXISTING"
  9412. // }
  9413. // },
  9414. @"control" : @"enum",
  9415. @"name" : @"paymentType",
  9416. @"single_select" : @"true"
  9417. },
  9418. @"title" : @"Payment Information"
  9419. }.mutableCopy;
  9420. NSNumber *hide = setting[@"PaymentInformationHide"];
  9421. [dic setValue:hide forKey:@"hide"];
  9422. return dic;
  9423. }
  9424. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9425. // params
  9426. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9427. orderCode = [self translateSingleQuote:orderCode];
  9428. // 缺货检查
  9429. 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];
  9430. __block BOOL outOfStock = NO;
  9431. sqlite3 *database = db;
  9432. if (!db) {
  9433. database = [iSalesDB get_db];
  9434. }
  9435. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9436. int availability = sqlite3_column_int(stmt, 0);
  9437. int item_qty = sqlite3_column_int(stmt, 1);
  9438. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9439. }];
  9440. if (!db) {
  9441. [iSalesDB close_db:database];
  9442. }
  9443. return outOfStock;
  9444. }
  9445. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9446. {
  9447. assert(params[@"user"]!=nil);
  9448. assert(params[@"contact_id"]!=nil);
  9449. assert(params[@"can_create_backorder"]!=nil);
  9450. sqlite3 *db = [iSalesDB get_db];
  9451. // params
  9452. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9453. orderCode = [self translateSingleQuote:orderCode];
  9454. // 缺货检查
  9455. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9456. if (![params[@"can_create_backorder"] boolValue]) {
  9457. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9458. if (out_of_stock) {
  9459. [iSalesDB close_db:db];
  9460. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9461. [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"];
  9462. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9463. return [RAConvertor dict2data:resultDic];
  9464. }
  9465. }
  9466. // UISetting
  9467. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9468. NSString *cachefolder = [paths objectAtIndex:0];
  9469. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9470. NSData* json =nil;
  9471. json=[NSData dataWithContentsOfFile:img_cache];
  9472. NSError *error=nil;
  9473. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9474. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9475. [params setObject:setting forKey:@"setting"];
  9476. int section_count = 0;
  9477. // 0 Order Type 1 Shipping Method 2 Payment Information
  9478. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9479. // 0 Order Type
  9480. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9481. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9482. [ret setValue:order_type_dic forKey:key0];
  9483. // 1 Shipping Method
  9484. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9485. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9486. [ret setValue:shipping_method_dic forKey:key1];
  9487. // 2 Payment Information
  9488. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9489. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9490. [ret setValue:payment_info_dic forKey:key2];
  9491. // 3 Customer
  9492. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9493. NSDictionary *customer_dic = [self customerDic:params db:db];
  9494. [ret setValue:customer_dic forKey:key3];
  9495. // 4 Ship To
  9496. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9497. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9498. [ret setValue:ship_to_dic forKey:key4];
  9499. // 5 Ship From
  9500. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9501. if (![shipFromDisable integerValue]) {
  9502. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9503. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9504. [ret setValue:ship_from_dic forKey:key5];
  9505. }
  9506. // 6 Freight Bill To
  9507. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9508. if (![freightBillToDisable integerValue]) {
  9509. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9510. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9511. [ret setValue:freight_bill_to forKey:key6];
  9512. }
  9513. // 7 Merchandise Bill To
  9514. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9515. if (![merchandiseBillToDisable integerValue]) {
  9516. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9517. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9518. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9519. }
  9520. // 8 Return To
  9521. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9522. if (![returnToDisable integerValue]) {
  9523. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9524. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9525. [ret setValue:return_to_dic forKey:key8];
  9526. }
  9527. // 9 Model Information
  9528. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9529. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9530. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9531. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9532. [ret setValue:model_info_dic forKey:key9];
  9533. // 10 Remarks Content
  9534. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9535. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9536. [ret setValue:remarks_content_dic forKey:key10];
  9537. // 11 Order Total
  9538. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9539. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9540. [ret setValue:order_total_dic forKey:key11];
  9541. // 12 Signature
  9542. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9543. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9544. [ret setValue:sign_dic forKey:key12];
  9545. [ret setValue:@(section_count) forKey:@"section_count"];
  9546. [iSalesDB close_db:db];
  9547. return [RAConvertor dict2data:ret];
  9548. // return nil;
  9549. }
  9550. #pragma mark addr editor
  9551. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9552. NSMutableDictionary *new_item = [item mutableCopy];
  9553. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9554. return new_item;
  9555. }
  9556. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9557. {
  9558. // "is_subaction" = true;
  9559. // orderCode = MOB1608240002;
  9560. // password = 123456;
  9561. // "subaction_tag" = 1;
  9562. // user = EvanK;
  9563. // {
  9564. // "is_subaction" = true;
  9565. // orderCode = MOB1608240002;
  9566. // password = 123456;
  9567. // "refresh_trigger" = zipcode;
  9568. // "subaction_tag" = 1;
  9569. // user = EvanK;
  9570. // }
  9571. NSString *country_code = nil;
  9572. NSString *zipCode = nil;
  9573. NSString *stateCode = nil;
  9574. NSString *city = nil;
  9575. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9576. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9577. NSString *code_id = params[@"country"];
  9578. country_code = [self countryCodeByid:code_id];
  9579. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9580. NSString *zip_code = params[@"zipcode"];
  9581. // 剔除全部为空格
  9582. int spaceCount = 0;
  9583. for (int i = 0; i < zip_code.length; i++) {
  9584. if ([zip_code characterAtIndex:i] == ' ') {
  9585. spaceCount++;
  9586. }
  9587. }
  9588. if (spaceCount == zip_code.length) {
  9589. zip_code = @"";
  9590. }
  9591. zipCode = zip_code;
  9592. if (zipCode.length > 0) {
  9593. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9594. country_code = [dic valueForKey:@"country_code"];
  9595. if (!country_code) {
  9596. // country_code = @"US";
  9597. NSString *code_id = params[@"country"];
  9598. country_code = [self countryCodeByid:code_id];
  9599. }
  9600. stateCode = [dic valueForKey:@"state_code"];
  9601. if(!stateCode.length) {
  9602. stateCode = params[@"state"];
  9603. }
  9604. city = [dic valueForKey:@"city"];
  9605. if (!city.length) {
  9606. city = params[@"city"];
  9607. }
  9608. // zip code
  9609. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9610. // [zipDic setValue:zipCode forKey:@"value"];
  9611. // [section_0 setValue:zipDic forKey:@"item_11"];
  9612. } else {
  9613. NSString *code_id = params[@"country"];
  9614. country_code = [self countryCodeByid:code_id];
  9615. stateCode = params[@"state"];
  9616. city = params[@"city"];
  9617. }
  9618. }
  9619. }
  9620. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9621. // [ret removeObjectForKey:@"up_params"];
  9622. [ret setObject:@"New Address" forKey:@"title"];
  9623. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9624. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9625. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9626. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9627. // [country_dic removeObjectForKey:@"refresh"];
  9628. // [country_dic removeObjectForKey:@"restore"];
  9629. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9630. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9631. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9632. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9633. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9634. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9635. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9636. // [zip_code_dic removeObjectForKey:@"refresh"];
  9637. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9638. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9639. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9640. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9641. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9642. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9643. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9644. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9645. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9646. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9647. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9648. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9649. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9650. // country
  9651. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9652. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9653. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9654. // state
  9655. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9656. // NSDictionary *tmpDic = @{
  9657. // @"value" : @"Other",
  9658. // @"value_id" : @"",
  9659. // @"check" : [NSNumber numberWithInteger:0]
  9660. // };
  9661. //
  9662. // for (int i = 0; i < allState.allKeys.count; i++) {
  9663. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9664. //
  9665. // NSDictionary *tmp = allState[key];
  9666. // [allState setValue:tmpDic forKey:key];
  9667. // tmpDic = tmp;
  9668. // }
  9669. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9670. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9671. [ret setValue:new_section_0 forKey:@"section_0"];
  9672. return [RAConvertor dict2data:ret];
  9673. }
  9674. #pragma mark save addr
  9675. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9676. {
  9677. // NSString *companyName = [self valueInParams:params key:@"company"];
  9678. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9679. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9680. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9681. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9682. // NSString *countryId = [self valueInParams:params key:@"country"];
  9683. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9684. // NSString *city = [self valueInParams:params key:@"city"];
  9685. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9686. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9687. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9688. // NSString *phone = [self valueInParams:params key:@"phone"];
  9689. // NSString *fax = [self valueInParams:params key:@"fax"];
  9690. // NSString *email = [self valueInParams:params key:@"email"];
  9691. return [self offline_saveContact:params update:NO isCustomer:NO];
  9692. }
  9693. #pragma mark cancel order
  9694. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9695. {
  9696. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9697. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9698. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9699. if (order_id.length) {
  9700. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9701. }
  9702. int ret = [iSalesDB execSql:sql];
  9703. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9704. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9705. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9706. // [dic setValue:@"160409" forKey:@"min_ver"];
  9707. return [RAConvertor dict2data:dic];
  9708. }
  9709. #pragma mark sign order
  9710. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9711. {
  9712. //参考 offline_saveBusinesscard
  9713. DebugLog(@"sign order params: %@",params);
  9714. // orderCode = MOB1608240002;
  9715. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9716. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9717. orderCode = [self translateSingleQuote:orderCode];
  9718. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9719. picPath = [self translateSingleQuote:picPath];
  9720. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9721. int ret = [iSalesDB execSql:sql];
  9722. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9723. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9724. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9725. // [dic setValue:@"160409" forKey:@"min_ver"];
  9726. return [RAConvertor dict2data:dic];
  9727. }
  9728. #pragma mark save order
  9729. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9730. NSString *ret = [self valueInParams:params key:key];
  9731. if (translate) {
  9732. ret = [self translateSingleQuote:ret];
  9733. }
  9734. return ret;
  9735. }
  9736. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9737. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9738. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9739. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9740. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9741. if (schedule_date.length==0 ) {
  9742. schedule_date = @"";
  9743. } else {
  9744. schedule_date = [self rchangeDateFormate:schedule_date];
  9745. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9746. }
  9747. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9748. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9749. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9750. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9751. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9752. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9753. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9754. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9755. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9756. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9757. if (![total_price isEqualToString:@""]) {
  9758. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9759. } else {
  9760. total_price = @"";
  9761. }
  9762. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9763. if ([paymentsAndCredits isEqualToString:@""]) {
  9764. paymentsAndCredits = @"";
  9765. } else {
  9766. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9767. }
  9768. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9769. if ([handling_fee_value isEqualToString:@""]) {
  9770. handling_fee_value = @"";
  9771. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9772. handling_fee_value = @"";
  9773. } else {
  9774. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9775. }
  9776. NSString *handling_fee_placeholder = handling_fee_value;
  9777. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9778. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9779. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9780. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9781. NSString *lift_gate_placeholder = @"";
  9782. if ([lift_gate_value isEqualToString:@""]) {
  9783. lift_gate_placeholder = @"";
  9784. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9785. lift_gate_placeholder = @"";
  9786. } else {
  9787. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9788. }
  9789. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9790. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9791. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9792. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9793. 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];
  9794. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9795. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9796. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9797. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9798. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9799. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9800. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9801. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9802. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9803. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9804. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9805. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9806. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9807. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9808. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9809. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9810. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9811. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9812. if (!number_nil) {
  9813. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9814. }
  9815. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9816. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9817. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9818. if (!security_code_nil) {
  9819. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9820. }
  9821. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9822. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9823. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9824. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9825. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9826. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9827. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9828. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9829. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9830. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9831. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9832. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9833. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9834. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9835. NSString *contact_id = customer_cid;
  9836. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9837. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9838. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9839. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9840. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9841. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9842. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9843. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9844. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9845. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9846. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9847. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9848. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9849. //
  9850. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9851. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9852. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9853. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9854. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9855. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9856. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9857. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9858. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9859. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9860. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9861. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9862. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9863. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9864. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9865. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9866. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9867. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9868. 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];
  9869. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9870. if (receive_cid.length) {
  9871. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9872. }
  9873. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9874. if (receive_name.length) {
  9875. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9876. }
  9877. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9878. if (receive_ext.length) {
  9879. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9880. }
  9881. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9882. if (receive_contact.length) {
  9883. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9884. }
  9885. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9886. if (receive_phone.length) {
  9887. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9888. }
  9889. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9890. if (receive_email.length) {
  9891. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9892. }
  9893. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9894. if (receive_fax.length) {
  9895. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9896. }
  9897. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9898. if (sender_cid.length) {
  9899. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9900. }
  9901. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9902. if (sender_name.length) {
  9903. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9904. }
  9905. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9906. if (sender_ext.length) {
  9907. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9908. }
  9909. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9910. if(sender_contact.length) {
  9911. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9912. }
  9913. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9914. if (sender_phone.length) {
  9915. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9916. }
  9917. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9918. if (sender_fax.length) {
  9919. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9920. }
  9921. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9922. if (sender_email.length) {
  9923. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9924. }
  9925. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9926. if (shipping_billto_cid.length) {
  9927. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9928. }
  9929. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9930. if (shipping_billto_name.length) {
  9931. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9932. }
  9933. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9934. if (shipping_billto_ext.length) {
  9935. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9936. }
  9937. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9938. if (shipping_billto_contact.length) {
  9939. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9940. }
  9941. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9942. if (shipping_billto_phone.length) {
  9943. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9944. }
  9945. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9946. if (shipping_billto_fax.length) {
  9947. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9948. }
  9949. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9950. if (shipping_billto_email.length) {
  9951. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9952. }
  9953. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9954. if (billing_cid.length) {
  9955. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9956. }
  9957. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9958. if (billing_name.length) {
  9959. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9960. }
  9961. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9962. if (billing_ext.length) {
  9963. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9964. }
  9965. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9966. if (billing_contact.length) {
  9967. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9968. }
  9969. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9970. if (billing_phone.length) {
  9971. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9972. }
  9973. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9974. if (billing_fax.length) {
  9975. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9976. }
  9977. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9978. if (billing_email.length) {
  9979. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9980. }
  9981. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9982. if (returnto_cid.length) {
  9983. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9984. }
  9985. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9986. if (returnto_name.length) {
  9987. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9988. }
  9989. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9990. if (returnto_ext.length) {
  9991. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9992. }
  9993. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9994. if (returnto_contact.length) {
  9995. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9996. }
  9997. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9998. if (returnto_phone.length) {
  9999. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10000. }
  10001. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10002. if (returnto_fax.length) {
  10003. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10004. }
  10005. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10006. if (returnto_email.length) {
  10007. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10008. }
  10009. NSString *order_status = @"status = 1,";
  10010. if (submit) {
  10011. order_status = @"status = -11,";
  10012. }
  10013. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10014. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10015. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10016. NSString *sync_sql = @"";
  10017. if (submit) {
  10018. param[@"truck_carrier"]=carrier;
  10019. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10020. [param setValue:sales_rep forKey:@"sales_rep"];
  10021. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10022. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10023. }
  10024. 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];
  10025. DebugLog(@"save order contactSql: %@",contactSql);
  10026. DebugLog(@"save order orderSql: %@",orderSql);
  10027. // int contact_ret = [iSalesDB execSql:contactSql];
  10028. int order_ret = [iSalesDB execSql:orderSql];
  10029. int ret = order_ret;
  10030. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10031. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10032. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10033. // [dic setValue:@"160409" forKey:@"min_ver"];
  10034. [dic setObject:so_id forKey:@"so#"];
  10035. return [RAConvertor dict2data:dic];
  10036. }
  10037. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10038. {
  10039. // id foo = nil;
  10040. // NSMutableArray *a = @[].mutableCopy;
  10041. // [a addObject:foo];
  10042. return [self saveorder:param submit:NO];
  10043. }
  10044. #pragma mark add to cart by name
  10045. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10046. {
  10047. NSString *orderCode = [params objectForKey:@"orderCode"];
  10048. NSDictionary *newParams = @{
  10049. @"product_id" : params[@"product_id_string"],
  10050. @"orderCode" : orderCode,
  10051. @"can_create_backorder":params[@"can_create_backorder"]
  10052. };
  10053. return [self offline_add2cart:[newParams mutableCopy]];
  10054. }
  10055. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10056. {
  10057. NSString *orderCode = [params objectForKey:@"orderCode"];
  10058. NSString *upccode = [params objectForKey:@"upc_code"];
  10059. // product_name = [self translateSingleQuote:product_name];
  10060. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10061. // bool search_upc = [params[@"search_upc"] boolValue];
  10062. sqlite3 *db = [iSalesDB get_db];
  10063. __block NSMutableString *product_id_string = [NSMutableString string];
  10064. // NSString *name = [product_name_array objectAtIndex:i];
  10065. NSString *sql =nil;
  10066. 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];
  10067. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10068. int product_id = sqlite3_column_int(stmt, 0);
  10069. [product_id_string appendFormat:@"%d",product_id];
  10070. }];
  10071. if (!orderCode) {
  10072. orderCode = @"";
  10073. }
  10074. NSDictionary *newParams = @{
  10075. @"product_id" : product_id_string,
  10076. @"orderCode" : orderCode,
  10077. @"can_create_backorder":params[@"can_create_backorder"]
  10078. };
  10079. [iSalesDB close_db:db];
  10080. return [self offline_add2cart:[newParams mutableCopy]];
  10081. }
  10082. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10083. {
  10084. NSString *orderCode = [params objectForKey:@"orderCode"];
  10085. NSString *product_name = [params objectForKey:@"product_name"];
  10086. product_name = [self translateSingleQuote:product_name];
  10087. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10088. bool search_upc = [params[@"search_upc"] boolValue];
  10089. sqlite3 *db = [iSalesDB get_db];
  10090. __block NSMutableString *product_id_string = [NSMutableString string];
  10091. for (int i = 0; i < product_name_array.count; i++) {
  10092. NSString *name = [product_name_array objectAtIndex:i];
  10093. NSString *sql =nil;
  10094. if(search_upc)
  10095. 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];
  10096. else
  10097. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10098. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10099. int product_id = sqlite3_column_int(stmt, 0);
  10100. if (i == product_name_array.count - 1) {
  10101. [product_id_string appendFormat:@"%d",product_id];
  10102. } else {
  10103. [product_id_string appendFormat:@"%d,",product_id];
  10104. }
  10105. }];
  10106. }
  10107. if (!orderCode) {
  10108. orderCode = @"";
  10109. }
  10110. NSDictionary *newParams = @{
  10111. @"product_id" : product_id_string,
  10112. @"orderCode" : orderCode,
  10113. @"can_create_backorder":params[@"can_create_backorder"]
  10114. };
  10115. [iSalesDB close_db:db];
  10116. return [self offline_add2cart:[newParams mutableCopy]];
  10117. }
  10118. #pragma mark reset order
  10119. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10120. {
  10121. // UIApplication * app = [UIApplication sharedApplication];
  10122. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10123. [iSalesDB disable_trigger];
  10124. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10125. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10126. [iSalesDB enable_trigger];
  10127. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10128. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10129. return [RAConvertor dict2data:dic];
  10130. }
  10131. #pragma mark submit order
  10132. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10133. {
  10134. return [self saveorder:params submit:YES];
  10135. }
  10136. #pragma mark copy order
  10137. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10138. {
  10139. NSMutableDictionary *ret = @{}.mutableCopy;
  10140. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10141. sqlite3 *db = [iSalesDB get_db];
  10142. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10143. // 首先查看联系人是否active
  10144. 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];
  10145. __block int customer_is_active = 1;
  10146. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10147. customer_is_active = sqlite3_column_int(stmt, 0);
  10148. }];
  10149. if (!customer_is_active) {
  10150. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10151. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10152. [iSalesDB close_db:db];
  10153. return [RAConvertor dict2data:ret];
  10154. }
  10155. // new order
  10156. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10157. NSString *new_order_code = [self get_offline_soid:db];
  10158. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10159. user = [self translateSingleQuote:user];
  10160. 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
  10161. __block int result = 1;
  10162. result = [iSalesDB execSql:insert_order_sql db:db];
  10163. if (!result) {
  10164. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10165. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10166. [iSalesDB close_db:db];
  10167. return [RAConvertor dict2data:ret];
  10168. }
  10169. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10170. // __block NSString *product_id = @"";
  10171. __weak typeof(self) weakSelf = self;
  10172. 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];
  10173. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10174. int is_active = sqlite3_column_int(stmt, 1);
  10175. if (is_active) {
  10176. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10177. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10178. 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];
  10179. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10180. }
  10181. }];
  10182. // product_id = [product_id substringFromIndex:1];
  10183. //
  10184. // [self offline_add2cart:@{}.mutableCopy];
  10185. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10186. [iSalesDB close_db:db];
  10187. if (result) {
  10188. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10189. } else {
  10190. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10191. }
  10192. [ret setObject:new_order_code forKey:@"so_id"];
  10193. return [RAConvertor dict2data:ret];
  10194. }
  10195. #pragma mark move wish list to cart
  10196. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10197. assert(params[@"can_create_backorder"]!=nil);
  10198. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10199. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10200. NSString *collectId = params[@"collectId"];
  10201. 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];
  10202. __block NSString *product_id = @"";
  10203. __block NSString *qty = @"";
  10204. __block int number_of_outOfStock = 0;
  10205. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10206. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10207. int productId = sqlite3_column_int(stmt, 0);
  10208. int item_qty = sqlite3_column_int(stmt, 1);
  10209. int availability = sqlite3_column_int(stmt, 2);
  10210. int _id = sqlite3_column_int(stmt, 3);
  10211. if (![params[@"can_create_backorder"] boolValue]) {
  10212. // 库存小于购买量为缺货
  10213. if (availability >= item_qty) {
  10214. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10215. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10216. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10217. } else {
  10218. number_of_outOfStock++;
  10219. }
  10220. } else {
  10221. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10222. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10223. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10224. }
  10225. }];
  10226. NSMutableDictionary *retDic = nil;
  10227. if (![params[@"can_create_backorder"] boolValue]) {
  10228. if (delete_collectId.count == 0) {
  10229. retDic = [NSMutableDictionary dictionary];
  10230. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10231. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10232. return [RAConvertor dict2data:retDic];
  10233. }
  10234. }
  10235. if (product_id.length > 1) {
  10236. product_id = [product_id substringFromIndex:1];
  10237. }
  10238. if (qty.length > 1) {
  10239. qty = [qty substringFromIndex:1];
  10240. }
  10241. NSString *orderCode = params[@"orderCode"];
  10242. if (!orderCode) {
  10243. orderCode = @"";
  10244. }
  10245. NSDictionary *newParams = @{
  10246. @"product_id" : product_id,
  10247. @"orderCode" : orderCode,
  10248. @"qty" : qty,
  10249. @"can_create_backorder":params[@"can_create_backorder"]
  10250. };
  10251. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10252. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10253. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10254. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10255. NSInteger ret = [wish_return[@"result"] intValue];
  10256. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10257. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10258. }
  10259. if (![params[@"can_create_backorder"] boolValue]) {
  10260. if (number_of_outOfStock > 0) {
  10261. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10262. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10263. }
  10264. }
  10265. return [RAConvertor dict2data:retDic];
  10266. }
  10267. #pragma mark - portfolio
  10268. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10269. // assert(params[@"contact_id"]!=nil);
  10270. assert(params[@"user"]!=nil);
  10271. assert(params[@"can_see_price"]!=nil);
  10272. int sort = [[params valueForKey:@"sort"] intValue];
  10273. int offset = [[params valueForKey:@"offset"] intValue];
  10274. int limit = [[params valueForKey:@"limit"] intValue];
  10275. NSString *orderBy = @"";
  10276. switch (sort) {
  10277. case 0:{
  10278. orderBy = @"p.modify_time desc";
  10279. }
  10280. break;
  10281. case 1:{
  10282. orderBy = @"modify_time asc";
  10283. }
  10284. break;
  10285. case 2:{
  10286. orderBy = @"p.name asc";
  10287. }
  10288. break;
  10289. case 3:{
  10290. orderBy = @"p.name desc";
  10291. }
  10292. break;
  10293. case 4:{
  10294. orderBy = @"p.description asc";
  10295. }
  10296. break;
  10297. case 5: {
  10298. orderBy = @"m.default_category asc";
  10299. }
  10300. default:
  10301. break;
  10302. }
  10303. // 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];
  10304. 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];
  10305. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10306. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10307. sqlite3 *db = [iSalesDB get_db];
  10308. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10309. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10310. int product_id = sqlite3_column_int(stmt, 0);
  10311. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10312. NSString *name = [self textAtColumn:1 statement:stmt];
  10313. NSString *description = [self textAtColumn:2 statement:stmt];
  10314. double price = sqlite3_column_double(stmt, 3);
  10315. double discount = sqlite3_column_double(stmt,4);
  10316. int qty = sqlite3_column_int(stmt, 5);
  10317. int percentage = sqlite3_column_int(stmt, 6);
  10318. int item_id = sqlite3_column_int(stmt, 7);
  10319. // int fashion_id = sqlite3_column_int(stmt, 8);
  10320. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10321. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10322. double percent = sqlite3_column_double(stmt, 11);
  10323. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10324. if ([price_null isEqualToString:@"null"]) {
  10325. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10326. }
  10327. NSMutableDictionary *item = @{
  10328. @"linenotes": line_note,
  10329. @"check": @(1),
  10330. @"product_id": product_id_string,
  10331. @"available_qty": @(qty),
  10332. @"available_percent" : @(percent),
  10333. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10334. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10335. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10336. @"img": img_path,
  10337. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10338. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10339. }.mutableCopy;
  10340. if (percentage) {
  10341. [item removeObjectForKey:@"available_qty"];
  10342. } else {
  10343. [item removeObjectForKey:@"available_percent"];
  10344. }
  10345. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10346. if ([qty_null isEqualToString:@"null"]) {
  10347. [item removeObjectForKey:@"available_qty"];
  10348. }
  10349. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10350. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10351. }];
  10352. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10353. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10354. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10355. }
  10356. [iSalesDB close_db:db];
  10357. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10358. [dic setValue:@"" forKey:@"email_content"];
  10359. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10360. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10361. return [RAConvertor dict2data:dic];
  10362. }
  10363. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10364. __block int qty = 0;
  10365. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10366. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10367. qty = sqlite3_column_int(stmt, 0);
  10368. }];
  10369. return qty;
  10370. }
  10371. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10372. {
  10373. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10374. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10375. // NSMutableDictionary * values = params[@"replaceValue"];
  10376. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10377. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10378. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10379. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10380. NSString *pdf_path = @"";
  10381. if (direct) {
  10382. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10383. } else {
  10384. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10385. }
  10386. NSString *create_user = [self valueInParams:params key:@"user"];
  10387. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10388. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10389. // model info
  10390. // 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];
  10391. // V1.90 more color
  10392. 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];
  10393. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10394. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10395. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10396. sqlite3 *db = [iSalesDB get_db];
  10397. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10398. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10399. int product_id = sqlite3_column_int(stmt, 0);
  10400. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10401. double price = sqlite3_column_double(stmt, 1);
  10402. double discount = sqlite3_column_double(stmt,2);
  10403. int qty = sqlite3_column_int(stmt, 3);
  10404. int percentage = sqlite3_column_int(stmt, 4);
  10405. int item_id = sqlite3_column_int(stmt, 5);
  10406. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10407. double percent = sqlite3_column_double(stmt, 7);
  10408. int more_color = sqlite3_column_int(stmt, 8);
  10409. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10410. /* if ([price_null isEqualToString:@"null"]) {
  10411. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10412. }
  10413. */
  10414. [product_ids_string appendFormat:@"%@,",product_id_string];
  10415. // Regular Price
  10416. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10417. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10418. // QTY
  10419. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10420. if ([qty_null isEqualToString:@"null"]) {
  10421. // 查available
  10422. qty = [self model_QTY:product_id_string db:db];
  10423. }
  10424. if (percentage) {
  10425. qty = qty * percent / 100;
  10426. }
  10427. // Special Price
  10428. if ([price_null isEqualToString:@"null"]) {
  10429. // price = regular price
  10430. price = [regular_price_str doubleValue];
  10431. }
  10432. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10433. if (![discount_null isEqualToString:@"null"]) {
  10434. price = price * (1 - discount / 100.0);
  10435. }
  10436. NSMutableDictionary *item = @{
  10437. @"line_note": line_note,
  10438. @"product_id": product_id_string,
  10439. @"available_qty": @(qty),
  10440. @"more_color":@(more_color),
  10441. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10442. @"regular_price" : regular_price_str,
  10443. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10444. }.mutableCopy;
  10445. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10446. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10447. }];
  10448. [iSalesDB close_db:db];
  10449. if (product_ids_string.length > 0) {
  10450. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10451. }
  10452. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10453. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10454. return [RAConvertor dict2data:resultDictionary];
  10455. }
  10456. NSString *model_info = [RAConvertor dict2string:dic];
  10457. // 创建PDF
  10458. // 在preview情况下保存,则不需要新建了
  10459. if (direct) {
  10460. NSMutableDictionary *tear_sheet_params = params;
  10461. // if (tear_sheet_id) {
  10462. // tear_sheet_params = values;
  10463. // }
  10464. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10465. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10466. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10467. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10468. resultDictionary = pdfInfo.mutableCopy;
  10469. } else { // 创建PDF失败
  10470. return pdfData;
  10471. }
  10472. } else {
  10473. // pdf_path 就是本地路径
  10474. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10475. }
  10476. // 将文件移动到PDF Cache文件夹
  10477. NSString *newPath = [pdf_path lastPathComponent];
  10478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10479. NSString *cachefolder = [paths objectAtIndex:0];
  10480. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10481. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10482. NSFileManager *fileManager = [NSFileManager defaultManager];
  10483. NSError *error = nil;
  10484. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10485. if (error) { // 移动文件失败
  10486. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10487. return [RAConvertor dict2data:resultDictionary];
  10488. }
  10489. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10490. pdf_path = [newPath lastPathComponent];
  10491. // 保存信息
  10492. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10493. NSString *off_params = [RAConvertor dict2string:params];
  10494. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10495. pdf_path = [self translateSingleQuote:pdf_path];
  10496. create_user = [self translateSingleQuote:create_user];
  10497. tear_note = [self translateSingleQuote:tear_note];
  10498. tear_name = [self translateSingleQuote:tear_name];
  10499. model_info = [self translateSingleQuote:model_info];
  10500. configureParams = [self translateSingleQuote:configureParams];
  10501. off_params = [self translateSingleQuote:off_params];
  10502. 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];
  10503. if (tear_sheet_id) {
  10504. int _id = [tear_sheet_id intValue];
  10505. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10506. }
  10507. int result = [iSalesDB execSql:save_pdf_sql];
  10508. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10509. //
  10510. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10511. if (remove_Item) {
  10512. // portfolioId
  10513. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10514. }
  10515. return [RAConvertor dict2data:resultDictionary];
  10516. }
  10517. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10518. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10519. }
  10520. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10521. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10522. }
  10523. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10524. int offset = [[params valueForKey:@"offset"] intValue];
  10525. int limit = [[params valueForKey:@"limit"] intValue];
  10526. NSString *keyword = [params valueForKey:@"keyWord"];
  10527. NSString *where = @"where is_delete is null or is_delete = 0";
  10528. if (keyword.length) {
  10529. keyword = [self translateSingleQuote:keyword];
  10530. 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];
  10531. }
  10532. 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
  10533. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10534. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10535. NSString *cachefolder = [paths objectAtIndex:0];
  10536. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10537. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10538. NSString *name = [self textAtColumn:0 statement:stmt];
  10539. NSString *note = [self textAtColumn:1 statement:stmt];
  10540. NSString *time = [self textAtColumn:2 statement:stmt];
  10541. NSString *user = [self textAtColumn:3 statement:stmt];
  10542. NSString *path = [self textAtColumn:4 statement:stmt];
  10543. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10544. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10545. path = [pdfFolder stringByAppendingPathComponent:path];
  10546. BOOL bdir=NO;
  10547. NSFileManager* fileManager = [NSFileManager defaultManager];
  10548. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10549. {
  10550. //pdf文件不存在
  10551. path=nil;
  10552. }
  10553. time = [self changeDateTimeFormate:time];
  10554. time = [time stringByAppendingString:@" PST"];
  10555. int sheet_id = sqlite3_column_int(stmt, 5);
  10556. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10557. item[@"tearsheetsId"]=@(sheet_id);
  10558. item[@"pdf_path"]=path;
  10559. item[@"create_time"]=time;
  10560. item[@"create_user"]=user;
  10561. item[@"tear_note"]=note;
  10562. item[@"tear_name"]=name;
  10563. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10564. item[@"model_info"]=model_info;
  10565. item[@"off_params"]=off_params;
  10566. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10567. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10568. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10569. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10570. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10571. }];
  10572. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10573. return [RAConvertor dict2data:dic];
  10574. }
  10575. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10576. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10577. // NSString *user = [params objectForKey:@"user"];
  10578. // 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];
  10579. 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];
  10580. int result = [iSalesDB execSql:sql];
  10581. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10582. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10583. if (result == RESULT_TRUE) {
  10584. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10585. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10586. sqlite3 *db = [iSalesDB get_db];
  10587. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10588. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10589. [iSalesDB close_db:db];
  10590. }
  10591. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10592. return [RAConvertor dict2data:dic];
  10593. }
  10594. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10595. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10596. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10597. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10598. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10599. NSDictionary *company_item = @{
  10600. @"control": @"text",
  10601. @"keyboard": @"text",
  10602. @"name": @"company_name",
  10603. @"value": COMPANY_FULL_NAME,
  10604. @"aname": @"Company Name"
  10605. };
  10606. [section_0 setObject:company_item forKey:@"item_0"];
  10607. [dic setObject:section_0 forKey:@"section_0"];
  10608. // Regurlar Price
  10609. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10610. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10611. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10612. __block long val_count = 0;
  10613. NSString *sql = @"select name,type,order_by from price order by order_by";
  10614. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10615. NSString *name = [self textAtColumn:0 statement:stmt];
  10616. int type = sqlite3_column_int(stmt, 1);
  10617. int order_by = sqlite3_column_int(stmt, 2);
  10618. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10619. NSDictionary *price_dic = @{
  10620. @"value" : name,
  10621. @"value_id" : [NSNumber numberWithInteger:type],
  10622. @"check" : order_by == 0 ? @(1) : @(0)
  10623. };
  10624. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10625. val_count = ++(*count);
  10626. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10627. val_count = 0;
  10628. }];
  10629. [cadedate setObject:@{@"value" : @"None",
  10630. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10631. val_count++;
  10632. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10633. [price setObject:cadedate forKey:@"cadedate"];
  10634. [section1 setObject:price forKey:@"item_2"];
  10635. [dic setObject:section1 forKey:@"section_1"];
  10636. return [RAConvertor dict2data:dic];
  10637. }
  10638. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10639. NSString *product_id = [params objectForKey:@"fashionId"];
  10640. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10641. __block int result = RESULT_TRUE;
  10642. __block int qty = 0;
  10643. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10644. qty = sqlite3_column_int(stmt, 0);
  10645. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10646. result = RESULT_FALSE;
  10647. }];
  10648. NSMutableDictionary *dic = @{
  10649. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10650. @"mode" : @"Regular Mode",
  10651. @"quantity_available" : @(qty),
  10652. @"result" : @(result),
  10653. }.mutableCopy;
  10654. return [RAConvertor dict2data:dic];
  10655. }
  10656. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10657. NSString *item_ids = [params objectForKey:@"item_id"];
  10658. NSString *line_notes = [params objectForKey:@"notes"];
  10659. NSString *price_str = [params objectForKey:@"price"];
  10660. NSString *discount_str = [params objectForKey:@"discount"];
  10661. NSString *percent = [params objectForKey:@"available_percent"];
  10662. NSString *qty = [params objectForKey:@"available_qty"];
  10663. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10664. int dot = 0;
  10665. if (line_notes) {
  10666. line_notes = [self translateSingleQuote:line_notes];
  10667. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10668. sql = [sql stringByAppendingString:line_notes];
  10669. dot = 1;
  10670. } else {
  10671. line_notes = @"";
  10672. }
  10673. if (price_str) {
  10674. if (dot) {
  10675. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10676. } else {
  10677. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10678. dot = 1;
  10679. }
  10680. sql = [sql stringByAppendingString:price_str];
  10681. } else {
  10682. price_str = @"";
  10683. }
  10684. if (discount_str) {
  10685. if (dot) {
  10686. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10687. } else {
  10688. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10689. dot = 1;
  10690. }
  10691. sql = [sql stringByAppendingString:discount_str];
  10692. } else {
  10693. discount_str = @"";
  10694. }
  10695. if (percent) {
  10696. if (dot) {
  10697. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10698. } else {
  10699. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10700. dot = 1;
  10701. }
  10702. sql = [sql stringByAppendingString:percent];
  10703. } else {
  10704. percent = @"";
  10705. }
  10706. if (qty) {
  10707. if (dot) {
  10708. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10709. } else {
  10710. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10711. dot = 1;
  10712. }
  10713. sql = [sql stringByAppendingString:qty];
  10714. } else {
  10715. qty = @"";
  10716. }
  10717. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10718. sql = [sql stringByAppendingString:where];
  10719. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10720. int result = [iSalesDB execSql:sql];
  10721. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10722. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10723. return [RAConvertor dict2data:dic];
  10724. }
  10725. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10726. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10727. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10728. sqlite3 *db = [iSalesDB get_db];
  10729. 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];
  10730. int result = [iSalesDB execSql:sql db:db];
  10731. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10732. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10733. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10734. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10735. [iSalesDB close_db:db];
  10736. return [RAConvertor dict2data:dic];
  10737. }
  10738. + (void)offline_removePDFWithName:(NSString *)name {
  10739. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10740. NSString *cachefolder = [paths objectAtIndex:0];
  10741. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10742. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10743. NSFileManager *fileManager = [NSFileManager defaultManager];
  10744. [fileManager removeItemAtPath:path error:nil];
  10745. }
  10746. + (void)offline_clear_PDFCache {
  10747. NSFileManager *fileManager = [NSFileManager defaultManager];
  10748. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10749. NSString *cachefolder = [paths objectAtIndex:0];
  10750. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10751. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10752. for (NSString *path in pdf_files) {
  10753. [self offline_removePDFWithName:[path lastPathComponent]];
  10754. }
  10755. }
  10756. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10757. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10758. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10759. NSString *user = [params objectForKey:@"user"];
  10760. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10761. if (![create_user isEqualToString:user]) {
  10762. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10763. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10764. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10765. return [RAConvertor dict2data:dic];
  10766. }
  10767. 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]];
  10768. __block int is_local = 0;
  10769. __block NSString *path = @"";
  10770. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10771. is_local = sqlite3_column_int(stmt, 0);
  10772. path = [self textAtColumn:1 statement:stmt];
  10773. }];
  10774. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10775. if (is_local == 1) {
  10776. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10777. // 删除文件
  10778. [self offline_removePDFWithName:path];
  10779. }
  10780. int result = [iSalesDB execSql:sql];
  10781. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10782. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10783. return [RAConvertor dict2data:dic];
  10784. }
  10785. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10786. {
  10787. // assert(add_params[@"contact_id"]!=nil);
  10788. assert(add_params[@"user"]!=nil);
  10789. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10790. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10791. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10792. NSString* where=@"1=1";
  10793. if (ver!=nil) {
  10794. where=@"is_dirty=1";
  10795. }
  10796. 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];
  10797. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10798. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10799. sqlite3 *db = [iSalesDB get_db];
  10800. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10801. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10802. NSInteger _id = sqlite3_column_int(stmt, 0);
  10803. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10804. NSString *name = [self textAtColumn:2 statement:stmt];
  10805. NSString *desc = [self textAtColumn:3 statement:stmt];
  10806. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10807. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10808. NSInteger qty = sqlite3_column_int(stmt, 6);
  10809. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10810. double percent = sqlite3_column_double(stmt, 8);
  10811. double price = sqlite3_column_double(stmt, 9);
  10812. double discount = sqlite3_column_double(stmt, 10);
  10813. NSString *img = [self textAtColumn:11 statement:stmt];
  10814. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10815. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10816. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10817. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10818. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10819. if ([price_null isEqualToString:@"null"]) {
  10820. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10821. }
  10822. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10823. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10824. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10825. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10826. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10827. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10828. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10829. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10830. if ([qty_null isEqualToString:@"null"]) {
  10831. // [item setValue:@"null" forKey:@"available_qty"];
  10832. } else {
  10833. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10834. }
  10835. if ([is_percent_null isEqualToString:@"null"]) {
  10836. // [item setValue:@"null" forKey:@"percentage"];
  10837. } else {
  10838. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10839. }
  10840. if ([percent_null isEqualToString:@"null"]) {
  10841. // [item setValue:@"null" forKey:@"percent"];
  10842. } else {
  10843. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10844. }
  10845. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10846. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10847. [item setValue:name forKey:@"name"];
  10848. [item setValue:desc forKey:@"description"];
  10849. [item setValue:img forKey:@"img"];
  10850. [item setValue:line_note forKey:@"line_note"];
  10851. [item setValue:create_time forKey:@"createtime"];
  10852. [item setValue:modify_time forKey:@"modifytime"];
  10853. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10854. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10855. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10856. }];
  10857. [iSalesDB close_db:db];
  10858. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10859. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10860. }
  10861. return ret;
  10862. }
  10863. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10864. {
  10865. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10866. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10867. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10868. NSString* where=@"1=1";
  10869. if (ver!=nil) {
  10870. where=@"is_dirty=1";
  10871. }
  10872. 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];
  10873. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10874. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10875. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10876. NSInteger _id = sqlite3_column_int(stmt, 0);
  10877. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10878. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10879. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10880. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10881. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10882. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10883. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10884. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10885. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10886. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10887. NSString *uuid = [NSUUID UUID].UUIDString;
  10888. int is_delete = sqlite3_column_int(stmt, 11);
  10889. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10890. int is_local = sqlite3_column_int(stmt, 12);
  10891. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10892. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10893. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10894. }
  10895. [item setObject:pdf_path forKey:@"pdf_path"];
  10896. [item setObject:create_user forKey:@"create_user"];
  10897. [item setObject:tear_note forKey:@"tear_note"];
  10898. [item setObject:tear_name forKey:@"tear_name"];
  10899. [item setObject:model_info forKey:@"model_info"];
  10900. [item setObject:createtime forKey:@"createtime"];
  10901. [item setObject:modifytime forKey:@"modifytime"];
  10902. [item setObject:urlParams forKey:@"urlParams"];
  10903. [item setObject:off_params forKey:@"off_params"];
  10904. [item setObject:uuid forKey:@"pdf_token"];
  10905. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10906. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10907. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10908. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10909. } else {
  10910. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10911. }
  10912. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10913. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10914. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10915. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10916. }];
  10917. return ret;
  10918. }
  10919. #pragma mark 2020
  10920. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10921. {
  10922. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10923. sqlite3 *db = [iSalesDB get_db];
  10924. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10925. if (contactType) {
  10926. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10927. } else {
  10928. contactType = @"1 = 1";
  10929. }
  10930. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10931. DebugLog(@"offline contact list keyword: %@",keyword);
  10932. // advanced search
  10933. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10934. if (contact_name) {
  10935. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10936. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10937. } else {
  10938. contact_name = @"";
  10939. }
  10940. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10941. if (customer_phone) {
  10942. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10943. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10944. } else {
  10945. customer_phone = @"";
  10946. }
  10947. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10948. if (customer_fax) {
  10949. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10950. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10951. } else {
  10952. customer_fax = @"";
  10953. }
  10954. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10955. if (customer_zipcode) {
  10956. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10957. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10958. } else {
  10959. customer_zipcode = @"";
  10960. }
  10961. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10962. if (customer_sales_rep) {
  10963. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10964. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10965. } else {
  10966. customer_sales_rep = @"";
  10967. }
  10968. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10969. if (customer_state) {
  10970. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10971. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10972. } else {
  10973. customer_state = @"";
  10974. }
  10975. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10976. if (customer_name) {
  10977. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10978. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10979. } else {
  10980. customer_name = @"";
  10981. }
  10982. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10983. if (customer_country) {
  10984. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10985. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10986. } else {
  10987. customer_country = @"";
  10988. }
  10989. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10990. if (customer_cid) {
  10991. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10992. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10993. } else {
  10994. customer_cid = @"";
  10995. }
  10996. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10997. if (customer_city) {
  10998. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10999. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11000. } else {
  11001. customer_city = @"";
  11002. }
  11003. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11004. if (customer_address) {
  11005. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11006. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11007. } else {
  11008. customer_address = @"";
  11009. }
  11010. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11011. if (customer_email) {
  11012. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11013. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11014. } else {
  11015. customer_email = @"";
  11016. }
  11017. NSString *price_name = [params valueForKey:@"price_name"];
  11018. if (price_name) {
  11019. if ([price_name containsString:@","]) {
  11020. // 首先从 price表中查处name
  11021. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11022. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11023. for (int i = 1;i < pArray.count;i++) {
  11024. NSString *p = pArray[i];
  11025. [mutablePStr appendFormat:@" or type = %@ ",p];
  11026. }
  11027. [mutablePStr appendString:@";"];
  11028. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11029. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11030. char *name = (char *)sqlite3_column_text(stmt, 0);
  11031. if (!name)
  11032. name = "";
  11033. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11034. }];
  11035. // 再根据name 拼sql
  11036. NSMutableString *mutable_price_name = [NSMutableString string];
  11037. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11038. for (int i = 1; i < price_name_array.count; i++) {
  11039. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11040. }
  11041. [mutable_price_name appendString:@")"];
  11042. price_name = mutable_price_name;
  11043. } else {
  11044. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11045. if ([price_name isEqualToString:@""]) {
  11046. price_name = @"";
  11047. } else {
  11048. __block NSString *price;
  11049. price_name = [self translateSingleQuote:price_name];
  11050. [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) {
  11051. char *p = (char *)sqlite3_column_text(stmt, 0);
  11052. if (p == NULL) {
  11053. p = "";
  11054. }
  11055. price = [NSString stringWithUTF8String:p];
  11056. }];
  11057. if ([price isEqualToString:@""]) {
  11058. price_name = @"";
  11059. } else {
  11060. price = [self translateSingleQuote:price];
  11061. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11062. }
  11063. }
  11064. }
  11065. } else {
  11066. price_name = @"";
  11067. }
  11068. int limit = [[params valueForKey:@"limit"] intValue];
  11069. int offset = [[params valueForKey:@"offset"] intValue];
  11070. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11071. 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];
  11072. 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];
  11073. // int result= [iSalesDB AddExFunction:db];
  11074. int count =0;
  11075. NSString *sqlQuery = nil;
  11076. if(keyword.length==0)
  11077. {
  11078. // 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];
  11079. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11080. sqlQuery = sql;
  11081. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11082. }
  11083. else
  11084. {
  11085. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11086. keyword = keyword.lowercaseString;
  11087. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11088. 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];
  11089. 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]];
  11090. }
  11091. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11092. sqlite3_stmt * statement;
  11093. [ret setValue:@"2" forKey:@"result"];
  11094. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11095. // 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";
  11096. int i = 0;
  11097. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11098. {
  11099. while (sqlite3_step(statement) == SQLITE_ROW)
  11100. {
  11101. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11102. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11103. int editable = sqlite3_column_int(statement, 0);
  11104. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11105. NSString *nscompany_name =nil;
  11106. if(company_name==nil)
  11107. nscompany_name=@"";
  11108. else
  11109. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11110. char *country = (char*)sqlite3_column_text(statement, 2);
  11111. if(country==nil)
  11112. country="";
  11113. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11114. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11115. // if(addr==nil)
  11116. // addr="";
  11117. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11118. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11119. if(zipcode==nil)
  11120. zipcode="";
  11121. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11122. char *state = (char*)sqlite3_column_text(statement, 5);
  11123. if(state==nil)
  11124. state="";
  11125. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11126. char *city = (char*)sqlite3_column_text(statement, 6);
  11127. if(city==nil)
  11128. city="";
  11129. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11130. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11131. // NSString *nscontact_name = nil;
  11132. // if(contact_name==nil)
  11133. // nscontact_name=@"";
  11134. // else
  11135. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11136. char *phone = (char*)sqlite3_column_text(statement, 8);
  11137. NSString *nsphone = nil;
  11138. if(phone==nil)
  11139. nsphone=@"";
  11140. else
  11141. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11142. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11143. if(contact_id==nil)
  11144. contact_id="";
  11145. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11146. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11147. if(addr_1==nil)
  11148. addr_1="";
  11149. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11150. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11151. if(addr_2==nil)
  11152. addr_2="";
  11153. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11154. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11155. if(addr_3==nil)
  11156. addr_3="";
  11157. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11158. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11159. if(addr_4==nil)
  11160. addr_4="";
  11161. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11162. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11163. if(first_name==nil)
  11164. first_name="";
  11165. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11166. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11167. if(last_name==nil)
  11168. last_name="";
  11169. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11170. char *fax = (char*)sqlite3_column_text(statement, 16);
  11171. NSString *nsfax = nil;
  11172. if(fax==nil)
  11173. nsfax=@"";
  11174. else
  11175. {
  11176. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11177. if(nsfax.length>0)
  11178. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11179. }
  11180. char *email = (char*)sqlite3_column_text(statement, 17);
  11181. NSString *nsemail = nil;
  11182. if(email==nil)
  11183. nsemail=@"";
  11184. else
  11185. {
  11186. nsemail= [[NSString alloc]initWithUTF8String:email];
  11187. if(nsemail.length>0)
  11188. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11189. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11190. }
  11191. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11192. [arr_name addObject:nsfirst_name];
  11193. [arr_name addObject:nslast_name];
  11194. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11195. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11196. {
  11197. // decrypt
  11198. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11199. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11200. nsphone=[AESCrypt fastdecrypt:nsphone];
  11201. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11202. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11203. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11204. }
  11205. [arr_addr addObject:nscompany_name];
  11206. [arr_addr addObject:nscontact_name];
  11207. [arr_addr addObject:@"<br>"];
  11208. [arr_addr addObject:nsaddr_1];
  11209. [arr_addr addObject:nsaddr_2];
  11210. [arr_addr addObject:nsaddr_3];
  11211. [arr_addr addObject:nsaddr_4];
  11212. //[arr_addr addObject:nsaddr];
  11213. [arr_addr addObject:nszipcode];
  11214. [arr_addr addObject:nscity];
  11215. [arr_addr addObject:nsstate];
  11216. [arr_addr addObject:nscountry];
  11217. [arr_addr addObject:@"<br>"];
  11218. [arr_addr addObject:nsphone];
  11219. [arr_addr addObject:nsfax];
  11220. [arr_addr addObject:nsemail];
  11221. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11222. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11223. [item setValue:name forKey:@"name"];
  11224. [item setValue:nscontact_id forKey:@"contact_id"];
  11225. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11226. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11227. i++;
  11228. }
  11229. sqlite3_finalize(statement);
  11230. }
  11231. [iSalesDB close_db:db];
  11232. #ifdef DEBUG
  11233. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11234. #endif
  11235. dispatch_async(dispatch_get_main_queue(), ^{
  11236. UIApplication * app = [UIApplication sharedApplication];
  11237. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11238. [ret setValue:appDelegate.mode forKey:@"mode"];
  11239. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11240. result(ret);
  11241. });
  11242. return ;
  11243. });
  11244. }
  11245. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11246. {
  11247. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11248. NSString* order_code= appDelegate.order_code;
  11249. NSString* user = appDelegate.user;
  11250. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11251. NSString* category = [params valueForKey:@"category"];
  11252. if (!category || [category isEqualToString:@""]) {
  11253. category = @"%";
  11254. }
  11255. category = [self translateSingleQuote:category];
  11256. int limit = [[params valueForKey:@"limit"] intValue];
  11257. int offset = [[params valueForKey:@"offset"] intValue];
  11258. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11259. NSString *limit_str = @"";
  11260. if (limited) {
  11261. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11262. }
  11263. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11264. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11265. sqlite3 *db = [iSalesDB get_db];
  11266. // [iSalesDB AddExFunction:db];
  11267. int count;
  11268. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11269. 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];
  11270. double price_min = 0;
  11271. double price_max = 0;
  11272. if ([params.allKeys containsObject:@"alert"]) {
  11273. // alert
  11274. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11275. NSString *alert = params[@"alert"];
  11276. if ([alert isEqualToString:@"Display All"]) {
  11277. alert = [NSString stringWithFormat:@""];
  11278. } else {
  11279. alert = [self translateSingleQuote:alert];
  11280. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11281. }
  11282. // available
  11283. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11284. NSString *available = params[@"available"];
  11285. NSString *available_condition;
  11286. if ([available isEqualToString:@"Display All"]) {
  11287. available_condition = @"";
  11288. } else if ([available isEqualToString:@"Available Now"]) {
  11289. available_condition = @"and availability > 0";
  11290. } else {
  11291. available_condition = @"and availability == 0";
  11292. }
  11293. // best seller
  11294. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11295. NSString *best_seller = @"";
  11296. NSString *order_best_seller = @"m.name asc";
  11297. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11298. best_seller = @"and best_seller > 0";
  11299. order_best_seller = @"m.best_seller desc,m.name asc";
  11300. }
  11301. // price
  11302. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11303. NSString *price = params[@"price"];
  11304. price_min = 0;
  11305. price_max = MAXFLOAT;
  11306. if (user && price != nil) {
  11307. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11308. NSMutableString *priceName = [NSMutableString string];
  11309. for (int i = 0; i < priceTypeArray.count; i++) {
  11310. NSString *pricetype = priceTypeArray[i];
  11311. pricetype = [self translateSingleQuote:pricetype];
  11312. if (i == 0) {
  11313. [priceName appendFormat:@"'%@'",pricetype];
  11314. } else {
  11315. [priceName appendFormat:@",'%@'",pricetype];
  11316. }
  11317. }
  11318. if ([price isEqualToString:@"Display All"]) {
  11319. price = [NSString stringWithFormat:@""];
  11320. } else if([price containsString:@"+"]){
  11321. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11322. price_min = [price doubleValue];
  11323. 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];
  11324. } else {
  11325. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11326. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11327. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11328. 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];
  11329. }
  11330. } else {
  11331. price = @"";
  11332. }
  11333. // sold_by_qty : Sold in quantities of %@
  11334. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11335. NSString *qty = params[@"sold_by_qty"];
  11336. if ([qty isEqualToString:@"Display All"]) {
  11337. qty = @"";
  11338. } else {
  11339. qty = [self translateSingleQuote:qty];
  11340. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11341. }
  11342. // cate
  11343. // category = [self translateSingleQuote:category];
  11344. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11345. // cate mutiple selection
  11346. NSString *category_id = params[@"category"];
  11347. NSMutableArray *cate_id_array = nil;
  11348. NSMutableString *cateWhere = [NSMutableString string];
  11349. if ([category_id isEqualToString:@""] || !category_id) {
  11350. [cateWhere appendString:@"1 = 1"];
  11351. } else {
  11352. if ([category_id containsString:@","]) {
  11353. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11354. } else {
  11355. cate_id_array = [@[category_id] mutableCopy];
  11356. }
  11357. [cateWhere appendString:@"("];
  11358. for (int i = 0; i < cate_id_array.count; i++) {
  11359. if (i == 0) {
  11360. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11361. } else {
  11362. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11363. }
  11364. }
  11365. [cateWhere appendString:@")"];
  11366. }
  11367. // where bestseller > 0 order by bestseller desc
  11368. // sql query: alert availability(int) best_seller(int) price qty
  11369. 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];
  11370. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11371. }
  11372. DebugLog(@"offline category where: %@",where);
  11373. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11374. if (!user) {
  11375. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11376. }
  11377. [ret setValue:filter forKey:@"filter"];
  11378. DebugLog(@"offline_category sql:%@",sqlQuery);
  11379. sqlite3_stmt * statement;
  11380. [ret setValue:@"2" forKey:@"result"];
  11381. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11382. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11383. // int count=0;
  11384. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11385. {
  11386. int i=0;
  11387. while (sqlite3_step(statement) == SQLITE_ROW)
  11388. {
  11389. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11390. char *name = (char*)sqlite3_column_text(statement, 0);
  11391. if(name==nil)
  11392. name="";
  11393. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11394. char *description = (char*)sqlite3_column_text(statement, 1);
  11395. if(description==nil)
  11396. description="";
  11397. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11398. int product_id = sqlite3_column_int(statement, 2);
  11399. int wid = sqlite3_column_int(statement, 3);
  11400. int closeout = sqlite3_column_int(statement, 4);
  11401. int cid = sqlite3_column_int(statement, 5);
  11402. int wisdelete = sqlite3_column_int(statement, 6);
  11403. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11404. int more_color = sqlite3_column_int(statement, 8);
  11405. // Defaul Category ID
  11406. __block NSString *categoryID = nil;
  11407. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11408. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11409. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11410. if(default_category==nil)
  11411. default_category="";
  11412. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11413. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11414. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11415. categoryID = nsdefault_category;
  11416. }];
  11417. if (!categoryID.length) {
  11418. NSString *cateIDs = params[@"category"];
  11419. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11420. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11421. for (NSString *cateID in requestCategoryArr) {
  11422. BOOL needBreak = NO;
  11423. for (NSString *itemCateIDBox in itemCategoryArr) {
  11424. if (itemCateIDBox.length > 4) {
  11425. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11426. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11427. if ([itemCategoryID isEqualToString:cateID]) {
  11428. needBreak = YES;
  11429. categoryID = itemCategoryID;
  11430. break;
  11431. }
  11432. }
  11433. }
  11434. if (needBreak) {
  11435. break;
  11436. }
  11437. }
  11438. }
  11439. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11440. if(wid !=0 && wisdelete != 1)
  11441. [item setValue:@"true" forKey:@"wish_exists"];
  11442. else
  11443. [item setValue:@"false" forKey:@"wish_exists"];
  11444. if(closeout==0)
  11445. [item setValue:@"false" forKey:@"is_closeout"];
  11446. else
  11447. [item setValue:@"true" forKey:@"is_closeout"];
  11448. if(cid==0)
  11449. [item setValue:@"false" forKey:@"cart_exists"];
  11450. else
  11451. [item setValue:@"true" forKey:@"cart_exists"];
  11452. if (more_color == 0) {
  11453. [item setObject:@(false) forKey:@"more_color"];
  11454. } else if (more_color == 1) {
  11455. [item setObject:@(true) forKey:@"more_color"];
  11456. }
  11457. [item addEntriesFromDictionary:imgjson];
  11458. // [item setValue:nsurl forKey:@"img"];
  11459. [item setValue:nsname forKey:@"name"];
  11460. [item setValue:nsdescription forKey:@"description"];
  11461. if (categoryID) {
  11462. [item setValue:categoryID forKey:@"item_category_id"];
  11463. }
  11464. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11465. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11466. i++;
  11467. }
  11468. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11469. [ret setObject:items forKey:@"items"];
  11470. sqlite3_finalize(statement);
  11471. } else {
  11472. DebugLog(@"nothing...");
  11473. }
  11474. DebugLog(@"count:%d",count);
  11475. [iSalesDB close_db:db];
  11476. #ifdef DEBUG
  11477. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11478. #endif
  11479. dispatch_async(dispatch_get_main_queue(), ^{
  11480. // UIApplication * app = [UIApplication sharedApplication];
  11481. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11482. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11483. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11484. result(ret);
  11485. });
  11486. });
  11487. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11488. }
  11489. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11490. {
  11491. [self categoryList:params limited:YES completionHandler:result];
  11492. }
  11493. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11494. {
  11495. UIApplication * app = [UIApplication sharedApplication];
  11496. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11497. NSString* order_code = appDelegate.order_code;
  11498. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11499. NSString* keyword = [params valueForKey:@"keyword"];
  11500. keyword=keyword.lowercaseString;
  11501. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11502. int limit = [[params valueForKey:@"limit"] intValue];
  11503. int offset = [[params valueForKey:@"offset"] intValue];
  11504. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11505. NSString *limit_str = @"";
  11506. if (limited) {
  11507. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11508. }
  11509. sqlite3 *db = [iSalesDB get_db];
  11510. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11511. NSString *sqlQuery = nil;
  11512. if(exactMatch )
  11513. 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 ;
  11514. else
  11515. 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
  11516. DebugLog(@"offline_search sql:%@",sqlQuery);
  11517. sqlite3_stmt * statement;
  11518. [ret setValue:@"2" forKey:@"result"];
  11519. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11520. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11521. // int count=0;
  11522. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11523. {
  11524. int i=0;
  11525. while (sqlite3_step(statement) == SQLITE_ROW)
  11526. {
  11527. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11528. // char *name = (char*)sqlite3_column_text(statement, 1);
  11529. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11530. char *name = (char*)sqlite3_column_text(statement, 0);
  11531. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11532. char *description = (char*)sqlite3_column_text(statement, 1);
  11533. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11534. int product_id = sqlite3_column_int(statement, 2);
  11535. // char *url = (char*)sqlite3_column_text(statement, 3);
  11536. // if(url==nil)
  11537. // url="";
  11538. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11539. int wid = sqlite3_column_int(statement, 3);
  11540. int closeout = sqlite3_column_int(statement, 4);
  11541. int cid = sqlite3_column_int(statement, 5);
  11542. int wisdelete = sqlite3_column_int(statement, 6);
  11543. int more_color = sqlite3_column_int(statement, 7);
  11544. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11545. if(wid !=0 && wisdelete != 1)
  11546. [item setValue:@"true" forKey:@"wish_exists"];
  11547. else
  11548. [item setValue:@"false" forKey:@"wish_exists"];
  11549. if(closeout==0)
  11550. [item setValue:@"false" forKey:@"is_closeout"];
  11551. else
  11552. [item setValue:@"true" forKey:@"is_closeout"];
  11553. if(cid==0)
  11554. [item setValue:@"false" forKey:@"cart_exists"];
  11555. else
  11556. [item setValue:@"true" forKey:@"cart_exists"];
  11557. if (more_color == 0) {
  11558. [item setObject:@(false) forKey:@"more_color"];
  11559. } else if (more_color == 1) {
  11560. [item setObject:@(true) forKey:@"more_color"];
  11561. }
  11562. [item addEntriesFromDictionary:imgjson];
  11563. // [item setValue:nsurl forKey:@"img"];
  11564. [item setValue:nsname forKey:@"fash_name"];
  11565. [item setValue:nsdescription forKey:@"description"];
  11566. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11567. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11568. i++;
  11569. }
  11570. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11571. [ret setObject:items forKey:@"items"];
  11572. sqlite3_finalize(statement);
  11573. }
  11574. DebugLog(@"count:%d",count);
  11575. [iSalesDB close_db:db];
  11576. #ifdef DEBUG
  11577. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11578. #endif
  11579. dispatch_async(dispatch_get_main_queue(), ^{
  11580. result(ret);
  11581. });
  11582. });
  11583. }
  11584. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11585. {
  11586. [self search:params limited:YES completionHandler:result];
  11587. }
  11588. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11589. {
  11590. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11591. NSString* offline_command=params[@"offline_Command"];
  11592. NSMutableDictionary* ret=nil;
  11593. if([offline_command isEqualToString:@"model_NIYMAL"])
  11594. {
  11595. NSString* category = params[@"category"];
  11596. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11597. }
  11598. dispatch_async(dispatch_get_main_queue(), ^{
  11599. result(ret);
  11600. });
  11601. });
  11602. }
  11603. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11604. {
  11605. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11606. int sort = [[params objectForKey:@"sort"] intValue];
  11607. NSString *sort_str = @"";
  11608. switch (sort) {
  11609. case 0:{
  11610. sort_str = @"order by w.modify_time desc";
  11611. }
  11612. break;
  11613. case 1:{
  11614. sort_str = @"order by w.modify_time asc";
  11615. }
  11616. break;
  11617. case 2:{
  11618. sort_str = @"order by m.name asc";
  11619. }
  11620. break;
  11621. case 3:{
  11622. sort_str = @"order by m.name desc";
  11623. }
  11624. break;
  11625. case 4:{
  11626. sort_str = @"order by m.description asc";
  11627. }
  11628. break;
  11629. default:
  11630. break;
  11631. }
  11632. // NSString* user = appDelegate.user;
  11633. sqlite3 *db = [iSalesDB get_db];
  11634. // order by w.create_time
  11635. 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];
  11636. // 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];
  11637. sqlite3_stmt * statement;
  11638. NSDate *date1 = [NSDate date];
  11639. // NSDate *date2 = nil;
  11640. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11641. int count=0;
  11642. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11643. {
  11644. while (sqlite3_step(statement) == SQLITE_ROW)
  11645. {
  11646. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11647. int product_id = sqlite3_column_double(statement, 0);
  11648. char *description = (char*)sqlite3_column_text(statement, 1);
  11649. if(description==nil)
  11650. description= "";
  11651. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11652. int item_id = sqlite3_column_double(statement, 2);
  11653. NSDate *date_image = [NSDate date];
  11654. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11655. printf("image : ");
  11656. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11657. // char *url = (char*)sqlite3_column_text(statement, 3);
  11658. // if(url==nil)
  11659. // url="";
  11660. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11661. int qty = sqlite3_column_int(statement, 3);
  11662. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11663. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11664. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11665. item[@"description"]= nsdescription;
  11666. item[@"img"]= nsurl;
  11667. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11668. count++;
  11669. }
  11670. printf("total time:");
  11671. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11672. ret[@"count"]= [NSNumber numberWithInt:count];
  11673. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11674. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11675. ret[@"result"]= [NSNumber numberWithInt:2];
  11676. sqlite3_finalize(statement);
  11677. }
  11678. [iSalesDB close_db:db];
  11679. dispatch_async(dispatch_get_main_queue(), ^{
  11680. UIApplication * app = [UIApplication sharedApplication];
  11681. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11682. appDelegate.wish_count =count;
  11683. [appDelegate update_count_mark];
  11684. result(ret);
  11685. });
  11686. });
  11687. }
  11688. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11689. {
  11690. // UIApplication * app = [UIApplication sharedApplication];
  11691. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11692. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11693. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11694. sqlite3 *db = [iSalesDB get_db];
  11695. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11696. NSString* product_id=params[@"product_id"];
  11697. NSString *item_count_str = params[@"item_count"];
  11698. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11699. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11700. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11701. // NSString *sql = @"";
  11702. for(int i=0;i<arr.count;i++)
  11703. {
  11704. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11705. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11706. __block int cart_count = 0;
  11707. if (!item_count_str) {
  11708. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11709. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11710. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11711. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11712. cart_count = [[model_set_components lastObject] intValue];
  11713. }];
  11714. }
  11715. if(count==0)
  11716. {
  11717. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11718. sqlite3_stmt *stmt;
  11719. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11720. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11721. if (item_count_arr) {
  11722. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11723. } else {
  11724. sqlite3_bind_int(stmt,2,cart_count);
  11725. }
  11726. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11727. [iSalesDB execSql:@"ROLLBACK" db:db];
  11728. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11729. [iSalesDB close_db:db];
  11730. DebugLog(@"add to wishlist error");
  11731. dispatch_async(dispatch_get_main_queue(), ^{
  11732. result(ret);
  11733. });
  11734. }
  11735. } else {
  11736. int qty = 0;
  11737. if (item_count_arr) {
  11738. qty = [item_count_arr[i] intValue];
  11739. } else {
  11740. qty = cart_count;
  11741. }
  11742. 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]];
  11743. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11744. [iSalesDB execSql:@"ROLLBACK" db:db];
  11745. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11746. [iSalesDB close_db:db];
  11747. DebugLog(@"add to wishlist error");
  11748. dispatch_async(dispatch_get_main_queue(), ^{
  11749. result(ret);
  11750. });
  11751. }
  11752. }
  11753. }
  11754. // [iSalesDB execSql:sql db:db];
  11755. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11756. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11757. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11758. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11759. [iSalesDB close_db:db];
  11760. ret[@"result"]= [NSNumber numberWithInt:2];
  11761. dispatch_async(dispatch_get_main_queue(), ^{
  11762. UIApplication * app = [UIApplication sharedApplication];
  11763. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11764. appDelegate.wish_count =count;
  11765. [appDelegate update_count_mark];
  11766. result(ret);
  11767. });
  11768. });
  11769. }
  11770. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11771. {
  11772. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11773. bool bLogin = appDelegate.bLogin;
  11774. __block NSString* contact_id = appDelegate.contact_id;
  11775. __block NSString* user = appDelegate.user;
  11776. __block NSString* order_code = appDelegate.order_code;
  11777. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11778. NSString* model_name = [params valueForKey:@"product_name"];
  11779. NSString* product_id = [params valueForKey:@"product_id"];
  11780. NSString* upc_code = [params valueForKey:@"upc_code"];
  11781. NSString* category = [params valueForKey:@"category"];
  11782. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11783. if(category==nil) {
  11784. category = default_category_id;
  11785. } else {
  11786. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11787. // 参数重有多个category id
  11788. if (arr_0.count > 1) {
  11789. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11790. // 取交集
  11791. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11792. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11793. [set_0 intersectSet:set_1];
  11794. if (set_0.count == 1) {
  11795. category = (NSString *)[set_0 anyObject];
  11796. } else {
  11797. if ([set_0 containsObject:default_category_id]) {
  11798. category = default_category_id;
  11799. } else {
  11800. category = (NSString *)[set_0 anyObject];
  11801. }
  11802. }
  11803. }
  11804. }
  11805. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11806. sqlite3 *db = [iSalesDB get_db];
  11807. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11808. NSString *sqlQuery = nil;
  11809. if(product_id==nil)
  11810. if(model_name==nil)
  11811. {
  11812. 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='%@';
  11813. }
  11814. else
  11815. {
  11816. 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='%@';
  11817. }
  11818. else
  11819. 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=%@;
  11820. sqlite3_stmt * statement;
  11821. [ret setValue:@"2" forKey:@"result"];
  11822. [ret setValue:@"3" forKey:@"detail_section_count"];
  11823. // int count=0;
  11824. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11825. {
  11826. if (sqlite3_step(statement) == SQLITE_ROW)
  11827. {
  11828. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11829. char *name = (char*)sqlite3_column_text(statement, 0);
  11830. if(name==nil)
  11831. name="";
  11832. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11833. char *description = (char*)sqlite3_column_text(statement, 1);
  11834. if(description==nil)
  11835. description="";
  11836. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11837. int product_id = sqlite3_column_int(statement, 2);
  11838. char *color = (char*)sqlite3_column_text(statement, 3);
  11839. if(color==nil)
  11840. color="";
  11841. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11842. //
  11843. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11844. // if(legcolor==nil)
  11845. // legcolor="";
  11846. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11847. //
  11848. //
  11849. int availability = sqlite3_column_int(statement, 5);
  11850. //
  11851. int incoming_stock = sqlite3_column_int(statement, 6);
  11852. char *demension = (char*)sqlite3_column_text(statement, 7);
  11853. if(demension==nil)
  11854. demension="";
  11855. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11856. // ,,,,,,,,,
  11857. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11858. if(seat_height==nil)
  11859. seat_height="";
  11860. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11861. char *material = (char*)sqlite3_column_text(statement, 9);
  11862. if(material==nil)
  11863. material="";
  11864. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11865. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11866. if(box_dim==nil)
  11867. box_dim="";
  11868. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11869. char *volume = (char*)sqlite3_column_text(statement, 11);
  11870. if(volume==nil)
  11871. volume="";
  11872. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11873. double weight = sqlite3_column_double(statement, 12);
  11874. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11875. if(model_set==nil)
  11876. model_set="";
  11877. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11878. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11879. if(load_ability==nil)
  11880. load_ability="";
  11881. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11882. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11883. // if(default_category==nil)
  11884. // default_category="";
  11885. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11886. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11887. if(fabric_content==nil)
  11888. fabric_content="";
  11889. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11890. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11891. if(assembling==nil)
  11892. assembling="";
  11893. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11894. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11895. if(made_in==nil)
  11896. made_in="";
  11897. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11898. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11899. if(special_remarks==nil)
  11900. special_remarks="";
  11901. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11902. int stockUcom = sqlite3_column_double(statement, 20);
  11903. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11904. if(product_group==nil)
  11905. product_group="";
  11906. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11907. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11908. // if(fashion_selector==nil)
  11909. // fashion_selector="";
  11910. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11911. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11912. if(selector_field==nil)
  11913. selector_field="";
  11914. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11915. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11916. if(property_field==nil)
  11917. property_field="";
  11918. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11919. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11920. if(packaging==nil)
  11921. packaging="";
  11922. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11923. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11924. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11925. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11926. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11927. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11928. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11929. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11930. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11931. NSString* Availability=nil;
  11932. if(availability>0)
  11933. Availability=[NSString stringWithFormat:@"%d",availability];
  11934. else
  11935. Availability = @"Out of Stock";
  11936. [img_section setValue:Availability forKey:@"Availability"];
  11937. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11938. char *eta = (char*)sqlite3_column_text(statement, 25);
  11939. if(eta==nil)
  11940. eta="";
  11941. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11942. if ([nseta isEqualToString:@"null"]) {
  11943. nseta = @"";
  11944. }
  11945. if (availability <= 0) {
  11946. [img_section setValue:nseta forKey:@"ETA"];
  11947. }
  11948. int item_id = sqlite3_column_int(statement, 26);
  11949. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11950. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11951. NSString* Price=nil;
  11952. if(bLogin==false)
  11953. Price=@"Must Sign in.";
  11954. else
  11955. {
  11956. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11957. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11958. if(price==nil)
  11959. Price=@"No Price.";
  11960. else
  11961. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11962. }
  11963. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11964. [img_section setValue:Price forKey:@"price"];
  11965. [img_section setValue:nsname forKey:@"model_name"];
  11966. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11967. if (order_code) { // 离线order code即so#
  11968. 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];
  11969. __block int cartQTY = 0;
  11970. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11971. cartQTY = sqlite3_column_int(stmt, 0);
  11972. }];
  11973. if (cartQTY > 0) {
  11974. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11975. }
  11976. }
  11977. [ret setObject:img_section forKey:@"img_section"];
  11978. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11979. int detail0_item_count=0;
  11980. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11981. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11982. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11983. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11984. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11985. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11986. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11987. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11988. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11989. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11990. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11991. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11992. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11993. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11994. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11995. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11996. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  11997. {
  11998. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  11999. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12000. }
  12001. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12002. [detail0_section setValue:@"kv" forKey:@"type"];
  12003. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12004. [ret setObject:detail0_section forKey:@"detail_0"];
  12005. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12006. // [detail1_section setValue:@"detail" forKey:@"target"];
  12007. // [detail1_section setValue:@"popup" forKey:@"action"];
  12008. // [detail1_section setValue:@"content" forKey:@"type"];
  12009. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12010. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12011. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12012. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12013. [ret setObject:detail1_section forKey:@"detail_1"];
  12014. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12015. [detail2_section setValue:@"detail" forKey:@"target"];
  12016. [detail2_section setValue:@"popup" forKey:@"action"];
  12017. [detail2_section setValue:@"content" forKey:@"type"];
  12018. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12019. [detail2_section setValue:@"true" forKey:@"single_row"];
  12020. [detail2_section setValue:@"local" forKey:@"data"];
  12021. [ret setObject:detail2_section forKey:@"detail_2"];
  12022. }
  12023. sqlite3_finalize(statement);
  12024. }
  12025. else
  12026. {
  12027. [ret setValue:@"8" forKey:@"result"];
  12028. }
  12029. // DebugLog(@"count:%d",count);
  12030. [iSalesDB close_db:db];
  12031. #ifdef DEBUG
  12032. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12033. #endif
  12034. dispatch_async(dispatch_get_main_queue(), ^{
  12035. result(ret);
  12036. });
  12037. });
  12038. }
  12039. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12040. //{
  12041. //
  12042. // UIApplication * app = [UIApplication sharedApplication];
  12043. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12044. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12045. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12046. // sqlite3 *db = [iSalesDB get_db];
  12047. // NSString* collectId=params[@"collectId"];
  12048. //
  12049. //
  12050. //
  12051. //
  12052. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12053. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12054. // [iSalesDB execSql:sqlQuery db:db];
  12055. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12056. // [iSalesDB close_db:db];
  12057. //
  12058. //
  12059. // ret[@"result"]= [NSNumber numberWithInt:2];
  12060. // dispatch_async(dispatch_get_main_queue(), ^{
  12061. //
  12062. // appDelegate.wish_count =count;
  12063. //
  12064. // [appDelegate update_count_mark];
  12065. // result(ret);
  12066. // });
  12067. //
  12068. // });
  12069. //}
  12070. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12071. {
  12072. UIApplication * app = [UIApplication sharedApplication];
  12073. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12074. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12075. [iSalesDB disable_trigger];
  12076. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12077. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12078. [iSalesDB enable_trigger];
  12079. dispatch_async(dispatch_get_main_queue(), ^{
  12080. //
  12081. // NSString* user = [params valueForKey:@"user"];
  12082. //
  12083. // NSString* password = [params valueForKey:@"password"];
  12084. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12085. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12086. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12087. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12088. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12089. [appDelegate update_count_mark];
  12090. appDelegate.can_show_price =false;
  12091. appDelegate.can_see_price =false;
  12092. appDelegate.can_create_portfolio =false;
  12093. appDelegate.can_create_order =false;
  12094. appDelegate.can_cancel_order =false;
  12095. appDelegate.can_set_cart_price =false;
  12096. appDelegate.can_delete_order =false;
  12097. appDelegate.can_submit_order =false;
  12098. appDelegate.can_set_tearsheet_price =false;
  12099. appDelegate.can_update_contact_info = false;
  12100. appDelegate.save_order_logout = false;
  12101. appDelegate.submit_order_logout = false;
  12102. appDelegate.alert_sold_in_quantities = false;
  12103. appDelegate.ipad_perm =nil ;
  12104. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12105. appDelegate.OrderFilter= nil;
  12106. [appDelegate SetSo:nil];
  12107. [appDelegate set_main_button_panel];
  12108. result(ret);
  12109. });
  12110. });
  12111. }
  12112. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12113. {
  12114. // iSalesDB.
  12115. // UIApplication * app = [UIApplication sharedApplication];
  12116. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12117. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12118. [iSalesDB disable_trigger];
  12119. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12120. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12121. [iSalesDB enable_trigger];
  12122. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12123. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12124. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12125. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12126. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12127. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12128. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12129. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12130. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12131. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12132. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12133. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12134. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12135. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12136. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12137. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12138. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12139. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12140. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12141. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12142. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12143. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12144. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12145. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12146. [arr_name addObject:customer_first_name];
  12147. [arr_name addObject:customer_last_name];
  12148. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12149. // default ship from
  12150. 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';";
  12151. __block NSString *cid = @"";
  12152. __block NSString *name = @"";
  12153. __block NSString *ext = @"";
  12154. __block NSString *contact = @"";
  12155. __block NSString *email = @"";
  12156. __block NSString *fax = @"";
  12157. __block NSString *phone = @"";
  12158. sqlite3 *db = [iSalesDB get_db];
  12159. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12160. cid = [self textAtColumn:0 statement:statment];
  12161. name = [self textAtColumn:1 statement:statment];
  12162. ext = [self textAtColumn:2 statement:statment];
  12163. contact = [self textAtColumn:3 statement:statment];
  12164. email = [self textAtColumn:4 statement:statment];
  12165. fax = [self textAtColumn:5 statement:statment];
  12166. phone = [self textAtColumn:6 statement:statment];
  12167. }];
  12168. // default carrier
  12169. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12170. __block NSString *carrier = @"";
  12171. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12172. carrier = [self textAtColumn:0 statement:statment];
  12173. }];
  12174. NSString* so_id = [self get_offline_soid:db];
  12175. if(so_id==nil)
  12176. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12177. 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];
  12178. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12179. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12180. //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'
  12181. //soId
  12182. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12183. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12184. sqlite3_stmt * statement;
  12185. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12186. {
  12187. if (sqlite3_step(statement) == SQLITE_ROW)
  12188. {
  12189. // char *name = (char*)sqlite3_column_text(statement, 1);
  12190. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12191. //ret = sqlite3_column_int(statement, 0);
  12192. char *soId = (char*)sqlite3_column_text(statement, 0);
  12193. if(soId==nil)
  12194. soId="";
  12195. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12196. [ret setValue:nssoId forKey:@"soId"];
  12197. [ret setValue:nssoId forKey:@"orderCode"];
  12198. }
  12199. sqlite3_finalize(statement);
  12200. }
  12201. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12202. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12203. [iSalesDB close_db:db];
  12204. dispatch_async(dispatch_get_main_queue(), ^{
  12205. result(ret);
  12206. });
  12207. });
  12208. }
  12209. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12210. {
  12211. UIApplication * app = [UIApplication sharedApplication];
  12212. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12213. bool can_create_backorder= appDelegate.can_create_backorder;
  12214. NSString* user = appDelegate.user;
  12215. NSString* contact_id = appDelegate.contact_id;
  12216. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12217. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12218. sqlite3 *db = [iSalesDB get_db];
  12219. NSString* orderCode=params[@"orderCode"];
  12220. int sort = [[params objectForKey:@"sort"] intValue];
  12221. NSString *sort_str = @"";
  12222. switch (sort) {
  12223. case 0:{
  12224. sort_str = @"order by c.modify_time desc";
  12225. }
  12226. break;
  12227. case 1:{
  12228. sort_str = @"order by c.modify_time asc";
  12229. }
  12230. break;
  12231. case 2:{
  12232. sort_str = @"order by m.name asc";
  12233. }
  12234. break;
  12235. case 3:{
  12236. sort_str = @"order by m.name desc";
  12237. }
  12238. break;
  12239. case 4:{
  12240. sort_str = @"order by m.description asc";
  12241. }
  12242. break;
  12243. default:
  12244. break;
  12245. }
  12246. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  12247. sqlite3_stmt * statement1;
  12248. double orderdiscount=0;
  12249. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  12250. if ( dbresult1== SQLITE_OK)
  12251. {
  12252. if (sqlite3_step(statement1) == SQLITE_ROW)
  12253. {
  12254. // NSDate *row_date = [NSDate date];
  12255. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12256. orderdiscount = sqlite3_column_double(statement1, 0);
  12257. }
  12258. sqlite3_finalize(statement1);
  12259. }
  12260. 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 ];
  12261. // 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 ];
  12262. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12263. DebugLog(@"offline_login sql:%@",sqlQuery);
  12264. sqlite3_stmt * statement;
  12265. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12266. NSDate *date1 = [NSDate date];
  12267. int count=0;
  12268. int cart_count=0;
  12269. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12270. if ( dbresult== SQLITE_OK)
  12271. {
  12272. while (sqlite3_step(statement) == SQLITE_ROW)
  12273. {
  12274. // NSDate *row_date = [NSDate date];
  12275. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12276. int product_id = sqlite3_column_int(statement, 0);
  12277. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12278. int item_id = sqlite3_column_int(statement, 7);
  12279. NSString* Price=nil;
  12280. if(str_price==nil)
  12281. {
  12282. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12283. // NSDate *price_date = [NSDate date];
  12284. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12285. // DebugLog(@"price time interval");
  12286. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12287. if(price==nil)
  12288. Price=@"No Price.";
  12289. else
  12290. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12291. }
  12292. else
  12293. {
  12294. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12295. }
  12296. double discount = sqlite3_column_double(statement, 2);
  12297. int item_count = sqlite3_column_int(statement, 3);
  12298. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12299. NSString *nsline_note=nil;
  12300. if(line_note!=nil)
  12301. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12302. char *name = (char*)sqlite3_column_text(statement, 5);
  12303. NSString *nsname=nil;
  12304. if(name!=nil)
  12305. nsname= [[NSString alloc]initWithUTF8String:name];
  12306. char *description = (char*)sqlite3_column_text(statement, 6);
  12307. NSString *nsdescription=nil;
  12308. if(description!=nil)
  12309. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12310. int stockUom = sqlite3_column_int(statement, 8);
  12311. int _id = sqlite3_column_int(statement, 9);
  12312. int availability = sqlite3_column_int(statement, 10);
  12313. // NSDate *subtotal_date = [NSDate date];
  12314. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12315. // DebugLog(@"subtotal_date time interval");
  12316. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12317. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12318. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12319. int carton=[bsubtotaljson[@"carton"] intValue];
  12320. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12321. int mpack =[bsubtotaljson[@"mpack"] intValue];
  12322. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12323. // NSDate *img_date = [NSDate date];
  12324. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12325. // DebugLog(@"img_date time interval");
  12326. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12327. itemjson[@"model"]=nsname;
  12328. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12329. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12330. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12331. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12332. itemjson[@"check"]=@"true";
  12333. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12334. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12335. itemjson[@"unit_price"]=Price;
  12336. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12337. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12338. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12339. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  12340. itemjson[@"note"]=nsline_note;
  12341. if (!can_create_backorder) {
  12342. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12343. }
  12344. // NSDate *date2 = [NSDate date];
  12345. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12346. // DebugLog(@"model_bundle time interval");
  12347. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12348. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12349. count++;
  12350. // DebugLog(@"row time interval");
  12351. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12352. }
  12353. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12354. sqlite3_finalize(statement);
  12355. }
  12356. DebugLog(@"request cart total time interval");
  12357. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12358. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12359. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12360. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12361. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12362. ret[@"count"]=[NSNumber numberWithInt:count ];
  12363. if(orderdiscount>0)
  12364. ret[@"orderDiscount"]=@(orderdiscount);
  12365. ret[@"mode"]=@"Regular Mode";
  12366. [iSalesDB close_db:db];
  12367. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12368. DebugLog(@"general notes :%@",general_note);
  12369. ret[@"general_note"]= general_note;
  12370. dispatch_async(dispatch_get_main_queue(), ^{
  12371. result(ret);
  12372. });
  12373. });
  12374. }
  12375. @end