OLDataProvider.m 684 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299
  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. #ifdef BUILD_CONTRAST
  4409. [header setValue:[NSNumber numberWithBool:1] forKey:@"alert_sold_in_quantities"];
  4410. #endif
  4411. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4412. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4413. [header setValue:nsusername forKey:@"username"];
  4414. NSError* error=nil;
  4415. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4416. [header setValue:statusFilter forKey:@"statusFilter"];
  4417. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4418. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4419. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4420. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4421. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4422. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4423. [ret setObject:header forKey:@"header"];
  4424. [ret setValue:nsmode forKey:@"mode"];
  4425. }
  4426. sqlite3_finalize(statement);
  4427. }
  4428. [iSalesDB close_db:db];
  4429. #ifdef DEBUG
  4430. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4431. #endif
  4432. return [RAConvertor dict2data:ret];
  4433. }
  4434. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4435. {
  4436. assert(params[@"mode"]!=nil);
  4437. NSString* contactId = [params valueForKey:@"contactId"];
  4438. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4439. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4440. sqlite3 *db = [iSalesDB get_db];
  4441. NSString *sqlQuery = nil;
  4442. {
  4443. 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];
  4444. }
  4445. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4446. sqlite3_stmt * statement;
  4447. [ret setValue:@"2" forKey:@"result"];
  4448. int carrier = -1;
  4449. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4450. {
  4451. //int i = 0;
  4452. if (sqlite3_step(statement) == SQLITE_ROW)
  4453. {
  4454. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4455. // int editable = sqlite3_column_int(statement, 0);
  4456. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4457. NSString *nscompany_name =nil;
  4458. if(company_name==nil)
  4459. nscompany_name=@"";
  4460. else
  4461. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4462. char *country = (char*)sqlite3_column_text(statement, 2);
  4463. if(country==nil)
  4464. country="";
  4465. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4466. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4467. // if(addr==nil)
  4468. // addr="";
  4469. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4470. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4471. if(zipcode==nil)
  4472. zipcode="";
  4473. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4474. char *state = (char*)sqlite3_column_text(statement, 5);
  4475. if(state==nil)
  4476. state="";
  4477. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4478. char *city = (char*)sqlite3_column_text(statement, 6);
  4479. if(city==nil)
  4480. city="";
  4481. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4482. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4483. // NSString *nscontact_name = nil;
  4484. // if(contact_name==nil)
  4485. // nscontact_name=@"";
  4486. // else
  4487. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4488. char *phone = (char*)sqlite3_column_text(statement, 8);
  4489. NSString *nsphone = nil;
  4490. if(phone==nil)
  4491. nsphone=@"";
  4492. else
  4493. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4494. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4495. // if(contact_id==nil)
  4496. // contact_id="";
  4497. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4498. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4499. if(addr_1==nil)
  4500. addr_1="";
  4501. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4502. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4503. if(addr_2==nil)
  4504. addr_2="";
  4505. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4506. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4507. if(addr_3==nil)
  4508. addr_3="";
  4509. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4510. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4511. if(addr_4==nil)
  4512. addr_4="";
  4513. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4514. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4515. if(first_name==nil)
  4516. first_name="";
  4517. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4518. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4519. if(last_name==nil)
  4520. last_name="";
  4521. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4522. char *fax = (char*)sqlite3_column_text(statement, 16);
  4523. NSString *nsfax = nil;
  4524. if(fax==nil)
  4525. nsfax=@"";
  4526. else
  4527. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4528. char *email = (char*)sqlite3_column_text(statement, 17);
  4529. NSString *nsemail = nil;
  4530. if(email==nil)
  4531. nsemail=@"";
  4532. else
  4533. nsemail= [[NSString alloc]initWithUTF8String:email];
  4534. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4535. NSString *nsimg_0 = nil;
  4536. if(img_0==nil)
  4537. nsimg_0=@"";
  4538. else
  4539. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4540. [self copy_bcardImg:nsimg_0];
  4541. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4542. NSString *nsimg_1 = nil;
  4543. if(img_1==nil)
  4544. nsimg_1=@"";
  4545. else
  4546. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4547. [self copy_bcardImg:nsimg_1];
  4548. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4549. NSString *nsimg_2 = nil;
  4550. if(img_2==nil)
  4551. nsimg_2=@"";
  4552. else
  4553. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4554. [self copy_bcardImg:nsimg_2];
  4555. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4556. NSString *nsprice_type = nil;
  4557. if(price_type==nil)
  4558. nsprice_type=@"";
  4559. else
  4560. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4561. char *notes = (char*)sqlite3_column_text(statement, 22);
  4562. NSString *nsnotes = nil;
  4563. if(notes==nil)
  4564. nsnotes=@"";
  4565. else
  4566. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4567. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4568. NSString *nssalesrep = nil;
  4569. if(salesrep==nil)
  4570. nssalesrep=@"";
  4571. else
  4572. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4573. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4574. carrier = sqlite3_column_int(statement, 25);
  4575. {
  4576. // decrypt
  4577. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4578. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4579. nsphone=[AESCrypt fastdecrypt:nsphone];
  4580. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4581. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4582. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4583. }
  4584. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4585. [arr_name addObject:nsfirst_name];
  4586. [arr_name addObject:nslast_name];
  4587. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4588. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4589. [arr_ext addObject:nsaddr_1];
  4590. [arr_ext addObject:nsaddr_2];
  4591. [arr_ext addObject:nsaddr_3];
  4592. [arr_ext addObject:nsaddr_4];
  4593. [arr_ext addObject:@"\r\n"];
  4594. [arr_ext addObject:nscity];
  4595. [arr_ext addObject:nsstate];
  4596. [arr_ext addObject:nszipcode];
  4597. [arr_ext addObject:nscountry];
  4598. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4599. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4600. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4601. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4602. [item setValue:nscountry forKey:@"customer_country"];
  4603. [item setValue:nsphone forKey:@"customer_phone"];
  4604. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4605. [item setValue:nscompany_name forKey:@"customer_name"];
  4606. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4607. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4608. [item setValue:nsext forKey:@"customer_contact_ext"];
  4609. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4610. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4611. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4612. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4613. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4614. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4615. [item setValue:nslast_name forKey:@"customer_last_name"];
  4616. [item setValue:nscity forKey:@"customer_city"];
  4617. [item setValue:nsstate forKey:@"customer_state"];
  4618. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4619. [item setValue:contactId forKey:@"customer_cid"];
  4620. [item setValue:nscontact_name forKey:@"customer_contact"];
  4621. [item setValue:nsfax forKey:@"customer_fax"];
  4622. [item setValue:nsemail forKey:@"customer_email"];
  4623. [item setValue:contact_type forKey:@"customer_contact_type"];
  4624. // i++;
  4625. }
  4626. // UIApplication * app = [UIApplication sharedApplication];
  4627. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4628. [ret setValue:params[@"mode"] forKey:@"mode"];
  4629. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4630. sqlite3_finalize(statement);
  4631. }
  4632. [iSalesDB close_db:db];
  4633. NSString* scarrier = [self offline_getCarrier:carrier];
  4634. item[@"customer_truck_carrier"] = scarrier;
  4635. [ret setObject:item forKey:@"customerInfo"];
  4636. #ifdef DEBUG
  4637. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4638. #endif
  4639. return ret;
  4640. }
  4641. + (bool) copy_bcardImg:(NSString*) filename
  4642. {
  4643. if(filename.length==0)
  4644. return false;
  4645. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4646. bool ret=false;
  4647. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4648. NSString *cachefolder = [paths objectAtIndex:0];
  4649. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4650. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4651. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4652. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4653. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4654. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4655. //
  4656. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4657. NSFileManager* fileManager = [NSFileManager defaultManager];
  4658. BOOL bdir=NO;
  4659. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4660. {
  4661. NSError *error = nil;
  4662. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4663. {
  4664. ret=false;
  4665. }
  4666. else
  4667. {
  4668. ret=true;
  4669. }
  4670. // NSError *error = nil;
  4671. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4672. //
  4673. // if(!bsuccess)
  4674. // {
  4675. // DebugLog(@"Create offline_createimg folder failed");
  4676. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4677. // return [RAConvertor dict2data:ret];
  4678. // }
  4679. // if(bsuccess)
  4680. // {
  4681. // sqlite3 *db = [self get_db];
  4682. //
  4683. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4684. // [iSalesDB close_db:db];
  4685. // }
  4686. }
  4687. return ret;
  4688. //
  4689. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4690. // if(bsuccess)
  4691. // {
  4692. // NSError *error = nil;
  4693. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4694. // {
  4695. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4696. // }
  4697. // else
  4698. // {
  4699. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4700. //
  4701. // ret[@"img_url_aname"]=filename;
  4702. // ret[@"img_url"]=savedImagePath;
  4703. // }
  4704. // }
  4705. }
  4706. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4707. {
  4708. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4709. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4710. NSString *cachefolder = [paths objectAtIndex:0];
  4711. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4712. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4713. NSFileManager* fileManager = [NSFileManager defaultManager];
  4714. BOOL bdir=YES;
  4715. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4716. {
  4717. NSError *error = nil;
  4718. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4719. if(!bsuccess)
  4720. {
  4721. DebugLog(@"Create offline_createimg folder failed");
  4722. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4723. return [RAConvertor dict2data:ret];
  4724. }
  4725. // if(bsuccess)
  4726. // {
  4727. // sqlite3 *db = [self get_db];
  4728. //
  4729. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4730. // [iSalesDB close_db:db];
  4731. // }
  4732. }
  4733. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4734. {
  4735. NSError *error = nil;
  4736. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4737. if(!bsuccess)
  4738. {
  4739. DebugLog(@"Create img_cache folder failed");
  4740. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4741. return [RAConvertor dict2data:ret];
  4742. }
  4743. // if(bsuccess)
  4744. // {
  4745. // sqlite3 *db = [self get_db];
  4746. //
  4747. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4748. // [iSalesDB close_db:db];
  4749. // }
  4750. }
  4751. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4752. //JEPG格式
  4753. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4754. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4755. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4756. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4757. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4758. if(bsuccess)
  4759. {
  4760. NSError *error = nil;
  4761. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4762. {
  4763. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4764. }
  4765. else
  4766. {
  4767. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4768. ret[@"img_url_aname"]=filename;
  4769. ret[@"img_url"]=filename;
  4770. }
  4771. }
  4772. else
  4773. {
  4774. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4775. }
  4776. return [RAConvertor dict2data:ret];
  4777. }
  4778. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4779. {
  4780. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4781. category = [category substringToIndex:3];
  4782. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4783. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4784. params[@"category"]= category;
  4785. ret[@"params"]= params;
  4786. [ret setValue:@"detail" forKey:@"target"];
  4787. [ret setValue:@"popup" forKey:@"action"];
  4788. [ret setValue:@"content" forKey:@"type"];
  4789. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4790. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4791. [ret setValue:@"true" forKey:@"single_row"];
  4792. [ret setValue:@"true" forKey:@"partial_refresh"];
  4793. // sqlite3 *db = [iSalesDB get_db];
  4794. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4795. NSString *sqlQuery =nil;
  4796. #ifdef BUILD_NPD
  4797. 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 ;
  4798. #else
  4799. 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];
  4800. #endif
  4801. sqlite3_stmt * statement;
  4802. int count = 0;
  4803. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4804. // int count=0;
  4805. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4806. {
  4807. int i=0;
  4808. while (sqlite3_step(statement) == SQLITE_ROW)
  4809. {
  4810. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4811. // char *name = (char*)sqlite3_column_text(statement, 1);
  4812. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4813. char *name = (char*)sqlite3_column_text(statement, 0);
  4814. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4815. int product_id = sqlite3_column_int(statement, 1);
  4816. char *url = (char*)sqlite3_column_text(statement, 2);
  4817. if(url==nil)
  4818. url="";
  4819. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4820. [item setValue:nsurl forKey:@"picture_path"];
  4821. [item setValue:nsname forKey:@"fash_name"];
  4822. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4823. [item setValue:category forKey:@"category"];
  4824. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4825. i++;
  4826. }
  4827. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4828. sqlite3_finalize(statement);
  4829. }
  4830. DebugLog(@"count:%d",count);
  4831. // [iSalesDB close_db:db];
  4832. return ret;
  4833. }
  4834. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4835. {
  4836. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4837. [ret setValue:key forKey:@"key"];
  4838. [ret setValue:value forKey:@"val"];
  4839. [ret setValue:@"price" forKey:@"type"];
  4840. return ret;
  4841. }
  4842. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4843. {
  4844. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4845. [ret setValue:key forKey:@"key"];
  4846. [ret setValue:value forKey:@"val"];
  4847. return ret;
  4848. }
  4849. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4850. {
  4851. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4852. [ret setValue:@"0" forKey:@"img_count"];
  4853. // sqlite3 *db = [iSalesDB get_db];
  4854. 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 ;
  4855. sqlite3_stmt * statement;
  4856. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4857. {
  4858. int i=0;
  4859. if (sqlite3_step(statement) == SQLITE_ROW)
  4860. {
  4861. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4862. // char *name = (char*)sqlite3_column_text(statement, 1);
  4863. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4864. char *value = (char*)sqlite3_column_text(statement, 0);
  4865. if(value==nil)
  4866. value="";
  4867. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4868. char *key = (char*)sqlite3_column_text(statement, 1);
  4869. if(key==nil)
  4870. key="";
  4871. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4872. [item setValue:nsvalue forKey:@"val"];
  4873. [item setValue:nskey forKey:@"key"];
  4874. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4875. [ret setValue:@"1" forKey:@"count"];
  4876. i++;
  4877. }
  4878. sqlite3_finalize(statement);
  4879. }
  4880. // [iSalesDB close_db:db];
  4881. return ret;
  4882. }
  4883. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4884. {
  4885. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4886. [ret setValue:@"0" forKey:@"count"];
  4887. // sqlite3 *db = [iSalesDB get_db];
  4888. 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;
  4889. sqlite3_stmt * statement;
  4890. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4891. {
  4892. int i=0;
  4893. while (sqlite3_step(statement) == SQLITE_ROW)
  4894. {
  4895. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4896. // char *name = (char*)sqlite3_column_text(statement, 1);
  4897. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4898. char *value = (char*)sqlite3_column_text(statement, 0);
  4899. if(value==nil)
  4900. value="";
  4901. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4902. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4903. if(selector_display==nil)
  4904. selector_display="";
  4905. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4906. int product_id = sqlite3_column_int(statement, 2);
  4907. char *category = (char*)sqlite3_column_text(statement, 3);
  4908. if(category==nil)
  4909. category="";
  4910. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4911. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4912. [item setValue:nsvalue forKey:@"title"];
  4913. [item setValue:url forKey:@"pic_url"];
  4914. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4915. [params setValue:@"2" forKey:@"count"];
  4916. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4917. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4918. [param0 setValue:@"product_id" forKey:@"name"];
  4919. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4920. [param1 setValue:nscategory forKey:@"val"];
  4921. [param1 setValue:@"category" forKey:@"name"];
  4922. [params setObject:param0 forKey:@"param_0"];
  4923. [params setObject:param1 forKey:@"param_1"];
  4924. [item setObject:params forKey:@"params"];
  4925. [ret setValue:nsselector_display forKey:@"name"];
  4926. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4927. i++;
  4928. }
  4929. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4930. [ret setValue:@"switch" forKey:@"action"];
  4931. sqlite3_finalize(statement);
  4932. }
  4933. // [iSalesDB close_db:db];
  4934. return ret;
  4935. }
  4936. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4937. {
  4938. // model 在 category search 显示的图片。
  4939. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4940. // sqlite3 *db = [iSalesDB get_db];
  4941. 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];
  4942. sqlite3_stmt * statement;
  4943. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4944. {
  4945. while (sqlite3_step(statement) == SQLITE_ROW)
  4946. {
  4947. char *url = (char*)sqlite3_column_text(statement, 0);
  4948. if(url==nil)
  4949. url="";
  4950. int type = sqlite3_column_int(statement, 1);
  4951. if(type==0)
  4952. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4953. else
  4954. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4955. }
  4956. sqlite3_finalize(statement);
  4957. }
  4958. // [iSalesDB close_db:db];
  4959. return ret;
  4960. }
  4961. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4962. {
  4963. int item_id=-1;
  4964. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4965. sqlite3_stmt * statement;
  4966. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4967. {
  4968. if (sqlite3_step(statement) == SQLITE_ROW)
  4969. {
  4970. item_id = sqlite3_column_int(statement, 0);
  4971. }
  4972. sqlite3_finalize(statement);
  4973. }
  4974. return item_id;
  4975. }
  4976. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4977. {
  4978. // NSString* ret = @"";
  4979. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4980. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4981. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4982. sqlite3_stmt * statement;
  4983. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4984. {
  4985. while (sqlite3_step(statement) == SQLITE_ROW)
  4986. {
  4987. int bitem_id = sqlite3_column_int(statement, 0);
  4988. int bitem_qty = sqlite3_column_int(statement, 1);
  4989. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4990. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4991. }
  4992. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4993. sqlite3_finalize(statement);
  4994. }
  4995. // if(ret==nil)
  4996. // ret=@"";
  4997. *count=arr_count;
  4998. return arr_bundle;
  4999. }
  5000. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5001. {
  5002. // get default sold qty, return -1 if model not found;
  5003. int ret = -1;
  5004. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5005. sqlite3_stmt * statement;
  5006. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5007. {
  5008. if (sqlite3_step(statement) == SQLITE_ROW)
  5009. {
  5010. ret = sqlite3_column_int(statement, 0);
  5011. }
  5012. sqlite3_finalize(statement);
  5013. }
  5014. return ret;
  5015. }
  5016. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5017. {
  5018. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5019. [ret setValue:@"0" forKey:@"img_count"];
  5020. // sqlite3 *db = [iSalesDB get_db];
  5021. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5022. sqlite3_stmt * statement;
  5023. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5024. {
  5025. int i=0;
  5026. while (sqlite3_step(statement) == SQLITE_ROW)
  5027. {
  5028. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5029. // char *name = (char*)sqlite3_column_text(statement, 1);
  5030. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5031. char *url = (char*)sqlite3_column_text(statement, 0);
  5032. if(url==nil)
  5033. url="";
  5034. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5035. [item setValue:nsurl forKey:@"s"];
  5036. [item setValue:nsurl forKey:@"l"];
  5037. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5038. i++;
  5039. }
  5040. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5041. sqlite3_finalize(statement);
  5042. }
  5043. // [iSalesDB close_db:db];
  5044. return ret;
  5045. }
  5046. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5047. // UIApplication * app = [UIApplication sharedApplication];
  5048. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5049. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5050. sqlite3 *db = [iSalesDB get_db];
  5051. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5052. NSString* product_id=params[@"product_id"];
  5053. NSString *item_count_str = params[@"item_count"];
  5054. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5055. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5056. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5057. // NSString *sql = @"";
  5058. for(int i=0;i<arr.count;i++)
  5059. {
  5060. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5061. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5062. __block int cart_count = 0;
  5063. if (!item_count_str) {
  5064. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5065. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5066. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5067. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5068. cart_count = [[model_set_components lastObject] intValue];
  5069. }];
  5070. }
  5071. if(count==0)
  5072. {
  5073. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5074. sqlite3_stmt *stmt;
  5075. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5076. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5077. if (item_count_arr) {
  5078. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5079. } else {
  5080. sqlite3_bind_int(stmt,2,cart_count);
  5081. }
  5082. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5083. [iSalesDB execSql:@"ROLLBACK" db:db];
  5084. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5085. [iSalesDB close_db:db];
  5086. DebugLog(@"add to wishlist error");
  5087. return ret;
  5088. }
  5089. } else {
  5090. int qty = 0;
  5091. if (item_count_arr) {
  5092. qty = [item_count_arr[i] intValue];
  5093. } else {
  5094. qty = cart_count;
  5095. }
  5096. 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]];
  5097. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5098. [iSalesDB execSql:@"ROLLBACK" db:db];
  5099. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5100. [iSalesDB close_db:db];
  5101. DebugLog(@"add to wishlist error");
  5102. return ret;
  5103. }
  5104. }
  5105. }
  5106. // [iSalesDB execSql:sql db:db];
  5107. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5108. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5109. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5110. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5111. [iSalesDB close_db:db];
  5112. ret[@"wish_count"]=@(count);
  5113. ret[@"result"]= [NSNumber numberWithInt:2];
  5114. return ret;
  5115. }
  5116. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5117. // 0 category
  5118. // 1 search
  5119. // 2 itemsearch
  5120. NSData *ret = nil;
  5121. NSDictionary *items = nil;
  5122. switch (from) {
  5123. case 0:{
  5124. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5125. }
  5126. break;
  5127. case 1:{
  5128. items = [[self search:params limited:NO] objectForKey:@"items"];
  5129. }
  5130. break;
  5131. case 2:{
  5132. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5133. }
  5134. break;
  5135. default:
  5136. break;
  5137. }
  5138. if (!items) {
  5139. return ret;
  5140. }
  5141. int count = [[items objectForKey:@"count"] intValue];
  5142. NSMutableString *product_id_str = [@"" mutableCopy];
  5143. for (int i = 0; i < count; i++) {
  5144. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5145. NSDictionary *item = [items objectForKey:key];
  5146. NSString *product_id = [item objectForKey:@"product_id"];
  5147. if (i == 0) {
  5148. [product_id_str appendString:product_id];
  5149. } else {
  5150. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5151. }
  5152. }
  5153. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5154. if ([add_to isEqualToString:@"cart"]) {
  5155. NSString *order_code = [params objectForKey:@"orderCode"];
  5156. if (order_code.length) {
  5157. NSDictionary *newParams = @{
  5158. @"product_id" : product_id_str,
  5159. @"orderCode" : order_code,
  5160. @"can_create_backorder":params[@"can_create_backorder"]
  5161. };
  5162. ret = [self offline_add2cart:newParams.mutableCopy];
  5163. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5164. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5165. ret = [RAConvertor dict2data:retDic];
  5166. }
  5167. } else if([add_to isEqualToString:@"wishlist"]) {
  5168. NSDictionary *newParams = @{
  5169. @"product_id" : product_id_str
  5170. };
  5171. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5172. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5173. ret = [RAConvertor dict2data:retDic];
  5174. } else if([add_to isEqualToString:@"portfolio"]) {
  5175. NSDictionary *newParams = @{
  5176. @"product_id" : product_id_str
  5177. };
  5178. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5179. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5180. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5181. ret = [RAConvertor dict2data:retDic];
  5182. }
  5183. return ret;
  5184. }
  5185. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5186. {
  5187. return [self addAll:params from:0];
  5188. }
  5189. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5190. {
  5191. return [self addAll:params from:1];
  5192. }
  5193. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5194. {
  5195. return [self addAll:params from:2];
  5196. }
  5197. #pragma mark - Jack
  5198. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5199. // "val_227" : {
  5200. // "check" : 1,
  5201. // "value" : "US United States",
  5202. // "value_id" : "228"
  5203. // },
  5204. if (!countryCode) {
  5205. countryCode = @"US";
  5206. }
  5207. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5208. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5209. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5210. int code_id = sqlite3_column_int(stmt, 3); // id
  5211. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5212. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5213. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5214. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5215. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5216. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5217. }
  5218. long n = *count;
  5219. *count = n + 1;
  5220. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5221. [container setValue:countryDic forKey:key];
  5222. }] mutableCopy];
  5223. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5224. return ret;
  5225. }
  5226. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5227. // "val_227" : {
  5228. // "check" : 1,
  5229. // "value" : "US United States",
  5230. // "value_id" : "228"
  5231. // },
  5232. __block NSString* OrderCarrier =@"";
  5233. // sqlite3 *db = [iSalesDB get_db];
  5234. // sqlite3_stmt * statement;
  5235. [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) {
  5236. OrderCarrier = [self textAtColumn:0 statement:statment];
  5237. // sqlite3_finalize(statement);
  5238. }];
  5239. // [iSalesDB close_db:db];
  5240. __block int c=0;
  5241. 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) {
  5242. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5243. int code_id = sqlite3_column_int(stmt, 2); // id
  5244. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5245. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5246. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5247. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5248. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5249. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5250. }
  5251. c++;
  5252. long n = *count;
  5253. *count = n + 1;
  5254. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5255. [container setValue:carrierDic forKey:key];
  5256. }] mutableCopy];
  5257. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5258. return ret;
  5259. }
  5260. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5261. // "val_227" : {
  5262. // "check" : 1,
  5263. // "value" : "US United States",
  5264. // "value_id" : "228"
  5265. // },
  5266. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5267. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5268. int code_id = sqlite3_column_int(stmt, 2); // id
  5269. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5270. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5271. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5272. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5273. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5274. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5275. }
  5276. long n = *count;
  5277. *count = n + 1;
  5278. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5279. [container setValue:carrierDic forKey:key];
  5280. }] mutableCopy];
  5281. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5282. return ret;
  5283. }
  5284. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5285. // // "val_227" : {
  5286. // // "check" : 1,
  5287. // // "value" : "US United States",
  5288. // // "value_id" : "228"
  5289. // // },
  5290. //
  5291. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5292. // NSString * code = [iSalesDB jk_queryText:Sql];
  5293. //
  5294. // return [self offline_getCarrier:code];
  5295. //
  5296. //}
  5297. + (NSString *)offline_getCarrier:(int)code {
  5298. // "val_227" : {
  5299. // "check" : 1,
  5300. // "value" : "US United States",
  5301. // "value_id" : "228"
  5302. // },
  5303. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5304. NSString * ret = [iSalesDB jk_queryText:Sql];
  5305. return ret;
  5306. }
  5307. + (int )offline_getCarrierCode:(NSString*)name {
  5308. // "val_227" : {
  5309. // "check" : 1,
  5310. // "value" : "US United States",
  5311. // "value_id" : "228"
  5312. // },
  5313. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5314. NSString * ret = [iSalesDB jk_queryText:Sql];
  5315. return [ret intValue];
  5316. }
  5317. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5318. countryCode = [self translateSingleQuote:countryCode];
  5319. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5320. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5321. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5322. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5323. if (name == NULL) {
  5324. name = "";
  5325. }
  5326. if (code == NULL) {
  5327. code = "";
  5328. }
  5329. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5330. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5331. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5332. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5333. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5334. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5335. }
  5336. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5337. [container setValue:stateDic forKey:key];
  5338. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5339. DebugLog(@"query all state error: %@",err_msg);
  5340. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5341. // [stateDic setValue:@"Other" forKey:@"value"];
  5342. // [stateDic setValue:@"" forKey:@"value_id"];
  5343. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5344. //
  5345. // if (state_code && [@"" isEqualToString:state_code]) {
  5346. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5347. // }
  5348. //
  5349. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5350. // [container setValue:stateDic forKey:key];
  5351. }] mutableCopy];
  5352. [ret removeObjectForKey:@"result"];
  5353. // failure 可以不用了,一样的
  5354. if (ret.allKeys.count == 0) {
  5355. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5356. [stateDic setValue:@"Other" forKey:@"value"];
  5357. [stateDic setValue:@"" forKey:@"value_id"];
  5358. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5359. if (state_code && [@"" isEqualToString:state_code]) {
  5360. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5361. }
  5362. NSString *key = [NSString stringWithFormat:@"val_0"];
  5363. [ret setValue:stateDic forKey:key];
  5364. }
  5365. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5366. return ret;
  5367. }
  5368. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5369. codeId = [self translateSingleQuote:codeId];
  5370. 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];
  5371. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5372. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5373. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5374. if (name == NULL) {
  5375. name = "";
  5376. }
  5377. if (code == NULL) {
  5378. code = "";
  5379. }
  5380. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5381. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5382. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5383. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5384. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5385. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5386. }
  5387. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5388. [container setValue:stateDic forKey:key];
  5389. }] mutableCopy];
  5390. [ret removeObjectForKey:@"result"];
  5391. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5392. return ret;
  5393. }
  5394. + (NSDictionary *)offline_getPrice {
  5395. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5396. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5397. char *name = (char *) sqlite3_column_text(stmt, 1);
  5398. int type = sqlite3_column_int(stmt, 2);
  5399. int orderBy = sqlite3_column_int(stmt, 3);
  5400. if (name == NULL) {
  5401. name = "";
  5402. }
  5403. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5404. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5405. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5406. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5407. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5408. if (orderBy == 0) {
  5409. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5410. }
  5411. [container setValue:priceDic forKey:key];
  5412. }] mutableCopy];
  5413. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5414. return ret;
  5415. }
  5416. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5417. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5418. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5419. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5420. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5421. // int _id = sqlite3_column_int(stmt, 0);
  5422. NSString *name = [self textAtColumn:1 statement:stmt];
  5423. NSDictionary *typeDic = @{
  5424. @"value_id" : name,
  5425. @"value" : name,
  5426. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5427. };
  5428. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5429. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5430. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5431. }];
  5432. return ret;
  5433. }
  5434. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5435. // 首先从offline_login表中取出sales_code
  5436. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5437. // NSString *user = app.user;
  5438. user = [self translateSingleQuote:user];
  5439. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5440. __block NSString *user_code = @"";
  5441. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5442. char *code = (char *)sqlite3_column_text(stmt, 0);
  5443. if (code == NULL) {
  5444. code = "";
  5445. }
  5446. user_code = [NSString stringWithUTF8String:code];
  5447. }];
  5448. // 再取所有salesRep
  5449. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5450. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5451. // 1 name 2 code 3 salesrep_id
  5452. char *name = (char *)sqlite3_column_text(stmt, 1);
  5453. char *code = (char *)sqlite3_column_text(stmt, 2);
  5454. int salesrep_id = sqlite3_column_int(stmt, 3);
  5455. if (name == NULL) {
  5456. name = "";
  5457. }
  5458. if (code == NULL) {
  5459. code = "";
  5460. }
  5461. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5462. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5463. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5464. // 比较code 相等则check
  5465. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5466. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5467. }
  5468. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5469. }] mutableCopy];
  5470. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5471. return ret;
  5472. }
  5473. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5474. zipcode = [self translateSingleQuote:zipcode];
  5475. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5476. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5477. char *country = (char *)sqlite3_column_text(stmt, 0);
  5478. char *state = (char *)sqlite3_column_text(stmt, 1);
  5479. char *city = (char *)sqlite3_column_text(stmt, 2);
  5480. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5481. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5482. if (country == NULL) {
  5483. country = "";
  5484. }
  5485. if (state == NULL) {
  5486. state = "";
  5487. }
  5488. if (city == NULL) {
  5489. city = "";
  5490. }
  5491. if (country_code == NULL) {
  5492. country_code = "";
  5493. }
  5494. if (state_code == NULL) {
  5495. state_code = "";
  5496. }
  5497. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5498. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5499. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5500. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5501. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5502. }] mutableCopy];
  5503. return ret;
  5504. }
  5505. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5506. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5507. [item setValue:value forKey:valueKey];
  5508. [dic setValue:item forKey:itemKey];
  5509. }
  5510. + (NSString *)countryCodeByid:(NSString *)code_id {
  5511. NSString *ret = nil;
  5512. code_id = [self translateSingleQuote:code_id];
  5513. sqlite3 *db = [iSalesDB get_db];
  5514. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5515. sqlite3_stmt * statement;
  5516. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5517. while (sqlite3_step(statement) == SQLITE_ROW) {
  5518. char *code = (char *)sqlite3_column_text(statement, 0);
  5519. if (code == NULL) {
  5520. code = "";
  5521. }
  5522. ret = [NSString stringWithUTF8String:code];
  5523. }
  5524. sqlite3_finalize(statement);
  5525. }
  5526. [iSalesDB close_db:db];
  5527. return ret;
  5528. }
  5529. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5530. NSString *ret = nil;
  5531. code = [self translateSingleQuote:code];
  5532. sqlite3 *db = [iSalesDB get_db];
  5533. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5534. sqlite3_stmt * statement;
  5535. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5536. while (sqlite3_step(statement) == SQLITE_ROW) {
  5537. char *_id = (char *)sqlite3_column_text(statement, 0);
  5538. if (_id == NULL) {
  5539. _id = "";
  5540. }
  5541. ret = [NSString stringWithFormat:@"%s",_id];
  5542. }
  5543. sqlite3_finalize(statement);
  5544. }
  5545. [iSalesDB close_db:db];
  5546. return ret;
  5547. }
  5548. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5549. NSString *name = nil;
  5550. codeId = [self translateSingleQuote:codeId];
  5551. sqlite3 *db = [iSalesDB get_db];
  5552. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5553. sqlite3_stmt * statement;
  5554. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5555. while (sqlite3_step(statement) == SQLITE_ROW) {
  5556. char *value = (char *)sqlite3_column_text(statement, 0);
  5557. if (value == NULL) {
  5558. value = "";
  5559. }
  5560. name = [NSString stringWithUTF8String:value];
  5561. }
  5562. sqlite3_finalize(statement);
  5563. }
  5564. [iSalesDB close_db:db];
  5565. return name;
  5566. }
  5567. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5568. NSString *ret = nil;
  5569. sqlite3 *db = [iSalesDB get_db];
  5570. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5571. sqlite3_stmt * statement;
  5572. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5573. while (sqlite3_step(statement) == SQLITE_ROW) {
  5574. char *name = (char *)sqlite3_column_text(statement, 0);
  5575. if (name == NULL) {
  5576. name = "";
  5577. }
  5578. ret = [NSString stringWithUTF8String:name];
  5579. }
  5580. sqlite3_finalize(statement);
  5581. }
  5582. [iSalesDB close_db:db];
  5583. return ret;
  5584. }
  5585. + (NSString *)salesRepCodeById:(NSString *)_id {
  5586. NSString *ret = nil;
  5587. _id = [self translateSingleQuote:_id];
  5588. sqlite3 *db = [iSalesDB get_db];
  5589. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5590. sqlite3_stmt * statement;
  5591. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5592. while (sqlite3_step(statement) == SQLITE_ROW) {
  5593. char *rep = (char *)sqlite3_column_text(statement, 0);
  5594. if (rep == NULL) {
  5595. rep = "";
  5596. }
  5597. ret = [NSString stringWithUTF8String:rep];
  5598. }
  5599. sqlite3_finalize(statement);
  5600. }
  5601. [iSalesDB close_db:db];
  5602. return ret;
  5603. }
  5604. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5605. char *tx = (char *)sqlite3_column_text(stmt, col);
  5606. if (tx == NULL) {
  5607. tx = "";
  5608. }
  5609. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5610. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5611. if (!text) {
  5612. text = @"";
  5613. }
  5614. // 将字符全部为' '的字符串干掉
  5615. int spaceCount = 0;
  5616. for (int i = 0; i < text.length; i++) {
  5617. if ([text characterAtIndex:i] == ' ') {
  5618. spaceCount++;
  5619. }
  5620. }
  5621. if (spaceCount == text.length) {
  5622. text = @"";
  5623. }
  5624. return text;
  5625. }
  5626. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5627. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5628. NSData *data = [NSData dataWithContentsOfFile:path];
  5629. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5630. return ret;
  5631. }
  5632. + (NSString *)textFileName:(NSString *)name {
  5633. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5634. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5635. if (!text) {
  5636. text = @"";
  5637. }
  5638. return text;
  5639. }
  5640. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5641. return [[dic objectForKey:key] mutableCopy];
  5642. }
  5643. + (id)translateSingleQuote:(NSString *)string {
  5644. if ([string isKindOfClass:[NSString class]])
  5645. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5646. return string;
  5647. }
  5648. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5649. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5650. }
  5651. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5652. NSString* ret= nil;
  5653. NSString *sqlQuery = nil;
  5654. // 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
  5655. 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];
  5656. sqlite3_stmt * statement;
  5657. // int count=0;
  5658. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5659. {
  5660. if (sqlite3_step(statement) == SQLITE_ROW)
  5661. {
  5662. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5663. if(imgurl==nil)
  5664. imgurl="";
  5665. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5666. ret=nsimgurl;
  5667. }
  5668. sqlite3_finalize(statement);
  5669. }
  5670. else
  5671. {
  5672. [ret setValue:@"8" forKey:@"result"];
  5673. }
  5674. // [iSalesDB close_db:db];
  5675. // DebugLog(@"data string: %@",ret );
  5676. return ret;
  5677. }
  5678. #pragma mark contact Advanced search
  5679. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5680. {
  5681. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5682. return [RAConvertor dict2data:contactAdvanceDic];
  5683. }
  5684. #pragma mark create new contact
  5685. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5686. assert(params[@"user"]!=nil);
  5687. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5688. NSData *data = [NSData dataWithContentsOfFile:path];
  5689. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5690. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5691. NSString *countryCode = nil;
  5692. NSString *countryCode_id = nil;
  5693. NSString *stateCode = nil;
  5694. NSString *city = nil;
  5695. NSString *zipCode = nil;
  5696. // NSString *carrierCode = nil;
  5697. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5698. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5699. NSString *code_id = params[@"country"];
  5700. countryCode_id = code_id;
  5701. countryCode = [self countryCodeByid:code_id];
  5702. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5703. NSString *zip_code = params[@"zipcode"];
  5704. // 剔除全部为空格
  5705. int spaceCount = 0;
  5706. for (int i = 0; i < zip_code.length; i++) {
  5707. if ([zip_code characterAtIndex:i] == ' ') {
  5708. spaceCount++;
  5709. }
  5710. }
  5711. if (spaceCount == zip_code.length) {
  5712. zip_code = @"";
  5713. }
  5714. zipCode = zip_code;
  5715. if (zipCode.length > 0) {
  5716. countryCode_id = params[@"country"];
  5717. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5718. countryCode = [dic valueForKey:@"country_code"];
  5719. if (!countryCode) {
  5720. // countryCode = @"US";
  5721. NSString *code_id = params[@"country"];
  5722. countryCode = [self countryCodeByid:code_id];
  5723. }
  5724. stateCode = [dic valueForKey:@"state_code"];
  5725. if (!stateCode.length) {
  5726. stateCode = params[@"state"];
  5727. }
  5728. city = [dic valueForKey:@"city"];
  5729. if (!city.length) {
  5730. city = params[@"city"];
  5731. }
  5732. // zip code
  5733. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5734. [zipDic setValue:zipCode forKey:@"value"];
  5735. [section_0 setValue:zipDic forKey:@"item_11"];
  5736. } else {
  5737. NSString *code_id = params[@"country"];
  5738. countryCode = [self countryCodeByid:code_id];
  5739. stateCode = params[@"state"];
  5740. city = params[@"city"];
  5741. }
  5742. }
  5743. } else {
  5744. // default: US United States
  5745. countryCode = @"US";
  5746. countryCode_id = @"228";
  5747. }
  5748. // country
  5749. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5750. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5751. // state
  5752. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5753. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5754. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5755. // city
  5756. if (city) {
  5757. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5758. [cityDic setValue:city forKey:@"value"];
  5759. [section_0 setValue:cityDic forKey:@"item_13"];
  5760. }
  5761. // price type
  5762. NSDictionary *priceDic = [self offline_getPrice];
  5763. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5764. // contact type
  5765. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5766. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5767. // Sales Rep
  5768. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5769. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5770. #ifdef BUILD_CONTRAST
  5771. //carrier
  5772. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5773. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5774. #endif
  5775. [ret setValue:section_0 forKey:@"section_0"];
  5776. return [RAConvertor dict2data:ret];
  5777. }
  5778. #pragma mark save
  5779. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5780. NSString *addr = nil;
  5781. NSString *contact_name = nil;
  5782. // int carrier = [params[@"carrier"] intValue];
  5783. NSString *companyName = [params objectForKey:@"company"];
  5784. if (companyName) {
  5785. companyName = [AESCrypt fastencrypt:companyName];
  5786. } else {
  5787. companyName = @"";
  5788. }
  5789. DebugLog(@"company");
  5790. companyName = [self translateSingleQuote:companyName];
  5791. NSString *addr1 = [params objectForKey:@"address"];
  5792. NSString *addr2 = [params objectForKey:@"address2"];
  5793. NSString *addr3 = [params objectForKey:@"address_3"];
  5794. NSString *addr4 = [params objectForKey:@"address_4"];
  5795. if (!addr2) {
  5796. addr2 = @"";
  5797. }
  5798. if (!addr3) {
  5799. addr3 = @"";
  5800. }
  5801. if (!addr4) {
  5802. addr4 = @"";
  5803. }
  5804. if (!addr1) {
  5805. addr1 = @"";
  5806. }
  5807. DebugLog(@"addr");
  5808. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5809. addr = [AESCrypt fastencrypt:addr];
  5810. addr = [self translateSingleQuote:addr];
  5811. if (addr1 && ![addr1 isEqualToString:@""]) {
  5812. addr1 = [AESCrypt fastencrypt:addr1];
  5813. }
  5814. addr1 = [self translateSingleQuote:addr1];
  5815. addr2 = [self translateSingleQuote:addr2];
  5816. addr3 = [self translateSingleQuote:addr3];
  5817. addr4 = [self translateSingleQuote:addr4];
  5818. NSString *country = [params objectForKey:@"country"];
  5819. if (country) {
  5820. country = [self countryNameByCountryCodeId:country];
  5821. } else {
  5822. country = @"";
  5823. }
  5824. int carrier = [params[@"carrier"] intValue];
  5825. DebugLog(@"country");
  5826. country = [self translateSingleQuote:country];
  5827. NSString *state = [params objectForKey:@"state"];
  5828. if (!state) {
  5829. state = @"";
  5830. }
  5831. DebugLog(@"state");
  5832. state = [self translateSingleQuote:state];
  5833. NSString *city = [params objectForKey:@"city"];
  5834. if (!city) {
  5835. city = @"";
  5836. }
  5837. city = [self translateSingleQuote:city];
  5838. NSString *zipcode = [params objectForKey:@"zipcode"];
  5839. if (!zipcode) {
  5840. zipcode = @"";
  5841. }
  5842. DebugLog(@"zip");
  5843. zipcode = [self translateSingleQuote:zipcode];
  5844. NSString *fistName = [params objectForKey:@"firstname"];
  5845. if (!fistName) {
  5846. fistName = @"";
  5847. }
  5848. NSString *lastName = [params objectForKey:@"lastname"];
  5849. if (!lastName) {
  5850. lastName = @"";
  5851. }
  5852. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5853. DebugLog(@"contact_name");
  5854. contact_name = [self translateSingleQuote:contact_name];
  5855. fistName = [self translateSingleQuote:fistName];
  5856. lastName = [self translateSingleQuote:lastName];
  5857. NSString *phone = [params objectForKey:@"phone"];
  5858. if (phone) {
  5859. phone = [AESCrypt fastencrypt:phone];
  5860. } else {
  5861. phone = @"";
  5862. }
  5863. DebugLog(@"PHONE");
  5864. phone = [self translateSingleQuote:phone];
  5865. NSString *fax = [params objectForKey:@"fax"];
  5866. if (!fax) {
  5867. fax = @"";
  5868. }
  5869. DebugLog(@"FAX");
  5870. fax = [self translateSingleQuote:fax];
  5871. NSString *email = [params objectForKey:@"email"];
  5872. if (!email) {
  5873. email = @"";
  5874. }
  5875. DebugLog(@"EMAIL:%@",email);
  5876. email = [self translateSingleQuote:email];
  5877. NSString *notes = [params objectForKey:@"contact_notes"];
  5878. if (!notes) {
  5879. notes = @"";
  5880. }
  5881. DebugLog(@"NOTE:%@",notes);
  5882. notes = [self translateSingleQuote:notes];
  5883. NSString *price = [params objectForKey:@"price_name"];
  5884. if (price) {
  5885. price = [self priceNameByPriceId:price];
  5886. } else {
  5887. price = @"";
  5888. }
  5889. DebugLog(@"PRICE");
  5890. price = [self translateSingleQuote:price];
  5891. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5892. if (salesRep) {
  5893. salesRep = [self salesRepCodeById:salesRep];
  5894. } else {
  5895. salesRep = @"";
  5896. }
  5897. salesRep = [self translateSingleQuote:salesRep];
  5898. NSString *img = [params objectForKey:@"business_card"];
  5899. NSArray *array = [img componentsSeparatedByString:@","];
  5900. NSString *img_0 = array[0];
  5901. if (!img_0) {
  5902. img_0 = @"";
  5903. }
  5904. img_0 = [self translateSingleQuote:img_0];
  5905. NSString *img_1 = array[1];
  5906. if (!img_1) {
  5907. img_1 = @"";
  5908. }
  5909. img_1 = [self translateSingleQuote:img_1];
  5910. NSString *img_2 = array[2];
  5911. if (!img_2) {
  5912. img_2 = @"";
  5913. }
  5914. img_2 = [self translateSingleQuote:img_2];
  5915. NSString *contact_id = [NSUUID UUID].UUIDString;
  5916. NSString *contact_type = [params objectForKey:@"type_name"];
  5917. if (!contact_type) {
  5918. contact_type = @"";
  5919. }
  5920. contact_type = [self translateSingleQuote:contact_type];
  5921. // 判断更新时是否为customer
  5922. if (update) {
  5923. contact_id = [params objectForKey:@"contact_id"];
  5924. if (!contact_id) {
  5925. contact_id = @"";
  5926. }
  5927. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5928. __block int customer = 0;
  5929. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5930. customer = sqlite3_column_int(stmt, 0);
  5931. }];
  5932. isCustomer = customer ? YES : NO;
  5933. }
  5934. NSMutableDictionary *sync_dic = [params mutableCopy];
  5935. if (isCustomer) {
  5936. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5937. } else {
  5938. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5939. }
  5940. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  5941. NSString *sync_data = nil;
  5942. NSString *sql = nil;
  5943. if (update){
  5944. contact_id = [params objectForKey:@"contact_id"];
  5945. if (!contact_id) {
  5946. contact_id = @"";
  5947. }
  5948. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5949. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5950. sync_data = [RAConvertor dict2string:sync_dic];
  5951. sync_data = [self translateSingleQuote:sync_data];
  5952. 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];
  5953. } else {
  5954. contact_id = [self translateSingleQuote:contact_id];
  5955. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5956. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5957. sync_data = [RAConvertor dict2string:sync_dic];
  5958. sync_data = [self translateSingleQuote:sync_data];
  5959. 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];
  5960. }
  5961. int result = [iSalesDB execSql:sql];
  5962. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5963. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5964. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5965. }
  5966. #pragma mark save new contact
  5967. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5968. {
  5969. return [self offline_saveContact:params update:NO isCustomer:YES];
  5970. }
  5971. #pragma mark edit contact
  5972. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5973. {
  5974. assert(params[@"user"]!=nil);
  5975. // {
  5976. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5977. // password = 123456;
  5978. // user = EvanK;
  5979. // }
  5980. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5981. NSData *data = [NSData dataWithContentsOfFile:path];
  5982. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5983. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5984. NSString *countryCode = nil;
  5985. NSString *countryCode_id = nil;
  5986. NSString *stateCode = nil;
  5987. __block int carrierCode = -1;
  5988. /*------contact infor------*/
  5989. __block NSString *country = nil;
  5990. __block NSString *company_name = nil;
  5991. __block NSString *contact_id = params[@"contact_id"];
  5992. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5993. __block NSString *zipcode = nil;
  5994. __block NSString *state = nil; // state_code
  5995. __block NSString *city = nil; //
  5996. __block NSString *firt_name,*last_name;
  5997. __block NSString *phone,*fax,*email;
  5998. __block NSString *notes,*price_type,*sales_rep;
  5999. __block NSString *img_0,*img_1,*img_2;
  6000. __block NSString *contact_type;
  6001. contact_id = [self translateSingleQuote:contact_id];
  6002. 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];
  6003. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6004. country = [self textAtColumn:0 statement:stmt]; // country name
  6005. company_name = [self textAtColumn:1 statement:stmt];
  6006. addr_1 = [self textAtColumn:2 statement:stmt];
  6007. addr_2 = [self textAtColumn:3 statement:stmt];
  6008. addr_3 = [self textAtColumn:4 statement:stmt];
  6009. addr_4 = [self textAtColumn:5 statement:stmt];
  6010. zipcode = [self textAtColumn:6 statement:stmt];
  6011. state = [self textAtColumn:7 statement:stmt]; // state code
  6012. city = [self textAtColumn:8 statement:stmt];
  6013. firt_name = [self textAtColumn:9 statement:stmt];
  6014. last_name = [self textAtColumn:10 statement:stmt];
  6015. phone = [self textAtColumn:11 statement:stmt];
  6016. fax = [self textAtColumn:12 statement:stmt];
  6017. email = [self textAtColumn:13 statement:stmt];
  6018. notes = [self textAtColumn:14 statement:stmt];
  6019. price_type = [self textAtColumn:15 statement:stmt]; // name
  6020. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6021. img_0 = [self textAtColumn:17 statement:stmt];
  6022. img_1 = [self textAtColumn:18 statement:stmt];
  6023. img_2 = [self textAtColumn:19 statement:stmt];
  6024. contact_type = [self textAtColumn:20 statement:stmt];
  6025. carrierCode = sqlite3_column_int(stmt, 21);
  6026. }];
  6027. // decrypt
  6028. if (company_name) {
  6029. company_name = [AESCrypt fastdecrypt:company_name];
  6030. }
  6031. if (addr_1) {
  6032. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6033. }
  6034. if (phone) {
  6035. phone = [AESCrypt fastdecrypt:phone];
  6036. }
  6037. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6038. countryCode = [iSalesDB jk_queryText:countrySql];
  6039. stateCode = state;
  6040. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6041. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6042. NSString *code_id = params[@"country"];
  6043. countryCode_id = code_id;
  6044. countryCode = [self countryCodeByid:code_id];
  6045. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6046. NSString *zip_code = params[@"zipcode"];
  6047. // 剔除全部为空格
  6048. int spaceCount = 0;
  6049. for (int i = 0; i < zip_code.length; i++) {
  6050. if ([zip_code characterAtIndex:i] == ' ') {
  6051. spaceCount++;
  6052. }
  6053. }
  6054. if (spaceCount == zip_code.length) {
  6055. zip_code = @"";
  6056. }
  6057. if (zipcode.length > 0) {
  6058. zipcode = zip_code;
  6059. countryCode_id = params[@"country"];
  6060. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6061. countryCode = [dic valueForKey:@"country_code"];
  6062. if (!countryCode) {
  6063. NSString *code_id = params[@"country"];
  6064. countryCode = [self countryCodeByid:code_id];
  6065. }
  6066. stateCode = [dic valueForKey:@"state_code"];
  6067. if (!stateCode.length) {
  6068. stateCode = params[@"state"];
  6069. }
  6070. city = [dic valueForKey:@"city"];
  6071. if (!city.length) {
  6072. city = params[@"city"];
  6073. }
  6074. // zip code
  6075. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6076. [zipDic setValue:zipcode forKey:@"value"];
  6077. [section_0 setValue:zipDic forKey:@"item_8"];
  6078. } else {
  6079. NSString *code_id = params[@"country"];
  6080. countryCode = [self countryCodeByid:code_id];
  6081. stateCode = params[@"state"];
  6082. city = params[@"city"];
  6083. }
  6084. }
  6085. }
  6086. // 0 Country
  6087. // 1 Company Name
  6088. // 2 Contact ID
  6089. // 3 Picture
  6090. // 4 Address 1
  6091. // 5 Address 2
  6092. // 6 Address 3
  6093. // 7 Address 4
  6094. // 8 Zip Code
  6095. // 9 State/Province
  6096. // 10 City
  6097. // 11 Contact First Name
  6098. // 12 Contact Last Name
  6099. // 13 Phone
  6100. // 14 Fax
  6101. // 15 Email
  6102. // 16 Contact Notes
  6103. // 17 Price Type
  6104. // 18 Contact Type
  6105. // 19 Sales Rep
  6106. // country
  6107. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6108. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6109. // company
  6110. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6111. // contact_id
  6112. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6113. // picture
  6114. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6115. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6116. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6117. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6118. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6119. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6120. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6121. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6122. // addr 1 2 3 4
  6123. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6124. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6125. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6126. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6127. // zip code
  6128. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6129. // state
  6130. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6131. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6132. // city
  6133. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6134. // first last
  6135. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6136. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6137. // phone fax email
  6138. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6139. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6140. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6141. // notes
  6142. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6143. // price
  6144. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6145. for (NSString *key in priceDic.allKeys) {
  6146. if ([key containsString:@"val_"]) {
  6147. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6148. if ([dic[@"value"] isEqualToString:price_type]) {
  6149. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6150. [priceDic setValue:dic forKey:key];
  6151. }
  6152. }
  6153. }
  6154. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6155. // Contact Rep
  6156. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6157. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6158. // Sales Rep
  6159. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6160. for (NSString *key in repDic.allKeys) {
  6161. if ([key containsString:@"val_"]) {
  6162. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6163. NSString *value = dic[@"value"];
  6164. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6165. if (code && [code isEqualToString:sales_rep]) {
  6166. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6167. [repDic setValue:dic forKey:key];
  6168. }
  6169. }
  6170. }
  6171. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6172. #ifdef BUILD_CONTRAST
  6173. //carrier
  6174. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6175. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6176. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6177. #endif
  6178. [ret setValue:section_0 forKey:@"section_0"];
  6179. return [RAConvertor dict2data:ret];
  6180. }
  6181. #pragma mark save contact
  6182. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6183. {
  6184. return [self offline_saveContact:params update:YES isCustomer:YES];
  6185. }
  6186. #pragma mark category
  6187. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6188. if (ck) {
  6189. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6190. for (NSString *key in res.allKeys) {
  6191. if (![key isEqualToString:@"count"]) {
  6192. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6193. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6194. if ([val[@"value"] isEqualToString:ck]) {
  6195. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6196. }
  6197. [res setValue:val forKey:key];
  6198. }
  6199. }
  6200. [dic setValue:res forKey:valueKey];
  6201. }
  6202. }
  6203. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6204. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6205. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6206. NSString* category = [params valueForKey:@"category"];
  6207. if (!category || [category isEqualToString:@""]) {
  6208. category = @"%";
  6209. }
  6210. category = [self translateSingleQuote:category];
  6211. int limit = [[params valueForKey:@"limit"] intValue];
  6212. int offset = [[params valueForKey:@"offset"] intValue];
  6213. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6214. NSString *limit_str = @"";
  6215. if (limited) {
  6216. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6217. }
  6218. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6219. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6220. sqlite3 *db = [iSalesDB get_db];
  6221. // [iSalesDB AddExFunction:db];
  6222. int count;
  6223. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6224. 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];
  6225. double price_min = 0;
  6226. double price_max = 0;
  6227. if ([params.allKeys containsObject:@"alert"]) {
  6228. // alert
  6229. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6230. NSString *alert = params[@"alert"];
  6231. if ([alert isEqualToString:@"Display All"]) {
  6232. alert = [NSString stringWithFormat:@""];
  6233. } else {
  6234. alert = [self translateSingleQuote:alert];
  6235. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6236. }
  6237. // available
  6238. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6239. NSString *available = params[@"available"];
  6240. NSString *available_condition;
  6241. if ([available isEqualToString:@"Display All"]) {
  6242. available_condition = @"";
  6243. } else if ([available isEqualToString:@"Available Now"]) {
  6244. available_condition = @"and availability > 0";
  6245. } else {
  6246. available_condition = @"and availability == 0";
  6247. }
  6248. // best seller
  6249. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6250. NSString *best_seller = @"";
  6251. NSString *order_best_seller = @"m.name asc";
  6252. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6253. best_seller = @"and best_seller > 0";
  6254. order_best_seller = @"m.best_seller desc,m.name asc";
  6255. }
  6256. // price
  6257. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6258. NSString *price = params[@"price"];
  6259. price_min = 0;
  6260. price_max = MAXFLOAT;
  6261. if (params[@"user"] && price != nil) {
  6262. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6263. NSMutableString *priceName = [NSMutableString string];
  6264. for (int i = 0; i < priceTypeArray.count; i++) {
  6265. NSString *pricetype = priceTypeArray[i];
  6266. pricetype = [self translateSingleQuote:pricetype];
  6267. if (i == 0) {
  6268. [priceName appendFormat:@"'%@'",pricetype];
  6269. } else {
  6270. [priceName appendFormat:@",'%@'",pricetype];
  6271. }
  6272. }
  6273. if ([price isEqualToString:@"Display All"]) {
  6274. price = [NSString stringWithFormat:@""];
  6275. } else if([price containsString:@"+"]){
  6276. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6277. price_min = [price doubleValue];
  6278. 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];
  6279. } else {
  6280. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6281. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6282. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6283. 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];
  6284. }
  6285. } else {
  6286. price = @"";
  6287. }
  6288. // sold_by_qty : Sold in quantities of %@
  6289. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6290. NSString *qty = params[@"sold_by_qty"];
  6291. if ([qty isEqualToString:@"Display All"]) {
  6292. qty = @"";
  6293. } else {
  6294. qty = [self translateSingleQuote:qty];
  6295. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6296. }
  6297. // model name;
  6298. NSString* modelname =params[@"modelName"];
  6299. if(modelname.length==0)
  6300. {
  6301. modelname=@"";
  6302. }
  6303. else
  6304. {
  6305. modelname = modelname.lowercaseString;
  6306. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6307. }
  6308. //modelDescription
  6309. NSString* modelDescription =params[@"modelDescription"];
  6310. if(modelDescription.length==0)
  6311. {
  6312. modelDescription=@"";
  6313. }
  6314. else
  6315. {
  6316. modelDescription = modelDescription.lowercaseString;
  6317. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6318. }
  6319. // cate
  6320. // category = [self translateSingleQuote:category];
  6321. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6322. // cate mutiple selection
  6323. NSString *category_id = params[@"category"];
  6324. NSMutableArray *cate_id_array = nil;
  6325. NSMutableString *cateWhere = [NSMutableString string];
  6326. if ([category_id isEqualToString:@""] || !category_id) {
  6327. [cateWhere appendString:@"1 = 1"];
  6328. } else {
  6329. if ([category_id containsString:@","]) {
  6330. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6331. } else {
  6332. cate_id_array = [@[category_id] mutableCopy];
  6333. }
  6334. [cateWhere appendString:@"("];
  6335. for (int i = 0; i < cate_id_array.count; i++) {
  6336. if (i == 0) {
  6337. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6338. } else {
  6339. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6340. }
  6341. }
  6342. [cateWhere appendString:@")"];
  6343. }
  6344. // where bestseller > 0 order by bestseller desc
  6345. // sql query: alert availability(int) best_seller(int) price qty
  6346. 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];
  6347. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6348. }
  6349. DebugLog(@"offline category where: %@",where);
  6350. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6351. if (!params[@"user"]) {
  6352. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6353. }
  6354. [ret setValue:filter forKey:@"filter"];
  6355. DebugLog(@"offline_category sql:%@",sqlQuery);
  6356. sqlite3_stmt * statement;
  6357. [ret setValue:@"2" forKey:@"result"];
  6358. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6359. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6360. // int count=0;
  6361. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6362. {
  6363. int i=0;
  6364. while (sqlite3_step(statement) == SQLITE_ROW)
  6365. {
  6366. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6367. char *name = (char*)sqlite3_column_text(statement, 0);
  6368. if(name==nil)
  6369. name="";
  6370. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6371. char *description = (char*)sqlite3_column_text(statement, 1);
  6372. if(description==nil)
  6373. description="";
  6374. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6375. int product_id = sqlite3_column_int(statement, 2);
  6376. int wid = sqlite3_column_int(statement, 3);
  6377. int closeout = sqlite3_column_int(statement, 4);
  6378. int cid = sqlite3_column_int(statement, 5);
  6379. int wisdelete = sqlite3_column_int(statement, 6);
  6380. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6381. int more_color = sqlite3_column_int(statement, 8);
  6382. // Defaul Category ID
  6383. __block NSString *categoryID = nil;
  6384. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6385. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6386. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6387. if(default_category==nil)
  6388. default_category="";
  6389. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6390. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6391. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6392. categoryID = nsdefault_category;
  6393. }];
  6394. if (!categoryID.length) {
  6395. NSString *cateIDs = params[@"category"];
  6396. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6397. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6398. for (NSString *cateID in requestCategoryArr) {
  6399. BOOL needBreak = NO;
  6400. for (NSString *itemCateIDBox in itemCategoryArr) {
  6401. if (itemCateIDBox.length > 4) {
  6402. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6403. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6404. if ([itemCategoryID isEqualToString:cateID]) {
  6405. needBreak = YES;
  6406. categoryID = itemCategoryID;
  6407. break;
  6408. }
  6409. }
  6410. }
  6411. if (needBreak) {
  6412. break;
  6413. }
  6414. }
  6415. }
  6416. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6417. if(wid !=0 && wisdelete != 1)
  6418. [item setValue:@"true" forKey:@"wish_exists"];
  6419. else
  6420. [item setValue:@"false" forKey:@"wish_exists"];
  6421. if(closeout==0)
  6422. [item setValue:@"false" forKey:@"is_closeout"];
  6423. else
  6424. [item setValue:@"true" forKey:@"is_closeout"];
  6425. if(cid==0)
  6426. [item setValue:@"false" forKey:@"cart_exists"];
  6427. else
  6428. [item setValue:@"true" forKey:@"cart_exists"];
  6429. if (more_color == 0) {
  6430. [item setObject:@(false) forKey:@"more_color"];
  6431. } else if (more_color == 1) {
  6432. [item setObject:@(true) forKey:@"more_color"];
  6433. }
  6434. [item addEntriesFromDictionary:imgjson];
  6435. // [item setValue:nsurl forKey:@"img"];
  6436. [item setValue:nsname forKey:@"name"];
  6437. [item setValue:nsdescription forKey:@"description"];
  6438. if (categoryID) {
  6439. [item setValue:categoryID forKey:@"item_category_id"];
  6440. }
  6441. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6442. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6443. i++;
  6444. }
  6445. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6446. [ret setObject:items forKey:@"items"];
  6447. sqlite3_finalize(statement);
  6448. } else {
  6449. DebugLog(@"nothing...");
  6450. }
  6451. DebugLog(@"count:%d",count);
  6452. [iSalesDB close_db:db];
  6453. #ifdef DEBUG
  6454. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6455. #endif
  6456. return ret;
  6457. }
  6458. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6459. {
  6460. return [self categoryList:params limited:YES];
  6461. }
  6462. # pragma mark item search
  6463. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6464. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6465. // assert(params[@"order_code"]);
  6466. // params[@"user"]
  6467. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6468. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6469. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6470. // category
  6471. NSDictionary *category_menu = [self offline_category_menu];
  6472. [filter setValue:category_menu forKey:@"category"];
  6473. NSString* where= nil;
  6474. NSString* orderby= @"m.name";
  6475. if (!filterSearch) {
  6476. int covertype = [[params valueForKey:@"covertype"] intValue];
  6477. switch (covertype) {
  6478. case 0:
  6479. {
  6480. where=@"m.category like'%%#005#%%'";
  6481. break;
  6482. }
  6483. case 1:
  6484. {
  6485. where=@"m.alert like '%QS%'";
  6486. break;
  6487. }
  6488. case 2:
  6489. {
  6490. where=@"m.availability>0";
  6491. break;
  6492. }
  6493. case 3:
  6494. {
  6495. where=@"m.best_seller>0";
  6496. orderby=@"m.best_seller desc,m.name asc";
  6497. break;
  6498. }
  6499. default:
  6500. where=@"1=1";
  6501. break;
  6502. }
  6503. }
  6504. int limit = [[params valueForKey:@"limit"] intValue];
  6505. int offset = [[params valueForKey:@"offset"] intValue];
  6506. NSString *limit_str = @"";
  6507. if (limited) {
  6508. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6509. }
  6510. sqlite3 *db = [iSalesDB get_db];
  6511. // [iSalesDB AddExFunction:db];
  6512. int count;
  6513. NSString *sqlQuery = nil;
  6514. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6515. 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];
  6516. double price_min = 0;
  6517. double price_max = 0;
  6518. if (filterSearch) {
  6519. // alert
  6520. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6521. NSString *alert = params[@"alert"];
  6522. if ([alert isEqualToString:@"Display All"]) {
  6523. alert = [NSString stringWithFormat:@""];
  6524. } else {
  6525. alert = [self translateSingleQuote:alert];
  6526. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6527. }
  6528. // available
  6529. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6530. NSString *available = params[@"available"];
  6531. NSString *available_condition;
  6532. if ([available isEqualToString:@"Display All"]) {
  6533. available_condition = @"";
  6534. } else if ([available isEqualToString:@"Available Now"]) {
  6535. available_condition = @"and availability > 0";
  6536. } else {
  6537. available_condition = @"and availability == 0";
  6538. }
  6539. // best seller
  6540. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6541. NSString *best_seller = @"";
  6542. NSString *order_best_seller = @"m.name asc";
  6543. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6544. best_seller = @"and best_seller > 0";
  6545. order_best_seller = @"m.best_seller desc,m.name asc";
  6546. }
  6547. // price
  6548. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6549. NSString *price = params[@"price"];
  6550. price_min = 0;
  6551. price_max = MAXFLOAT;
  6552. if (params[@"user"]) {
  6553. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6554. NSMutableString *priceName = [NSMutableString string];
  6555. for (int i = 0; i < priceTypeArray.count; i++) {
  6556. NSString *pricetype = priceTypeArray[i];
  6557. pricetype = [self translateSingleQuote:pricetype];
  6558. if (i == 0) {
  6559. [priceName appendFormat:@"'%@'",pricetype];
  6560. } else {
  6561. [priceName appendFormat:@",'%@'",pricetype];
  6562. }
  6563. }
  6564. if ([price isEqualToString:@"Display All"]) {
  6565. price = [NSString stringWithFormat:@""];
  6566. } else if([price containsString:@"+"]){
  6567. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6568. price_min = [price doubleValue];
  6569. 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];
  6570. } else {
  6571. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6572. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6573. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6574. 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];
  6575. }
  6576. } else {
  6577. price = @"";
  6578. }
  6579. // sold_by_qty : Sold in quantities of %@
  6580. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6581. NSString *qty = params[@"sold_by_qty"];
  6582. if ([qty isEqualToString:@"Display All"]) {
  6583. qty = @"";
  6584. } else {
  6585. qty = [self translateSingleQuote:qty];
  6586. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6587. }
  6588. // model name;
  6589. NSString* modelname =params[@"modelName"];
  6590. if(modelname.length==0)
  6591. {
  6592. modelname=@"";
  6593. }
  6594. else
  6595. {
  6596. modelname = modelname.lowercaseString;
  6597. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6598. }
  6599. //modelDescription
  6600. NSString* modelDescription =params[@"modelDescription"];
  6601. if(modelDescription.length==0)
  6602. {
  6603. modelDescription=@"";
  6604. }
  6605. else
  6606. {
  6607. modelDescription = modelDescription.lowercaseString;
  6608. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6609. }
  6610. //lower(description) like'%%%@%%'
  6611. // category
  6612. NSString *category_id = params[@"ctgId"];
  6613. NSMutableArray *cate_id_array = nil;
  6614. NSMutableString *cateWhere = [NSMutableString string];
  6615. if ([category_id isEqualToString:@""] || !category_id) {
  6616. [cateWhere appendString:@"1 = 1"];
  6617. } else {
  6618. if ([category_id containsString:@","]) {
  6619. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6620. } else {
  6621. cate_id_array = [@[category_id] mutableCopy];
  6622. }
  6623. /*-----------*/
  6624. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6625. [cateWhere appendString:@"("];
  6626. for (int i = 0; i < cate_id_array.count; i++) {
  6627. for (NSString *key0 in cateDic.allKeys) {
  6628. if ([key0 containsString:@"category_"]) {
  6629. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6630. for (NSString *key1 in category0.allKeys) {
  6631. if ([key1 containsString:@"category_"]) {
  6632. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6633. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6634. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6635. cate_id_array[i] = [category1 objectForKey:@"id"];
  6636. if (i == 0) {
  6637. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6638. } else {
  6639. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6640. }
  6641. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6642. [category0 setValue:category1 forKey:key1];
  6643. [cateDic setValue:category0 forKey:key0];
  6644. }
  6645. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6646. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6647. cate_id_array[i] = [category0 objectForKey:@"id"];
  6648. if (i == 0) {
  6649. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6650. } else {
  6651. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6652. }
  6653. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6654. [cateDic setValue:category0 forKey:key0];
  6655. }
  6656. }
  6657. }
  6658. }
  6659. }
  6660. }
  6661. [cateWhere appendString:@")"];
  6662. [filter setValue:cateDic forKey:@"category"];
  6663. }
  6664. // where bestseller > 0 order by bestseller desc
  6665. // sql query: alert availability(int) best_seller(int) price qty
  6666. 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];
  6667. // count
  6668. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6669. }
  6670. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6671. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6672. if (!params[@"user"]) {
  6673. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6674. }
  6675. [ret setValue:filter forKey:@"filter"];
  6676. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6677. sqlite3_stmt * statement;
  6678. [ret setValue:@"2" forKey:@"result"];
  6679. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6680. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6681. // int count=0;
  6682. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6683. {
  6684. int i=0;
  6685. while (sqlite3_step(statement) == SQLITE_ROW)
  6686. {
  6687. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6688. char *name = (char*)sqlite3_column_text(statement, 0);
  6689. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6690. char *description = (char*)sqlite3_column_text(statement, 1);
  6691. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6692. int product_id = sqlite3_column_int(statement, 2);
  6693. int wid = sqlite3_column_int(statement, 3);
  6694. int closeout = sqlite3_column_int(statement, 4);
  6695. int cid = sqlite3_column_int(statement, 5);
  6696. int wisdelete = sqlite3_column_int(statement, 6);
  6697. int more_color = sqlite3_column_int(statement, 7);
  6698. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6699. if(wid !=0 && wisdelete != 1)
  6700. [item setValue:@"true" forKey:@"wish_exists"];
  6701. else
  6702. [item setValue:@"false" forKey:@"wish_exists"];
  6703. if(closeout==0)
  6704. [item setValue:@"false" forKey:@"is_closeout"];
  6705. else
  6706. [item setValue:@"true" forKey:@"is_closeout"];
  6707. if(cid==0)
  6708. [item setValue:@"false" forKey:@"cart_exists"];
  6709. else
  6710. [item setValue:@"true" forKey:@"cart_exists"];
  6711. if (more_color == 0) {
  6712. [item setObject:@(false) forKey:@"more_color"];
  6713. } else if (more_color == 1) {
  6714. [item setObject:@(true) forKey:@"more_color"];
  6715. }
  6716. [item addEntriesFromDictionary:imgjson];
  6717. // [item setValue:nsurl forKey:@"img"];
  6718. [item setValue:nsname forKey:@"fash_name"];
  6719. [item setValue:nsdescription forKey:@"description"];
  6720. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6721. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6722. i++;
  6723. }
  6724. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6725. [ret setObject:items forKey:@"items"];
  6726. sqlite3_finalize(statement);
  6727. }
  6728. [iSalesDB close_db:db];
  6729. #ifdef DEBUG
  6730. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6731. #endif
  6732. return ret;
  6733. }
  6734. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6735. {
  6736. return [self itemsearch:params limited:YES];
  6737. }
  6738. #pragma mark order detail
  6739. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6740. if (str1.length == 0) {
  6741. str1 = @"&nbsp";
  6742. }
  6743. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6744. return str;
  6745. }
  6746. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6747. {
  6748. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6749. [fromformatter setDateFormat:from];
  6750. NSDate *date = [fromformatter dateFromString:datetime];
  6751. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6752. [toformatter setDateFormat:to];
  6753. NSString * ret = [toformatter stringFromDate:date];
  6754. return ret;
  6755. }
  6756. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6757. // 把毫秒去掉
  6758. if ([dateTime containsString:@"."]) {
  6759. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6760. }
  6761. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6762. formatter.dateFormat = formate;
  6763. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6764. NSDate *date = [formatter dateFromString:dateTime];
  6765. formatter.dateFormat = newFormate;
  6766. NSString *result = [formatter stringFromDate:date];
  6767. return result ? result : @"";
  6768. }
  6769. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6770. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6771. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6772. }
  6773. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6774. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6775. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6776. }
  6777. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6778. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6779. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6780. }
  6781. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6782. {
  6783. assert(params[@"mode"]!=nil);
  6784. assert(params[@"user"]!=nil);
  6785. DebugLog(@"offline oderdetail params: %@",params);
  6786. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6787. int orderId = [params[@"orderId"] intValue];
  6788. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6789. // decrypt card number and card security code
  6790. // 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 ];
  6791. 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];
  6792. sqlite3 *db = [iSalesDB get_db];
  6793. sqlite3_stmt * statement;
  6794. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6795. NSString *nssoid = @"";
  6796. NSString* orderinfo = @"";
  6797. NSString *moreInfo = @"";
  6798. double handlingFee = 0;
  6799. double payments_and_credist = 0;
  6800. double totalPrice = 0;
  6801. double shippingFee = 0;
  6802. double lift_gate = 0;
  6803. NSString *customer_contact = @"";
  6804. NSString *customer_email = @"";
  6805. NSString *customer_fax = @"";
  6806. NSString *customer_phone = @"";
  6807. NSString *customer_city = @"";
  6808. NSString *customer_state = @"";
  6809. NSString *customer_zipcode = @"";
  6810. NSString *customer_country = @"";
  6811. BOOL must_call = NO;
  6812. double discount = 0;
  6813. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6814. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6815. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6816. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6817. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6818. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6819. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6820. {
  6821. if (sqlite3_step(statement) == SQLITE_ROW)
  6822. {
  6823. // int order_id = sqlite3_column_int(statement, 0);
  6824. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6825. // ret[@"sign_url"] = sign_url;
  6826. section_1 = @{
  6827. @"data":sign_url,
  6828. @"title":@"Signature",
  6829. @"type":@"sign_url"
  6830. }.mutableCopy;
  6831. [ret setObject:section_1 forKey:@"section_1"];
  6832. customer_contact = [self textAtColumn:52 statement:statement];
  6833. customer_email = [self textAtColumn:53 statement:statement];
  6834. customer_phone = [self textAtColumn:54 statement:statement];
  6835. customer_fax = [self textAtColumn:55 statement:statement];
  6836. customer_city = [self textAtColumn:60 statement:statement];
  6837. customer_state = [self textAtColumn:61 statement:statement];
  6838. customer_zipcode = [self textAtColumn:62 statement:statement];
  6839. customer_country = [self textAtColumn:63 statement:statement];
  6840. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6841. discount =sqlite3_column_double(statement, 65);
  6842. int offline_edit=sqlite3_column_int(statement, 56);
  6843. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6844. char *soid = (char*)sqlite3_column_text(statement, 1);
  6845. if(soid==nil)
  6846. soid= "";
  6847. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6848. // so#
  6849. ret[@"so#"] = nssoid;
  6850. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6851. if(poNumber==nil)
  6852. poNumber= "";
  6853. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6854. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6855. if(create_time==nil)
  6856. create_time= "";
  6857. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6858. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6859. int status = sqlite3_column_int(statement, 4);
  6860. int erpStatus = sqlite3_column_int(statement, 49);
  6861. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6862. // status
  6863. if (status > 1 && status != 3) {
  6864. status = erpStatus;
  6865. } else if (status == 3) {
  6866. status = 15;
  6867. }
  6868. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6869. ret[@"order_status"] = nsstatus;
  6870. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6871. if(company_name==nil)
  6872. company_name= "";
  6873. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6874. // company name
  6875. ret[@"company_name"] = nscompany_name;
  6876. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6877. if(customer_contact==nil)
  6878. customer_contact= "";
  6879. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6880. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6881. if(addr_1==nil)
  6882. addr_1="";
  6883. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6884. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6885. if(addr_2==nil)
  6886. addr_2="";
  6887. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6888. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6889. if(addr_3==nil)
  6890. addr_3="";
  6891. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6892. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6893. if(addr_4==nil)
  6894. addr_4="";
  6895. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6896. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6897. [arr_addr addObject:nsaddr_1];
  6898. [arr_addr addObject:nsaddr_2];
  6899. [arr_addr addObject:nsaddr_3];
  6900. [arr_addr addObject:nsaddr_4];
  6901. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6902. [arr_addr addObject:customer_state];
  6903. [arr_addr addObject:customer_zipcode];
  6904. [arr_addr addObject:customer_country];
  6905. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6906. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6907. char *logist = (char*)sqlite3_column_text(statement, 11);
  6908. if(logist==nil)
  6909. logist= "";
  6910. NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  6911. if (/*status == -11 || */status == 10 || status == 11) {
  6912. DebugLogist = [self textAtColumn:59 statement:statement];
  6913. };
  6914. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6915. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6916. shipping = @"Shipping To Be Quoted";
  6917. } else {
  6918. shippingFee = sqlite3_column_double(statement, 12);
  6919. }
  6920. // Shipping
  6921. // ret[@"Shipping"] = shipping;
  6922. NSDictionary *shipping_item = @{
  6923. @"title":@"Shipping",
  6924. @"value":shipping
  6925. };
  6926. [price_data setObject:shipping_item forKey:@"item_1"];
  6927. int have_lift_gate = sqlite3_column_int(statement, 17);
  6928. lift_gate = sqlite3_column_double(statement, 13);
  6929. // Liftgate Fee(No loading dock)
  6930. if (!have_lift_gate) {
  6931. lift_gate = 0;
  6932. }
  6933. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6934. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6935. if (sqlite3_column_int(statement, 57)) {
  6936. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6937. liftgate_value = @"Shipping To Be Quoted";
  6938. }
  6939. NSDictionary *liftgate_item = @{
  6940. @"title":@"Liftgate Fee(No loading dock)",
  6941. @"value":liftgate_value
  6942. };
  6943. [price_data setObject:liftgate_item forKey:@"item_2"];
  6944. // if ([DebugLogist isEqualToString:@"WILL CALL"]) {
  6945. // [ret removeObjectForKey:@"Shipping"];
  6946. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6947. // }
  6948. //
  6949. // if (have_lift_gate) {
  6950. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6951. // }
  6952. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6953. if(general_notes==nil)
  6954. general_notes= "";
  6955. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6956. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6957. if(internal_notes==nil)
  6958. internal_notes= "";
  6959. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6960. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6961. if(payment_type==nil)
  6962. payment_type= "";
  6963. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6964. // order info
  6965. orderinfo = [self textFileName:@"order_info.html"];
  6966. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6967. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6968. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6969. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6970. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6971. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6972. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6973. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  6974. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6975. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6976. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6977. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6978. NSString *payment = nil;
  6979. // id -> show
  6980. __block NSString *show_pay_type = nspayment_type;
  6981. [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) {
  6982. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6983. if (show_typ_ch != NULL) {
  6984. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6985. }
  6986. }];
  6987. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6988. {
  6989. payment = [self textFileName:@"creditcardpayment.html"];
  6990. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6991. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6992. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6993. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6994. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6995. NSString *card_type = [self textAtColumn:42 statement:statement];
  6996. if (card_type.length > 0) { // 显示星号
  6997. card_type = @"****";
  6998. }
  6999. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7000. if (card_number.length > 0 && card_number.length > 4) {
  7001. for (int i = 0; i < card_number.length - 4; i++) {
  7002. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7003. }
  7004. } else {
  7005. card_number = @"";
  7006. }
  7007. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7008. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7009. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7010. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7011. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7012. card_expiration = @"****";
  7013. }
  7014. NSString *card_city = [self textAtColumn:46 statement:statement];
  7015. NSString *card_state = [self textAtColumn:47 statement:statement];
  7016. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7017. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7018. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7019. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7020. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7021. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7022. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7023. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7024. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7025. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7026. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7027. }
  7028. else
  7029. {
  7030. payment=[self textFileName:@"normalpayment.html"];
  7031. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7032. }
  7033. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7034. ret[@"result"]= [NSNumber numberWithInt:2];
  7035. // more info
  7036. moreInfo = [self textFileName:@"more_info.html"];
  7037. /*****ship to******/
  7038. // ShipToCompany_or_&nbsp
  7039. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7040. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7041. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7042. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7043. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7044. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7045. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7046. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7047. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7048. /*****ship from******/
  7049. // ShipFromCompany_or_&nbsp
  7050. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7051. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7052. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7053. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7054. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7055. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7056. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7057. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7058. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7059. /*****freight to******/
  7060. // FreightBillToCompany_or_&nbsp
  7061. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7062. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7063. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7064. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7065. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7066. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7067. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7068. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7069. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7070. /*****merchandise to******/
  7071. // MerchandiseBillToCompany_or_&nbsp
  7072. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7073. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7074. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7075. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7076. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7077. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7078. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7079. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7080. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7081. /*****return to******/
  7082. // ReturnToCompany_or_&nbsp
  7083. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7084. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7085. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7086. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7087. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7088. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7089. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7090. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7091. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7092. //
  7093. // DebugLog(@"more info : %@",moreInfo);
  7094. // handling fee
  7095. handlingFee = sqlite3_column_double(statement, 33);
  7096. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7097. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7098. if (sqlite3_column_int(statement, 58)) {
  7099. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7100. handling_fee_value = @"Shipping To Be Quoted";
  7101. }
  7102. NSDictionary *handling_fee_item = @{
  7103. @"title":@"Handling Fee",
  7104. @"value":handling_fee_value
  7105. };
  7106. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7107. //
  7108. // customer info
  7109. customerID = [self textAtColumn:36 statement:statement];
  7110. // mode
  7111. ret[@"mode"] = params[@"mode"];
  7112. // model_count
  7113. ret[@"model_count"] = @(0);
  7114. }
  7115. sqlite3_finalize(statement);
  7116. }
  7117. [iSalesDB close_db:db];
  7118. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7119. // "customer_email" = "Shui Hu";
  7120. // "customer_fax" = "";
  7121. // "customer_first_name" = F;
  7122. // "customer_last_name" = L;
  7123. // "customer_name" = ",da He Xiang Dong Liu A";
  7124. // "customer_phone" = "Hey Xuan Feng";
  7125. contactInfo[@"customer_phone"] = customer_phone;
  7126. contactInfo[@"customer_fax"] = customer_fax;
  7127. contactInfo[@"customer_email"] = customer_email;
  7128. NSString *first_name = @"";
  7129. NSString *last_name = @"";
  7130. if ([customer_contact isEqualToString:@""]) {
  7131. } else if ([customer_contact containsString:@" "]) {
  7132. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7133. first_name = [customer_contact substringToIndex:first_space_index];
  7134. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7135. }
  7136. contactInfo[@"customer_first_name"] = first_name;
  7137. contactInfo[@"customer_last_name"] = last_name;
  7138. ret[@"customerInfo"] = contactInfo;
  7139. // models
  7140. if (nssoid) {
  7141. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7142. __block double TotalCuft = 0;
  7143. __block double TotalWeight = 0;
  7144. __block int TotalCarton = 0;
  7145. __block double allItemPrice = 0;
  7146. 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];
  7147. sqlite3 *db1 = [iSalesDB get_db];
  7148. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7149. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7150. int product_id = sqlite3_column_int(stmt, 0);
  7151. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7152. int item_id = sqlite3_column_int(stmt, 7);
  7153. NSString* Price=nil;
  7154. if(str_price==nil)
  7155. {
  7156. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7157. if(price==nil)
  7158. Price=@"No Price.";
  7159. else
  7160. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7161. }
  7162. else
  7163. {
  7164. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7165. }
  7166. double discount = sqlite3_column_double(stmt, 2);
  7167. int item_count = sqlite3_column_int(stmt, 3);
  7168. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7169. NSString *nsline_note=nil;
  7170. if(line_note!=nil)
  7171. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7172. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7173. // NSString *nsname = nil;
  7174. // if(name!=nil)
  7175. // nsname= [[NSString alloc]initWithUTF8String:name];
  7176. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7177. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7178. // NSString *nsdescription=nil;
  7179. // if(description!=nil)
  7180. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7181. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7182. // int stockUom = sqlite3_column_int(stmt, 8);
  7183. // int _id = sqlite3_column_int(stmt, 9);
  7184. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7185. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7186. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7187. int carton=[bsubtotaljson[@"carton"] intValue];
  7188. TotalCuft += cuft;
  7189. TotalWeight += weight;
  7190. TotalCarton += carton;
  7191. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7192. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7193. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7194. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7195. itemjson[@"note"]=nsline_note;
  7196. itemjson[@"origin_price"] = Price;
  7197. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7198. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7199. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7200. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7201. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7202. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7203. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7204. if(itemjson[@"combine"] != nil)
  7205. {
  7206. // int citem=0;
  7207. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7208. for(int bc=0;bc<bcount;bc++)
  7209. {
  7210. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7211. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7212. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7213. subTotal += uprice * modulus * item_count;
  7214. }
  7215. }
  7216. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7217. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7218. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7219. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7220. itemjson[@"type"] = @"order_item";
  7221. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7222. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7223. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7224. allItemPrice += subTotal;
  7225. }];
  7226. section_3[@"data"] = model_data;
  7227. section_3[@"type"] = @"sub_order";
  7228. section_3[@"title"] = @"Models";
  7229. section_3[@"switch"] = @(false);
  7230. ret[@"section_3"] = section_3;
  7231. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7232. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7234. // payments/Credits
  7235. // payments_and_credist = sqlite3_column_double(statement, 34);
  7236. payments_and_credist = allItemPrice;
  7237. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7238. NSDictionary *sub_total_item = @{
  7239. @"title":@"Sub-Total",
  7240. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7241. };
  7242. [price_data setObject:sub_total_item forKey:@"item_0"];
  7243. // // total
  7244. // totalPrice = sqlite3_column_double(statement, 35);
  7245. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7246. } else {
  7247. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7248. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7249. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7250. // payments/Credits
  7251. payments_and_credist = 0;
  7252. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7253. NSDictionary *sub_total_item = @{
  7254. @"title":@"Sub-Total",
  7255. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7256. };
  7257. [price_data setObject:sub_total_item forKey:@"item_0"];
  7258. }
  7259. // total
  7260. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7261. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7262. if(discount>0)
  7263. {
  7264. NSDictionary *total_item = @{
  7265. @"title":@"Total",
  7266. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist*(1-(discount/100.0))+ lift_gate + shippingFee + handlingFee]
  7267. };
  7268. NSDictionary *discount_item = @{
  7269. @"title":@"Order Discount",
  7270. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7271. };
  7272. [price_data setObject:discount_item forKey:@"item_4"];
  7273. [price_data setObject:total_item forKey:@"item_5"];
  7274. }
  7275. else
  7276. {
  7277. NSDictionary *total_item = @{
  7278. @"title":@"Total",
  7279. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7280. };
  7281. [price_data setObject:total_item forKey:@"item_4"];
  7282. }
  7283. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7284. section_4 = @{
  7285. @"data":price_data,
  7286. @"title":@"Price Info",
  7287. @"type":@"price_info"
  7288. }.mutableCopy;
  7289. ret[@"section_4"] = section_4;
  7290. // ret[@"order_info"]= orderinfo;
  7291. section_0 = @{
  7292. @"data":orderinfo,
  7293. @"title":@"Order Info",
  7294. @"type":@"order_info"
  7295. }.mutableCopy;
  7296. ret[@"section_0"] = section_0;
  7297. // ret[@"more_order_info"] = moreInfo;
  7298. section_2 = @{
  7299. @"data":moreInfo,
  7300. @"title":@"More Info",
  7301. @"type":@"more_order_info"
  7302. }.mutableCopy;
  7303. ret[@"section_2"] = section_2;
  7304. ret[@"count"] = @(5);
  7305. return [RAConvertor dict2data:ret];
  7306. }
  7307. #pragma mark order list
  7308. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7309. NSString *nsstatus = @"";
  7310. switch (status) {
  7311. case 0:
  7312. {
  7313. nsstatus=@"Temp Order";
  7314. break;
  7315. }
  7316. case 1:
  7317. {
  7318. nsstatus=@"Saved Order";
  7319. break;
  7320. }
  7321. case 2: {
  7322. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7323. break;
  7324. }
  7325. case 3:
  7326. case 15:
  7327. {
  7328. nsstatus=@"Cancelled";
  7329. break;
  7330. }
  7331. case 10:
  7332. {
  7333. nsstatus=@"Quote Submitted";
  7334. break;
  7335. }
  7336. case 11:
  7337. {
  7338. nsstatus=@"Sales Order Submitted";
  7339. break;
  7340. }
  7341. case 12:
  7342. {
  7343. nsstatus=@"Processing";
  7344. break;
  7345. }
  7346. case 13:
  7347. {
  7348. nsstatus=@"Shipped";
  7349. break;
  7350. }
  7351. case 14:
  7352. {
  7353. nsstatus=@"Closed";
  7354. break;
  7355. }
  7356. case -11:
  7357. {
  7358. nsstatus = @"Ready For Submit";
  7359. break;
  7360. }
  7361. default:
  7362. break;
  7363. }
  7364. return nsstatus;
  7365. }
  7366. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7367. {
  7368. double total = 0;
  7369. __block double payments_and_credist = 0;
  7370. __block double allItemPrice = 0;
  7371. // sqlite3 *db1 = [iSalesDB get_db];
  7372. if (so_id) {
  7373. // 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];
  7374. 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];
  7375. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7376. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7377. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7378. int product_id = sqlite3_column_int(stmt, 0);
  7379. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7380. int discount = sqlite3_column_int(stmt, 2);
  7381. int item_count = sqlite3_column_int(stmt, 3);
  7382. // int item_id = sqlite3_column_int(stmt, 7);
  7383. int item_id = sqlite3_column_int(stmt, 4);
  7384. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7385. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7386. NSString* Price=nil;
  7387. if(str_price==nil)
  7388. {
  7389. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7390. if(price==nil)
  7391. Price=@"No Price.";
  7392. else
  7393. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7394. }
  7395. else
  7396. {
  7397. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7398. }
  7399. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7400. itemjson[@"The unit price"]=Price;
  7401. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7402. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7403. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7404. if(itemjson[@"combine"] != nil)
  7405. {
  7406. // int citem=0;
  7407. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7408. for(int bc=0;bc<bcount;bc++)
  7409. {
  7410. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7411. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7412. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7413. subTotal += uprice * modulus * item_count;
  7414. }
  7415. }
  7416. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7417. allItemPrice += subTotal;
  7418. }];
  7419. payments_and_credist = allItemPrice;
  7420. } else {
  7421. // payments/Credits
  7422. payments_and_credist = 0;
  7423. }
  7424. // lift_gate handlingFee shippingFee
  7425. __block double lift_gate = 0;
  7426. __block double handlingFee = 0;
  7427. __block double shippingFee = 0;
  7428. __block double order_discount = 0;
  7429. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7430. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7431. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7432. if (have_lift_gate) {
  7433. lift_gate = sqlite3_column_double(stmt, 1);
  7434. }
  7435. handlingFee = sqlite3_column_double(stmt, 2);
  7436. shippingFee = sqlite3_column_double(stmt, 3);
  7437. order_discount = sqlite3_column_double(stmt, 4);
  7438. }];
  7439. if(order_discount>0)
  7440. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7441. // total
  7442. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7443. if (close) {
  7444. [iSalesDB close_db:db1];
  7445. }
  7446. return total;
  7447. }
  7448. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7449. {
  7450. assert(params[@"user"]!=nil);
  7451. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7452. int limit = [[params valueForKey:@"limit"] intValue];
  7453. int offset = [[params valueForKey:@"offset"] intValue];
  7454. NSString* keyword = [params valueForKey:@"keyWord"];
  7455. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7456. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7457. NSString* where=@"1 = 1";
  7458. if(keyword.length>0)
  7459. 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]];
  7460. if (orderStatus.length > 0) {
  7461. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7462. if (order_status_array.count == 1) {
  7463. int status_value = [[order_status_array firstObject] intValue];
  7464. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7465. if (status_value == 15 || status_value == 3) {
  7466. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7467. } else {
  7468. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7469. }
  7470. } else {
  7471. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7472. }
  7473. } else if (order_status_array.count > 1) {
  7474. for (int i = 0; i < order_status_array.count;i++) {
  7475. NSString *status = order_status_array[i];
  7476. NSString *condition = @" or";
  7477. if (i == 0) {
  7478. condition = @" and (";
  7479. }
  7480. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7481. int status_value = [status intValue];
  7482. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7483. if (status_value == 15 || status_value == 3) {
  7484. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7485. } else {
  7486. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7487. }
  7488. } else {
  7489. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7490. }
  7491. }
  7492. where = [where stringByAppendingString:@" )"];
  7493. }
  7494. }
  7495. 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];
  7496. // DebugLog(@"order list sql: %@",sqlQuery);
  7497. sqlite3 *db = [iSalesDB get_db];
  7498. sqlite3_stmt * statement;
  7499. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7500. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7501. {
  7502. int count=0;
  7503. while (sqlite3_step(statement) == SQLITE_ROW)
  7504. {
  7505. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7506. int order_id = sqlite3_column_double(statement, 0);
  7507. char *soid = (char*)sqlite3_column_text(statement, 1);
  7508. if(soid==nil)
  7509. soid= "";
  7510. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7511. int status = sqlite3_column_double(statement, 2);
  7512. int erpStatus = sqlite3_column_double(statement, 9);
  7513. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7514. if(sales_rep==nil)
  7515. sales_rep= "";
  7516. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7517. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7518. if(create_by==nil)
  7519. create_by= "";
  7520. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7521. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7522. if(company_name==nil)
  7523. company_name= "";
  7524. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7525. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7526. if(create_time==nil)
  7527. create_time= "";
  7528. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7529. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7530. // double total_price = sqlite3_column_double(statement, 7);
  7531. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7532. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7533. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7534. int offline_edit = sqlite3_column_int(statement, 10);
  7535. // ": "JH",
  7536. // "": "$8307.00",
  7537. // "": "MOB1608050001",
  7538. // "": "ArpithaT",
  7539. // "": "1st Stage Property Transformations",
  7540. // "": "JANICE SUTTON",
  7541. // "": 2255,
  7542. // "": "08/02/2016 09:49:18",
  7543. // "": 1,
  7544. // "": "Saved Order"
  7545. // "": "1470384050483",
  7546. // "model_count": "6 / 28"
  7547. item[@"sales_rep"]= nssales_rep;
  7548. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7549. item[@"so#"]= nssoid;
  7550. item[@"create_by"]= nscreate_by;
  7551. item[@"customer_name"]= nscompany_name;
  7552. item[@"customer_contact"] = customer_contact;
  7553. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7554. item[@"purchase_time"]= nscreate_time;
  7555. int statusCode = status;
  7556. if (statusCode == 2) {
  7557. statusCode = erpStatus;
  7558. } else if (statusCode == 3) {
  7559. statusCode = 15;
  7560. }
  7561. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7562. item[@"order_status"]= nsstatus;
  7563. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7564. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7565. // item[@"model_count"]
  7566. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7567. count++;
  7568. }
  7569. ret[@"count"]= [NSNumber numberWithInt:count];
  7570. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7571. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7572. ret[@"result"]= [NSNumber numberWithInt:2];
  7573. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7574. ret[@"time_zone"] = @"PST";
  7575. sqlite3_finalize(statement);
  7576. }
  7577. 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];
  7578. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7579. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7580. [iSalesDB close_db:db];
  7581. return [RAConvertor dict2data:ret];
  7582. }
  7583. #pragma mark update gnotes
  7584. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7585. {
  7586. DebugLog(@"params: %@",params);
  7587. // comments = Meyoyoyoyoyoyoy;
  7588. // orderCode = MOB1608110001;
  7589. // password = 123456;
  7590. // user = EvanK;
  7591. 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]];
  7592. int ret = [iSalesDB execSql:sql];
  7593. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7594. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7595. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7596. // [dic setValue:@"160409" forKey:@"min_ver"];
  7597. return [RAConvertor dict2data:dic];
  7598. }
  7599. #pragma mark move to wishlist
  7600. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7601. {
  7602. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7603. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7604. _id = [NSString stringWithFormat:@"(%@)",_id];
  7605. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7606. sqlite3 *db = [iSalesDB get_db];
  7607. __block NSString *product_id = @"";
  7608. __block NSString *item_count_str = @"";
  7609. // __block NSString *item_id = nil;
  7610. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7611. // product_id = [self textAtColumn:0 statement:stmt];
  7612. int item_count = sqlite3_column_int(stmt, 1);
  7613. // item_id = [self textAtColumn:2 statement:stmt];
  7614. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7615. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7616. if (p_id.length) {
  7617. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7618. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7619. }
  7620. }];
  7621. [iSalesDB close_db:db];
  7622. // 去除第一个,
  7623. if (product_id.length > 1) {
  7624. product_id = [product_id substringFromIndex:1];
  7625. }
  7626. if (item_count_str.length > 1) {
  7627. item_count_str = [item_count_str substringFromIndex:1];
  7628. }
  7629. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7630. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7631. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7632. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7633. sqlite3 *db1 = [iSalesDB get_db];
  7634. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7635. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7636. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7637. // 删除
  7638. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7639. int ret = [iSalesDB execSql:deleteSql db:db1];
  7640. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7641. [iSalesDB close_db:db1];
  7642. return [RAConvertor dict2data:dic];
  7643. }
  7644. #pragma mark cart delete
  7645. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7646. {
  7647. // cartItemId = 548;
  7648. // orderCode = MOB1608110001;
  7649. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7650. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7651. _id = [NSString stringWithFormat:@"(%@)",_id];
  7652. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7653. int ret = [iSalesDB execSql:sql];
  7654. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7655. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7656. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7657. // [dic setValue:@"160409" forKey:@"min_ver"];
  7658. return [RAConvertor dict2data:dic];
  7659. }
  7660. #pragma mark set order discount
  7661. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7662. //{
  7663. // DebugLog(@"cart set price params: %@",params);
  7664. // // "cartitem_id" = 1;
  7665. // // discount = "0.000000";
  7666. // // "item_note" = "";
  7667. // // price = "269.000000";
  7668. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7669. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7670. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7671. //// NSString *price = [self valueInParams:params key:@"price"];
  7672. //
  7673. // // bool badd_price_changed=false;
  7674. // // sqlite3* db=[iSalesDB get_db];
  7675. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7676. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7677. // // NSRange range;
  7678. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7679. // //
  7680. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7681. // // badd_price_changed=true;
  7682. // // [iSalesDB close_db:db];
  7683. // //
  7684. // // if(badd_price_changed)
  7685. // // {
  7686. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7687. // // }
  7688. // //
  7689. //
  7690. //
  7691. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7692. //
  7693. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7694. //
  7695. // int ret = [iSalesDB execSql:sql];
  7696. //
  7697. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7698. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7699. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7700. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7701. //
  7702. //// return [RAConvertor dict2data:dic];
  7703. // return dic;
  7704. //
  7705. //}
  7706. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7707. {
  7708. DebugLog(@"order set discount params: %@",params);
  7709. // "cartitem_id" = 1;
  7710. // discount = "0.000000";
  7711. // "item_note" = "";
  7712. // price = "269.000000";
  7713. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7714. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7715. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7716. // NSString *price = [self valueInParams:params key:@"price"];
  7717. // bool badd_price_changed=false;
  7718. // sqlite3* db=[iSalesDB get_db];
  7719. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7720. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7721. // NSRange range;
  7722. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7723. //
  7724. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7725. // badd_price_changed=true;
  7726. // [iSalesDB close_db:db];
  7727. //
  7728. // if(badd_price_changed)
  7729. // {
  7730. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7731. // }
  7732. //
  7733. // sqlite3 *db = [iSalesDB get_db];
  7734. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7735. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7736. //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];
  7737. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7738. int ret = [iSalesDB execSql:sql];
  7739. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7740. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7741. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7742. // [dic setValue:@"160409" forKey:@"min_ver"];
  7743. // return [RAConvertor dict2data:dic];
  7744. return dic;
  7745. }
  7746. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7747. {
  7748. DebugLog(@"cart set price params: %@",params);
  7749. // "cartitem_id" = 1;
  7750. // discount = "0.000000";
  7751. // "item_note" = "";
  7752. // price = "269.000000";
  7753. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7754. NSString *notes = [self valueInParams:params key:@"item_note"];
  7755. NSString *discount = [self valueInParams:params key:@"discount"];
  7756. NSString *price = [self valueInParams:params key:@"price"];
  7757. // bool badd_price_changed=false;
  7758. // sqlite3* db=[iSalesDB get_db];
  7759. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7760. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7761. // NSRange range;
  7762. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7763. //
  7764. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7765. // badd_price_changed=true;
  7766. // [iSalesDB close_db:db];
  7767. //
  7768. // if(badd_price_changed)
  7769. // {
  7770. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7771. // }
  7772. //
  7773. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7774. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7775. int ret = [iSalesDB execSql:sql];
  7776. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7777. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7778. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7779. // [dic setValue:@"160409" forKey:@"min_ver"];
  7780. return [RAConvertor dict2data:dic];
  7781. }
  7782. #pragma mark set line notes
  7783. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7784. {
  7785. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7786. NSString *notes = [self valueInParams:params key:@"notes"];
  7787. notes = [self translateSingleQuote:notes];
  7788. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7789. int ret = [iSalesDB execSql:sql];
  7790. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7791. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7792. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7793. // [dic setValue:@"160409" forKey:@"min_ver"];
  7794. return [RAConvertor dict2data:dic];
  7795. }
  7796. #pragma mark set qty
  7797. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7798. {
  7799. assert(params[@"can_create_backorder"]!=nil);
  7800. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7801. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7802. int item_count = [params[@"inputInt"] intValue];
  7803. // 购买检查数量大于库存
  7804. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7805. if (!params[@"can_create_backorder"]) {
  7806. 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];
  7807. __block BOOL out_of_stock = NO;
  7808. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7809. int availability = sqlite3_column_int(stmt, 0);
  7810. if (availability < item_count) {
  7811. out_of_stock = YES;
  7812. }
  7813. }];
  7814. if (out_of_stock) { // 缺货
  7815. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7816. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7817. return [RAConvertor dict2data:dic];
  7818. }
  7819. }
  7820. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7821. int ret = [iSalesDB execSql:sql];
  7822. __block int item_id = 0;
  7823. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7824. item_id = sqlite3_column_int(stmt, 0);
  7825. }];
  7826. sqlite3 *db = [iSalesDB get_db];
  7827. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7828. [iSalesDB close_db:db];
  7829. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7830. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7831. // [dic setValue:@"160409" forKey:@"min_ver"];
  7832. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7833. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7834. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7835. return [RAConvertor dict2data:dic];
  7836. }
  7837. #pragma mark place order
  7838. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7839. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7840. if (aname) {
  7841. [dic setValue:aname forKey:@"aname"];
  7842. }
  7843. if (control) {
  7844. [dic setValue:control forKey:@"control"];
  7845. }
  7846. if (keyboard) {
  7847. [dic setValue:keyboard forKey:@"keyboard"];
  7848. }
  7849. if (name) {
  7850. [dic setValue:name forKey:@"name"];
  7851. }
  7852. if (value) {
  7853. [dic setValue:value forKey:@"value"];
  7854. }
  7855. return dic;
  7856. }
  7857. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7858. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7859. orderCode = [self translateSingleQuote:orderCode];
  7860. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7861. 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];
  7862. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7863. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7864. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7865. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7866. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7867. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7868. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7869. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7870. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7871. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7872. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7873. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7874. NSString *email = [self textAtColumn:11 statement:stmt];
  7875. NSString *phone = [self textAtColumn:12 statement:stmt];
  7876. NSString *fax = [self textAtColumn:13 statement:stmt];
  7877. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7878. NSString *city = [self textAtColumn:15 statement:stmt];
  7879. NSString *state = [self textAtColumn:16 statement:stmt];
  7880. NSString *country = [self textAtColumn:17 statement:stmt];
  7881. NSString *name = [self textAtColumn:18 statement:stmt];
  7882. // contact id
  7883. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7884. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7885. [contact_id_dic setValue:@"text" forKey:@"control"];
  7886. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7887. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7888. [contact_id_dic setValue:@"true" forKey:@"required"];
  7889. [contact_id_dic setValue:contact_id forKey:@"value"];
  7890. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7891. // business card
  7892. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7893. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7894. [business_card_dic setValue:@"img" forKey:@"control"];
  7895. [business_card_dic setValue:@"1" forKey:@"disable"];
  7896. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7897. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7898. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7899. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7900. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7901. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7902. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7903. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7904. // fax
  7905. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7906. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7907. // zipcode
  7908. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7909. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7910. // city
  7911. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7912. [customer_dic setValue:city_dic forKey:@"item_12"];
  7913. // state
  7914. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7915. [customer_dic setValue:state_dic forKey:@"item_13"];
  7916. // country
  7917. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7918. [customer_dic setValue:country_dic forKey:@"item_14"];
  7919. // company name
  7920. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7921. [customer_dic setValue:company_dic forKey:@"item_2"];
  7922. // addr_1
  7923. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7924. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7925. // addr_2
  7926. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7927. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7928. // addr_3
  7929. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7930. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7931. // addr_4
  7932. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7933. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7934. // Contact
  7935. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7936. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7937. // email
  7938. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7939. [customer_dic setValue:email_dic forKey:@"item_8"];
  7940. // phone
  7941. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7942. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7943. // title
  7944. [customer_dic setValue:@"Customer" forKey:@"title"];
  7945. // count
  7946. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7947. }];
  7948. // setting
  7949. NSDictionary *setting = params[@"setting"];
  7950. NSNumber *hide = setting[@"CustomerHide"];
  7951. [customer_dic setValue:hide forKey:@"hide"];
  7952. return customer_dic;
  7953. }
  7954. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7955. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7956. // setting
  7957. NSDictionary *setting = params[@"setting"];
  7958. NSNumber *hide = setting[@"ShipToHide"];
  7959. [dic setValue:hide forKey:@"hide"];
  7960. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7961. orderCode = [self translateSingleQuote:orderCode];
  7962. 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];
  7963. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7964. NSString *cid = [self textAtColumn:0 statement:stmt];
  7965. NSString *name = [self textAtColumn:1 statement:stmt];
  7966. NSString *ext = [self textAtColumn:2 statement:stmt];
  7967. NSString *contact = [self textAtColumn:3 statement:stmt];
  7968. NSString *email = [self textAtColumn:4 statement:stmt];
  7969. NSString *fax = [self textAtColumn:5 statement:stmt];
  7970. NSString *phone = [self textAtColumn:6 statement:stmt];
  7971. // count
  7972. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7973. // title
  7974. [dic setValue:@"Ship To" forKey:@"title"];
  7975. // choose
  7976. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7977. [choose_dic setValue:@"choose" forKey:@"aname"];
  7978. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7979. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7980. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7981. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7982. @"receive_contact" : @"customer_contact",
  7983. @"receive_email" : @"customer_email",
  7984. @"receive_ext" : @"customer_contact_ext",
  7985. @"receive_fax" : @"customer_fax",
  7986. @"receive_name" : @"customer_name",
  7987. @"receive_phone" : @"customer_phone"},
  7988. @"refresh" : [NSNumber numberWithInteger:1],
  7989. @"type" : @"pull"};
  7990. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7991. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7992. @"name" : @"Add new address",
  7993. @"refresh" : [NSNumber numberWithInteger:1],
  7994. @"value" : @"new_addr"
  7995. };
  7996. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7997. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7998. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7999. @"receive_contact" : @"customer_contact",
  8000. @"receive_email" : @"customer_email",
  8001. @"receive_ext" : @"customer_contact_ext",
  8002. @"receive_fax" : @"customer_fax",
  8003. @"receive_name" : @"customer_name",
  8004. @"receive_phone" : @"customer_phone"},
  8005. @"name" : @"select_ship_to",
  8006. @"refresh" : [NSNumber numberWithInteger:1],
  8007. @"value" : @"Sales_Order_Ship_To"};
  8008. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8009. [dic setValue:choose_dic forKey:@"item_0"];
  8010. // contact id
  8011. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8012. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8013. [contact_id_dic setValue:@"true" forKey:@"required"];
  8014. [dic setValue:contact_id_dic forKey:@"item_1"];
  8015. // company name
  8016. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8017. [dic setValue:company_name_dic forKey:@"item_2"];
  8018. // address
  8019. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8020. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8021. if ([required integerValue]) {
  8022. [ext_dic setValue:@"true" forKey:@"required"];
  8023. }
  8024. [dic setValue:ext_dic forKey:@"item_3"];
  8025. // contact
  8026. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8027. [dic setValue:contact_dic forKey:@"item_4"];
  8028. // phone
  8029. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8030. [dic setValue:phone_dic forKey:@"item_5"];
  8031. // fax
  8032. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8033. [dic setValue:fax_dic forKey:@"item_6"];
  8034. // email
  8035. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8036. [dic setValue:email_dic forKey:@"item_7"];
  8037. }];
  8038. return dic;
  8039. }
  8040. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8041. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8042. // setting
  8043. NSDictionary *setting = params[@"setting"];
  8044. NSNumber *hide = setting[@"ShipFromHide"];
  8045. [dic setValue:hide forKey:@"hide"];
  8046. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8047. orderCode = [self translateSingleQuote:orderCode];
  8048. 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];
  8049. 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';";
  8050. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8051. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8052. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8053. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8054. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8055. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8056. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8057. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8058. if (!cid.length) {
  8059. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8060. cid = [self textAtColumn:0 statement:statment];
  8061. name = [self textAtColumn:1 statement:statment];
  8062. ext = [self textAtColumn:2 statement:statment];
  8063. contact = [self textAtColumn:3 statement:statment];
  8064. email = [self textAtColumn:4 statement:statment];
  8065. fax = [self textAtColumn:5 statement:statment];
  8066. phone = [self textAtColumn:6 statement:statment];
  8067. }];
  8068. }
  8069. // count
  8070. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8071. // title
  8072. [dic setValue:@"Ship From" forKey:@"title"];
  8073. // hide
  8074. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8075. // choose
  8076. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8077. [choose_dic setValue:@"choose" forKey:@"aname"];
  8078. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8079. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8080. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8081. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8082. @"sender_contact" : @"customer_contact",
  8083. @"sender_email" : @"customer_email",
  8084. @"sender_ext" : @"customer_contact_ext",
  8085. @"sender_fax" : @"customer_fax",
  8086. @"sender_name" : @"customer_name",
  8087. @"sender_phone" : @"customer_phone"},
  8088. @"name" : @"select_cid",
  8089. @"refresh" : [NSNumber numberWithInteger:0],
  8090. @"value" : @"Sales_Order_Ship_From"};
  8091. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8092. [dic setValue:choose_dic forKey:@"item_0"];
  8093. // contact id
  8094. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8095. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8096. [contact_id_dic setValue:@"true" forKey:@"required"];
  8097. [dic setValue:contact_id_dic forKey:@"item_1"];
  8098. // company name
  8099. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8100. [dic setValue:company_name_dic forKey:@"item_2"];
  8101. // address
  8102. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8103. [dic setValue:ext_dic forKey:@"item_3"];
  8104. // contact
  8105. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8106. [dic setValue:contact_dic forKey:@"item_4"];
  8107. // phone
  8108. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8109. [dic setValue:phone_dic forKey:@"item_5"];
  8110. // fax
  8111. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8112. [dic setValue:fax_dic forKey:@"item_6"];
  8113. // email
  8114. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8115. [dic setValue:email_dic forKey:@"item_7"];
  8116. }];
  8117. return dic;
  8118. }
  8119. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8120. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8121. // setting
  8122. NSDictionary *setting = params[@"setting"];
  8123. NSNumber *hide = setting[@"FreightBillToHide"];
  8124. [dic setValue:hide forKey:@"hide"];
  8125. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8126. orderCode = [self translateSingleQuote:orderCode];
  8127. 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];
  8128. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8129. NSString *cid = [self textAtColumn:0 statement:stmt];
  8130. NSString *name = [self textAtColumn:1 statement:stmt];
  8131. NSString *ext = [self textAtColumn:2 statement:stmt];
  8132. NSString *contact = [self textAtColumn:3 statement:stmt];
  8133. NSString *email = [self textAtColumn:4 statement:stmt];
  8134. NSString *fax = [self textAtColumn:5 statement:stmt];
  8135. NSString *phone = [self textAtColumn:6 statement:stmt];
  8136. // count
  8137. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8138. // title
  8139. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8140. // hide
  8141. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8142. // choose
  8143. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8144. [choose_dic setValue:@"choose" forKey:@"aname"];
  8145. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8146. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8147. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8148. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8149. @"shipping_billto_contact" : @"receive_contact",
  8150. @"shipping_billto_email" : @"receive_email",
  8151. @"shipping_billto_ext" : @"receive_ext",
  8152. @"shipping_billto_fax" : @"receive_fax",
  8153. @"shipping_billto_name" : @"receive_name",
  8154. @"shipping_billto_phone" : @"receive_phone"},
  8155. @"type" : @"pull"};
  8156. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8157. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8158. @"type" : @"pull",
  8159. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8160. @"shipping_billto_contact" : @"customer_contact",
  8161. @"shipping_billto_email" : @"customer_email",
  8162. @"shipping_billto_ext" : @"customer_contact_ext",
  8163. @"shipping_billto_fax" : @"customer_fax",
  8164. @"shipping_billto_name" : @"customer_name",
  8165. @"shipping_billto_phone" : @"customer_phone"}
  8166. };
  8167. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8168. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8169. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8170. @"shipping_billto_contact" : @"sender_contact",
  8171. @"shipping_billto_email" : @"sender_email",
  8172. @"shipping_billto_ext" : @"sender_ext",
  8173. @"shipping_billto_fax" : @"sender_fax",
  8174. @"shipping_billto_name" : @"sender_name",
  8175. @"shipping_billto_phone" : @"sender_phone"},
  8176. @"type" : @"pull"
  8177. };
  8178. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8179. NSDictionary *select_freight_bill_to_dic = @{
  8180. @"aname" : @"Select freight bill to",
  8181. @"name" : @"select_cid",
  8182. @"refresh" : [NSNumber numberWithInteger:0],
  8183. @"value" : @"Sales_Order_Freight_Bill_To",
  8184. @"key_map" : @{
  8185. @"shipping_billto_cid" : @"customer_cid",
  8186. @"shipping_billto_contact" : @"customer_contact",
  8187. @"shipping_billto_email" : @"customer_email",
  8188. @"shipping_billto_ext" : @"customer_contact_ext",
  8189. @"shipping_billto_fax" : @"customer_fax",
  8190. @"shipping_billto_name" : @"customer_name",
  8191. @"shipping_billto_phone" : @"customer_phone"
  8192. }
  8193. };
  8194. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8195. [dic setValue:choose_dic forKey:@"item_0"];
  8196. // contact id
  8197. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8198. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8199. [contact_id_dic setValue:@"true" forKey:@"required"];
  8200. [dic setValue:contact_id_dic forKey:@"item_1"];
  8201. // company name
  8202. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8203. [dic setValue:company_name_dic forKey:@"item_2"];
  8204. // address
  8205. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8206. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8207. if ([ext_required integerValue]) {
  8208. [ext_dic setValue:@"true" forKey:@"required"];
  8209. }
  8210. [dic setValue:ext_dic forKey:@"item_3"];
  8211. // contact
  8212. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8213. [dic setValue:contact_dic forKey:@"item_4"];
  8214. // phone
  8215. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8216. [dic setValue:phone_dic forKey:@"item_5"];
  8217. // fax
  8218. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8219. [dic setValue:fax_dic forKey:@"item_6"];
  8220. // email
  8221. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8222. [dic setValue:email_dic forKey:@"item_7"];
  8223. }];
  8224. return dic;
  8225. }
  8226. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8227. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8228. // setting
  8229. NSDictionary *setting = params[@"setting"];
  8230. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8231. [dic setValue:hide forKey:@"hide"];
  8232. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8233. orderCode = [self translateSingleQuote:orderCode];
  8234. 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];
  8235. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8236. NSString *cid = [self textAtColumn:0 statement:stmt];
  8237. NSString *name = [self textAtColumn:1 statement:stmt];
  8238. NSString *ext = [self textAtColumn:2 statement:stmt];
  8239. NSString *contact = [self textAtColumn:3 statement:stmt];
  8240. NSString *email = [self textAtColumn:4 statement:stmt];
  8241. NSString *fax = [self textAtColumn:5 statement:stmt];
  8242. NSString *phone = [self textAtColumn:6 statement:stmt];
  8243. // count
  8244. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8245. // title
  8246. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8247. // hide
  8248. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8249. // choose
  8250. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8251. [choose_dic setValue:@"choose" forKey:@"aname"];
  8252. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8253. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8254. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8255. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8256. @"billing_contact" : @"receive_contact",
  8257. @"billing_email" : @"receive_email",
  8258. @"billing_ext" : @"receive_ext",
  8259. @"billing_fax" : @"receive_fax",
  8260. @"billing_name" : @"receive_name",
  8261. @"billing_phone" : @"receive_phone"},
  8262. @"type" : @"pull"};
  8263. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8264. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8265. @"type" : @"pull",
  8266. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8267. @"billing_contact" : @"customer_contact",
  8268. @"billing_email" : @"customer_email",
  8269. @"billing_ext" : @"customer_contact_ext",
  8270. @"billing_fax" : @"customer_fax",
  8271. @"billing_name" : @"customer_name",
  8272. @"billing_phone" : @"customer_phone"}
  8273. };
  8274. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8275. NSDictionary *select_bill_to_dic = @{
  8276. @"aname" : @"Select bill to",
  8277. @"name" : @"select_cid",
  8278. @"refresh" : [NSNumber numberWithInteger:0],
  8279. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8280. @"key_map" : @{
  8281. @"billing_cid" : @"customer_cid",
  8282. @"billing_contact" : @"customer_contact",
  8283. @"billing_email" : @"customer_email",
  8284. @"billing_ext" : @"customer_contact_ext",
  8285. @"billing_fax" : @"customer_fax",
  8286. @"billing_name" : @"customer_name",
  8287. @"billing_phone" : @"customer_phone"
  8288. }
  8289. };
  8290. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8291. [dic setValue:choose_dic forKey:@"item_0"];
  8292. // contact id
  8293. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8294. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8295. [contact_id_dic setValue:@"true" forKey:@"required"];
  8296. [dic setValue:contact_id_dic forKey:@"item_1"];
  8297. // company name
  8298. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8299. [dic setValue:company_name_dic forKey:@"item_2"];
  8300. // address
  8301. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8302. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8303. if ([ext_required integerValue]) {
  8304. [ext_dic setValue:@"true" forKey:@"required"];
  8305. }
  8306. [dic setValue:ext_dic forKey:@"item_3"];
  8307. // contact
  8308. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8309. [dic setValue:contact_dic forKey:@"item_4"];
  8310. // phone
  8311. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8312. [dic setValue:phone_dic forKey:@"item_5"];
  8313. // fax
  8314. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8315. [dic setValue:fax_dic forKey:@"item_6"];
  8316. // email
  8317. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8318. [dic setValue:email_dic forKey:@"item_7"];
  8319. }];
  8320. return dic;
  8321. }
  8322. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8323. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8324. // setting
  8325. NSDictionary *setting = params[@"setting"];
  8326. NSNumber *hide = setting[@"ReturnToHide"];
  8327. [dic setValue:hide forKey:@"hide"];
  8328. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8329. orderCode = [self translateSingleQuote:orderCode];
  8330. 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];
  8331. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8332. NSString *cid = [self textAtColumn:0 statement:stmt];
  8333. NSString *name = [self textAtColumn:1 statement:stmt];
  8334. NSString *ext = [self textAtColumn:2 statement:stmt];
  8335. NSString *contact = [self textAtColumn:3 statement:stmt];
  8336. NSString *email = [self textAtColumn:4 statement:stmt];
  8337. NSString *fax = [self textAtColumn:5 statement:stmt];
  8338. NSString *phone = [self textAtColumn:6 statement:stmt];
  8339. // count
  8340. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8341. // title
  8342. [dic setValue:@"Return To" forKey:@"title"];
  8343. // hide
  8344. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8345. // choose
  8346. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8347. [choose_dic setValue:@"choose" forKey:@"aname"];
  8348. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8349. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8350. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8351. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8352. @"returnto_contact" : @"sender_contact",
  8353. @"returnto_email" : @"sender_email",
  8354. @"returnto_ext" : @"sender_ext",
  8355. @"returnto_fax" : @"sender_fax",
  8356. @"returnto_name" : @"sender_name",
  8357. @"returnto_phone" : @"sender_phone"},
  8358. @"type" : @"pull"};
  8359. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8360. NSDictionary *select_return_to_dic = @{
  8361. @"aname" : @"Select return to",
  8362. @"name" : @"select_cid",
  8363. @"refresh" : [NSNumber numberWithInteger:0],
  8364. @"value" : @"Contact_Return_To",
  8365. @"key_map" : @{
  8366. @"returnto_cid" : @"customer_cid",
  8367. @"returnto_contact" : @"customer_contact",
  8368. @"returnto_email" : @"customer_email",
  8369. @"returnto_ext" : @"customer_contact_ext",
  8370. @"returnto_fax" : @"customer_fax",
  8371. @"returnto_name" : @"customer_name",
  8372. @"returnto_phone" : @"customer_phone"
  8373. }
  8374. };
  8375. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8376. [dic setValue:choose_dic forKey:@"item_0"];
  8377. // contact id
  8378. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8379. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8380. [contact_id_dic setValue:@"true" forKey:@"required"];
  8381. [dic setValue:contact_id_dic forKey:@"item_1"];
  8382. // company name
  8383. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8384. [dic setValue:company_name_dic forKey:@"item_2"];
  8385. // address
  8386. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8387. [dic setValue:ext_dic forKey:@"item_3"];
  8388. // contact
  8389. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8390. [dic setValue:contact_dic forKey:@"item_4"];
  8391. // phone
  8392. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8393. [dic setValue:phone_dic forKey:@"item_5"];
  8394. // fax
  8395. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8396. [dic setValue:fax_dic forKey:@"item_6"];
  8397. // email
  8398. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8399. [dic setValue:email_dic forKey:@"item_7"];
  8400. }];
  8401. return dic;
  8402. }
  8403. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8404. {
  8405. assert(params[@"user"]!=nil);
  8406. assert(params[@"contact_id"]!=nil);
  8407. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8408. orderCode = [self translateSingleQuote:orderCode];
  8409. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8410. __block double TotalCuft = 0;
  8411. __block double TotalWeight = 0;
  8412. __block int TotalCarton = 0;
  8413. __block double payments = 0;
  8414. // setting
  8415. NSDictionary *setting = params[@"setting"];
  8416. NSNumber *hide = setting[@"ModelInformationHide"];
  8417. [dic setValue:hide forKey:@"hide"];
  8418. 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];
  8419. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8420. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8421. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8422. // item id
  8423. int item_id = sqlite3_column_int(stmt, 0);
  8424. // count
  8425. int item_count = sqlite3_column_int(stmt, 1);
  8426. // stockUom
  8427. int stockUom = sqlite3_column_int(stmt, 2);
  8428. // unit price
  8429. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8430. NSString* Price=nil;
  8431. if([str_price isEqualToString:@""])
  8432. {
  8433. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8434. if(price==nil)
  8435. Price=@"No Price.";
  8436. else
  8437. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8438. }
  8439. else
  8440. {
  8441. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8442. }
  8443. // discount
  8444. double discount = sqlite3_column_double(stmt, 4);
  8445. // name
  8446. NSString *name = [self textAtColumn:5 statement:stmt];
  8447. // description
  8448. NSString *description = [self textAtColumn:6 statement:stmt];
  8449. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8450. // line note
  8451. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8452. int avaulability = sqlite3_column_int(stmt, 8);
  8453. // img
  8454. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8455. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8456. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8457. if(combine != nil)
  8458. {
  8459. // int citem=0;
  8460. int bcount=[[combine valueForKey:@"count"] intValue];
  8461. for(int bc=0;bc<bcount;bc++)
  8462. {
  8463. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8464. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8465. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8466. subTotal += uprice * modulus * item_count;
  8467. }
  8468. }
  8469. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8470. [model_dic setValue:@"model" forKey:@"control"];
  8471. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8472. [model_dic setValue:description forKey:@"description"];
  8473. [model_dic setValue:line_note forKey:@"note"];
  8474. [model_dic setValue:img forKey:@"img_url"];
  8475. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8476. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8477. [model_dic setValue:Price forKey:@"unit_price"];
  8478. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8479. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8480. if (combine) {
  8481. [model_dic setValue:combine forKey:@"combine"];
  8482. }
  8483. // well,what under the row is the info for total
  8484. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8485. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8486. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8487. int carton=[bsubtotaljson[@"carton"] intValue];
  8488. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8489. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8490. TotalCuft += cuft;
  8491. TotalWeight += weight;
  8492. TotalCarton += carton;
  8493. payments += subTotal;
  8494. //---------------------------
  8495. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8496. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8497. }];
  8498. [dic setValue:@"Model Information" forKey:@"title"];
  8499. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8500. return dic;
  8501. }
  8502. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8503. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8504. [dic setValue:@"Remarks Content" forKey:@"title"];
  8505. // setting
  8506. NSDictionary *setting = params[@"setting"];
  8507. NSNumber *hide = setting[@"RemarksContentHide"];
  8508. [dic setValue:hide forKey:@"hide"];
  8509. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8510. orderCode = [self translateSingleQuote:orderCode];
  8511. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8512. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8513. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8514. int mustCall = sqlite3_column_int(stmt, 1);
  8515. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8516. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8517. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8518. NSDictionary *po_dic = @{
  8519. @"aname" : @"PO#",
  8520. @"control" : @"edit",
  8521. @"keyboard" : @"text",
  8522. @"name" : @"poNumber",
  8523. @"value" : poNumber
  8524. };
  8525. #ifdef BUILD_CONTRAST
  8526. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8527. NSDictionary *schedule_dict = @{
  8528. @"aname" : @"Schdule Date",
  8529. @"control" : @"datepicker",
  8530. @"type": @"date",
  8531. @"required": @"true",
  8532. @"name" : @"schedule_date_str",
  8533. @"value" : nsdate
  8534. };
  8535. #endif
  8536. NSDictionary *must_call_dic = @{
  8537. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8538. @"control" : @"switch",
  8539. @"name" : @"must_call",
  8540. @"value" : mustCall ? @"true" : @"false"
  8541. };
  8542. NSDictionary *general_notes_dic = @{
  8543. @"aname" : @"General notes",
  8544. @"control" : @"text_view",
  8545. @"keyboard" : @"text",
  8546. @"name" : @"comments",
  8547. @"value" : generalNotes
  8548. };
  8549. // NSDictionary *internal_notes_dic = @{
  8550. // @"aname" : @"Internal notes",
  8551. // @"control" : @"text_view",
  8552. // @"keyboard" : @"text",
  8553. // @"name" : @"internal_notes",
  8554. // @"value" : internalNotes
  8555. // };
  8556. #ifdef BUILD_CONTRAST
  8557. [dic setValue:po_dic forKey:@"item_0"];
  8558. [dic setValue:schedule_dict forKey:@"item_1"];
  8559. [dic setValue:must_call_dic forKey:@"item_2"];
  8560. [dic setValue:general_notes_dic forKey:@"item_3"];
  8561. #else
  8562. [dic setValue:po_dic forKey:@"item_0"];
  8563. [dic setValue:must_call_dic forKey:@"item_1"];
  8564. [dic setValue:general_notes_dic forKey:@"item_2"];
  8565. #endif
  8566. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8567. }];
  8568. #ifdef BUILD_CONTRAST
  8569. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8570. #else
  8571. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8572. #endif
  8573. return dic;
  8574. }
  8575. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8576. // params
  8577. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8578. orderCode = [self translateSingleQuote:orderCode];
  8579. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8580. // setting
  8581. NSDictionary *setting = params[@"setting"];
  8582. NSNumber *hide = setting[@"OrderTotalHide"];
  8583. [dic setValue:hide forKey:@"hide"];
  8584. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8585. __block double lift_gate_value = 0;
  8586. __block double handling_fee = 0;
  8587. __block double shipping = 0;
  8588. __block double discount = 0;
  8589. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8590. int lift_gate = sqlite3_column_int(stmt, 0);
  8591. lift_gate_value = sqlite3_column_double(stmt, 1);
  8592. shipping = sqlite3_column_double(stmt, 2);
  8593. handling_fee = sqlite3_column_double(stmt, 3);
  8594. discount = sqlite3_column_double(stmt, 4);
  8595. if (!lift_gate) {
  8596. lift_gate_value = 0;
  8597. }
  8598. }];
  8599. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8600. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8601. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8602. double payments = [[total valueForKey:@"payments"] doubleValue];
  8603. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8604. double totalPrice = payments* (1-discount/100.0);
  8605. [dic setValue:@"Order Total" forKey:@"title"];
  8606. NSDictionary *payments_dic = @{
  8607. @"align" : @"right",
  8608. @"aname" : @"Payments/Credits",
  8609. @"control" : @"text",
  8610. @"name" : @"paymentsAndCredits",
  8611. @"type" : @"price",
  8612. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8613. };
  8614. [dic setValue:payments_dic forKey:@"item_0"];
  8615. // version 1.71 remove
  8616. // NSDictionary *handling_fee_dic = @{
  8617. // @"align" : @"right",
  8618. // @"aname" : @"Handling Fee",
  8619. // @"control" : @"text",
  8620. // @"name" : @"handling_fee_value",
  8621. // @"required" : @"true",
  8622. // @"type" : @"price",
  8623. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8624. // };
  8625. NSDictionary *shipping_dic = @{
  8626. @"align" : @"right",
  8627. @"aname" : @"Shipping*",
  8628. @"control" : @"text",
  8629. @"name" : @"shipping",
  8630. @"required" : @"true",
  8631. @"type" : @"price",
  8632. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8633. };
  8634. NSDictionary *lift_gate_dic = @{
  8635. @"align" : @"right",
  8636. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8637. @"control" : @"text",
  8638. @"name" : @"lift_gate_value",
  8639. @"required" : @"true",
  8640. @"type" : @"price",
  8641. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8642. };
  8643. int item_count = 1;
  8644. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8645. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8646. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8647. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8648. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8649. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8650. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8651. } else {
  8652. }
  8653. }
  8654. // version 1.71 remove
  8655. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8656. // NSString* strdiscount;
  8657. if(discount>0)
  8658. {
  8659. NSDictionary *discount_dict = @{
  8660. @"align" : @"right",
  8661. @"aname" : @"Order Discount",
  8662. @"control" : @"text",
  8663. @"name" : @"orderDiscount",
  8664. // @"type" : @"price",
  8665. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8666. };
  8667. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8668. }
  8669. NSDictionary *total_price_dic = @{
  8670. @"align" : @"right",
  8671. @"aname" : @"Total",
  8672. @"control" : @"text",
  8673. @"name" : @"totalPrice",
  8674. @"type" : @"price",
  8675. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8676. };
  8677. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8678. NSDictionary *total_cuft_dic = @{
  8679. @"align" : @"right",
  8680. @"aname" : @"Total Cuft",
  8681. @"control" : @"text",
  8682. @"name" : @"",
  8683. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8684. };
  8685. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8686. NSDictionary *total_weight_dic = @{
  8687. @"align" : @"right",
  8688. @"aname" : @"Total Weight",
  8689. @"control" : @"text",
  8690. @"name" : @"",
  8691. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8692. };
  8693. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8694. NSDictionary *total_carton_dic = @{
  8695. @"align" : @"right",
  8696. @"aname" : @"Total Carton",
  8697. @"control" : @"text",
  8698. @"name" : @"",
  8699. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8700. };
  8701. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8702. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8703. return dic;
  8704. }
  8705. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8706. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8707. orderCode = [self translateSingleQuote:orderCode];
  8708. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8709. // setting
  8710. NSDictionary *setting = params[@"setting"];
  8711. NSNumber *hide = setting[@"SignatureHide"];
  8712. [dic setValue:hide forKey:@"hide"];
  8713. [dic setValue:@"Signature" forKey:@"title"];
  8714. __block NSString *pic_path = @"";
  8715. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8716. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8717. pic_path = [self textAtColumn:0 statement:stmt];
  8718. }];
  8719. NSDictionary *pic_dic = @{
  8720. @"aname" : @"Signature",
  8721. @"avalue" :pic_path,
  8722. @"control" : @"signature",
  8723. @"name" : @"sign_picpath",
  8724. @"value" : pic_path
  8725. };
  8726. [dic setValue:pic_dic forKey:@"item_0"];
  8727. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8728. return dic;
  8729. }
  8730. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8731. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8732. // setting
  8733. NSDictionary *setting = params[@"setting"];
  8734. NSNumber *hide = setting[@"ShippingMethodHide"];
  8735. [dic setValue:hide forKey:@"hide"];
  8736. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8737. orderCode = [self translateSingleQuote:orderCode];
  8738. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8739. __block NSString *logist = @"";
  8740. __block NSString *carrier = @"";
  8741. __block NSString *lift_gate = @"";
  8742. __block int lift_gate_integer = 0;
  8743. __block NSString *logistic_note = @"";
  8744. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8745. logist = [self textAtColumn:0 statement:stmt];
  8746. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8747. logistic_note = [self textAtColumn:2 statement:stmt];
  8748. carrier = [self textAtColumn:3 statement:stmt];
  8749. }];
  8750. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8751. [dic setValue:@"Shipping Method" forKey:@"title"];
  8752. // val_0
  8753. int PERSONAL_PICK_UP_check = 0;
  8754. int USE_MY_CARRIER_check = 0;
  8755. NSString *logistic_note_text = @"";
  8756. int will_call_check = 0;
  8757. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8758. will_call_check = 1;
  8759. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8760. PERSONAL_PICK_UP_check = 1;
  8761. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8762. USE_MY_CARRIER_check = 1;
  8763. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8764. logistic_note = [logistic_note_array firstObject];
  8765. if (logistic_note_array.count > 1) {
  8766. logistic_note_text = [logistic_note_array lastObject];
  8767. }
  8768. }
  8769. }
  8770. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8771. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8772. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8773. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8774. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8775. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8776. NSDictionary *val0_subItem_item0 = @{
  8777. @"aname" : @"Option",
  8778. @"cadedate" : @{
  8779. @"count" : [NSNumber numberWithInteger:2],
  8780. @"val_0" : @{
  8781. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8782. @"refresh" : [NSNumber numberWithInteger:0],
  8783. @"value" : @"PERSONAL PICK UP",
  8784. @"value_id" : @"PERSONAL PICK UP"
  8785. },
  8786. @"val_1" : @{
  8787. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8788. @"refresh" : [NSNumber numberWithInteger:0],
  8789. @"sub_item" : @{
  8790. @"count" : [NSNumber numberWithInteger:1],
  8791. @"item_0" : @{
  8792. @"aname" : @"BOL",
  8793. @"control" : @"edit",
  8794. @"keyboard" : @"text",
  8795. @"name" : @"logist_note_text",
  8796. @"refresh" : [NSNumber numberWithInteger:0],
  8797. @"required" : @"false",
  8798. @"value" : logistic_note_text
  8799. }
  8800. },
  8801. @"value" : @"USE MY CARRIER",
  8802. @"value_id" : @"USE MY CARRIER"
  8803. }
  8804. },
  8805. @"control" : @"enum",
  8806. @"name" : @"logistic_note",
  8807. @"required" : @"true",
  8808. @"single_select" : @"true"
  8809. };
  8810. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8811. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8812. // val_1
  8813. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8814. if([lift_gate isEqualToString:@""]) {
  8815. if (lift_gate_integer == 1) {
  8816. lift_gate = @"true";
  8817. } else {
  8818. lift_gate = @"false";
  8819. }
  8820. }
  8821. // carrier = [self valueInParams:params key:@"carrier"];
  8822. // if([carrier isEqualToString:@""]) {
  8823. //
  8824. // if (lift_gate_integer == 1) {
  8825. // lift_gate = @"true";
  8826. // } else {
  8827. // lift_gate = @"false";
  8828. // }
  8829. //
  8830. // }
  8831. int common_carrier_check = 0;
  8832. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8833. common_carrier_check = 1;
  8834. [params setValue:lift_gate forKey:@"lift_gate"];
  8835. #ifdef BUILD_CONTRAST
  8836. [params setValue:carrier forKey:@"carrier"];
  8837. #endif
  8838. }
  8839. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8840. // NSString * code = [iSalesDB jk_queryText:Sql];
  8841. // [self offline_getCarrierCode:offline_getCarrierCode];
  8842. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8843. NSDictionary *val_1 = @{
  8844. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8845. @"sub_item" : @{
  8846. @"item_0" : @{
  8847. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8848. @"control" : @"switch",
  8849. @"name" : @"lift_gate",
  8850. @"refresh" : [NSNumber numberWithInteger:1],
  8851. @"required" : @"true",
  8852. @"value" : lift_gate
  8853. },
  8854. #ifdef BUILD_CONTRAST
  8855. @"count" : [NSNumber numberWithInteger:2],
  8856. @"item_1" : @{
  8857. @"aname" : @"Carrier",
  8858. @"cadedate" :carrierDic,
  8859. @"control" : @"enum",
  8860. @"name" : @"carrier",
  8861. @"required" : @"false",
  8862. @"value" : @"",
  8863. @"single_select" : @"true"
  8864. }
  8865. #else
  8866. @"count" : [NSNumber numberWithInteger:1]
  8867. #endif
  8868. },
  8869. @"value" : @"COMMON CARRIER",
  8870. @"value_id" : @"COMMON CARRIER"
  8871. };
  8872. // cadedate
  8873. NSDictionary *cadedate = @{
  8874. @"count" : [NSNumber numberWithInteger:2],
  8875. @"val_0" : val_0,
  8876. @"val_1" : val_1
  8877. };
  8878. // item_0
  8879. NSMutableDictionary *item_0 = @{
  8880. @"aname" : @"Shipping",
  8881. @"cadedate" : cadedate,
  8882. @"control" : @"enum",
  8883. @"name" : @"logist",
  8884. @"refresh" : [NSNumber numberWithInteger:1],
  8885. @"single_select" : @"true",
  8886. }.mutableCopy;
  8887. NSNumber *required = setting[@"ShippingMethodRequire"];
  8888. if ([required integerValue]) {
  8889. [item_0 setValue:@"true" forKey:@"required"];
  8890. }
  8891. [dic setValue:item_0 forKey:@"item_0"];
  8892. if ([logist isEqualToString:@"WILL CALL"]) {
  8893. [dic removeObjectForKey:@"lift_gate"];
  8894. }
  8895. return dic;
  8896. }
  8897. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8898. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8899. orderCode = [self translateSingleQuote:orderCode];
  8900. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8901. __block int submit_as_integer = 0;
  8902. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8903. submit_as_integer = sqlite3_column_int(stmt, 0);
  8904. }];
  8905. int check11 = 0;
  8906. int check10 = 0;
  8907. if (submit_as_integer == 11) {
  8908. check11 = 1;
  8909. }
  8910. if (submit_as_integer == 10) {
  8911. check10 = 1;
  8912. }
  8913. // section_0
  8914. NSMutableDictionary *dic = @{
  8915. @"count" : @(1),
  8916. @"item_0" : @{
  8917. @"aname" : @"Submit Order As",
  8918. @"cadedate" : @{
  8919. @"count" : @(2),
  8920. @"val_0" : @{
  8921. @"check" : [NSNumber numberWithInteger:check11],
  8922. @"value" : @"Sales Order",
  8923. @"value_id" : @(11)
  8924. },
  8925. @"val_1" : @{
  8926. @"check" : [NSNumber numberWithInteger:check10],
  8927. @"value" : @"Quote",
  8928. @"value_id" : @(10)
  8929. }
  8930. },
  8931. @"control" : @"enum",
  8932. @"name" : @"erpOrderStatus",
  8933. @"required" : @"true",
  8934. @"single_select" : @"true"
  8935. },
  8936. @"title" : @"Order Type"
  8937. }.mutableCopy;
  8938. // setting
  8939. NSDictionary *setting = params[@"setting"];
  8940. NSNumber *hide = setting[@"OrderTypeHide"];
  8941. [dic setValue:hide forKey:@"hide"];
  8942. return dic;
  8943. }
  8944. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8945. countryCode = [self translateSingleQuote:countryCode];
  8946. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8947. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8948. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8949. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8950. if (name == NULL) {
  8951. name = "";
  8952. }
  8953. if (code == NULL) {
  8954. code = "";
  8955. }
  8956. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8957. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8958. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8959. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8960. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8961. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8962. }
  8963. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8964. [container setValue:stateDic forKey:key];
  8965. }] mutableCopy];
  8966. [ret removeObjectForKey:@"result"];
  8967. // failure 可以不用了,一样的
  8968. if (ret.allKeys.count == 0) {
  8969. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8970. [stateDic setValue:@"Other" forKey:@"value"];
  8971. [stateDic setValue:@"" forKey:@"value_id"];
  8972. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8973. if (state_code && [@"" isEqualToString:state_code]) {
  8974. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8975. }
  8976. NSString *key = [NSString stringWithFormat:@"val_0"];
  8977. [ret setValue:stateDic forKey:key];
  8978. }
  8979. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8980. return ret;
  8981. }
  8982. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8983. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8984. orderCode = [self translateSingleQuote:orderCode];
  8985. 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];
  8986. __block NSString *payType = @"";
  8987. __block NSString *firstName = @"";
  8988. __block NSString *lastName = @"";
  8989. __block NSString *addr1 = @"";
  8990. __block NSString *addr2 = @"";
  8991. __block NSString *zipcode = @"";
  8992. __block NSString *cardType = @"";
  8993. __block NSString *cardNumber = @"";
  8994. __block NSString *securityCode = @"";
  8995. __block NSString *month = @"";
  8996. __block NSString *year = @"";
  8997. __block NSString *city = @"";
  8998. __block NSString *state = @"";
  8999. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9000. payType = [self textAtColumn:0 statement:stmt];
  9001. firstName = [self textAtColumn:1 statement:stmt];
  9002. lastName = [self textAtColumn:2 statement:stmt];
  9003. addr1 = [self textAtColumn:3 statement:stmt];
  9004. addr2 = [self textAtColumn:4 statement:stmt];
  9005. zipcode = [self textAtColumn:5 statement:stmt];
  9006. cardType = [self textAtColumn:6 statement:stmt];
  9007. cardNumber = [self textAtColumn:7 statement:stmt];
  9008. securityCode = [self textAtColumn:8 statement:stmt];
  9009. month = [self textAtColumn:9 statement:stmt];
  9010. year = [self textAtColumn:10 statement:stmt];
  9011. city = [self textAtColumn:11 statement:stmt];
  9012. state = [self textAtColumn:12 statement:stmt];
  9013. }];
  9014. NSString *required = @"true";
  9015. // setting
  9016. NSDictionary *setting = params[@"setting"];
  9017. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9018. if ([requiredNumber integerValue]) {
  9019. required = @"true";
  9020. } else {
  9021. required = @"false";
  9022. }
  9023. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9024. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9025. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9026. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9027. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9028. NSString *type = [self textAtColumn:1 statement:stmt];
  9029. NSMutableDictionary *val = @{
  9030. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9031. @"value" : type,
  9032. @"value_id" : type_id
  9033. }.mutableCopy;
  9034. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9035. NSDictionary *sub_item =nil;
  9036. #ifdef BUILD_CONTRAST
  9037. sub_item = @{
  9038. @"count" : @(3),
  9039. @"item_0" : @{
  9040. @"aname" : @"Number",
  9041. @"control" : @"edit",
  9042. @"keyboard" : @"int",
  9043. @"length" : @"16",
  9044. @"name" : @"credit_card_number",
  9045. @"required" : @"false",
  9046. @"value" : cardNumber
  9047. },
  9048. @"item_1" : @{
  9049. @"aname" : @"Expiration Date",
  9050. @"control" : @"monthpicker",
  9051. @"name" : @"credit_card_expiration",
  9052. @"required" : @"false",
  9053. @"type" : @"date",
  9054. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9055. },
  9056. @"item_2" : @{
  9057. @"aname" : @"Security Code",
  9058. @"control" : @"edit",
  9059. @"keyboard" : @"int",
  9060. @"length" : @"4",
  9061. @"name" : @"credit_card_security_code",
  9062. @"required" : @"false",
  9063. @"value" : securityCode
  9064. },
  9065. };
  9066. #else
  9067. sub_item= @{
  9068. @"count" : @(3),
  9069. @"item_0" : @{
  9070. @"aname" : @"choose",
  9071. @"control" : @"multi_action",
  9072. @"count" : @(1),
  9073. @"item_0" : @{
  9074. @"aname" : @"Same as customer",
  9075. @"key_map" : @{
  9076. @"credit_card_address1" : @"customer_address1",
  9077. @"credit_card_address2" : @"customer_address2",
  9078. @"credit_card_city" : @"customer_city",
  9079. @"credit_card_first_name" : @"customer_first_name",
  9080. @"credit_card_last_name" : @"customer_last_name",
  9081. @"credit_card_state" : @"customer_state",
  9082. @"credit_card_zipcode" : @"customer_zipcode"
  9083. },
  9084. @"type" : @"pull"
  9085. }
  9086. },
  9087. @"item_1" : @{
  9088. @"aname" : @"",
  9089. @"color" : @"red",
  9090. @"control" : @"text",
  9091. @"name" : @"",
  9092. @"value" : @"USA Credit cards only"
  9093. },
  9094. @"item_2" : @{
  9095. @"aname" : @"Fill",
  9096. @"cadedate" : @{
  9097. @"count" : @(2),
  9098. @"val_0" : @{
  9099. @"check" : @(1),
  9100. @"sub_item" : @{
  9101. @"count" : @(11),
  9102. @"item_0" : @{
  9103. @"aname" : @"Type",
  9104. @"cadedate" : @{
  9105. @"count" : @(2),
  9106. @"val_0" : @{
  9107. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9108. @"value" : @"VISA",
  9109. @"value_id" : @"VISA"/*@(0)*/
  9110. },
  9111. @"val_1" : @{
  9112. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9113. @"value" : @"MASTER CARD",
  9114. @"value_id" : @"MASTER CARD"/*@(1)*/
  9115. }
  9116. },
  9117. @"control" : @"enum",
  9118. @"name" : @"credit_card_type",
  9119. @"required" : @"true",
  9120. @"single_select" : @"true"
  9121. },
  9122. @"item_1" : @{
  9123. @"aname" : @"Number",
  9124. @"control" : @"edit",
  9125. @"keyboard" : @"int",
  9126. @"length" : @"16",
  9127. @"name" : @"credit_card_number",
  9128. @"required" : @"true",
  9129. @"value" : cardNumber
  9130. },
  9131. @"item_10" : @{
  9132. @"aname" : @"State",
  9133. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9134. @"control" : @"enum",
  9135. @"enum" : @"true",
  9136. @"name" : @"credit_card_state",
  9137. @"required" : @"true",
  9138. @"single_select" : @"true"
  9139. },
  9140. @"item_2" : @{
  9141. @"aname" : @"Expiration Date",
  9142. @"control" : @"monthpicker",
  9143. @"name" : @"credit_card_expiration",
  9144. @"required" : @"true",
  9145. @"type" : @"date",
  9146. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9147. },
  9148. @"item_3" : @{
  9149. @"aname" : @"Security Code",
  9150. @"control" : @"edit",
  9151. @"keyboard" : @"int",
  9152. @"length" : @"3",
  9153. @"name" : @"credit_card_security_code",
  9154. @"required" : @"true",
  9155. @"value" : securityCode
  9156. },
  9157. @"item_4" : @{
  9158. @"aname" : @"First Name",
  9159. @"control" : @"edit",
  9160. @"keyboard" : @"text",
  9161. @"name" : @"credit_card_first_name",
  9162. @"required" : @"true",
  9163. @"value" : firstName
  9164. },
  9165. @"item_5" : @{
  9166. @"aname" : @"Last Name",
  9167. @"control" : @"edit",
  9168. @"keyboard" : @"text",
  9169. @"name" : @"credit_card_last_name",
  9170. @"required" : @"true",
  9171. @"value" : lastName
  9172. },
  9173. @"item_6" : @{
  9174. @"aname" : @"Address 1",
  9175. @"control" : @"edit",
  9176. @"keyboard" : @"text",
  9177. @"name" : @"credit_card_address1",
  9178. @"required" : @"true",
  9179. @"value" : addr1
  9180. },
  9181. @"item_7" : @{
  9182. @"aname" : @"Address 2",
  9183. @"control" : @"edit",
  9184. @"keyboard" : @"text",
  9185. @"name" : @"credit_card_address2",
  9186. @"value" : addr2
  9187. },
  9188. @"item_8" : @{
  9189. @"aname" : @"zip code",
  9190. @"control" : @"edit",
  9191. @"keyboard" : @"text",
  9192. @"name" : @"credit_card_zipcode",
  9193. @"required" : @"true",
  9194. @"value" : zipcode
  9195. },
  9196. @"item_9" : @{
  9197. @"aname" : @"City",
  9198. @"control" : @"edit",
  9199. @"keyboard" : @"text",
  9200. @"name" : @"credit_card_city",
  9201. @"required" : @"true",
  9202. @"value" : city
  9203. }
  9204. },
  9205. @"value" : @"Fill Now",
  9206. @"value_id" : @""
  9207. },
  9208. @"val_1" : @{
  9209. @"check" : @(0),
  9210. @"value" : @"Fill Later",
  9211. @"value_id" : @""
  9212. }
  9213. },
  9214. @"control" : @"enum",
  9215. @"name" : @"",
  9216. @"single_select" : @"true"
  9217. }
  9218. };
  9219. #endif
  9220. [val setObject:sub_item forKey:@"sub_item"];
  9221. }
  9222. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9223. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9224. }];
  9225. // "section_2"
  9226. NSMutableDictionary *dic = @{
  9227. @"count" : @(1),
  9228. @"item_0" : @{
  9229. @"aname" : @"Payment",
  9230. @"required" : required,
  9231. @"cadedate" : cadedate,
  9232. // @{
  9233. // @"count" : @(6),
  9234. // @"val_3" : @{
  9235. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9236. // @"value" : @"Check",
  9237. // @"value_id" : @"Check"
  9238. // },
  9239. // @"val_2" : @{
  9240. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9241. // @"value" : @"Cash",
  9242. // @"value_id" : @"Cash"
  9243. // },
  9244. // @"val_1" : @{
  9245. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9246. // @"value" : @"NET 60",
  9247. // @"value_id" : @"NET 30"
  9248. // },
  9249. // @"val_4" : @{
  9250. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9251. // @"value" : @"Wire Transfer",
  9252. // @"value_id" : @"Wire Transfer"
  9253. // },
  9254. // @"val_0" : @{
  9255. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9256. // @"sub_item" : @{
  9257. // @"count" : @(3),
  9258. // @"item_0" : @{
  9259. // @"aname" : @"choose",
  9260. // @"control" : @"multi_action",
  9261. // @"count" : @(1),
  9262. // @"item_0" : @{
  9263. // @"aname" : @"Same as customer",
  9264. // @"key_map" : @{
  9265. // @"credit_card_address1" : @"customer_address1",
  9266. // @"credit_card_address2" : @"customer_address2",
  9267. // @"credit_card_city" : @"customer_city",
  9268. // @"credit_card_first_name" : @"customer_first_name",
  9269. // @"credit_card_last_name" : @"customer_last_name",
  9270. // @"credit_card_state" : @"customer_state",
  9271. // @"credit_card_zipcode" : @"customer_zipcode"
  9272. // },
  9273. // @"type" : @"pull"
  9274. // }
  9275. // },
  9276. // @"item_1" : @{
  9277. // @"aname" : @"",
  9278. // @"color" : @"red",
  9279. // @"control" : @"text",
  9280. // @"name" : @"",
  9281. // @"value" : @"USA Credit cards only"
  9282. // },
  9283. // @"item_2" : @{
  9284. // @"aname" : @"Fill",
  9285. // @"cadedate" : @{
  9286. // @"count" : @(2),
  9287. // @"val_0" : @{
  9288. // @"check" : @(1),
  9289. // @"sub_item" : @{
  9290. // @"count" : @(11),
  9291. // @"item_0" : @{
  9292. // @"aname" : @"Type",
  9293. // @"cadedate" : @{
  9294. // @"count" : @(2),
  9295. // @"val_0" : @{
  9296. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9297. // @"value" : @"VISA",
  9298. // @"value_id" : @(0)
  9299. // },
  9300. // @"val_1" : @{
  9301. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9302. // @"value" : @"MASTER CARD",
  9303. // @"value_id" : @(1)
  9304. // }
  9305. // },
  9306. // @"control" : @"enum",
  9307. // @"name" : @"credit_card_type",
  9308. // @"required" : @"true",
  9309. // @"single_select" : @"true"
  9310. // },
  9311. // @"item_1" : @{
  9312. // @"aname" : @"Number",
  9313. // @"control" : @"edit",
  9314. // @"keyboard" : @"int",
  9315. // @"length" : @"16",
  9316. // @"name" : @"credit_card_number",
  9317. // @"required" : @"true",
  9318. // @"value" : cardNumber
  9319. // },
  9320. // @"item_10" : @{
  9321. // @"aname" : @"State",
  9322. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9323. // @"control" : @"enum",
  9324. // @"enum" : @"true",
  9325. // @"name" : @"credit_card_state",
  9326. // @"required" : @"true",
  9327. // @"single_select" : @"true"
  9328. // },
  9329. // @"item_2" : @{
  9330. // @"aname" : @"Expiration Date",
  9331. // @"control" : @"monthpicker",
  9332. // @"name" : @"credit_card_expiration",
  9333. // @"required" : @"true",
  9334. // @"type" : @"date",
  9335. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9336. // },
  9337. // @"item_3" : @{
  9338. // @"aname" : @"Security Code",
  9339. // @"control" : @"edit",
  9340. // @"keyboard" : @"int",
  9341. // @"length" : @"3",
  9342. // @"name" : @"credit_card_security_code",
  9343. // @"required" : @"true",
  9344. // @"value" : securityCode
  9345. // },
  9346. // @"item_4" : @{
  9347. // @"aname" : @"First Name",
  9348. // @"control" : @"edit",
  9349. // @"keyboard" : @"text",
  9350. // @"name" : @"credit_card_first_name",
  9351. // @"required" : @"true",
  9352. // @"value" : firstName
  9353. // },
  9354. // @"item_5" : @{
  9355. // @"aname" : @"Last Name",
  9356. // @"control" : @"edit",
  9357. // @"keyboard" : @"text",
  9358. // @"name" : @"credit_card_last_name",
  9359. // @"required" : @"true",
  9360. // @"value" : lastName
  9361. // },
  9362. // @"item_6" : @{
  9363. // @"aname" : @"Address 1",
  9364. // @"control" : @"edit",
  9365. // @"keyboard" : @"text",
  9366. // @"name" : @"credit_card_address1",
  9367. // @"required" : @"true",
  9368. // @"value" : addr1
  9369. // },
  9370. // @"item_7" : @{
  9371. // @"aname" : @"Address 2",
  9372. // @"control" : @"edit",
  9373. // @"keyboard" : @"text",
  9374. // @"name" : @"credit_card_address2",
  9375. // @"value" : addr2
  9376. // },
  9377. // @"item_8" : @{
  9378. // @"aname" : @"zip code",
  9379. // @"control" : @"edit",
  9380. // @"keyboard" : @"text",
  9381. // @"name" : @"credit_card_zipcode",
  9382. // @"required" : @"true",
  9383. // @"value" : zipcode
  9384. // },
  9385. // @"item_9" : @{
  9386. // @"aname" : @"City",
  9387. // @"control" : @"edit",
  9388. // @"keyboard" : @"text",
  9389. // @"name" : @"credit_card_city",
  9390. // @"required" : @"true",
  9391. // @"value" : city
  9392. // }
  9393. // },
  9394. // @"value" : @"Fill Now",
  9395. // @"value_id" : @""
  9396. // },
  9397. // @"val_1" : @{
  9398. // @"check" : @(0),
  9399. // @"value" : @"Fill Later",
  9400. // @"value_id" : @""
  9401. // }
  9402. // },
  9403. // @"control" : @"enum",
  9404. // @"name" : @"",
  9405. // @"single_select" : @"true"
  9406. // }
  9407. // },
  9408. // @"value" : @"Visa/Master",
  9409. // @"value_id" : @"Credit Card"
  9410. // },
  9411. // @"val_5" : @{
  9412. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9413. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9414. // @"value_id" : @"FOLLOW EXISTING"
  9415. // }
  9416. // },
  9417. @"control" : @"enum",
  9418. @"name" : @"paymentType",
  9419. @"single_select" : @"true"
  9420. },
  9421. @"title" : @"Payment Information"
  9422. }.mutableCopy;
  9423. NSNumber *hide = setting[@"PaymentInformationHide"];
  9424. [dic setValue:hide forKey:@"hide"];
  9425. return dic;
  9426. }
  9427. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9428. // params
  9429. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9430. orderCode = [self translateSingleQuote:orderCode];
  9431. // 缺货检查
  9432. 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];
  9433. __block BOOL outOfStock = NO;
  9434. sqlite3 *database = db;
  9435. if (!db) {
  9436. database = [iSalesDB get_db];
  9437. }
  9438. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9439. int availability = sqlite3_column_int(stmt, 0);
  9440. int item_qty = sqlite3_column_int(stmt, 1);
  9441. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9442. }];
  9443. if (!db) {
  9444. [iSalesDB close_db:database];
  9445. }
  9446. return outOfStock;
  9447. }
  9448. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9449. {
  9450. assert(params[@"user"]!=nil);
  9451. assert(params[@"contact_id"]!=nil);
  9452. assert(params[@"can_create_backorder"]!=nil);
  9453. sqlite3 *db = [iSalesDB get_db];
  9454. // params
  9455. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9456. orderCode = [self translateSingleQuote:orderCode];
  9457. // 缺货检查
  9458. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9459. if (![params[@"can_create_backorder"] boolValue]) {
  9460. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9461. if (out_of_stock) {
  9462. [iSalesDB close_db:db];
  9463. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9464. [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"];
  9465. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9466. return [RAConvertor dict2data:resultDic];
  9467. }
  9468. }
  9469. // UISetting
  9470. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9471. NSString *cachefolder = [paths objectAtIndex:0];
  9472. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9473. NSData* json =nil;
  9474. json=[NSData dataWithContentsOfFile:img_cache];
  9475. NSError *error=nil;
  9476. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9477. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9478. [params setObject:setting forKey:@"setting"];
  9479. int section_count = 0;
  9480. // 0 Order Type 1 Shipping Method 2 Payment Information
  9481. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9482. // 0 Order Type
  9483. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9484. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9485. [ret setValue:order_type_dic forKey:key0];
  9486. // 1 Shipping Method
  9487. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9488. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9489. [ret setValue:shipping_method_dic forKey:key1];
  9490. // 2 Payment Information
  9491. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9492. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9493. [ret setValue:payment_info_dic forKey:key2];
  9494. // 3 Customer
  9495. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9496. NSDictionary *customer_dic = [self customerDic:params db:db];
  9497. [ret setValue:customer_dic forKey:key3];
  9498. // 4 Ship To
  9499. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9500. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9501. [ret setValue:ship_to_dic forKey:key4];
  9502. // 5 Ship From
  9503. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9504. if (![shipFromDisable integerValue]) {
  9505. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9506. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9507. [ret setValue:ship_from_dic forKey:key5];
  9508. }
  9509. // 6 Freight Bill To
  9510. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9511. if (![freightBillToDisable integerValue]) {
  9512. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9513. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9514. [ret setValue:freight_bill_to forKey:key6];
  9515. }
  9516. // 7 Merchandise Bill To
  9517. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9518. if (![merchandiseBillToDisable integerValue]) {
  9519. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9520. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9521. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9522. }
  9523. // 8 Return To
  9524. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9525. if (![returnToDisable integerValue]) {
  9526. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9527. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9528. [ret setValue:return_to_dic forKey:key8];
  9529. }
  9530. // 9 Model Information
  9531. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9532. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9533. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9534. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9535. [ret setValue:model_info_dic forKey:key9];
  9536. // 10 Remarks Content
  9537. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9538. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9539. [ret setValue:remarks_content_dic forKey:key10];
  9540. // 11 Order Total
  9541. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9542. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9543. [ret setValue:order_total_dic forKey:key11];
  9544. // 12 Signature
  9545. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9546. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9547. [ret setValue:sign_dic forKey:key12];
  9548. [ret setValue:@(section_count) forKey:@"section_count"];
  9549. [iSalesDB close_db:db];
  9550. return [RAConvertor dict2data:ret];
  9551. // return nil;
  9552. }
  9553. #pragma mark addr editor
  9554. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9555. NSMutableDictionary *new_item = [item mutableCopy];
  9556. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9557. return new_item;
  9558. }
  9559. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9560. {
  9561. // "is_subaction" = true;
  9562. // orderCode = MOB1608240002;
  9563. // password = 123456;
  9564. // "subaction_tag" = 1;
  9565. // user = EvanK;
  9566. // {
  9567. // "is_subaction" = true;
  9568. // orderCode = MOB1608240002;
  9569. // password = 123456;
  9570. // "refresh_trigger" = zipcode;
  9571. // "subaction_tag" = 1;
  9572. // user = EvanK;
  9573. // }
  9574. NSString *country_code = nil;
  9575. NSString *zipCode = nil;
  9576. NSString *stateCode = nil;
  9577. NSString *city = nil;
  9578. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9579. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9580. NSString *code_id = params[@"country"];
  9581. country_code = [self countryCodeByid:code_id];
  9582. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9583. NSString *zip_code = params[@"zipcode"];
  9584. // 剔除全部为空格
  9585. int spaceCount = 0;
  9586. for (int i = 0; i < zip_code.length; i++) {
  9587. if ([zip_code characterAtIndex:i] == ' ') {
  9588. spaceCount++;
  9589. }
  9590. }
  9591. if (spaceCount == zip_code.length) {
  9592. zip_code = @"";
  9593. }
  9594. zipCode = zip_code;
  9595. if (zipCode.length > 0) {
  9596. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9597. country_code = [dic valueForKey:@"country_code"];
  9598. if (!country_code) {
  9599. // country_code = @"US";
  9600. NSString *code_id = params[@"country"];
  9601. country_code = [self countryCodeByid:code_id];
  9602. }
  9603. stateCode = [dic valueForKey:@"state_code"];
  9604. if(!stateCode.length) {
  9605. stateCode = params[@"state"];
  9606. }
  9607. city = [dic valueForKey:@"city"];
  9608. if (!city.length) {
  9609. city = params[@"city"];
  9610. }
  9611. // zip code
  9612. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9613. // [zipDic setValue:zipCode forKey:@"value"];
  9614. // [section_0 setValue:zipDic forKey:@"item_11"];
  9615. } else {
  9616. NSString *code_id = params[@"country"];
  9617. country_code = [self countryCodeByid:code_id];
  9618. stateCode = params[@"state"];
  9619. city = params[@"city"];
  9620. }
  9621. }
  9622. }
  9623. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9624. // [ret removeObjectForKey:@"up_params"];
  9625. [ret setObject:@"New Address" forKey:@"title"];
  9626. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9627. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9628. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9629. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9630. // [country_dic removeObjectForKey:@"refresh"];
  9631. // [country_dic removeObjectForKey:@"restore"];
  9632. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9633. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9634. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9635. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9636. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9637. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9638. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9639. // [zip_code_dic removeObjectForKey:@"refresh"];
  9640. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9641. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9642. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9643. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9644. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9645. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9646. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9647. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9648. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9649. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9650. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9651. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9652. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9653. // country
  9654. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9655. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9656. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9657. // state
  9658. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9659. // NSDictionary *tmpDic = @{
  9660. // @"value" : @"Other",
  9661. // @"value_id" : @"",
  9662. // @"check" : [NSNumber numberWithInteger:0]
  9663. // };
  9664. //
  9665. // for (int i = 0; i < allState.allKeys.count; i++) {
  9666. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9667. //
  9668. // NSDictionary *tmp = allState[key];
  9669. // [allState setValue:tmpDic forKey:key];
  9670. // tmpDic = tmp;
  9671. // }
  9672. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9673. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9674. [ret setValue:new_section_0 forKey:@"section_0"];
  9675. return [RAConvertor dict2data:ret];
  9676. }
  9677. #pragma mark save addr
  9678. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9679. {
  9680. // NSString *companyName = [self valueInParams:params key:@"company"];
  9681. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9682. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9683. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9684. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9685. // NSString *countryId = [self valueInParams:params key:@"country"];
  9686. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9687. // NSString *city = [self valueInParams:params key:@"city"];
  9688. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9689. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9690. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9691. // NSString *phone = [self valueInParams:params key:@"phone"];
  9692. // NSString *fax = [self valueInParams:params key:@"fax"];
  9693. // NSString *email = [self valueInParams:params key:@"email"];
  9694. return [self offline_saveContact:params update:NO isCustomer:NO];
  9695. }
  9696. #pragma mark cancel order
  9697. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9698. {
  9699. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9700. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9701. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9702. if (order_id.length) {
  9703. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9704. }
  9705. int ret = [iSalesDB execSql:sql];
  9706. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9707. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9708. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9709. // [dic setValue:@"160409" forKey:@"min_ver"];
  9710. return [RAConvertor dict2data:dic];
  9711. }
  9712. #pragma mark sign order
  9713. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9714. {
  9715. //参考 offline_saveBusinesscard
  9716. DebugLog(@"sign order params: %@",params);
  9717. // orderCode = MOB1608240002;
  9718. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9719. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9720. orderCode = [self translateSingleQuote:orderCode];
  9721. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9722. picPath = [self translateSingleQuote:picPath];
  9723. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9724. int ret = [iSalesDB execSql:sql];
  9725. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9726. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9727. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9728. // [dic setValue:@"160409" forKey:@"min_ver"];
  9729. return [RAConvertor dict2data:dic];
  9730. }
  9731. #pragma mark save order
  9732. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9733. NSString *ret = [self valueInParams:params key:key];
  9734. if (translate) {
  9735. ret = [self translateSingleQuote:ret];
  9736. }
  9737. return ret;
  9738. }
  9739. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9740. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9741. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9742. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9743. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9744. if (schedule_date.length==0 ) {
  9745. schedule_date = @"";
  9746. } else {
  9747. schedule_date = [self rchangeDateFormate:schedule_date];
  9748. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9749. }
  9750. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9751. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9752. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9753. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9754. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9755. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9756. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9757. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9758. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9759. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9760. if (![total_price isEqualToString:@""]) {
  9761. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9762. } else {
  9763. total_price = @"";
  9764. }
  9765. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9766. if ([paymentsAndCredits isEqualToString:@""]) {
  9767. paymentsAndCredits = @"";
  9768. } else {
  9769. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9770. }
  9771. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9772. if ([handling_fee_value isEqualToString:@""]) {
  9773. handling_fee_value = @"";
  9774. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9775. handling_fee_value = @"";
  9776. } else {
  9777. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9778. }
  9779. NSString *handling_fee_placeholder = handling_fee_value;
  9780. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9781. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9782. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9783. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9784. NSString *lift_gate_placeholder = @"";
  9785. if ([lift_gate_value isEqualToString:@""]) {
  9786. lift_gate_placeholder = @"";
  9787. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9788. lift_gate_placeholder = @"";
  9789. } else {
  9790. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9791. }
  9792. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9793. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9794. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9795. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9796. 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];
  9797. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9798. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9799. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9800. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9801. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9802. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9803. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9804. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9805. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9806. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9807. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9808. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9809. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9810. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9811. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9812. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9813. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9814. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9815. if (!number_nil) {
  9816. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9817. }
  9818. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9819. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9820. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9821. if (!security_code_nil) {
  9822. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9823. }
  9824. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9825. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9826. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9827. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9828. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9829. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9830. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9831. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9832. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9833. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9834. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9835. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9836. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9837. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9838. NSString *contact_id = customer_cid;
  9839. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9840. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9841. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9842. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9843. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9844. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9845. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9846. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9847. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9848. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9849. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9850. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9851. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9852. //
  9853. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9854. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9855. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9856. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9857. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9858. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9859. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9860. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9861. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9862. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9863. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9864. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9865. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9866. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9867. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9868. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9869. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9870. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9871. 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];
  9872. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9873. if (receive_cid.length) {
  9874. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9875. }
  9876. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9877. if (receive_name.length) {
  9878. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9879. }
  9880. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9881. if (receive_ext.length) {
  9882. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9883. }
  9884. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9885. if (receive_contact.length) {
  9886. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9887. }
  9888. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9889. if (receive_phone.length) {
  9890. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9891. }
  9892. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9893. if (receive_email.length) {
  9894. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9895. }
  9896. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9897. if (receive_fax.length) {
  9898. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9899. }
  9900. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9901. if (sender_cid.length) {
  9902. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9903. }
  9904. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9905. if (sender_name.length) {
  9906. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9907. }
  9908. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9909. if (sender_ext.length) {
  9910. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9911. }
  9912. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9913. if(sender_contact.length) {
  9914. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9915. }
  9916. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9917. if (sender_phone.length) {
  9918. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9919. }
  9920. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9921. if (sender_fax.length) {
  9922. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9923. }
  9924. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9925. if (sender_email.length) {
  9926. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9927. }
  9928. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9929. if (shipping_billto_cid.length) {
  9930. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9931. }
  9932. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9933. if (shipping_billto_name.length) {
  9934. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9935. }
  9936. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9937. if (shipping_billto_ext.length) {
  9938. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9939. }
  9940. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9941. if (shipping_billto_contact.length) {
  9942. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9943. }
  9944. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9945. if (shipping_billto_phone.length) {
  9946. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9947. }
  9948. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9949. if (shipping_billto_fax.length) {
  9950. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9951. }
  9952. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9953. if (shipping_billto_email.length) {
  9954. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9955. }
  9956. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9957. if (billing_cid.length) {
  9958. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9959. }
  9960. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9961. if (billing_name.length) {
  9962. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9963. }
  9964. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9965. if (billing_ext.length) {
  9966. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9967. }
  9968. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9969. if (billing_contact.length) {
  9970. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9971. }
  9972. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9973. if (billing_phone.length) {
  9974. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9975. }
  9976. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9977. if (billing_fax.length) {
  9978. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9979. }
  9980. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9981. if (billing_email.length) {
  9982. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9983. }
  9984. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9985. if (returnto_cid.length) {
  9986. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9987. }
  9988. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9989. if (returnto_name.length) {
  9990. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9991. }
  9992. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9993. if (returnto_ext.length) {
  9994. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9995. }
  9996. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9997. if (returnto_contact.length) {
  9998. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9999. }
  10000. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10001. if (returnto_phone.length) {
  10002. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10003. }
  10004. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10005. if (returnto_fax.length) {
  10006. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10007. }
  10008. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10009. if (returnto_email.length) {
  10010. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10011. }
  10012. NSString *order_status = @"status = 1,";
  10013. if (submit) {
  10014. order_status = @"status = -11,";
  10015. }
  10016. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10017. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10018. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10019. NSString *sync_sql = @"";
  10020. if (submit) {
  10021. param[@"truck_carrier"]=carrier;
  10022. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10023. [param setValue:sales_rep forKey:@"sales_rep"];
  10024. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10025. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10026. }
  10027. 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];
  10028. DebugLog(@"save order contactSql: %@",contactSql);
  10029. DebugLog(@"save order orderSql: %@",orderSql);
  10030. // int contact_ret = [iSalesDB execSql:contactSql];
  10031. int order_ret = [iSalesDB execSql:orderSql];
  10032. int ret = order_ret;
  10033. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10034. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10035. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10036. // [dic setValue:@"160409" forKey:@"min_ver"];
  10037. [dic setObject:so_id forKey:@"so#"];
  10038. return [RAConvertor dict2data:dic];
  10039. }
  10040. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10041. {
  10042. // id foo = nil;
  10043. // NSMutableArray *a = @[].mutableCopy;
  10044. // [a addObject:foo];
  10045. return [self saveorder:param submit:NO];
  10046. }
  10047. #pragma mark add to cart by name
  10048. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10049. {
  10050. NSString *orderCode = [params objectForKey:@"orderCode"];
  10051. NSDictionary *newParams = @{
  10052. @"product_id" : params[@"product_id_string"],
  10053. @"orderCode" : orderCode,
  10054. @"can_create_backorder":params[@"can_create_backorder"]
  10055. };
  10056. return [self offline_add2cart:[newParams mutableCopy]];
  10057. }
  10058. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10059. {
  10060. NSString *orderCode = [params objectForKey:@"orderCode"];
  10061. NSString *upccode = [params objectForKey:@"upc_code"];
  10062. // product_name = [self translateSingleQuote:product_name];
  10063. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10064. // bool search_upc = [params[@"search_upc"] boolValue];
  10065. sqlite3 *db = [iSalesDB get_db];
  10066. __block NSMutableString *product_id_string = [NSMutableString string];
  10067. // NSString *name = [product_name_array objectAtIndex:i];
  10068. NSString *sql =nil;
  10069. 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];
  10070. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10071. int product_id = sqlite3_column_int(stmt, 0);
  10072. [product_id_string appendFormat:@"%d",product_id];
  10073. }];
  10074. if (!orderCode) {
  10075. orderCode = @"";
  10076. }
  10077. NSDictionary *newParams = @{
  10078. @"product_id" : product_id_string,
  10079. @"orderCode" : orderCode,
  10080. @"can_create_backorder":params[@"can_create_backorder"]
  10081. };
  10082. [iSalesDB close_db:db];
  10083. return [self offline_add2cart:[newParams mutableCopy]];
  10084. }
  10085. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10086. {
  10087. NSString *orderCode = [params objectForKey:@"orderCode"];
  10088. NSString *product_name = [params objectForKey:@"product_name"];
  10089. product_name = [self translateSingleQuote:product_name];
  10090. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10091. bool search_upc = [params[@"search_upc"] boolValue];
  10092. sqlite3 *db = [iSalesDB get_db];
  10093. __block NSMutableString *product_id_string = [NSMutableString string];
  10094. for (int i = 0; i < product_name_array.count; i++) {
  10095. NSString *name = [product_name_array objectAtIndex:i];
  10096. NSString *sql =nil;
  10097. if(search_upc)
  10098. 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];
  10099. else
  10100. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10101. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10102. int product_id = sqlite3_column_int(stmt, 0);
  10103. if (i == product_name_array.count - 1) {
  10104. [product_id_string appendFormat:@"%d",product_id];
  10105. } else {
  10106. [product_id_string appendFormat:@"%d,",product_id];
  10107. }
  10108. }];
  10109. }
  10110. if (!orderCode) {
  10111. orderCode = @"";
  10112. }
  10113. NSDictionary *newParams = @{
  10114. @"product_id" : product_id_string,
  10115. @"orderCode" : orderCode,
  10116. @"can_create_backorder":params[@"can_create_backorder"]
  10117. };
  10118. [iSalesDB close_db:db];
  10119. return [self offline_add2cart:[newParams mutableCopy]];
  10120. }
  10121. #pragma mark reset order
  10122. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10123. {
  10124. // UIApplication * app = [UIApplication sharedApplication];
  10125. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10126. [iSalesDB disable_trigger];
  10127. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10128. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10129. [iSalesDB enable_trigger];
  10130. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10131. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10132. return [RAConvertor dict2data:dic];
  10133. }
  10134. #pragma mark submit order
  10135. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10136. {
  10137. return [self saveorder:params submit:YES];
  10138. }
  10139. #pragma mark copy order
  10140. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10141. {
  10142. NSMutableDictionary *ret = @{}.mutableCopy;
  10143. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10144. sqlite3 *db = [iSalesDB get_db];
  10145. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10146. // 首先查看联系人是否active
  10147. 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];
  10148. __block int customer_is_active = 1;
  10149. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10150. customer_is_active = sqlite3_column_int(stmt, 0);
  10151. }];
  10152. if (!customer_is_active) {
  10153. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10154. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10155. [iSalesDB close_db:db];
  10156. return [RAConvertor dict2data:ret];
  10157. }
  10158. // new order
  10159. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10160. NSString *new_order_code = [self get_offline_soid:db];
  10161. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10162. user = [self translateSingleQuote:user];
  10163. 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
  10164. __block int result = 1;
  10165. result = [iSalesDB execSql:insert_order_sql db:db];
  10166. if (!result) {
  10167. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10168. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10169. [iSalesDB close_db:db];
  10170. return [RAConvertor dict2data:ret];
  10171. }
  10172. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10173. // __block NSString *product_id = @"";
  10174. __weak typeof(self) weakSelf = self;
  10175. 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];
  10176. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10177. int is_active = sqlite3_column_int(stmt, 1);
  10178. if (is_active) {
  10179. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10180. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10181. 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];
  10182. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10183. }
  10184. }];
  10185. // product_id = [product_id substringFromIndex:1];
  10186. //
  10187. // [self offline_add2cart:@{}.mutableCopy];
  10188. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10189. [iSalesDB close_db:db];
  10190. if (result) {
  10191. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10192. } else {
  10193. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10194. }
  10195. [ret setObject:new_order_code forKey:@"so_id"];
  10196. return [RAConvertor dict2data:ret];
  10197. }
  10198. #pragma mark move wish list to cart
  10199. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10200. assert(params[@"can_create_backorder"]!=nil);
  10201. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10202. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10203. NSString *collectId = params[@"collectId"];
  10204. 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];
  10205. __block NSString *product_id = @"";
  10206. __block NSString *qty = @"";
  10207. __block int number_of_outOfStock = 0;
  10208. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10209. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10210. int productId = sqlite3_column_int(stmt, 0);
  10211. int item_qty = sqlite3_column_int(stmt, 1);
  10212. int availability = sqlite3_column_int(stmt, 2);
  10213. int _id = sqlite3_column_int(stmt, 3);
  10214. if (![params[@"can_create_backorder"] boolValue]) {
  10215. // 库存小于购买量为缺货
  10216. if (availability >= item_qty) {
  10217. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10218. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10219. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10220. } else {
  10221. number_of_outOfStock++;
  10222. }
  10223. } else {
  10224. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10225. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10226. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10227. }
  10228. }];
  10229. NSMutableDictionary *retDic = nil;
  10230. if (![params[@"can_create_backorder"] boolValue]) {
  10231. if (delete_collectId.count == 0) {
  10232. retDic = [NSMutableDictionary dictionary];
  10233. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10234. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10235. return [RAConvertor dict2data:retDic];
  10236. }
  10237. }
  10238. if (product_id.length > 1) {
  10239. product_id = [product_id substringFromIndex:1];
  10240. }
  10241. if (qty.length > 1) {
  10242. qty = [qty substringFromIndex:1];
  10243. }
  10244. NSString *orderCode = params[@"orderCode"];
  10245. if (!orderCode) {
  10246. orderCode = @"";
  10247. }
  10248. NSDictionary *newParams = @{
  10249. @"product_id" : product_id,
  10250. @"orderCode" : orderCode,
  10251. @"qty" : qty,
  10252. @"can_create_backorder":params[@"can_create_backorder"]
  10253. };
  10254. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10255. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10256. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10257. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10258. NSInteger ret = [wish_return[@"result"] intValue];
  10259. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10260. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10261. }
  10262. if (![params[@"can_create_backorder"] boolValue]) {
  10263. if (number_of_outOfStock > 0) {
  10264. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10265. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10266. }
  10267. }
  10268. return [RAConvertor dict2data:retDic];
  10269. }
  10270. #pragma mark - portfolio
  10271. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10272. // assert(params[@"contact_id"]!=nil);
  10273. assert(params[@"user"]!=nil);
  10274. assert(params[@"can_see_price"]!=nil);
  10275. int sort = [[params valueForKey:@"sort"] intValue];
  10276. int offset = [[params valueForKey:@"offset"] intValue];
  10277. int limit = [[params valueForKey:@"limit"] intValue];
  10278. NSString *orderBy = @"";
  10279. switch (sort) {
  10280. case 0:{
  10281. orderBy = @"p.modify_time desc";
  10282. }
  10283. break;
  10284. case 1:{
  10285. orderBy = @"modify_time asc";
  10286. }
  10287. break;
  10288. case 2:{
  10289. orderBy = @"p.name asc";
  10290. }
  10291. break;
  10292. case 3:{
  10293. orderBy = @"p.name desc";
  10294. }
  10295. break;
  10296. case 4:{
  10297. orderBy = @"p.description asc";
  10298. }
  10299. break;
  10300. case 5: {
  10301. orderBy = @"m.default_category asc";
  10302. }
  10303. default:
  10304. break;
  10305. }
  10306. // 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];
  10307. 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];
  10308. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10309. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10310. sqlite3 *db = [iSalesDB get_db];
  10311. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10312. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10313. int product_id = sqlite3_column_int(stmt, 0);
  10314. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10315. NSString *name = [self textAtColumn:1 statement:stmt];
  10316. NSString *description = [self textAtColumn:2 statement:stmt];
  10317. double price = sqlite3_column_double(stmt, 3);
  10318. double discount = sqlite3_column_double(stmt,4);
  10319. int qty = sqlite3_column_int(stmt, 5);
  10320. int percentage = sqlite3_column_int(stmt, 6);
  10321. int item_id = sqlite3_column_int(stmt, 7);
  10322. // int fashion_id = sqlite3_column_int(stmt, 8);
  10323. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10324. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10325. double percent = sqlite3_column_double(stmt, 11);
  10326. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10327. if ([price_null isEqualToString:@"null"]) {
  10328. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10329. }
  10330. NSMutableDictionary *item = @{
  10331. @"linenotes": line_note,
  10332. @"check": @(1),
  10333. @"product_id": product_id_string,
  10334. @"available_qty": @(qty),
  10335. @"available_percent" : @(percent),
  10336. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10337. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10338. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10339. @"img": img_path,
  10340. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10341. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10342. }.mutableCopy;
  10343. if (percentage) {
  10344. [item removeObjectForKey:@"available_qty"];
  10345. } else {
  10346. [item removeObjectForKey:@"available_percent"];
  10347. }
  10348. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10349. if ([qty_null isEqualToString:@"null"]) {
  10350. [item removeObjectForKey:@"available_qty"];
  10351. }
  10352. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10353. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10354. }];
  10355. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10356. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10357. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10358. }
  10359. [iSalesDB close_db:db];
  10360. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10361. [dic setValue:@"" forKey:@"email_content"];
  10362. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10363. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10364. return [RAConvertor dict2data:dic];
  10365. }
  10366. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10367. __block int qty = 0;
  10368. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10369. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10370. qty = sqlite3_column_int(stmt, 0);
  10371. }];
  10372. return qty;
  10373. }
  10374. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10375. {
  10376. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10377. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10378. // NSMutableDictionary * values = params[@"replaceValue"];
  10379. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10380. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10381. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10382. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10383. NSString *pdf_path = @"";
  10384. if (direct) {
  10385. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10386. } else {
  10387. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10388. }
  10389. NSString *create_user = [self valueInParams:params key:@"user"];
  10390. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10391. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10392. // model info
  10393. // 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];
  10394. // V1.90 more color
  10395. 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];
  10396. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10397. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10398. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10399. sqlite3 *db = [iSalesDB get_db];
  10400. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10401. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10402. int product_id = sqlite3_column_int(stmt, 0);
  10403. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10404. double price = sqlite3_column_double(stmt, 1);
  10405. double discount = sqlite3_column_double(stmt,2);
  10406. int qty = sqlite3_column_int(stmt, 3);
  10407. int percentage = sqlite3_column_int(stmt, 4);
  10408. int item_id = sqlite3_column_int(stmt, 5);
  10409. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10410. double percent = sqlite3_column_double(stmt, 7);
  10411. int more_color = sqlite3_column_int(stmt, 8);
  10412. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10413. /* if ([price_null isEqualToString:@"null"]) {
  10414. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10415. }
  10416. */
  10417. [product_ids_string appendFormat:@"%@,",product_id_string];
  10418. // Regular Price
  10419. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10420. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10421. // QTY
  10422. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10423. if ([qty_null isEqualToString:@"null"]) {
  10424. // 查available
  10425. qty = [self model_QTY:product_id_string db:db];
  10426. }
  10427. if (percentage) {
  10428. qty = qty * percent / 100;
  10429. }
  10430. // Special Price
  10431. if ([price_null isEqualToString:@"null"]) {
  10432. // price = regular price
  10433. price = [regular_price_str doubleValue];
  10434. }
  10435. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10436. if (![discount_null isEqualToString:@"null"]) {
  10437. price = price * (1 - discount / 100.0);
  10438. }
  10439. NSMutableDictionary *item = @{
  10440. @"line_note": line_note,
  10441. @"product_id": product_id_string,
  10442. @"available_qty": @(qty),
  10443. @"more_color":@(more_color),
  10444. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10445. @"regular_price" : regular_price_str,
  10446. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10447. }.mutableCopy;
  10448. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10449. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10450. }];
  10451. [iSalesDB close_db:db];
  10452. if (product_ids_string.length > 0) {
  10453. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10454. }
  10455. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10456. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10457. return [RAConvertor dict2data:resultDictionary];
  10458. }
  10459. NSString *model_info = [RAConvertor dict2string:dic];
  10460. // 创建PDF
  10461. // 在preview情况下保存,则不需要新建了
  10462. if (direct) {
  10463. NSMutableDictionary *tear_sheet_params = params;
  10464. // if (tear_sheet_id) {
  10465. // tear_sheet_params = values;
  10466. // }
  10467. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10468. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10469. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10470. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10471. resultDictionary = pdfInfo.mutableCopy;
  10472. } else { // 创建PDF失败
  10473. return pdfData;
  10474. }
  10475. } else {
  10476. // pdf_path 就是本地路径
  10477. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10478. }
  10479. // 将文件移动到PDF Cache文件夹
  10480. NSString *newPath = [pdf_path lastPathComponent];
  10481. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10482. NSString *cachefolder = [paths objectAtIndex:0];
  10483. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10484. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10485. NSFileManager *fileManager = [NSFileManager defaultManager];
  10486. NSError *error = nil;
  10487. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10488. if (error) { // 移动文件失败
  10489. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10490. return [RAConvertor dict2data:resultDictionary];
  10491. }
  10492. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10493. pdf_path = [newPath lastPathComponent];
  10494. // 保存信息
  10495. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10496. NSString *off_params = [RAConvertor dict2string:params];
  10497. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10498. pdf_path = [self translateSingleQuote:pdf_path];
  10499. create_user = [self translateSingleQuote:create_user];
  10500. tear_note = [self translateSingleQuote:tear_note];
  10501. tear_name = [self translateSingleQuote:tear_name];
  10502. model_info = [self translateSingleQuote:model_info];
  10503. configureParams = [self translateSingleQuote:configureParams];
  10504. off_params = [self translateSingleQuote:off_params];
  10505. 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];
  10506. if (tear_sheet_id) {
  10507. int _id = [tear_sheet_id intValue];
  10508. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10509. }
  10510. int result = [iSalesDB execSql:save_pdf_sql];
  10511. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10512. //
  10513. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10514. if (remove_Item) {
  10515. // portfolioId
  10516. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10517. }
  10518. return [RAConvertor dict2data:resultDictionary];
  10519. }
  10520. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10521. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10522. }
  10523. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10524. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10525. }
  10526. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10527. int offset = [[params valueForKey:@"offset"] intValue];
  10528. int limit = [[params valueForKey:@"limit"] intValue];
  10529. NSString *keyword = [params valueForKey:@"keyWord"];
  10530. NSString *where = @"where is_delete is null or is_delete = 0";
  10531. if (keyword.length) {
  10532. keyword = [self translateSingleQuote:keyword];
  10533. 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];
  10534. }
  10535. 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
  10536. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10537. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10538. NSString *cachefolder = [paths objectAtIndex:0];
  10539. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10540. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10541. NSString *name = [self textAtColumn:0 statement:stmt];
  10542. NSString *note = [self textAtColumn:1 statement:stmt];
  10543. NSString *time = [self textAtColumn:2 statement:stmt];
  10544. NSString *user = [self textAtColumn:3 statement:stmt];
  10545. NSString *path = [self textAtColumn:4 statement:stmt];
  10546. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10547. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10548. path = [pdfFolder stringByAppendingPathComponent:path];
  10549. BOOL bdir=NO;
  10550. NSFileManager* fileManager = [NSFileManager defaultManager];
  10551. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10552. {
  10553. //pdf文件不存在
  10554. path=nil;
  10555. }
  10556. time = [self changeDateTimeFormate:time];
  10557. time = [time stringByAppendingString:@" PST"];
  10558. int sheet_id = sqlite3_column_int(stmt, 5);
  10559. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10560. item[@"tearsheetsId"]=@(sheet_id);
  10561. item[@"pdf_path"]=path;
  10562. item[@"create_time"]=time;
  10563. item[@"create_user"]=user;
  10564. item[@"tear_note"]=note;
  10565. item[@"tear_name"]=name;
  10566. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10567. item[@"model_info"]=model_info;
  10568. item[@"off_params"]=off_params;
  10569. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10570. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10571. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10572. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10573. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10574. }];
  10575. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10576. return [RAConvertor dict2data:dic];
  10577. }
  10578. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10579. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10580. // NSString *user = [params objectForKey:@"user"];
  10581. // 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];
  10582. 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];
  10583. int result = [iSalesDB execSql:sql];
  10584. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10585. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10586. if (result == RESULT_TRUE) {
  10587. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10588. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10589. sqlite3 *db = [iSalesDB get_db];
  10590. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10591. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10592. [iSalesDB close_db:db];
  10593. }
  10594. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10595. return [RAConvertor dict2data:dic];
  10596. }
  10597. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10598. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10599. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10600. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10601. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10602. NSDictionary *company_item = @{
  10603. @"control": @"text",
  10604. @"keyboard": @"text",
  10605. @"name": @"company_name",
  10606. @"value": COMPANY_FULL_NAME,
  10607. @"aname": @"Company Name"
  10608. };
  10609. [section_0 setObject:company_item forKey:@"item_0"];
  10610. [dic setObject:section_0 forKey:@"section_0"];
  10611. // Regurlar Price
  10612. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10613. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10614. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10615. __block long val_count = 0;
  10616. NSString *sql = @"select name,type,order_by from price order by order_by";
  10617. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10618. NSString *name = [self textAtColumn:0 statement:stmt];
  10619. int type = sqlite3_column_int(stmt, 1);
  10620. int order_by = sqlite3_column_int(stmt, 2);
  10621. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10622. NSDictionary *price_dic = @{
  10623. @"value" : name,
  10624. @"value_id" : [NSNumber numberWithInteger:type],
  10625. @"check" : order_by == 0 ? @(1) : @(0)
  10626. };
  10627. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10628. val_count = ++(*count);
  10629. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10630. val_count = 0;
  10631. }];
  10632. [cadedate setObject:@{@"value" : @"None",
  10633. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10634. val_count++;
  10635. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10636. [price setObject:cadedate forKey:@"cadedate"];
  10637. [section1 setObject:price forKey:@"item_2"];
  10638. [dic setObject:section1 forKey:@"section_1"];
  10639. return [RAConvertor dict2data:dic];
  10640. }
  10641. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10642. NSString *product_id = [params objectForKey:@"fashionId"];
  10643. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10644. __block int result = RESULT_TRUE;
  10645. __block int qty = 0;
  10646. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10647. qty = sqlite3_column_int(stmt, 0);
  10648. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10649. result = RESULT_FALSE;
  10650. }];
  10651. NSMutableDictionary *dic = @{
  10652. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10653. @"mode" : @"Regular Mode",
  10654. @"quantity_available" : @(qty),
  10655. @"result" : @(result),
  10656. }.mutableCopy;
  10657. return [RAConvertor dict2data:dic];
  10658. }
  10659. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10660. NSString *item_ids = [params objectForKey:@"item_id"];
  10661. NSString *line_notes = [params objectForKey:@"notes"];
  10662. NSString *price_str = [params objectForKey:@"price"];
  10663. NSString *discount_str = [params objectForKey:@"discount"];
  10664. NSString *percent = [params objectForKey:@"available_percent"];
  10665. NSString *qty = [params objectForKey:@"available_qty"];
  10666. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10667. int dot = 0;
  10668. if (line_notes) {
  10669. line_notes = [self translateSingleQuote:line_notes];
  10670. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10671. sql = [sql stringByAppendingString:line_notes];
  10672. dot = 1;
  10673. } else {
  10674. line_notes = @"";
  10675. }
  10676. if (price_str) {
  10677. if (dot) {
  10678. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10679. } else {
  10680. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10681. dot = 1;
  10682. }
  10683. sql = [sql stringByAppendingString:price_str];
  10684. } else {
  10685. price_str = @"";
  10686. }
  10687. if (discount_str) {
  10688. if (dot) {
  10689. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10690. } else {
  10691. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10692. dot = 1;
  10693. }
  10694. sql = [sql stringByAppendingString:discount_str];
  10695. } else {
  10696. discount_str = @"";
  10697. }
  10698. if (percent) {
  10699. if (dot) {
  10700. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10701. } else {
  10702. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10703. dot = 1;
  10704. }
  10705. sql = [sql stringByAppendingString:percent];
  10706. } else {
  10707. percent = @"";
  10708. }
  10709. if (qty) {
  10710. if (dot) {
  10711. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10712. } else {
  10713. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10714. dot = 1;
  10715. }
  10716. sql = [sql stringByAppendingString:qty];
  10717. } else {
  10718. qty = @"";
  10719. }
  10720. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10721. sql = [sql stringByAppendingString:where];
  10722. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10723. int result = [iSalesDB execSql:sql];
  10724. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10725. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10726. return [RAConvertor dict2data:dic];
  10727. }
  10728. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10729. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10730. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10731. sqlite3 *db = [iSalesDB get_db];
  10732. 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];
  10733. int result = [iSalesDB execSql:sql db:db];
  10734. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10735. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10736. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10737. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10738. [iSalesDB close_db:db];
  10739. return [RAConvertor dict2data:dic];
  10740. }
  10741. + (void)offline_removePDFWithName:(NSString *)name {
  10742. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10743. NSString *cachefolder = [paths objectAtIndex:0];
  10744. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10745. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10746. NSFileManager *fileManager = [NSFileManager defaultManager];
  10747. [fileManager removeItemAtPath:path error:nil];
  10748. }
  10749. + (void)offline_clear_PDFCache {
  10750. NSFileManager *fileManager = [NSFileManager defaultManager];
  10751. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10752. NSString *cachefolder = [paths objectAtIndex:0];
  10753. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10754. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10755. for (NSString *path in pdf_files) {
  10756. [self offline_removePDFWithName:[path lastPathComponent]];
  10757. }
  10758. }
  10759. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10760. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10761. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10762. NSString *user = [params objectForKey:@"user"];
  10763. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10764. if (![create_user isEqualToString:user]) {
  10765. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10766. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10767. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10768. return [RAConvertor dict2data:dic];
  10769. }
  10770. 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]];
  10771. __block int is_local = 0;
  10772. __block NSString *path = @"";
  10773. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10774. is_local = sqlite3_column_int(stmt, 0);
  10775. path = [self textAtColumn:1 statement:stmt];
  10776. }];
  10777. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10778. if (is_local == 1) {
  10779. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10780. // 删除文件
  10781. [self offline_removePDFWithName:path];
  10782. }
  10783. int result = [iSalesDB execSql:sql];
  10784. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10785. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10786. return [RAConvertor dict2data:dic];
  10787. }
  10788. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10789. {
  10790. // assert(add_params[@"contact_id"]!=nil);
  10791. assert(add_params[@"user"]!=nil);
  10792. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10793. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10794. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10795. NSString* where=@"1=1";
  10796. if (ver!=nil) {
  10797. where=@"is_dirty=1";
  10798. }
  10799. 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];
  10800. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10801. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10802. sqlite3 *db = [iSalesDB get_db];
  10803. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10804. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10805. NSInteger _id = sqlite3_column_int(stmt, 0);
  10806. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10807. NSString *name = [self textAtColumn:2 statement:stmt];
  10808. NSString *desc = [self textAtColumn:3 statement:stmt];
  10809. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10810. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10811. NSInteger qty = sqlite3_column_int(stmt, 6);
  10812. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10813. double percent = sqlite3_column_double(stmt, 8);
  10814. double price = sqlite3_column_double(stmt, 9);
  10815. double discount = sqlite3_column_double(stmt, 10);
  10816. NSString *img = [self textAtColumn:11 statement:stmt];
  10817. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10818. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10819. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10820. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10821. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10822. if ([price_null isEqualToString:@"null"]) {
  10823. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10824. }
  10825. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10826. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10827. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10828. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10829. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10830. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10831. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10832. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10833. if ([qty_null isEqualToString:@"null"]) {
  10834. // [item setValue:@"null" forKey:@"available_qty"];
  10835. } else {
  10836. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10837. }
  10838. if ([is_percent_null isEqualToString:@"null"]) {
  10839. // [item setValue:@"null" forKey:@"percentage"];
  10840. } else {
  10841. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10842. }
  10843. if ([percent_null isEqualToString:@"null"]) {
  10844. // [item setValue:@"null" forKey:@"percent"];
  10845. } else {
  10846. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10847. }
  10848. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10849. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10850. [item setValue:name forKey:@"name"];
  10851. [item setValue:desc forKey:@"description"];
  10852. [item setValue:img forKey:@"img"];
  10853. [item setValue:line_note forKey:@"line_note"];
  10854. [item setValue:create_time forKey:@"createtime"];
  10855. [item setValue:modify_time forKey:@"modifytime"];
  10856. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10857. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10858. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10859. }];
  10860. [iSalesDB close_db:db];
  10861. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10862. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10863. }
  10864. return ret;
  10865. }
  10866. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10867. {
  10868. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10869. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10870. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10871. NSString* where=@"1=1";
  10872. if (ver!=nil) {
  10873. where=@"is_dirty=1";
  10874. }
  10875. 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];
  10876. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10877. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10878. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10879. NSInteger _id = sqlite3_column_int(stmt, 0);
  10880. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10881. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10882. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10883. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10884. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10885. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10886. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10887. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10888. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10889. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10890. NSString *uuid = [NSUUID UUID].UUIDString;
  10891. int is_delete = sqlite3_column_int(stmt, 11);
  10892. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10893. int is_local = sqlite3_column_int(stmt, 12);
  10894. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10895. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10896. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10897. }
  10898. [item setObject:pdf_path forKey:@"pdf_path"];
  10899. [item setObject:create_user forKey:@"create_user"];
  10900. [item setObject:tear_note forKey:@"tear_note"];
  10901. [item setObject:tear_name forKey:@"tear_name"];
  10902. [item setObject:model_info forKey:@"model_info"];
  10903. [item setObject:createtime forKey:@"createtime"];
  10904. [item setObject:modifytime forKey:@"modifytime"];
  10905. [item setObject:urlParams forKey:@"urlParams"];
  10906. [item setObject:off_params forKey:@"off_params"];
  10907. [item setObject:uuid forKey:@"pdf_token"];
  10908. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10909. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10910. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10911. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10912. } else {
  10913. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10914. }
  10915. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10916. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10917. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10918. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10919. }];
  10920. return ret;
  10921. }
  10922. #pragma mark 2020
  10923. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10924. {
  10925. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10926. sqlite3 *db = [iSalesDB get_db];
  10927. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10928. if (contactType) {
  10929. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10930. } else {
  10931. contactType = @"1 = 1";
  10932. }
  10933. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10934. DebugLog(@"offline contact list keyword: %@",keyword);
  10935. // advanced search
  10936. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10937. if (contact_name) {
  10938. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10939. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10940. } else {
  10941. contact_name = @"";
  10942. }
  10943. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10944. if (customer_phone) {
  10945. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10946. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10947. } else {
  10948. customer_phone = @"";
  10949. }
  10950. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10951. if (customer_fax) {
  10952. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10953. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10954. } else {
  10955. customer_fax = @"";
  10956. }
  10957. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10958. if (customer_zipcode) {
  10959. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10960. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10961. } else {
  10962. customer_zipcode = @"";
  10963. }
  10964. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10965. if (customer_sales_rep) {
  10966. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10967. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10968. } else {
  10969. customer_sales_rep = @"";
  10970. }
  10971. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10972. if (customer_state) {
  10973. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10974. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10975. } else {
  10976. customer_state = @"";
  10977. }
  10978. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10979. if (customer_name) {
  10980. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10981. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10982. } else {
  10983. customer_name = @"";
  10984. }
  10985. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10986. if (customer_country) {
  10987. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10988. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10989. } else {
  10990. customer_country = @"";
  10991. }
  10992. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10993. if (customer_cid) {
  10994. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10995. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10996. } else {
  10997. customer_cid = @"";
  10998. }
  10999. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11000. if (customer_city) {
  11001. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11002. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11003. } else {
  11004. customer_city = @"";
  11005. }
  11006. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11007. if (customer_address) {
  11008. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11009. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11010. } else {
  11011. customer_address = @"";
  11012. }
  11013. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11014. if (customer_email) {
  11015. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11016. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11017. } else {
  11018. customer_email = @"";
  11019. }
  11020. NSString *price_name = [params valueForKey:@"price_name"];
  11021. if (price_name) {
  11022. if ([price_name containsString:@","]) {
  11023. // 首先从 price表中查处name
  11024. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11025. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11026. for (int i = 1;i < pArray.count;i++) {
  11027. NSString *p = pArray[i];
  11028. [mutablePStr appendFormat:@" or type = %@ ",p];
  11029. }
  11030. [mutablePStr appendString:@";"];
  11031. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11032. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11033. char *name = (char *)sqlite3_column_text(stmt, 0);
  11034. if (!name)
  11035. name = "";
  11036. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11037. }];
  11038. // 再根据name 拼sql
  11039. NSMutableString *mutable_price_name = [NSMutableString string];
  11040. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11041. for (int i = 1; i < price_name_array.count; i++) {
  11042. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11043. }
  11044. [mutable_price_name appendString:@")"];
  11045. price_name = mutable_price_name;
  11046. } else {
  11047. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11048. if ([price_name isEqualToString:@""]) {
  11049. price_name = @"";
  11050. } else {
  11051. __block NSString *price;
  11052. price_name = [self translateSingleQuote:price_name];
  11053. [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) {
  11054. char *p = (char *)sqlite3_column_text(stmt, 0);
  11055. if (p == NULL) {
  11056. p = "";
  11057. }
  11058. price = [NSString stringWithUTF8String:p];
  11059. }];
  11060. if ([price isEqualToString:@""]) {
  11061. price_name = @"";
  11062. } else {
  11063. price = [self translateSingleQuote:price];
  11064. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11065. }
  11066. }
  11067. }
  11068. } else {
  11069. price_name = @"";
  11070. }
  11071. int limit = [[params valueForKey:@"limit"] intValue];
  11072. int offset = [[params valueForKey:@"offset"] intValue];
  11073. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11074. 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];
  11075. 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];
  11076. // int result= [iSalesDB AddExFunction:db];
  11077. int count =0;
  11078. NSString *sqlQuery = nil;
  11079. if(keyword.length==0)
  11080. {
  11081. // 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];
  11082. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11083. sqlQuery = sql;
  11084. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11085. }
  11086. else
  11087. {
  11088. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11089. keyword = keyword.lowercaseString;
  11090. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11091. 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];
  11092. 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]];
  11093. }
  11094. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11095. sqlite3_stmt * statement;
  11096. [ret setValue:@"2" forKey:@"result"];
  11097. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11098. // 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";
  11099. int i = 0;
  11100. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11101. {
  11102. while (sqlite3_step(statement) == SQLITE_ROW)
  11103. {
  11104. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11105. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11106. int editable = sqlite3_column_int(statement, 0);
  11107. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11108. NSString *nscompany_name =nil;
  11109. if(company_name==nil)
  11110. nscompany_name=@"";
  11111. else
  11112. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11113. char *country = (char*)sqlite3_column_text(statement, 2);
  11114. if(country==nil)
  11115. country="";
  11116. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11117. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11118. // if(addr==nil)
  11119. // addr="";
  11120. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11121. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11122. if(zipcode==nil)
  11123. zipcode="";
  11124. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11125. char *state = (char*)sqlite3_column_text(statement, 5);
  11126. if(state==nil)
  11127. state="";
  11128. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11129. char *city = (char*)sqlite3_column_text(statement, 6);
  11130. if(city==nil)
  11131. city="";
  11132. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11133. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11134. // NSString *nscontact_name = nil;
  11135. // if(contact_name==nil)
  11136. // nscontact_name=@"";
  11137. // else
  11138. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11139. char *phone = (char*)sqlite3_column_text(statement, 8);
  11140. NSString *nsphone = nil;
  11141. if(phone==nil)
  11142. nsphone=@"";
  11143. else
  11144. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11145. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11146. if(contact_id==nil)
  11147. contact_id="";
  11148. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11149. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11150. if(addr_1==nil)
  11151. addr_1="";
  11152. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11153. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11154. if(addr_2==nil)
  11155. addr_2="";
  11156. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11157. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11158. if(addr_3==nil)
  11159. addr_3="";
  11160. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11161. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11162. if(addr_4==nil)
  11163. addr_4="";
  11164. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11165. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11166. if(first_name==nil)
  11167. first_name="";
  11168. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11169. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11170. if(last_name==nil)
  11171. last_name="";
  11172. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11173. char *fax = (char*)sqlite3_column_text(statement, 16);
  11174. NSString *nsfax = nil;
  11175. if(fax==nil)
  11176. nsfax=@"";
  11177. else
  11178. {
  11179. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11180. if(nsfax.length>0)
  11181. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11182. }
  11183. char *email = (char*)sqlite3_column_text(statement, 17);
  11184. NSString *nsemail = nil;
  11185. if(email==nil)
  11186. nsemail=@"";
  11187. else
  11188. {
  11189. nsemail= [[NSString alloc]initWithUTF8String:email];
  11190. if(nsemail.length>0)
  11191. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11192. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11193. }
  11194. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11195. [arr_name addObject:nsfirst_name];
  11196. [arr_name addObject:nslast_name];
  11197. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11198. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11199. {
  11200. // decrypt
  11201. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11202. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11203. nsphone=[AESCrypt fastdecrypt:nsphone];
  11204. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11205. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11206. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11207. }
  11208. [arr_addr addObject:nscompany_name];
  11209. [arr_addr addObject:nscontact_name];
  11210. [arr_addr addObject:@"<br>"];
  11211. [arr_addr addObject:nsaddr_1];
  11212. [arr_addr addObject:nsaddr_2];
  11213. [arr_addr addObject:nsaddr_3];
  11214. [arr_addr addObject:nsaddr_4];
  11215. //[arr_addr addObject:nsaddr];
  11216. [arr_addr addObject:nszipcode];
  11217. [arr_addr addObject:nscity];
  11218. [arr_addr addObject:nsstate];
  11219. [arr_addr addObject:nscountry];
  11220. [arr_addr addObject:@"<br>"];
  11221. [arr_addr addObject:nsphone];
  11222. [arr_addr addObject:nsfax];
  11223. [arr_addr addObject:nsemail];
  11224. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11225. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11226. [item setValue:name forKey:@"name"];
  11227. [item setValue:nscontact_id forKey:@"contact_id"];
  11228. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11229. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11230. i++;
  11231. }
  11232. sqlite3_finalize(statement);
  11233. }
  11234. [iSalesDB close_db:db];
  11235. #ifdef DEBUG
  11236. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11237. #endif
  11238. dispatch_async(dispatch_get_main_queue(), ^{
  11239. UIApplication * app = [UIApplication sharedApplication];
  11240. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11241. [ret setValue:appDelegate.mode forKey:@"mode"];
  11242. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11243. result(ret);
  11244. });
  11245. return ;
  11246. });
  11247. }
  11248. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11249. {
  11250. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11251. NSString* order_code= appDelegate.order_code;
  11252. NSString* user = appDelegate.user;
  11253. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11254. NSString* category = [params valueForKey:@"category"];
  11255. if (!category || [category isEqualToString:@""]) {
  11256. category = @"%";
  11257. }
  11258. category = [self translateSingleQuote:category];
  11259. int limit = [[params valueForKey:@"limit"] intValue];
  11260. int offset = [[params valueForKey:@"offset"] intValue];
  11261. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11262. NSString *limit_str = @"";
  11263. if (limited) {
  11264. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11265. }
  11266. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11267. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11268. sqlite3 *db = [iSalesDB get_db];
  11269. // [iSalesDB AddExFunction:db];
  11270. int count;
  11271. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11272. 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];
  11273. double price_min = 0;
  11274. double price_max = 0;
  11275. if ([params.allKeys containsObject:@"alert"]) {
  11276. // alert
  11277. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11278. NSString *alert = params[@"alert"];
  11279. if ([alert isEqualToString:@"Display All"]) {
  11280. alert = [NSString stringWithFormat:@""];
  11281. } else {
  11282. alert = [self translateSingleQuote:alert];
  11283. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11284. }
  11285. // available
  11286. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11287. NSString *available = params[@"available"];
  11288. NSString *available_condition;
  11289. if ([available isEqualToString:@"Display All"]) {
  11290. available_condition = @"";
  11291. } else if ([available isEqualToString:@"Available Now"]) {
  11292. available_condition = @"and availability > 0";
  11293. } else {
  11294. available_condition = @"and availability == 0";
  11295. }
  11296. // best seller
  11297. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11298. NSString *best_seller = @"";
  11299. NSString *order_best_seller = @"m.name asc";
  11300. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11301. best_seller = @"and best_seller > 0";
  11302. order_best_seller = @"m.best_seller desc,m.name asc";
  11303. }
  11304. // price
  11305. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11306. NSString *price = params[@"price"];
  11307. price_min = 0;
  11308. price_max = MAXFLOAT;
  11309. if (user && price != nil) {
  11310. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11311. NSMutableString *priceName = [NSMutableString string];
  11312. for (int i = 0; i < priceTypeArray.count; i++) {
  11313. NSString *pricetype = priceTypeArray[i];
  11314. pricetype = [self translateSingleQuote:pricetype];
  11315. if (i == 0) {
  11316. [priceName appendFormat:@"'%@'",pricetype];
  11317. } else {
  11318. [priceName appendFormat:@",'%@'",pricetype];
  11319. }
  11320. }
  11321. if ([price isEqualToString:@"Display All"]) {
  11322. price = [NSString stringWithFormat:@""];
  11323. } else if([price containsString:@"+"]){
  11324. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11325. price_min = [price doubleValue];
  11326. 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];
  11327. } else {
  11328. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11329. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11330. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11331. 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];
  11332. }
  11333. } else {
  11334. price = @"";
  11335. }
  11336. // sold_by_qty : Sold in quantities of %@
  11337. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11338. NSString *qty = params[@"sold_by_qty"];
  11339. if ([qty isEqualToString:@"Display All"]) {
  11340. qty = @"";
  11341. } else {
  11342. qty = [self translateSingleQuote:qty];
  11343. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11344. }
  11345. // cate
  11346. // category = [self translateSingleQuote:category];
  11347. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11348. // cate mutiple selection
  11349. NSString *category_id = params[@"category"];
  11350. NSMutableArray *cate_id_array = nil;
  11351. NSMutableString *cateWhere = [NSMutableString string];
  11352. if ([category_id isEqualToString:@""] || !category_id) {
  11353. [cateWhere appendString:@"1 = 1"];
  11354. } else {
  11355. if ([category_id containsString:@","]) {
  11356. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11357. } else {
  11358. cate_id_array = [@[category_id] mutableCopy];
  11359. }
  11360. [cateWhere appendString:@"("];
  11361. for (int i = 0; i < cate_id_array.count; i++) {
  11362. if (i == 0) {
  11363. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11364. } else {
  11365. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11366. }
  11367. }
  11368. [cateWhere appendString:@")"];
  11369. }
  11370. // where bestseller > 0 order by bestseller desc
  11371. // sql query: alert availability(int) best_seller(int) price qty
  11372. 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];
  11373. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11374. }
  11375. DebugLog(@"offline category where: %@",where);
  11376. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11377. if (!user) {
  11378. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11379. }
  11380. [ret setValue:filter forKey:@"filter"];
  11381. DebugLog(@"offline_category sql:%@",sqlQuery);
  11382. sqlite3_stmt * statement;
  11383. [ret setValue:@"2" forKey:@"result"];
  11384. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11385. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11386. // int count=0;
  11387. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11388. {
  11389. int i=0;
  11390. while (sqlite3_step(statement) == SQLITE_ROW)
  11391. {
  11392. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11393. char *name = (char*)sqlite3_column_text(statement, 0);
  11394. if(name==nil)
  11395. name="";
  11396. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11397. char *description = (char*)sqlite3_column_text(statement, 1);
  11398. if(description==nil)
  11399. description="";
  11400. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11401. int product_id = sqlite3_column_int(statement, 2);
  11402. int wid = sqlite3_column_int(statement, 3);
  11403. int closeout = sqlite3_column_int(statement, 4);
  11404. int cid = sqlite3_column_int(statement, 5);
  11405. int wisdelete = sqlite3_column_int(statement, 6);
  11406. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11407. int more_color = sqlite3_column_int(statement, 8);
  11408. // Defaul Category ID
  11409. __block NSString *categoryID = nil;
  11410. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11411. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11412. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11413. if(default_category==nil)
  11414. default_category="";
  11415. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11416. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11417. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11418. categoryID = nsdefault_category;
  11419. }];
  11420. if (!categoryID.length) {
  11421. NSString *cateIDs = params[@"category"];
  11422. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11423. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11424. for (NSString *cateID in requestCategoryArr) {
  11425. BOOL needBreak = NO;
  11426. for (NSString *itemCateIDBox in itemCategoryArr) {
  11427. if (itemCateIDBox.length > 4) {
  11428. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11429. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11430. if ([itemCategoryID isEqualToString:cateID]) {
  11431. needBreak = YES;
  11432. categoryID = itemCategoryID;
  11433. break;
  11434. }
  11435. }
  11436. }
  11437. if (needBreak) {
  11438. break;
  11439. }
  11440. }
  11441. }
  11442. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11443. if(wid !=0 && wisdelete != 1)
  11444. [item setValue:@"true" forKey:@"wish_exists"];
  11445. else
  11446. [item setValue:@"false" forKey:@"wish_exists"];
  11447. if(closeout==0)
  11448. [item setValue:@"false" forKey:@"is_closeout"];
  11449. else
  11450. [item setValue:@"true" forKey:@"is_closeout"];
  11451. if(cid==0)
  11452. [item setValue:@"false" forKey:@"cart_exists"];
  11453. else
  11454. [item setValue:@"true" forKey:@"cart_exists"];
  11455. if (more_color == 0) {
  11456. [item setObject:@(false) forKey:@"more_color"];
  11457. } else if (more_color == 1) {
  11458. [item setObject:@(true) forKey:@"more_color"];
  11459. }
  11460. [item addEntriesFromDictionary:imgjson];
  11461. // [item setValue:nsurl forKey:@"img"];
  11462. [item setValue:nsname forKey:@"name"];
  11463. [item setValue:nsdescription forKey:@"description"];
  11464. if (categoryID) {
  11465. [item setValue:categoryID forKey:@"item_category_id"];
  11466. }
  11467. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11468. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11469. i++;
  11470. }
  11471. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11472. [ret setObject:items forKey:@"items"];
  11473. sqlite3_finalize(statement);
  11474. } else {
  11475. DebugLog(@"nothing...");
  11476. }
  11477. DebugLog(@"count:%d",count);
  11478. [iSalesDB close_db:db];
  11479. #ifdef DEBUG
  11480. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11481. #endif
  11482. dispatch_async(dispatch_get_main_queue(), ^{
  11483. // UIApplication * app = [UIApplication sharedApplication];
  11484. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11485. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11486. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11487. result(ret);
  11488. });
  11489. });
  11490. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11491. }
  11492. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11493. {
  11494. [self categoryList:params limited:YES completionHandler:result];
  11495. }
  11496. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11497. {
  11498. UIApplication * app = [UIApplication sharedApplication];
  11499. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11500. NSString* order_code = appDelegate.order_code;
  11501. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11502. NSString* keyword = [params valueForKey:@"keyword"];
  11503. keyword=keyword.lowercaseString;
  11504. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11505. int limit = [[params valueForKey:@"limit"] intValue];
  11506. int offset = [[params valueForKey:@"offset"] intValue];
  11507. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11508. NSString *limit_str = @"";
  11509. if (limited) {
  11510. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11511. }
  11512. sqlite3 *db = [iSalesDB get_db];
  11513. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11514. NSString *sqlQuery = nil;
  11515. if(exactMatch )
  11516. 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 ;
  11517. else
  11518. 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
  11519. DebugLog(@"offline_search sql:%@",sqlQuery);
  11520. sqlite3_stmt * statement;
  11521. [ret setValue:@"2" forKey:@"result"];
  11522. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11523. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11524. // int count=0;
  11525. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11526. {
  11527. int i=0;
  11528. while (sqlite3_step(statement) == SQLITE_ROW)
  11529. {
  11530. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11531. // char *name = (char*)sqlite3_column_text(statement, 1);
  11532. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11533. char *name = (char*)sqlite3_column_text(statement, 0);
  11534. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11535. char *description = (char*)sqlite3_column_text(statement, 1);
  11536. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11537. int product_id = sqlite3_column_int(statement, 2);
  11538. // char *url = (char*)sqlite3_column_text(statement, 3);
  11539. // if(url==nil)
  11540. // url="";
  11541. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11542. int wid = sqlite3_column_int(statement, 3);
  11543. int closeout = sqlite3_column_int(statement, 4);
  11544. int cid = sqlite3_column_int(statement, 5);
  11545. int wisdelete = sqlite3_column_int(statement, 6);
  11546. int more_color = sqlite3_column_int(statement, 7);
  11547. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11548. if(wid !=0 && wisdelete != 1)
  11549. [item setValue:@"true" forKey:@"wish_exists"];
  11550. else
  11551. [item setValue:@"false" forKey:@"wish_exists"];
  11552. if(closeout==0)
  11553. [item setValue:@"false" forKey:@"is_closeout"];
  11554. else
  11555. [item setValue:@"true" forKey:@"is_closeout"];
  11556. if(cid==0)
  11557. [item setValue:@"false" forKey:@"cart_exists"];
  11558. else
  11559. [item setValue:@"true" forKey:@"cart_exists"];
  11560. if (more_color == 0) {
  11561. [item setObject:@(false) forKey:@"more_color"];
  11562. } else if (more_color == 1) {
  11563. [item setObject:@(true) forKey:@"more_color"];
  11564. }
  11565. [item addEntriesFromDictionary:imgjson];
  11566. // [item setValue:nsurl forKey:@"img"];
  11567. [item setValue:nsname forKey:@"fash_name"];
  11568. [item setValue:nsdescription forKey:@"description"];
  11569. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11570. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11571. i++;
  11572. }
  11573. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11574. [ret setObject:items forKey:@"items"];
  11575. sqlite3_finalize(statement);
  11576. }
  11577. DebugLog(@"count:%d",count);
  11578. [iSalesDB close_db:db];
  11579. #ifdef DEBUG
  11580. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11581. #endif
  11582. dispatch_async(dispatch_get_main_queue(), ^{
  11583. result(ret);
  11584. });
  11585. });
  11586. }
  11587. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11588. {
  11589. [self search:params limited:YES completionHandler:result];
  11590. }
  11591. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11592. {
  11593. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11594. NSString* offline_command=params[@"offline_Command"];
  11595. NSMutableDictionary* ret=nil;
  11596. if([offline_command isEqualToString:@"model_NIYMAL"])
  11597. {
  11598. NSString* category = params[@"category"];
  11599. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11600. }
  11601. dispatch_async(dispatch_get_main_queue(), ^{
  11602. result(ret);
  11603. });
  11604. });
  11605. }
  11606. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11607. {
  11608. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11609. int sort = [[params objectForKey:@"sort"] intValue];
  11610. NSString *sort_str = @"";
  11611. switch (sort) {
  11612. case 0:{
  11613. sort_str = @"order by w.modify_time desc";
  11614. }
  11615. break;
  11616. case 1:{
  11617. sort_str = @"order by w.modify_time asc";
  11618. }
  11619. break;
  11620. case 2:{
  11621. sort_str = @"order by m.name asc";
  11622. }
  11623. break;
  11624. case 3:{
  11625. sort_str = @"order by m.name desc";
  11626. }
  11627. break;
  11628. case 4:{
  11629. sort_str = @"order by m.description asc";
  11630. }
  11631. break;
  11632. default:
  11633. break;
  11634. }
  11635. // NSString* user = appDelegate.user;
  11636. sqlite3 *db = [iSalesDB get_db];
  11637. // order by w.create_time
  11638. 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];
  11639. // 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];
  11640. sqlite3_stmt * statement;
  11641. NSDate *date1 = [NSDate date];
  11642. // NSDate *date2 = nil;
  11643. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11644. int count=0;
  11645. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11646. {
  11647. while (sqlite3_step(statement) == SQLITE_ROW)
  11648. {
  11649. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11650. int product_id = sqlite3_column_double(statement, 0);
  11651. char *description = (char*)sqlite3_column_text(statement, 1);
  11652. if(description==nil)
  11653. description= "";
  11654. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11655. int item_id = sqlite3_column_double(statement, 2);
  11656. NSDate *date_image = [NSDate date];
  11657. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11658. printf("image : ");
  11659. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11660. // char *url = (char*)sqlite3_column_text(statement, 3);
  11661. // if(url==nil)
  11662. // url="";
  11663. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11664. int qty = sqlite3_column_int(statement, 3);
  11665. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11666. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11667. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11668. item[@"description"]= nsdescription;
  11669. item[@"img"]= nsurl;
  11670. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11671. count++;
  11672. }
  11673. printf("total time:");
  11674. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11675. ret[@"count"]= [NSNumber numberWithInt:count];
  11676. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11677. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11678. ret[@"result"]= [NSNumber numberWithInt:2];
  11679. sqlite3_finalize(statement);
  11680. }
  11681. [iSalesDB close_db:db];
  11682. dispatch_async(dispatch_get_main_queue(), ^{
  11683. UIApplication * app = [UIApplication sharedApplication];
  11684. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11685. appDelegate.wish_count =count;
  11686. [appDelegate update_count_mark];
  11687. result(ret);
  11688. });
  11689. });
  11690. }
  11691. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11692. {
  11693. // UIApplication * app = [UIApplication sharedApplication];
  11694. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11695. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11696. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11697. sqlite3 *db = [iSalesDB get_db];
  11698. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11699. NSString* product_id=params[@"product_id"];
  11700. NSString *item_count_str = params[@"item_count"];
  11701. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11702. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11703. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11704. // NSString *sql = @"";
  11705. for(int i=0;i<arr.count;i++)
  11706. {
  11707. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11708. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11709. __block int cart_count = 0;
  11710. if (!item_count_str) {
  11711. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11712. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11713. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11714. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11715. cart_count = [[model_set_components lastObject] intValue];
  11716. }];
  11717. }
  11718. if(count==0)
  11719. {
  11720. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11721. sqlite3_stmt *stmt;
  11722. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11723. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11724. if (item_count_arr) {
  11725. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11726. } else {
  11727. sqlite3_bind_int(stmt,2,cart_count);
  11728. }
  11729. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11730. [iSalesDB execSql:@"ROLLBACK" db:db];
  11731. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11732. [iSalesDB close_db:db];
  11733. DebugLog(@"add to wishlist error");
  11734. dispatch_async(dispatch_get_main_queue(), ^{
  11735. result(ret);
  11736. });
  11737. }
  11738. } else {
  11739. int qty = 0;
  11740. if (item_count_arr) {
  11741. qty = [item_count_arr[i] intValue];
  11742. } else {
  11743. qty = cart_count;
  11744. }
  11745. 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]];
  11746. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11747. [iSalesDB execSql:@"ROLLBACK" db:db];
  11748. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11749. [iSalesDB close_db:db];
  11750. DebugLog(@"add to wishlist error");
  11751. dispatch_async(dispatch_get_main_queue(), ^{
  11752. result(ret);
  11753. });
  11754. }
  11755. }
  11756. }
  11757. // [iSalesDB execSql:sql db:db];
  11758. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11759. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11760. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11761. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11762. [iSalesDB close_db:db];
  11763. ret[@"result"]= [NSNumber numberWithInt:2];
  11764. dispatch_async(dispatch_get_main_queue(), ^{
  11765. UIApplication * app = [UIApplication sharedApplication];
  11766. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11767. appDelegate.wish_count =count;
  11768. [appDelegate update_count_mark];
  11769. result(ret);
  11770. });
  11771. });
  11772. }
  11773. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11774. {
  11775. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11776. bool bLogin = appDelegate.bLogin;
  11777. __block NSString* contact_id = appDelegate.contact_id;
  11778. __block NSString* user = appDelegate.user;
  11779. __block NSString* order_code = appDelegate.order_code;
  11780. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11781. NSString* model_name = [params valueForKey:@"product_name"];
  11782. NSString* product_id = [params valueForKey:@"product_id"];
  11783. NSString* upc_code = [params valueForKey:@"upc_code"];
  11784. NSString* category = [params valueForKey:@"category"];
  11785. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11786. if(category==nil) {
  11787. category = default_category_id;
  11788. } else {
  11789. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11790. // 参数重有多个category id
  11791. if (arr_0.count > 1) {
  11792. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11793. // 取交集
  11794. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11795. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11796. [set_0 intersectSet:set_1];
  11797. if (set_0.count == 1) {
  11798. category = (NSString *)[set_0 anyObject];
  11799. } else {
  11800. if ([set_0 containsObject:default_category_id]) {
  11801. category = default_category_id;
  11802. } else {
  11803. category = (NSString *)[set_0 anyObject];
  11804. }
  11805. }
  11806. }
  11807. }
  11808. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11809. sqlite3 *db = [iSalesDB get_db];
  11810. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11811. NSString *sqlQuery = nil;
  11812. if(product_id==nil)
  11813. if(model_name==nil)
  11814. {
  11815. 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='%@';
  11816. }
  11817. else
  11818. {
  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 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='%@';
  11820. }
  11821. else
  11822. 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=%@;
  11823. sqlite3_stmt * statement;
  11824. [ret setValue:@"2" forKey:@"result"];
  11825. [ret setValue:@"3" forKey:@"detail_section_count"];
  11826. // int count=0;
  11827. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11828. {
  11829. if (sqlite3_step(statement) == SQLITE_ROW)
  11830. {
  11831. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11832. char *name = (char*)sqlite3_column_text(statement, 0);
  11833. if(name==nil)
  11834. name="";
  11835. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11836. char *description = (char*)sqlite3_column_text(statement, 1);
  11837. if(description==nil)
  11838. description="";
  11839. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11840. int product_id = sqlite3_column_int(statement, 2);
  11841. char *color = (char*)sqlite3_column_text(statement, 3);
  11842. if(color==nil)
  11843. color="";
  11844. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11845. //
  11846. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11847. // if(legcolor==nil)
  11848. // legcolor="";
  11849. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11850. //
  11851. //
  11852. int availability = sqlite3_column_int(statement, 5);
  11853. //
  11854. int incoming_stock = sqlite3_column_int(statement, 6);
  11855. char *demension = (char*)sqlite3_column_text(statement, 7);
  11856. if(demension==nil)
  11857. demension="";
  11858. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11859. // ,,,,,,,,,
  11860. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11861. if(seat_height==nil)
  11862. seat_height="";
  11863. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11864. char *material = (char*)sqlite3_column_text(statement, 9);
  11865. if(material==nil)
  11866. material="";
  11867. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11868. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11869. if(box_dim==nil)
  11870. box_dim="";
  11871. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11872. char *volume = (char*)sqlite3_column_text(statement, 11);
  11873. if(volume==nil)
  11874. volume="";
  11875. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11876. double weight = sqlite3_column_double(statement, 12);
  11877. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11878. if(model_set==nil)
  11879. model_set="";
  11880. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11881. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11882. if(load_ability==nil)
  11883. load_ability="";
  11884. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11885. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11886. // if(default_category==nil)
  11887. // default_category="";
  11888. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11889. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11890. if(fabric_content==nil)
  11891. fabric_content="";
  11892. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11893. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11894. if(assembling==nil)
  11895. assembling="";
  11896. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11897. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11898. if(made_in==nil)
  11899. made_in="";
  11900. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11901. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11902. if(special_remarks==nil)
  11903. special_remarks="";
  11904. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11905. int stockUcom = sqlite3_column_double(statement, 20);
  11906. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11907. if(product_group==nil)
  11908. product_group="";
  11909. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11910. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11911. // if(fashion_selector==nil)
  11912. // fashion_selector="";
  11913. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11914. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11915. if(selector_field==nil)
  11916. selector_field="";
  11917. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11918. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11919. if(property_field==nil)
  11920. property_field="";
  11921. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11922. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11923. if(packaging==nil)
  11924. packaging="";
  11925. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11926. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11927. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11928. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11929. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11930. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11931. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11932. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11933. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11934. NSString* Availability=nil;
  11935. if(availability>0)
  11936. Availability=[NSString stringWithFormat:@"%d",availability];
  11937. else
  11938. Availability = @"Out of Stock";
  11939. [img_section setValue:Availability forKey:@"Availability"];
  11940. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11941. char *eta = (char*)sqlite3_column_text(statement, 25);
  11942. if(eta==nil)
  11943. eta="";
  11944. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11945. if ([nseta isEqualToString:@"null"]) {
  11946. nseta = @"";
  11947. }
  11948. if (availability <= 0) {
  11949. [img_section setValue:nseta forKey:@"ETA"];
  11950. }
  11951. int item_id = sqlite3_column_int(statement, 26);
  11952. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11953. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11954. NSString* Price=nil;
  11955. if(bLogin==false)
  11956. Price=@"Must Sign in.";
  11957. else
  11958. {
  11959. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11960. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11961. if(price==nil)
  11962. Price=@"No Price.";
  11963. else
  11964. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11965. }
  11966. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11967. [img_section setValue:Price forKey:@"price"];
  11968. [img_section setValue:nsname forKey:@"model_name"];
  11969. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11970. if (order_code) { // 离线order code即so#
  11971. 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];
  11972. __block int cartQTY = 0;
  11973. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11974. cartQTY = sqlite3_column_int(stmt, 0);
  11975. }];
  11976. if (cartQTY > 0) {
  11977. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11978. }
  11979. }
  11980. [ret setObject:img_section forKey:@"img_section"];
  11981. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11982. int detail0_item_count=0;
  11983. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11984. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11985. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11986. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11987. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11988. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11989. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11990. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11991. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11992. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11993. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11994. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11995. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11996. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11997. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11998. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11999. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12000. {
  12001. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12002. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12003. }
  12004. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12005. [detail0_section setValue:@"kv" forKey:@"type"];
  12006. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12007. [ret setObject:detail0_section forKey:@"detail_0"];
  12008. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12009. // [detail1_section setValue:@"detail" forKey:@"target"];
  12010. // [detail1_section setValue:@"popup" forKey:@"action"];
  12011. // [detail1_section setValue:@"content" forKey:@"type"];
  12012. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12013. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12014. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12015. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12016. [ret setObject:detail1_section forKey:@"detail_1"];
  12017. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12018. [detail2_section setValue:@"detail" forKey:@"target"];
  12019. [detail2_section setValue:@"popup" forKey:@"action"];
  12020. [detail2_section setValue:@"content" forKey:@"type"];
  12021. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12022. [detail2_section setValue:@"true" forKey:@"single_row"];
  12023. [detail2_section setValue:@"local" forKey:@"data"];
  12024. [ret setObject:detail2_section forKey:@"detail_2"];
  12025. }
  12026. sqlite3_finalize(statement);
  12027. }
  12028. else
  12029. {
  12030. [ret setValue:@"8" forKey:@"result"];
  12031. }
  12032. // DebugLog(@"count:%d",count);
  12033. [iSalesDB close_db:db];
  12034. #ifdef DEBUG
  12035. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12036. #endif
  12037. dispatch_async(dispatch_get_main_queue(), ^{
  12038. result(ret);
  12039. });
  12040. });
  12041. }
  12042. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12043. //{
  12044. //
  12045. // UIApplication * app = [UIApplication sharedApplication];
  12046. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12047. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12048. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12049. // sqlite3 *db = [iSalesDB get_db];
  12050. // NSString* collectId=params[@"collectId"];
  12051. //
  12052. //
  12053. //
  12054. //
  12055. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12056. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12057. // [iSalesDB execSql:sqlQuery db:db];
  12058. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12059. // [iSalesDB close_db:db];
  12060. //
  12061. //
  12062. // ret[@"result"]= [NSNumber numberWithInt:2];
  12063. // dispatch_async(dispatch_get_main_queue(), ^{
  12064. //
  12065. // appDelegate.wish_count =count;
  12066. //
  12067. // [appDelegate update_count_mark];
  12068. // result(ret);
  12069. // });
  12070. //
  12071. // });
  12072. //}
  12073. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12074. {
  12075. UIApplication * app = [UIApplication sharedApplication];
  12076. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12077. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12078. [iSalesDB disable_trigger];
  12079. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12080. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12081. [iSalesDB enable_trigger];
  12082. dispatch_async(dispatch_get_main_queue(), ^{
  12083. //
  12084. // NSString* user = [params valueForKey:@"user"];
  12085. //
  12086. // NSString* password = [params valueForKey:@"password"];
  12087. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12088. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12089. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12090. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12091. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12092. [appDelegate update_count_mark];
  12093. appDelegate.can_show_price =false;
  12094. appDelegate.can_see_price =false;
  12095. appDelegate.can_create_portfolio =false;
  12096. appDelegate.can_create_order =false;
  12097. appDelegate.can_cancel_order =false;
  12098. appDelegate.can_set_cart_price =false;
  12099. appDelegate.can_delete_order =false;
  12100. appDelegate.can_submit_order =false;
  12101. appDelegate.can_set_tearsheet_price =false;
  12102. appDelegate.can_update_contact_info = false;
  12103. appDelegate.save_order_logout = false;
  12104. appDelegate.submit_order_logout = false;
  12105. appDelegate.alert_sold_in_quantities = false;
  12106. appDelegate.ipad_perm =nil ;
  12107. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12108. appDelegate.OrderFilter= nil;
  12109. [appDelegate SetSo:nil];
  12110. [appDelegate set_main_button_panel];
  12111. result(ret);
  12112. });
  12113. });
  12114. }
  12115. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12116. {
  12117. // iSalesDB.
  12118. // UIApplication * app = [UIApplication sharedApplication];
  12119. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12120. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12121. [iSalesDB disable_trigger];
  12122. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12123. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12124. [iSalesDB enable_trigger];
  12125. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12126. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12127. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12128. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12129. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12130. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12131. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12132. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12133. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12134. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12135. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12136. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12137. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12138. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12139. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12140. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12141. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12142. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12143. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12144. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12145. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12146. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12147. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12148. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12149. [arr_name addObject:customer_first_name];
  12150. [arr_name addObject:customer_last_name];
  12151. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12152. // default ship from
  12153. 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';";
  12154. __block NSString *cid = @"";
  12155. __block NSString *name = @"";
  12156. __block NSString *ext = @"";
  12157. __block NSString *contact = @"";
  12158. __block NSString *email = @"";
  12159. __block NSString *fax = @"";
  12160. __block NSString *phone = @"";
  12161. sqlite3 *db = [iSalesDB get_db];
  12162. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12163. cid = [self textAtColumn:0 statement:statment];
  12164. name = [self textAtColumn:1 statement:statment];
  12165. ext = [self textAtColumn:2 statement:statment];
  12166. contact = [self textAtColumn:3 statement:statment];
  12167. email = [self textAtColumn:4 statement:statment];
  12168. fax = [self textAtColumn:5 statement:statment];
  12169. phone = [self textAtColumn:6 statement:statment];
  12170. }];
  12171. // default carrier
  12172. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12173. __block NSString *carrier = @"";
  12174. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12175. carrier = [self textAtColumn:0 statement:statment];
  12176. }];
  12177. NSString* so_id = [self get_offline_soid:db];
  12178. if(so_id==nil)
  12179. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12180. 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];
  12181. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12182. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12183. //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'
  12184. //soId
  12185. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12186. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12187. sqlite3_stmt * statement;
  12188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12189. {
  12190. if (sqlite3_step(statement) == SQLITE_ROW)
  12191. {
  12192. // char *name = (char*)sqlite3_column_text(statement, 1);
  12193. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12194. //ret = sqlite3_column_int(statement, 0);
  12195. char *soId = (char*)sqlite3_column_text(statement, 0);
  12196. if(soId==nil)
  12197. soId="";
  12198. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12199. [ret setValue:nssoId forKey:@"soId"];
  12200. [ret setValue:nssoId forKey:@"orderCode"];
  12201. }
  12202. sqlite3_finalize(statement);
  12203. }
  12204. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12205. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12206. [iSalesDB close_db:db];
  12207. dispatch_async(dispatch_get_main_queue(), ^{
  12208. result(ret);
  12209. });
  12210. });
  12211. }
  12212. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12213. {
  12214. UIApplication * app = [UIApplication sharedApplication];
  12215. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12216. bool can_create_backorder= appDelegate.can_create_backorder;
  12217. NSString* user = appDelegate.user;
  12218. NSString* contact_id = appDelegate.contact_id;
  12219. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12220. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12221. sqlite3 *db = [iSalesDB get_db];
  12222. NSString* orderCode=params[@"orderCode"];
  12223. int sort = [[params objectForKey:@"sort"] intValue];
  12224. NSString *sort_str = @"";
  12225. switch (sort) {
  12226. case 0:{
  12227. sort_str = @"order by c.modify_time desc";
  12228. }
  12229. break;
  12230. case 1:{
  12231. sort_str = @"order by c.modify_time asc";
  12232. }
  12233. break;
  12234. case 2:{
  12235. sort_str = @"order by m.name asc";
  12236. }
  12237. break;
  12238. case 3:{
  12239. sort_str = @"order by m.name desc";
  12240. }
  12241. break;
  12242. case 4:{
  12243. sort_str = @"order by m.description asc";
  12244. }
  12245. break;
  12246. default:
  12247. break;
  12248. }
  12249. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  12250. sqlite3_stmt * statement1;
  12251. double orderdiscount=0;
  12252. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  12253. if ( dbresult1== SQLITE_OK)
  12254. {
  12255. if (sqlite3_step(statement1) == SQLITE_ROW)
  12256. {
  12257. // NSDate *row_date = [NSDate date];
  12258. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12259. orderdiscount = sqlite3_column_double(statement1, 0);
  12260. }
  12261. sqlite3_finalize(statement1);
  12262. }
  12263. 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 ];
  12264. // 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 ];
  12265. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12266. DebugLog(@"offline_login sql:%@",sqlQuery);
  12267. sqlite3_stmt * statement;
  12268. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12269. NSDate *date1 = [NSDate date];
  12270. int count=0;
  12271. int cart_count=0;
  12272. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12273. if ( dbresult== SQLITE_OK)
  12274. {
  12275. while (sqlite3_step(statement) == SQLITE_ROW)
  12276. {
  12277. // NSDate *row_date = [NSDate date];
  12278. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12279. int product_id = sqlite3_column_int(statement, 0);
  12280. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12281. int item_id = sqlite3_column_int(statement, 7);
  12282. NSString* Price=nil;
  12283. if(str_price==nil)
  12284. {
  12285. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12286. // NSDate *price_date = [NSDate date];
  12287. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12288. // DebugLog(@"price time interval");
  12289. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12290. if(price==nil)
  12291. Price=@"No Price.";
  12292. else
  12293. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12294. }
  12295. else
  12296. {
  12297. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12298. }
  12299. double discount = sqlite3_column_double(statement, 2);
  12300. int item_count = sqlite3_column_int(statement, 3);
  12301. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12302. NSString *nsline_note=nil;
  12303. if(line_note!=nil)
  12304. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12305. char *name = (char*)sqlite3_column_text(statement, 5);
  12306. NSString *nsname=nil;
  12307. if(name!=nil)
  12308. nsname= [[NSString alloc]initWithUTF8String:name];
  12309. char *description = (char*)sqlite3_column_text(statement, 6);
  12310. NSString *nsdescription=nil;
  12311. if(description!=nil)
  12312. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12313. int stockUom = sqlite3_column_int(statement, 8);
  12314. int _id = sqlite3_column_int(statement, 9);
  12315. int availability = sqlite3_column_int(statement, 10);
  12316. // NSDate *subtotal_date = [NSDate date];
  12317. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12318. // DebugLog(@"subtotal_date time interval");
  12319. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12320. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12321. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12322. int carton=[bsubtotaljson[@"carton"] intValue];
  12323. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12324. int mpack =[bsubtotaljson[@"mpack"] intValue];
  12325. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12326. // NSDate *img_date = [NSDate date];
  12327. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12328. // DebugLog(@"img_date time interval");
  12329. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12330. itemjson[@"model"]=nsname;
  12331. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12332. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12333. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12334. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12335. itemjson[@"check"]=@"true";
  12336. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12337. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12338. itemjson[@"unit_price"]=Price;
  12339. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12340. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12341. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12342. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  12343. itemjson[@"note"]=nsline_note;
  12344. if (!can_create_backorder) {
  12345. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12346. }
  12347. // NSDate *date2 = [NSDate date];
  12348. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12349. // DebugLog(@"model_bundle time interval");
  12350. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12351. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12352. count++;
  12353. // DebugLog(@"row time interval");
  12354. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12355. }
  12356. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12357. sqlite3_finalize(statement);
  12358. }
  12359. DebugLog(@"request cart total time interval");
  12360. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12361. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12362. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12363. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12364. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12365. ret[@"count"]=[NSNumber numberWithInt:count ];
  12366. if(orderdiscount>0)
  12367. ret[@"orderDiscount"]=@(orderdiscount);
  12368. ret[@"mode"]=@"Regular Mode";
  12369. [iSalesDB close_db:db];
  12370. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12371. DebugLog(@"general notes :%@",general_note);
  12372. ret[@"general_note"]= general_note;
  12373. dispatch_async(dispatch_get_main_queue(), ^{
  12374. result(ret);
  12375. });
  12376. });
  12377. }
  12378. @end